-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubcrawl.py
397 lines (317 loc) · 13.6 KB
/
pubcrawl.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# pubcrawl.py
# Created by frankV
# PythonPubCrawler
# https://github.com/frankV/pythonpubcrawl
""" pubcrawl.py -- main """
import os, argparse, collections, yaml, re, fnmatch
import cPickle as pickle
from dbtask import *
from filemeta import *
""" argparse options
usage: pubcrawl.py [-h] [-v] [-d] [-f] directory [settings]
py pub crawler, stumbles through a given directory and stores metadata for
every file it finds.
positional arguments:
directory directory to use
settings settings.yaml file location (optional)
optional arguments:
-h, --help displays help menu
-v, --verbose verbose output from crawler
-d, --dump dumps and replaces existing dictionaries
-f, --fake crawl only, nothing stored to DB
"""
parser = argparse.ArgumentParser(
description='py pub crawler, stumbles through a given directory and \
stores metadata for every file it finds.', fromfile_prefix_chars="@" )
parser.add_argument('-v', '--verbose',
help='verbose output from crawler',
action="store_true")
parser.add_argument('-d', '--dump',
help='dumps and replaces existing dictionaries',
action="store_true")
parser.add_argument('-f', '--fake',
help='crawl only, nothing stored to DB',
action="store_true")
parser.add_argument('directory', help='directory to use', action='store')
parser.add_argument('settings', nargs='?',
help='settings.yaml file location (optional)', action='store')
args = parser.parse_args() # parse arguments
if args.verbose: verbose = True # verbose output
else: verbose = False
if args.dump: dump = True # dump; will override any existing
else: dump = False # dictionaries and drop existing tables
if args.fake: fake = True # fake; crawl only, will not update
else: fake = False
newFiles = 0 # GLOBAL "newFiles"; new files
delFiles = 0 # GLOBAL "delFiles"; not found
updFiles = 0 # GLOBAL "updFiles"; updated
files = {} # GLOBAL "files"; main dictionary
# extensions = collections.defaultdict(int)
""" args.settings
optional argument "settings" defines a yaml file that can be used to specify
certain rules for the crawler to follow in specific directories including
creation of database tables, column specifications, etc.
RULES = project_name, project_directory(s), categories, nomenclature
"""
if args.settings and args.settings[-5:len(args.settings)] == '.yaml':
import yamlRx
yamlRx.verify(args.settings)
settings_stream = open(args.settings, 'r')
settingsMap = yaml.safe_load(settings_stream)
yaml_stream = True
if verbose: print 'using yaml file: ' + args.settings
print yaml.load(settings_stream)
else:
print 'YAML Parse Error: check settings file'
prompt = raw_input('If you continue, settings will not be applied.\
\nContinue? (q = quit) ')
if prompt == 'q':
sys.exit()
# ---------------------------------------------------------------------------- #
# function - crawlDir
# using os.path, crawl directory and all sub-directories, for each file found
# add filename, full path and other meta data to dict "files".
# ---------------------------------------------------------------------------- #
def crawlDir():
# globals
global newFiles, files, extensions, verbose, dump, fake
# directory to crawl = directory passed in by command line
directory = args.directory
# dictionary selection
# - if dump flag; replace existing dictionaries
# - if pickle found use; else start new dictionaries
if not dump:
if pickleTry():
pickleLoad()
if verbose: print 'Using existing dictionary...\n'
else:
if verbose: print 'Starting new dictionary...'
else:
if pickleTry():
if verbose: print 'Replacing existing dictionaries.'
""" this is UGLY! fix this soon!!! """
dirList = []
if args.settings:
i = 0
print 'loaded settings for: '
for project in settingsMap:
print project + ', ' + settingsMap[project][0]["proj_directory"]
dirList.append(settingsMap[project][0]["proj_directory"])
for cats in settingsMap[project][1]['categories']:
for cat in cats:
i+=1
print str(i) + ':' + cat,
print '\n'
print 'rules loaded for:'
for project in settingsMap:
print settingsMap[project][0]["proj_directory"]
mainDir = settingsMap[project][0]["proj_directory"]
for cats in settingsMap[project][1]['categories']:
for cat in cats:
dirList.append(mainDir + '/' + cat)
print dirList
""" seriously ^ that does not a pythonist make! """
prompt = raw_input('\nContinue? (q = quit) ')
if prompt == 'q':
sys.exit()
# when the dictionary 'files' already exists
# for each file, check if file is not already in dict "files"
# then store file meta data accordingly
if verbose: print 'Crawling:', directory
for dirname, dirnames, filenames in os.walk(directory, topdown=True):
if verbose: print '\nsearching... ' + dirname
if dirname in dirList:
print 'using rules for' + dirname
prompt = raw_input('press any key to continue')
for filename in filenames:
if not inFiles(os.path.abspath(os.path.join(dirname, filename))) and \
not ignoredFiles(filename) and \
not ignoredDirectories(dirname):
# prompt = raw_input('I\'m about to drop that table like it\'s hot')
# fullpath = os.path.dirname(os.path.realpath(filename))
fileobject = FileMeta(os.path.abspath(os.path.join(dirname, \
filename)), filename)
newFiles += 1 # number of new files added to dict
# extensions[os.path.splitext(filename)[1].lower()] += 1
files[fileobject.fullPathFileName] = fileobject.fileMetaList()
if not fake:
dbStore(fileobject.fullPathFileName, fileobject.fileMetaList())
if verbose:
print '+ added...', fileobject.fullPathFileName
# file already listed in files dict
elif inFiles(os.path.abspath(os.path.join(dirname, filename))):
# update file meta data and verify file still exists
if verbose: print '\n--- file already found ---',
updateFiles(os.path.abspath(os.path.join(dirname, filename)))
# ---------------------------------------------------------------------------- #
# function - ignoredFiles
# checks if file is in ignore list
#
# function - ignoredDirectories
# checks if directory is in ignore list
# ---------------------------------------------------------------------------- #
def ignoredFiles(filename = None):
ignore = [ '.DS_Store', '*.pyc', '__init__.py', '*.p' ]
for ignored_file in ignore:
if re.search(fnmatch.translate(ignored_file), filename):
return True
return False
def ignoredDirectories(directory = None):
ignore = [ '.git/*', 'env/*' ]
for ignored_directory in ignore:
if re.search(fnmatch.translate(ignored_directory), directory):
return True
return False
# ---------------------------------------------------------------------------- #
# function - inFiles
# checks dict for existence of filename with path
# ---------------------------------------------------------------------------- #
def inFiles(fullPathFileName = None):
# check file is not default case
if fullPathFileName != None:
if fullPathFileName in files: return True
else: return False
# ---------------------------------------------------------------------------- #
# function - verifyFiles
# checks dict for existence of filename with path
# ---------------------------------------------------------------------------- #
def verifyFiles():
global delFiles, vebose
for existingFile in files.keys():
if not os.path.exists(existingFile):
if verbose: print '- removed:', exfile
del files[existingFile]
delFiles += 1
# ---------------------------------------------------------------------------- #
# function - dbStore
# stores file data to database -- uses imported push_to_db
# ---------------------------------------------------------------------------- #
def dbStore(fullpath, fileInfo):
#global fake
#if not fake:
push_to_db(fullpath, fileInfo)
# ---------------------------------------------------------------------------- #
# function - updateFiles
# verify prev collected file meta data and update accordingly
# ---------------------------------------------------------------------------- #
# [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ]
# ['name', 'extension', 'created', 'modified', 'size', 'owner', 'permissions']
"""
there has to be a MORE pythonic way to do this! there is no need to check each
item in the file list explicitly. use len(fileinfo) and do this more
efficiently! that way you can continue to add members to the filemeta class
and not have to continue altering this damn function each time
"""
def updateFiles(fullPathFileName = None):
global updFiles
updated = False
# check filename is not default case
if fullPathFileName != None and fullPathFileName in files:
exfileInfo = []
exfileInfo = files.get(fullPathFileName, "empty")
try: # file stat
st = os.stat(fullPathFileName)
except IOError, TypeError:
print "failed to get file info"
return
else:
# get file size and created date
created = time.ctime(os.path.getctime(fullPathFileName))
modified = time.ctime(os.path.getmtime(fullPathFileName))
size = st[ST_SIZE]
owner = st[ST_UID]
permissions = oct(st[ST_MODE])[-3:]
fileInfo = ['filename', 'ext', created, modified, size, owner, permissions]
if fileInfo[2] != exfileInfo[2]:
exfileInfo[2] = fileInfo[2]
updated = True
if fileInfo[3] != exfileInfo[3]:
exfileInfo[3] = fileInfo[3]
updated = True
if fileInfo[4] != exfileInfo[4]:
exfileInfo[4] = fileInfo[4]
updated = True
if fileInfo[5] != exfileInfo[5]:
exfileInfo[5] = fileInfo[5]
updated = True
if fileInfo[6] != exfileInfo[6]:
exfileInfo[6] = fileInfo[6]
updated = True
if updated is True:
if verbose: print '- updated:', fullPathFileName
updFiles += 1
# ---------------------------------------------------------------------------- #
# function - pickleDump
# saves "files" and "extensions" dict to a file
# ---------------------------------------------------------------------------- #
def pickleDump():
global files, extensions, verbose, fake
if not fake:
if verbose: print 'pickling...'
pickle.dump( files, open( "filesdict.p", "wb" ) )
# pickle.dump( extensions, open( "extensionsdict.p", "wb" ) )
# ---------------------------------------------------------------------------- #
# function - pickleLoad
# loads "files" and "extensions" dict from a file
# ---------------------------------------------------------------------------- #
def pickleLoad():
global files, extensions, verbose
cwd = os.getcwd()
fileDictPickle = str(cwd) + '/filesdict.p'
# extDictPickle = str(cwd) + '/extensionsdict.p'
if verbose: print 'Loading files...'
files = pickle.load( open( fileDictPickle, "rb" ) )
# if verbose: print 'Loading extensions...'
# extensions = pickle.load( open( extDictPickle, "rb" ) )
# ---------------------------------------------------------------------------- #
# function - pickleTry
# checks if pickled files already exist
# ---------------------------------------------------------------------------- #
def pickleTry():
global verbose
cwd = os.getcwd()
try:
with open(cwd+'/filesdict.p') as f:
pass
if verbose: print 'pickle found'
return True
except IOError as e:
print 'pickle not found'
return False
# ---------------------------------------------------------------------------- #
# function - printExtensions
# prints list of extensions along with number of files of that type
# ---------------------------------------------------------------------------- #
def printExtensions():
global verbose
for key,value in extensions.items():
if verbose: print 'Extension: ', key, ' ', value, 'items'
# ---------------------------------------------------------------------------- #
# function - print_files
# prints list of filenames from dict "files"
# ---------------------------------------------------------------------------- #
def print_fileNames():
newLine = '\n'
count = 0
for key,value in files.iteritems():
count += 1
print count,'-', key, value, '\n'
return (newLine)
# ---------------------------------------------------------------------------- #
# function - print_dictTotal(D) ; where D is a python dict object
# prints all elements in dictionary
# ---------------------------------------------------------------------------- #
def print_dictTotal(D):
return (len(D))
# ---------------------------------------------------------------------------- #
# main program
# ---------------------------------------------------------------------------- #
crawlDir()
if not fake: verifyFiles()
if verbose:
print '\n'
print 'Added: ', newFiles, 'new file(s) to list.\n'
print 'Removed:', delFiles, 'file(s) from list.\n'
print 'Updated:', updFiles, 'of', print_dictTotal(files), 'file(s) in list.\n'
print 'Total: ', print_dictTotal(files), 'entries in list.\n'
pickleDump()