Show More
@@ -1,158 +1,158 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 | """ |
|
3 | """ | |
4 | Utility for inspecting files in various ways. |
|
4 | Utility for inspecting files in various ways. | |
5 |
|
5 | |||
6 | This tool is like the collection of tools found in a unix environment but are |
|
6 | This tool is like the collection of tools found in a unix environment but are | |
7 | cross platform and stable and suitable for our needs in the test suite. |
|
7 | cross platform and stable and suitable for our needs in the test suite. | |
8 |
|
8 | |||
9 | This can be used instead of tools like: |
|
9 | This can be used instead of tools like: | |
10 | [ |
|
10 | [ | |
11 | dd |
|
11 | dd | |
12 | find |
|
12 | find | |
13 | head |
|
13 | head | |
14 | hexdump |
|
14 | hexdump | |
15 | ls |
|
15 | ls | |
16 | md5sum |
|
16 | md5sum | |
17 | readlink |
|
17 | readlink | |
18 | sha1sum |
|
18 | sha1sum | |
19 | stat |
|
19 | stat | |
20 | tail |
|
20 | tail | |
21 | test |
|
21 | test | |
22 | readlink.py |
|
22 | readlink.py | |
23 | md5sum.py |
|
23 | md5sum.py | |
24 | """ |
|
24 | """ | |
25 |
|
25 | |||
26 | import sys, os, errno, re, glob, optparse |
|
26 | import sys, os, errno, re, glob, optparse | |
27 |
|
27 | |||
28 | def visit(opts, filenames, outfile): |
|
28 | def visit(opts, filenames, outfile): | |
29 | """Process filenames in the way specified in opts, writing output to |
|
29 | """Process filenames in the way specified in opts, writing output to | |
30 | outfile.""" |
|
30 | outfile.""" | |
31 | for f in sorted(filenames): |
|
31 | for f in sorted(filenames): | |
32 | isstdin = f == '-' |
|
32 | isstdin = f == '-' | |
33 | if not isstdin and not os.path.lexists(f): |
|
33 | if not isstdin and not os.path.lexists(f): | |
34 | outfile.write('%s: file not found\n' % f) |
|
34 | outfile.write('%s: file not found\n' % f) | |
35 | continue |
|
35 | continue | |
36 | quiet = opts.quiet and not opts.recurse or isstdin |
|
36 | quiet = opts.quiet and not opts.recurse or isstdin | |
37 | isdir = os.path.isdir(f) |
|
37 | isdir = os.path.isdir(f) | |
38 | islink = os.path.islink(f) |
|
38 | islink = os.path.islink(f) | |
39 | isfile = os.path.isfile(f) and not islink |
|
39 | isfile = os.path.isfile(f) and not islink | |
40 | dirfiles = None |
|
40 | dirfiles = None | |
41 | content = None |
|
41 | content = None | |
42 | facts = [] |
|
42 | facts = [] | |
43 | if isfile: |
|
43 | if isfile: | |
44 | if opts.type: |
|
44 | if opts.type: | |
45 | facts.append('file') |
|
45 | facts.append('file') | |
46 | if opts.hexdump or opts.dump or opts.md5: |
|
46 | if opts.hexdump or opts.dump or opts.md5: | |
47 | content = file(f).read() |
|
47 | content = file(f).read() | |
48 | elif islink: |
|
48 | elif islink: | |
49 | if opts.type: |
|
49 | if opts.type: | |
50 | facts.append('link') |
|
50 | facts.append('link') | |
51 | content = os.readlink(f) |
|
51 | content = os.readlink(f) | |
52 | elif isstdin: |
|
52 | elif isstdin: | |
53 | content = sys.stdin.read() |
|
53 | content = sys.stdin.read() | |
54 | if opts.size: |
|
54 | if opts.size: | |
55 | facts.append('size=%s' % len(content)) |
|
55 | facts.append('size=%s' % len(content)) | |
56 | elif isdir: |
|
56 | elif isdir: | |
57 | if opts.recurse or opts.type: |
|
57 | if opts.recurse or opts.type: | |
58 | dirfiles = glob.glob(f + '/*') |
|
58 | dirfiles = glob.glob(f + '/*') | |
59 | facts.append('directory with %s files' % len(dirfiles)) |
|
59 | facts.append('directory with %s files' % len(dirfiles)) | |
60 | elif opts.type: |
|
60 | elif opts.type: | |
61 | facts.append('type unknown') |
|
61 | facts.append('type unknown') | |
62 | if not isstdin: |
|
62 | if not isstdin: | |
63 | stat = os.lstat(f) |
|
63 | stat = os.lstat(f) | |
64 | if opts.size and not isdir: |
|
64 | if opts.size and not isdir: | |
65 | facts.append('size=%s' % stat.st_size) |
|
65 | facts.append('size=%s' % stat.st_size) | |
66 | if opts.mode: |
|
66 | if opts.mode and not islink: | |
67 | facts.append('mode=%o' % (stat.st_mode & 0777)) |
|
67 | facts.append('mode=%o' % (stat.st_mode & 0777)) | |
68 | if opts.links: |
|
68 | if opts.links: | |
69 | facts.append('links=%s' % stat.st_nlink) |
|
69 | facts.append('links=%s' % stat.st_nlink) | |
70 | if opts.newer: |
|
70 | if opts.newer: | |
71 | # mtime might be in whole seconds so newer file might be same |
|
71 | # mtime might be in whole seconds so newer file might be same | |
72 | if stat.st_mtime >= os.stat(opts.newer).st_mtime: |
|
72 | if stat.st_mtime >= os.stat(opts.newer).st_mtime: | |
73 | facts.append('newer than %s' % opts.newer) |
|
73 | facts.append('newer than %s' % opts.newer) | |
74 | else: |
|
74 | else: | |
75 | facts.append('older than %s' % opts.newer) |
|
75 | facts.append('older than %s' % opts.newer) | |
76 | if opts.md5 and content is not None: |
|
76 | if opts.md5 and content is not None: | |
77 | try: |
|
77 | try: | |
78 | from hashlib import md5 |
|
78 | from hashlib import md5 | |
79 | except ImportError: |
|
79 | except ImportError: | |
80 | from md5 import md5 |
|
80 | from md5 import md5 | |
81 | facts.append('md5=%s' % md5(content).hexdigest()[:opts.bytes]) |
|
81 | facts.append('md5=%s' % md5(content).hexdigest()[:opts.bytes]) | |
82 | if opts.sha1 and content is not None: |
|
82 | if opts.sha1 and content is not None: | |
83 | try: |
|
83 | try: | |
84 | from hashlib import sha1 |
|
84 | from hashlib import sha1 | |
85 | except ImportError: |
|
85 | except ImportError: | |
86 | from sha import sha as sha1 |
|
86 | from sha import sha as sha1 | |
87 | facts.append('sha1=%s' % sha1(content).hexdigest()[:opts.bytes]) |
|
87 | facts.append('sha1=%s' % sha1(content).hexdigest()[:opts.bytes]) | |
88 | if isstdin: |
|
88 | if isstdin: | |
89 | outfile.write(', '.join(facts) + '\n') |
|
89 | outfile.write(', '.join(facts) + '\n') | |
90 | elif facts: |
|
90 | elif facts: | |
91 | outfile.write('%s: %s\n' % (f, ', '.join(facts))) |
|
91 | outfile.write('%s: %s\n' % (f, ', '.join(facts))) | |
92 | elif not quiet: |
|
92 | elif not quiet: | |
93 | outfile.write('%s:\n' % f) |
|
93 | outfile.write('%s:\n' % f) | |
94 | if content is not None: |
|
94 | if content is not None: | |
95 | chunk = content |
|
95 | chunk = content | |
96 | if not islink: |
|
96 | if not islink: | |
97 | if opts.lines: |
|
97 | if opts.lines: | |
98 | if opts.lines >= 0: |
|
98 | if opts.lines >= 0: | |
99 | chunk = ''.join(chunk.splitlines(True)[:opts.lines]) |
|
99 | chunk = ''.join(chunk.splitlines(True)[:opts.lines]) | |
100 | else: |
|
100 | else: | |
101 | chunk = ''.join(chunk.splitlines(True)[opts.lines:]) |
|
101 | chunk = ''.join(chunk.splitlines(True)[opts.lines:]) | |
102 | if opts.bytes: |
|
102 | if opts.bytes: | |
103 | if opts.bytes >= 0: |
|
103 | if opts.bytes >= 0: | |
104 | chunk = chunk[:opts.bytes] |
|
104 | chunk = chunk[:opts.bytes] | |
105 | else: |
|
105 | else: | |
106 | chunk = chunk[opts.bytes:] |
|
106 | chunk = chunk[opts.bytes:] | |
107 | if opts.hexdump: |
|
107 | if opts.hexdump: | |
108 | for i in range(0, len(chunk), 16): |
|
108 | for i in range(0, len(chunk), 16): | |
109 | s = chunk[i:i+16] |
|
109 | s = chunk[i:i+16] | |
110 | outfile.write('%04x: %-47s |%s|\n' % |
|
110 | outfile.write('%04x: %-47s |%s|\n' % | |
111 | (i, ' '.join('%02x' % ord(c) for c in s), |
|
111 | (i, ' '.join('%02x' % ord(c) for c in s), | |
112 | re.sub('[^ -~]', '.', s))) |
|
112 | re.sub('[^ -~]', '.', s))) | |
113 | if opts.dump: |
|
113 | if opts.dump: | |
114 | if not quiet: |
|
114 | if not quiet: | |
115 | outfile.write('>>>\n') |
|
115 | outfile.write('>>>\n') | |
116 | outfile.write(chunk) |
|
116 | outfile.write(chunk) | |
117 | if not quiet: |
|
117 | if not quiet: | |
118 | if chunk.endswith('\n'): |
|
118 | if chunk.endswith('\n'): | |
119 | outfile.write('<<<\n') |
|
119 | outfile.write('<<<\n') | |
120 | else: |
|
120 | else: | |
121 | outfile.write('\n<<< no trailing newline\n') |
|
121 | outfile.write('\n<<< no trailing newline\n') | |
122 | if opts.recurse and dirfiles: |
|
122 | if opts.recurse and dirfiles: | |
123 | assert not isstdin |
|
123 | assert not isstdin | |
124 | visit(opts, dirfiles, outfile) |
|
124 | visit(opts, dirfiles, outfile) | |
125 |
|
125 | |||
126 | if __name__ == "__main__": |
|
126 | if __name__ == "__main__": | |
127 | parser = optparse.OptionParser("%prog [options] [filenames]") |
|
127 | parser = optparse.OptionParser("%prog [options] [filenames]") | |
128 | parser.add_option("-t", "--type", action="store_true", |
|
128 | parser.add_option("-t", "--type", action="store_true", | |
129 | help="show file type (file or directory)") |
|
129 | help="show file type (file or directory)") | |
130 | parser.add_option("-m", "--mode", action="store_true", |
|
130 | parser.add_option("-m", "--mode", action="store_true", | |
131 | help="show file mode") |
|
131 | help="show file mode") | |
132 | parser.add_option("-l", "--links", action="store_true", |
|
132 | parser.add_option("-l", "--links", action="store_true", | |
133 | help="show number of links") |
|
133 | help="show number of links") | |
134 | parser.add_option("-s", "--size", action="store_true", |
|
134 | parser.add_option("-s", "--size", action="store_true", | |
135 | help="show size of file") |
|
135 | help="show size of file") | |
136 | parser.add_option("-n", "--newer", action="store", |
|
136 | parser.add_option("-n", "--newer", action="store", | |
137 | help="check if file is newer (or same)") |
|
137 | help="check if file is newer (or same)") | |
138 | parser.add_option("-r", "--recurse", action="store_true", |
|
138 | parser.add_option("-r", "--recurse", action="store_true", | |
139 | help="recurse into directories") |
|
139 | help="recurse into directories") | |
140 | parser.add_option("-S", "--sha1", action="store_true", |
|
140 | parser.add_option("-S", "--sha1", action="store_true", | |
141 | help="show sha1 hash of the content") |
|
141 | help="show sha1 hash of the content") | |
142 | parser.add_option("-M", "--md5", action="store_true", |
|
142 | parser.add_option("-M", "--md5", action="store_true", | |
143 | help="show md5 hash of the content") |
|
143 | help="show md5 hash of the content") | |
144 | parser.add_option("-D", "--dump", action="store_true", |
|
144 | parser.add_option("-D", "--dump", action="store_true", | |
145 | help="dump file content") |
|
145 | help="dump file content") | |
146 | parser.add_option("-H", "--hexdump", action="store_true", |
|
146 | parser.add_option("-H", "--hexdump", action="store_true", | |
147 | help="hexdump file content") |
|
147 | help="hexdump file content") | |
148 | parser.add_option("-B", "--bytes", type="int", |
|
148 | parser.add_option("-B", "--bytes", type="int", | |
149 | help="number of characters to dump") |
|
149 | help="number of characters to dump") | |
150 | parser.add_option("-L", "--lines", type="int", |
|
150 | parser.add_option("-L", "--lines", type="int", | |
151 | help="number of lines to dump") |
|
151 | help="number of lines to dump") | |
152 | parser.add_option("-q", "--quiet", action="store_true", |
|
152 | parser.add_option("-q", "--quiet", action="store_true", | |
153 | help="no default output") |
|
153 | help="no default output") | |
154 | (opts, filenames) = parser.parse_args(sys.argv[1:]) |
|
154 | (opts, filenames) = parser.parse_args(sys.argv[1:]) | |
155 | if not filenames: |
|
155 | if not filenames: | |
156 | filenames = ['-'] |
|
156 | filenames = ['-'] | |
157 |
|
157 | |||
158 | visit(opts, filenames, sys.stdout) |
|
158 | visit(opts, filenames, sys.stdout) |
@@ -1,108 +1,108 | |||||
1 | Tests of the file helper tool |
|
1 | Tests of the file helper tool | |
2 |
|
2 | |||
3 | $ f -h |
|
3 | $ f -h | |
4 | ?sage: f [options] [filenames] (glob) |
|
4 | ?sage: f [options] [filenames] (glob) | |
5 |
|
5 | |||
6 | Options: |
|
6 | Options: | |
7 | -h, --help show this help message and exit |
|
7 | -h, --help show this help message and exit | |
8 | -t, --type show file type (file or directory) |
|
8 | -t, --type show file type (file or directory) | |
9 | -m, --mode show file mode |
|
9 | -m, --mode show file mode | |
10 | -l, --links show number of links |
|
10 | -l, --links show number of links | |
11 | -s, --size show size of file |
|
11 | -s, --size show size of file | |
12 | -n NEWER, --newer=NEWER |
|
12 | -n NEWER, --newer=NEWER | |
13 | check if file is newer (or same) |
|
13 | check if file is newer (or same) | |
14 | -r, --recurse recurse into directories |
|
14 | -r, --recurse recurse into directories | |
15 | -S, --sha1 show sha1 hash of the content |
|
15 | -S, --sha1 show sha1 hash of the content | |
16 | -M, --md5 show md5 hash of the content |
|
16 | -M, --md5 show md5 hash of the content | |
17 | -D, --dump dump file content |
|
17 | -D, --dump dump file content | |
18 | -H, --hexdump hexdump file content |
|
18 | -H, --hexdump hexdump file content | |
19 | -B BYTES, --bytes=BYTES |
|
19 | -B BYTES, --bytes=BYTES | |
20 | number of characters to dump |
|
20 | number of characters to dump | |
21 | -L LINES, --lines=LINES |
|
21 | -L LINES, --lines=LINES | |
22 | number of lines to dump |
|
22 | number of lines to dump | |
23 | -q, --quiet no default output |
|
23 | -q, --quiet no default output | |
24 |
|
24 | |||
25 | $ mkdir dir |
|
25 | $ mkdir dir | |
26 | $ cd dir |
|
26 | $ cd dir | |
27 |
|
27 | |||
28 | $ f --size |
|
28 | $ f --size | |
29 | size=0 |
|
29 | size=0 | |
30 |
|
30 | |||
31 | $ echo hello | f --md5 --size |
|
31 | $ echo hello | f --md5 --size | |
32 | size=6, md5=b1946ac92492d2347c6235b4d2611184 |
|
32 | size=6, md5=b1946ac92492d2347c6235b4d2611184 | |
33 |
|
33 | |||
34 | $ f foo |
|
34 | $ f foo | |
35 | foo: file not found |
|
35 | foo: file not found | |
36 |
|
36 | |||
37 | $ echo foo > foo |
|
37 | $ echo foo > foo | |
38 | $ f foo |
|
38 | $ f foo | |
39 | foo: |
|
39 | foo: | |
40 |
|
40 | |||
41 | #if symlink |
|
41 | #if symlink | |
42 | $ f foo --mode |
|
42 | $ f foo --mode | |
43 | foo: mode=644 |
|
43 | foo: mode=644 | |
44 | #endif |
|
44 | #endif | |
45 |
|
45 | |||
46 | $ seq 10 > bar |
|
46 | $ seq 10 > bar | |
47 | #if unix-permissions symlink |
|
47 | #if unix-permissions symlink | |
48 | $ chmod +x bar |
|
48 | $ chmod +x bar | |
49 | $ f bar --newer foo --mode --type --size --dump --links --bytes 7 |
|
49 | $ f bar --newer foo --mode --type --size --dump --links --bytes 7 | |
50 | bar: file, size=21, mode=755, links=1, newer than foo |
|
50 | bar: file, size=21, mode=755, links=1, newer than foo | |
51 | >>> |
|
51 | >>> | |
52 | 1 |
|
52 | 1 | |
53 | 2 |
|
53 | 2 | |
54 | 3 |
|
54 | 3 | |
55 | 4 |
|
55 | 4 | |
56 | <<< no trailing newline |
|
56 | <<< no trailing newline | |
57 | #endif |
|
57 | #endif | |
58 |
|
58 | |||
59 | $ ln bar baz |
|
59 | $ ln bar baz | |
60 | $ f bar -n baz -l --hexdump -t --sha1 --lines=9 -B 20 |
|
60 | $ f bar -n baz -l --hexdump -t --sha1 --lines=9 -B 20 | |
61 | bar: file, links=2, newer than baz, sha1=612ca68d0305c821750a |
|
61 | bar: file, links=2, newer than baz, sha1=612ca68d0305c821750a | |
62 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| |
|
62 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| | |
63 | 0010: 39 0a |9.| |
|
63 | 0010: 39 0a |9.| | |
64 |
|
64 | |||
65 | #if unix-permissions symlink |
|
65 | #if unix-permissions symlink | |
66 | $ ln -s yadda l |
|
66 | $ ln -s yadda l | |
67 | $ f . --recurse -MStmsB4 |
|
67 | $ f . --recurse -MStmsB4 | |
68 | .: directory with 4 files, mode=755 |
|
68 | .: directory with 4 files, mode=755 | |
69 | ./bar: file, size=21, mode=755, md5=3b03, sha1=612c |
|
69 | ./bar: file, size=21, mode=755, md5=3b03, sha1=612c | |
70 | ./baz: file, size=21, mode=755, md5=3b03, sha1=612c |
|
70 | ./baz: file, size=21, mode=755, md5=3b03, sha1=612c | |
71 | ./foo: file, size=4, mode=644, md5=d3b0, sha1=f1d2 |
|
71 | ./foo: file, size=4, mode=644, md5=d3b0, sha1=f1d2 | |
72 |
./l: link, size=5, |
|
72 | ./l: link, size=5, md5=2faa, sha1=af93 | |
73 | #endif |
|
73 | #endif | |
74 |
|
74 | |||
75 | $ f --quiet bar -DL 3 |
|
75 | $ f --quiet bar -DL 3 | |
76 | 1 |
|
76 | 1 | |
77 | 2 |
|
77 | 2 | |
78 | 3 |
|
78 | 3 | |
79 |
|
79 | |||
80 | $ cd .. |
|
80 | $ cd .. | |
81 |
|
81 | |||
82 | Yadda is a symlink |
|
82 | Yadda is a symlink | |
83 | #if symlink |
|
83 | #if symlink | |
84 | $ f -qr dir -HB 17 |
|
84 | $ f -qr dir -HB 17 | |
85 | dir: directory with 4 files |
|
85 | dir: directory with 4 files | |
86 | dir/bar: |
|
86 | dir/bar: | |
87 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| |
|
87 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| | |
88 | 0010: 39 |9| |
|
88 | 0010: 39 |9| | |
89 | dir/baz: |
|
89 | dir/baz: | |
90 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| |
|
90 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| | |
91 | 0010: 39 |9| |
|
91 | 0010: 39 |9| | |
92 | dir/foo: |
|
92 | dir/foo: | |
93 | 0000: 66 6f 6f 0a |foo.| |
|
93 | 0000: 66 6f 6f 0a |foo.| | |
94 | dir/l: |
|
94 | dir/l: | |
95 | 0000: 79 61 64 64 61 |yadda| |
|
95 | 0000: 79 61 64 64 61 |yadda| | |
96 | #else |
|
96 | #else | |
97 | $ f -qr dir -HB 17 |
|
97 | $ f -qr dir -HB 17 | |
98 | dir: directory with 3 files |
|
98 | dir: directory with 3 files | |
99 | dir/bar: (glob) |
|
99 | dir/bar: (glob) | |
100 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| |
|
100 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| | |
101 | 0010: 39 |9| |
|
101 | 0010: 39 |9| | |
102 | dir/baz: (glob) |
|
102 | dir/baz: (glob) | |
103 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| |
|
103 | 0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.| | |
104 | 0010: 39 |9| |
|
104 | 0010: 39 |9| | |
105 | dir/foo: (glob) |
|
105 | dir/foo: (glob) | |
106 | 0000: 66 6f 6f 0a |foo.| |
|
106 | 0000: 66 6f 6f 0a |foo.| | |
107 | #endif |
|
107 | #endif | |
108 |
|
108 |
General Comments 0
You need to be logged in to leave comments.
Login now