Skip to content

Commit 1bcf08e

Browse files
committed
Merge pull request #2 from vortispy/issue1
Issue1
2 parents 3294ae4 + 2c3ea5f commit 1bcf08e

File tree

1 file changed

+50
-35
lines changed

1 file changed

+50
-35
lines changed

ls.py

+50-35
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,81 @@
11
#!/usr/bin/env python
22
# encoding: utf-8
33
import os
4+
import stat
5+
import pwd
6+
import grp
7+
import time
48
import argparse
59

610
parser = argparse.ArgumentParser()
711
parser.add_argument("path", default=".", nargs="?")
812
parser.add_argument("-a", "--all", action="store_true")
9-
parser.add_argument("-l", "--list", action="store_true")
13+
parser.add_argument("-l", "--long", action="store_true")
1014
args = parser.parse_args()
1115

1216
def ls():
1317
path = args.path
14-
listdir = os.listdir(path)
18+
if args.all:
19+
listdir = os.listdir(path)
20+
else:
21+
listdir = [f for f in os.listdir(path) if not f.startswith(".")]
1522
listdir.sort()
16-
for filename in listdir:
17-
if args.all:
18-
print filename,
19-
elif not filename.startswith("."):
20-
print filename,
21-
elif args.list:
22-
filepath = os.path.join(path, filename)
23-
mode = os.stat(filepath).st_mode
24-
access = convertAccess(mode)
25-
print access + " " + filename
23+
output = ""
24+
if args.long:
25+
output = longlist(path, listdir)
26+
else:
27+
output = defOutput(listdir)
28+
print output
2629

2730

28-
def convertAccess(mode):
29-
access = ["-"] * 10
31+
def defOutput(listdir):
32+
output = ""
33+
for filename in listdir:
34+
output += filename + " "
35+
return output
3036

31-
S_IFDIR = 004000
37+
def longlist(argPath, listdir):
38+
output = ""
39+
for filename in listdir:
40+
filepath = os.path.join(argPath, filename)
41+
file_stat = os.lstat(filepath)
42+
mode = file_stat.st_mode
43+
filename += "/" if stat.S_ISDIR(mode) else ""
44+
nlink = file_stat.st_nlink
45+
username = pwd.getpwuid(file_stat.st_uid).pw_name
46+
group = grp.getgrgid(file_stat.st_gid).gr_name
47+
fsize = file_stat.st_size
48+
access = convertAccess(mode)
49+
ctime = time.strftime("%b %d %H:%M", time.localtime(file_stat.st_ctime))
50+
output += "%s %d %s %s %d %s %s\n" % (access, nlink, username, group, fsize, ctime, filename)
51+
52+
return output
3253

33-
S_IRUSR = 00400
34-
S_IWUSR = 00200
35-
S_IXUSR = 00100
36-
37-
S_IRGRP = 00040
38-
S_IWGRP = 00020
39-
S_IXGRP = 00010
4054

41-
S_IROTH = 00004
42-
S_IWOTH = 00002
43-
S_IXOTH = 00001
55+
def convertAccess(mode):
56+
access = ["-"] * 10
4457

45-
if S_IFDIR & mode:
58+
if stat.S_ISDIR(mode):
4659
access[0] = "d"
47-
if S_IRUSR & mode:
60+
elif stat.S_ISLNK(mode):
61+
access[0] = "l"
62+
if stat.S_IRUSR & mode:
4863
access[1] = "r"
49-
if S_IWUSR & mode:
64+
if stat.S_IWUSR & mode:
5065
access[2] = "w"
51-
if S_IXUSR & mode:
66+
if stat.S_IXUSR & mode:
5267
access[3] = "x"
53-
if S_IRGRP & mode:
68+
if stat.S_IRGRP & mode:
5469
access[4] = "r"
55-
if S_IWGRP & mode:
70+
if stat.S_IWGRP & mode:
5671
access[5] = "w"
57-
if S_IXGRP & mode:
72+
if stat.S_IXGRP & mode:
5873
access[6] = "x"
59-
if S_IROTH & mode:
74+
if stat.S_IROTH & mode:
6075
access[7] = "r"
61-
if S_IWOTH & mode:
76+
if stat.S_IWOTH & mode:
6277
access[8] = "w"
63-
if S_IXOTH & mode:
78+
if stat.S_IXOTH & mode:
6479
access[9] = "x"
6580

6681
return "".join(access)

0 commit comments

Comments
 (0)