-
Notifications
You must be signed in to change notification settings - Fork 1
/
imghashdir.py
104 lines (90 loc) · 2.87 KB
/
imghashdir.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
# Filename: imghashdir.py
'''
imghashdir
@author: Andrew Philpot
@version 0.5
trbot/wat imghashdir module
directory-level hashing of images
suitable to be called at YYYYMMDD dir root
Usage: python imghashdir.py <dir>
Options:
\t-h, --help:\tprint help to STDOUT and quit
\t-v, --verbose:\tverbose output
\t-r, --repo:\trepository root
'''
import logging
from watlog import watlog
logger = watlog("wat.imghash.imghashdir")
logger.info('wat.imghash.imghashdir initialized')
import sys
import getopt
import os
import time
import Image
from imghash import Imghash, Error, InputError, MissingInputFile, CorruptInputFile, ProcessingError, FailedHashAttemptError, IntegrityError, SizeMismatchError, REPO
import util
VERSION = '0.5'
# defaults
VERBOSE = True
REVISION = '$Revision: 22997 $'.replace('$','')
class Imghashdir(object):
def __init__(self, root, repo=REPO, verbose=VERBOSE, owner=None):
"""calls imghash on all image files below ROOT"""
self.root = root
self.repo = repo
self.verbose = verbose
self.owner = owner
def hashdir(self):
seen = 0
hashed = 0
t0 = time.time()
for (dirpath, dirnames, filenames) in os.walk(self.root, followlinks=True):
for name in filenames:
fullpath = os.path.join(dirpath, name)
if os.path.exists(fullpath) and os.path.isfile(fullpath):
try:
seen += 1
logger.debug('PROCESS file %s' % fullpath)
ih = Imghash(fullpath, repo=self.repo, verbose=self.verbose, owner=self.owner)
ih.process()
hashed +=1
except IOError:
logger.debug('SKIPPED unreadable %s' % fullpath)
t1 = time.time()
logger.info("DIR: %s; SEEN: %s; HASHED: %s; ELAPSED: %s sec" % (self.root, seen, hashed, t1-t0))
def process(self):
self.hashdir()
def main(argv=None):
'''this is called if run from command line'''
# process command line arguments
if argv is None:
argv = sys.argv
try:
opts, args = getopt.getopt(argv[1:], "hvr:",
["echo=", "help", "verbose"])
except getopt.error, msg:
print msg
print "for help use --help"
sys.exit(2)
# default options
my_verbose = VERBOSE
my_repo = REPO
# process options
for o,a in opts:
if o in ("-h","--help"):
print __doc__
sys.exit(0)
if o in ("--echo", ):
print a
if o in ("-v", "--verbose", ):
my_verbose = True
if o in ("-r", "--repo", ):
my_repo = a
imgdir = args[0]
ihd = Imghashdir(imgdir, repo=my_repo)
ihd.process()
# call main() if this is run as standalone
if __name__ == "__main__":
sys.exit(main())
# End of imghashdir.py