-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha1.py
executable file
·50 lines (37 loc) · 963 Bytes
/
sha1.py
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
#!//usr/bin/python
import sys, os, hashlib
def hashfile(filepath):
sha1 = hashlib.sha1()
f = open(filepath, 'rb')
try:
sha1.update(f.read())
finally:
f.close()
return sha1.hexdigest()
def touch(fname, times=None):
with file(fname, 'a'):
os.utime(fname, times)
def usage():
print "usage: %s <file> [action]" %(sys.argv[0])
print " action: print or touch - defaults to print if not specified"
sys.exit(1)
def main():
if len(sys.argv) < 2:
usage()
file = sys.argv[1]
sha1= hashfile(file)
action = "print"
if len(sys.argv) > 2:
action = sys.argv[2]
else:
action = "print"
if action in ['touch','Touch','TOUCH']:
sha1file=file+".sha1."+sha1
touch(sha1file)
print sha1file
elif action in ['print','Print','PRINT']:
print sha1
else:
usage()
if __name__ == "__main__":
main()