-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlstimes
More file actions
executable file
·227 lines (185 loc) · 6.81 KB
/
Copy pathlstimes
File metadata and controls
executable file
·227 lines (185 loc) · 6.81 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
#
# lstimes: Like ls, but with much more information.
# 2016-02-05: Written by Steven J. DeRose.
#
import sys
import os
import argparse
import stat
import pwd
import time
__metadata__ = {
"title" : "lstimes",
"description" : "Like ls, but with much more information.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.7",
"created" : "2016-02-05",
"modified" : "2022-04-12",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__["modified"]
descr = """
=Description=
UNFINISHED
Show lots more information about a file than ls does.
=Related Commands=
`ls`, `stat -x`, `file`, `PowerStat.py`, ....
=Known bugs and Limitations=
=History=
* 2016-02-05: Written by Steven J. DeRose.
* 2016-02-05, 2019-10-29: Lint.
* 2020-08-23: layout, fix report format.
* 2022-03-11: lint, POD to MarkDown.
* 2022-04-12: Factor out display formatting. Add --convert for times.
=To do=
* INTEGRATE WITH "lss", "lsoutline". Subsume into PowerStat?
* Add options to actually set the display format.
* Add H-size conversion
=Rights=
Copyright 2016-02-05 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share-alike 3.0 unported license.
For further information on this license, see
[https://creativecommons.org/licenses/by-sa/3.0].
For the most recent version, see [http://www.derose.net/steve/utilities]
or [https://github.com/sderose].
=Options=
"""
def warn(lvl, msg):
if (args.verbose >= lvl): sys.stderr.write(msg+"\n")
def convTime(epochTime):
return time.ctime(epochTime)
# Format is a list of tuples of (fieldName, displayName, sprintfCode).
#
scode = "%30s"
defaultFormat = [
#(fieldName, displayName, sprintfCode), conversionFunc
("nm", "filename", scode, None),
("isDir", "is directory", scode, None),
("lnk", "is symlink", scode, None),
("filemode","permissions", scode, None),
#("eh", "eh", scode, None),
("usr", "user", scode, None),
("grp", "group", scode, None),
("sz", "size", scode, None),
("ct", "created", scode, convTime),
("mt", "modified", scode, convTime),
("at", "accessed", scode, convTime),
("ino", "inode", scode, None),
("flag", "flags", scode, None),
]
def formatInfo(theDict, theFormat):
buf = ""
for fieldName, displayName, sprintfCode, conv in theFormat:
thisFmt = " %-16s: " + sprintfCode + "\n"
fieldValue = "???"
if (fieldName in theDict):
fieldValue = theDict[fieldName]
if (args.convert and conv is not None): fieldValue = conv(fieldValue)
buf += thisFmt % (displayName, fieldValue)
return buf
###############################################################################
#
def tryOneItem(path, theFormat):
"""Try to open a file (or directory, if -r is set).
"""
warn(1, "Starting item '%s'" % (path))
recnum = 0
if (not os.path.exists(path)):
sys.stderr.write("Couldn't find '%s'.\n" % (path))
elif (os.path.isdir(path)):
if (args.recursive):
for child in os.listdir(path):
recnum += tryOneItem(os.path.join(path,child), theFormat)
else:
warn(1, "Skipping directory '%s'." % (path))
else:
doOneFile(path, theFormat)
return(recnum)
def doOneFile(path, theFormat):
"""Deal with one individual file.
"""
theInfo = {}
s = os.stat(path) # follow_symlinks?
mode = s.st_mode # protection bits,
theInfo["mode"] = mode
theInfo["ino"] = s.st_ino # inode number,
theInfo["dev"] = s.st_dev # device,
theInfo["nl"] = s.st_nlink # number of hard links,
theInfo["uid"] = s.st_uid # user id of owner,
theInfo["gid"] = s.st_gid # group id of owner,
theInfo["sz"] = s.st_size # size of file, in bytes,
theInfo["at"] = s.st_atime # time of most recent access,
theInfo["mt"] = s.st_mtime # time of most recent content modification,
theInfo["ct"] = s.st_ctime # time of last metadata change on Unix; cre on Win
theInfo["isDir"] = os.path.isdir(path)
theInfo["lnk"] = os.path.islink(path)
theInfo["eh"] = -1
theInfo["usr"] = pwd.getpwuid(theInfo["uid"])[0]
# heInfo["grp"] = grp.getgrgid(theInfo["gid"])[0]
theInfo["grp"] = "???"
#theInfo["sz"] = os.path.getsize(path)
#theInfo["ct"] = os.path.getctime(path)
#theInfo["mt"] = os.path.getmtime(path)
#theInfo["at"] = os.path.getatime(path)
theInfo["nm"] = os.path.basename(path)
theInfo["flag"] = getFileFlagChar(path)
theInfo["filemode"] = stat.filemode(mode)
print(path + ":\n" + formatInfo(theInfo, theFormat))
###############################################################################
#
def getFileFlagChar(path):
# There's a more complete version of this in PowerStat.getFlag()....
if (os.path.isdir(path)): return("/")
return("")
def printHeader(theFormat):
print(theFormat %
("per", "lnk", "eh",
"usr", "grp", "sz", "ct",
"mt", "at", "nm", "flag", "dir"))
return
###############################################################################
# Main
#
def processOptions():
try:
from BlockFormatter import BlockFormatter
parser = argparse.ArgumentParser(
description=descr, formatter_class=BlockFormatter)
except ImportError:
parser = argparse.ArgumentParser(description=descr)
parser.add_argument(
"--convert", action="store_true", default=True,
help="Convert times to readable form (default).")
parser.add_argument(
"--no-convert", action="store_false", dest="convert",
help="Do not convert times to readable form.")
parser.add_argument(
"--format", "-f", type=str,
help="(not yet supported, see PowerStat.py)")
parser.add_argument(
"--quiet", "-q", action="store_true",
help="Suppress most messages.")
parser.add_argument(
"--recursive", "-r", action="store_true",
help="Traverse into directories.")
parser.add_argument(
"--verbose", "-v", action="count", default=0,
help="Add more messages (repeatable).")
parser.add_argument(
"--version", action="version", version=__version__,
help="Display version information, then exit.")
parser.add_argument(
"files", type=str, nargs=argparse.REMAINDER,
help="Path(s) to input file(s)")
args0 = parser.parse_args()
return(args0)
args = processOptions()
if (len(args.files) == 0):
warn(0, "No files specified....")
sys.exit()
for f in (args.files):
tryOneItem(f, defaultFormat)