Closed mdhowle closed 5 years ago
Hm, uxy is meant to reformat the output of existing tools rather than provide tools of its own.
I guess calling ls
with -b
option would make spaces deterministically parsable.
From what I can tell -b
only escapes the file name.
I agree, perhaps a better workaround rather than re-implementing ls
would be to use -n, --numeric-uid-gid
with some minimal post-processing. The numeric ids can be resolved using the built-in pwd
and grp
modules. If -n
was passed by the user, no resolving should be done. -n
is supported by other UNIXes.
def uxy_cmd_ls(args, uargs):
resolve_ids = True
if "-n" in uargs or "--numeric-uid-gid" in uargs:
resolve_ids = False
proc = subprocess.Popen(
['ls','-l', '--time-style=full-iso', '-n'] + uargs,
stdout=subprocess.PIPE)
regexp = re.compile(r'(.)([^\s]*)\s+([^\s]*)\s+([^\s]*)\s+([^\s]*)\s+([^\s]*)\s+([^\s]*\s+[^\s]*\s+[^\s]*)\s+(.*)')
fmt = Format("TYPE PERMISSIONS LINKS OWNER GROUP SIZE TIME NAME")
writeout(fmt.render())
for ln in proc.stdout:
ln = trim_newline(ln.decode("utf-8"))
if ln.startswith('total'):
continue
m = regexp.match(ln)
if not m:
continue
fields = []
for i in range(1, regexp.groups + 1):
value = m.group(i)
if resolve_ids:
try:
if i == 4:
value = pwd.getpwuid(int(value)).pw_name
elif i == 5:
value = grp.getgrgid(int(value)).gr_name
except (KeyError, ValueError):
pass
fields.append(encode_field(value))
writeout(fmt.render(fields))
If that implementation is ok, I can make a pull request.
Thanks!
I'm in an environment where groups with spaces are common. The regular expression that extracts the output of
ls
cannot handle this properly. Since there are no delimiters for user and group names, it would not be possible to get the correct user and group.For example, this is the output with spaces in the user and group names: (users: howlem and "test user ignore" and groups: "domain users")
I wrote a quick implementation of
ls
. It does lose the extra ls arguments and the date/time format isn't the same.