-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash-dir-creator.py
executable file
·64 lines (52 loc) · 1.28 KB
/
hash-dir-creator.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
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python2
import datetime
import errno
import hashlib
import os
import sys
# md5
# sha1
# sha256
# sha512
# set default values
# datetime.now() simple enough to be unique
data = str(datetime.datetime.now())
hash_def = 'sha256'
# the chunk size (split chars at this)
step = 6
# create a hash using defined hash and data string
def _hashit(hash_name,data):
m = hashlib.new(hash_name)
m.update(data)
return m.hexdigest()
# split the data at 'step' characters and put in list
def _split_by_n(data,n):
while data:
yield data[:n]
data = data[n:]
# directory-ize it by appending '/' while iterating list to string
def _ize_it(datalist):
ized = ''
for li in datalist:
ized = ized + li + '/'
return ized
# create dir structure
def _make_path(path):
try:
os.makedirs(path,0751)
os.chmod(path,0755)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
# see if we were passed any args and use them to override if so
try:
hash_name = sys.argv[1]
except IndexError:
hash_name = hash_def
else:
try:
data = sys.argv[2]
except IndexError:
data = str(datetime.datetime.now())
# make it reality
_make_path(_ize_it(list(_split_by_n(_hashit(hash_name,data),step))))