|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | # encoding: utf-8
|
3 | 3 | import os
|
| 4 | +import stat |
| 5 | +import pwd |
| 6 | +import grp |
| 7 | +import time |
4 | 8 | import argparse
|
5 | 9 |
|
6 | 10 | parser = argparse.ArgumentParser()
|
7 | 11 | parser.add_argument("path", default=".", nargs="?")
|
8 | 12 | 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") |
10 | 14 | args = parser.parse_args()
|
11 | 15 |
|
12 | 16 | def ls():
|
13 | 17 | 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(".")] |
15 | 22 | 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 |
26 | 29 |
|
27 | 30 |
|
28 |
| -def convertAccess(mode): |
29 |
| - access = ["-"] * 10 |
| 31 | +def defOutput(listdir): |
| 32 | + output = "" |
| 33 | + for filename in listdir: |
| 34 | + output += filename + " " |
| 35 | + return output |
30 | 36 |
|
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 |
32 | 53 |
|
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 |
40 | 54 |
|
41 |
| - S_IROTH = 00004 |
42 |
| - S_IWOTH = 00002 |
43 |
| - S_IXOTH = 00001 |
| 55 | +def convertAccess(mode): |
| 56 | + access = ["-"] * 10 |
44 | 57 |
|
45 |
| - if S_IFDIR & mode: |
| 58 | + if stat.S_ISDIR(mode): |
46 | 59 | access[0] = "d"
|
47 |
| - if S_IRUSR & mode: |
| 60 | + elif stat.S_ISLNK(mode): |
| 61 | + access[0] = "l" |
| 62 | + if stat.S_IRUSR & mode: |
48 | 63 | access[1] = "r"
|
49 |
| - if S_IWUSR & mode: |
| 64 | + if stat.S_IWUSR & mode: |
50 | 65 | access[2] = "w"
|
51 |
| - if S_IXUSR & mode: |
| 66 | + if stat.S_IXUSR & mode: |
52 | 67 | access[3] = "x"
|
53 |
| - if S_IRGRP & mode: |
| 68 | + if stat.S_IRGRP & mode: |
54 | 69 | access[4] = "r"
|
55 |
| - if S_IWGRP & mode: |
| 70 | + if stat.S_IWGRP & mode: |
56 | 71 | access[5] = "w"
|
57 |
| - if S_IXGRP & mode: |
| 72 | + if stat.S_IXGRP & mode: |
58 | 73 | access[6] = "x"
|
59 |
| - if S_IROTH & mode: |
| 74 | + if stat.S_IROTH & mode: |
60 | 75 | access[7] = "r"
|
61 |
| - if S_IWOTH & mode: |
| 76 | + if stat.S_IWOTH & mode: |
62 | 77 | access[8] = "w"
|
63 |
| - if S_IXOTH & mode: |
| 78 | + if stat.S_IXOTH & mode: |
64 | 79 | access[9] = "x"
|
65 | 80 |
|
66 | 81 | return "".join(access)
|
|
0 commit comments