forked from kieranjol/IFIscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.py
executable file
·89 lines (73 loc) · 3.63 KB
/
manifest.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
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
#!/usr/bin/env python
import sys
import subprocess
import os
import getpass
import argparse
import time
from ififuncs import generate_log
from ififuncs import manifest_file_count
from ififuncs import make_manifest
def remove_bad_files(root_dir):
rm_these = ['.DS_Store', 'Thumbs.db', 'desktop.ini']
for root, dirs, files in os.walk(root_dir):
for name in files:
path = os.path.join(root, name)
for i in rm_these:
if name == i:
print '***********************' + 'removing: ' + path
generate_log(log_name_source, 'EVENT = Unwanted file removal - %s was removed' % path)
os.remove(path)
parser = argparse.ArgumentParser(description='Generate manifest with checksums for a directory'
' Written by Kieran O\'Leary.')
parser.add_argument('source', help='Input directory')
parser.add_argument('-s', '-sidecar', action='store_true', help='Generates Sidecar')
args = parser.parse_args()
source = args.source
source_parent_dir = os.path.dirname(source)
normpath = os.path.normpath(source)
dirname = os.path.split(os.path.basename(source))[1]
relative_path = normpath.split(os.sep)[-1]
if args.s:
manifest = source_parent_dir + '/%s_manifest.md5' % relative_path
else:
manifest_ = '/%s_manifest.md5' % relative_path
manifest = os.path.expanduser("~/Desktop/%s") % manifest_
log_name_source_ = os.path.basename(args.source) + time.strftime("_%Y_%m_%dT%H_%M_%S")
log_name_source = os.path.expanduser("~/Desktop/%s.log") % log_name_source_
generate_log(log_name_source, 'move.py started.')
generate_log(log_name_source, 'Source: %s' % source)
if os.path.isfile(source):
print '\nFile checksum is not currently supported, only directories.\n'
generate_log(log_name_source, 'Error: Attempted to generate manifest for file. Only Directories/Folders are currently supported')
generate_log(log_name_source, 'move.py exit')
sys.exit()
elif not os.path.isdir(source):
print ' %s is either not a directory or it does not exist' % source
generate_log(log_name_source, ' %s is either not a directory or it does not exist' % source)
generate_log(log_name_source, 'move.py exit')
sys.exit()
remove_bad_files(source)
source_count = 0
for root, directories, filenames in os.walk(source):
for files in filenames:
source_count +=1 #works in windows at least
if os.path.isfile(manifest):
count_in_manifest = manifest_file_count(manifest)
if source_count != count_in_manifest:
print 'This manifest may be outdated as the number of files in your directory does not match the number of files in the manifest'
generate_log(log_name_source, 'EVENT = Existing source manifest check - Failure - The number of files in the source directory is not equal to the number of files in the source manifest ')
sys.exit()
source_manifest_start_time = time.time()
if not os.path.isfile(manifest):
try:
print 'Generating source manifest'
make_manifest(source_parent_dir, relative_path,manifest)
generate_log(log_name_source, 'EVENT = Generating source manifest')
except OSError:
print 'You do not have access to this directory. Perhaps it is read only, or the wrong file system\n'
sys.exit()
else:
generate_log(log_name_source, 'EVENT = Existing source manifest check - Source manifest already exists. Script will exit. ')
source_manifest_time = time.time() - source_manifest_start_time
print source_manifest_time