-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathls.py
More file actions
124 lines (94 loc) · 3.32 KB
/
ls.py
File metadata and controls
124 lines (94 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
"""
A poor mans implementation of ls.
Supports (for some definition of the term) the -l option.
List of bugs
1. fix formatting for short listing
2. Treatment of directories in input arguments
3.
"""
import argparse
import datetime
import glob
import grp
import logging
import os
import pwd
import stat
import sys
logging.basicConfig(level = logging.CRITICAL)
def get_filenames(d):
"""
Returns filenames in directory d
If d is None:
then return all files in current directory
if d is a list of things
then don't expand. Just return them.
"""
if not d:
return glob.iglob("*")
else:
return d
def short_output(file_list):
for i in file_list:
print "{} ".format(i),
def create_links(info):
return info.st_nlink
def create_owner(info):
return pwd.getpwuid(info.st_uid).pw_name
def create_group(info):
g = grp.getgrgid(info.st_gid)
return g.gr_name
def create_size(info):
return info.st_size
def create_atime(info):
d = datetime.datetime.fromtimestamp(info.st_atime)
return d.strftime("%b %d %H:%M")
def create_permstring(info):
def format_string(mask, c):
if info.st_mode & mask:
return c
else:
return '-'
return "-{}{}{}{}{}{}{}{}{}".format(format_string(stat.S_IRUSR, "r"),
format_string(stat.S_IWUSR, "w"),
format_string(stat.S_IXUSR, "x"),
format_string(stat.S_IRGRP, "r"),
format_string(stat.S_IWGRP, "w"),
format_string(stat.S_IXGRP, "x"),
format_string(stat.S_IROTH, "r"),
format_string(stat.S_IWOTH, "w"),
format_string(stat.S_IXOTH, "x"))
def create_long_line(f):
info = os.stat(f)
return "{} {} {} {} {:5} {} {}".format(create_permstring(info),
create_links(info),
create_owner(info),
create_group(info),
create_size(info),
create_atime(info),
f)
def long_output(file_list):
logging.debug("Trying to print long format")
for f in file_list:
print create_long_line(f)
def parse_args(args):
parser = argparse.ArgumentParser(description = "List information about the FILEs")
parser.add_argument("-l", "--long", help = "Long format output", action = "store_true")
parser.add_argument("-v", "--verbose", help = "Turns on debugging", action = "store_true")
parser.add_argument("files", nargs = argparse.REMAINDER)
args = parser.parse_args()
return args
def main():
args = parse_args(sys.argv[1:])
if args.verbose:
logging.root.setLevel(logging.DEBUG)
logging.debug("Long is set to {}".format(args.long))
logging.debug("Files is set to {}".format(args.files))
filenames = get_filenames(args.files)
if args.long:
long_output(filenames)
else:
short_output(filenames)
if __name__ == '__main__':
main()