##// END OF EJS Templates
py3: port f to Python 3...
Gregory Szorc -
r36275:c69e78ef default
parent child Browse files
Show More
@@ -25,6 +25,7 b' This can be used instead of tools like:'
25
25
26 from __future__ import absolute_import
26 from __future__ import absolute_import
27
27
28 import binascii
28 import glob
29 import glob
29 import hashlib
30 import hashlib
30 import optparse
31 import optparse
@@ -58,46 +59,47 b' def visit(opts, filenames, outfile):'
58 facts = []
59 facts = []
59 if isfile:
60 if isfile:
60 if opts.type:
61 if opts.type:
61 facts.append('file')
62 facts.append(b'file')
62 if any((opts.hexdump, opts.dump, opts.md5, opts.sha1, opts.sha256)):
63 if any((opts.hexdump, opts.dump, opts.md5, opts.sha1, opts.sha256)):
63 content = open(f, 'rb').read()
64 content = open(f, 'rb').read()
64 elif islink:
65 elif islink:
65 if opts.type:
66 if opts.type:
66 facts.append('link')
67 facts.append(b'link')
67 content = os.readlink(f)
68 content = os.readlink(f)
68 elif isstdin:
69 elif isstdin:
69 content = getattr(sys.stdin, 'buffer', sys.stdin).read()
70 content = getattr(sys.stdin, 'buffer', sys.stdin).read()
70 if opts.size:
71 if opts.size:
71 facts.append('size=%s' % len(content))
72 facts.append(b'size=%d' % len(content))
72 elif isdir:
73 elif isdir:
73 if opts.recurse or opts.type:
74 if opts.recurse or opts.type:
74 dirfiles = glob.glob(f + '/*')
75 dirfiles = glob.glob(f + '/*')
75 facts.append('directory with %s files' % len(dirfiles))
76 facts.append(b'directory with %d files' % len(dirfiles))
76 elif opts.type:
77 elif opts.type:
77 facts.append('type unknown')
78 facts.append(b'type unknown')
78 if not isstdin:
79 if not isstdin:
79 stat = os.lstat(f)
80 stat = os.lstat(f)
80 if opts.size and not isdir:
81 if opts.size and not isdir:
81 facts.append('size=%s' % stat.st_size)
82 facts.append(b'size=%d' % stat.st_size)
82 if opts.mode and not islink:
83 if opts.mode and not islink:
83 facts.append('mode=%o' % (stat.st_mode & 0o777))
84 facts.append(b'mode=%o' % (stat.st_mode & 0o777))
84 if opts.links:
85 if opts.links:
85 facts.append('links=%s' % stat.st_nlink)
86 facts.append(b'links=%s' % stat.st_nlink)
86 if opts.newer:
87 if opts.newer:
87 # mtime might be in whole seconds so newer file might be same
88 # mtime might be in whole seconds so newer file might be same
88 if stat.st_mtime >= os.stat(opts.newer).st_mtime:
89 if stat.st_mtime >= os.stat(opts.newer).st_mtime:
89 facts.append('newer than %s' % opts.newer)
90 facts.append(b'newer than %s' % opts.newer)
90 else:
91 else:
91 facts.append('older than %s' % opts.newer)
92 facts.append(b'older than %s' % opts.newer)
92 if opts.md5 and content is not None:
93 if opts.md5 and content is not None:
93 h = hashlib.md5(content)
94 h = hashlib.md5(content)
94 facts.append('md5=%s' % h.hexdigest()[:opts.bytes])
95 facts.append(b'md5=%s' % binascii.hexlify(h.digest())[:opts.bytes])
95 if opts.sha1 and content is not None:
96 if opts.sha1 and content is not None:
96 h = hashlib.sha1(content)
97 h = hashlib.sha1(content)
97 facts.append('sha1=%s' % h.hexdigest()[:opts.bytes])
98 facts.append(b'sha1=%s' % binascii.hexlify(h.digest())[:opts.bytes])
98 if opts.sha256 and content is not None:
99 if opts.sha256 and content is not None:
99 h = hashlib.sha256(content)
100 h = hashlib.sha256(content)
100 facts.append('sha256=%s' % h.hexdigest()[:opts.bytes])
101 facts.append(b'sha256=%s' %
102 binascii.hexlify(h.digest())[:opts.bytes])
101 if isstdin:
103 if isstdin:
102 outfile.write(b', '.join(facts) + b'\n')
104 outfile.write(b', '.join(facts) + b'\n')
103 elif facts:
105 elif facts:
General Comments 0
You need to be logged in to leave comments. Login now