-
Notifications
You must be signed in to change notification settings - Fork 1
/
source_collector.py
103 lines (78 loc) · 3.32 KB
/
source_collector.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
#!/usr/bin/env python3
import constants
def endsWithExtension(filename, extensionList):
import os
basefilepath, extension = os.path.splitext(filename)
for x in extensionList:
dotX = "."+x
if dotX == extension:
return True
return False
def isHeader(args, filename):
return endsWithExtension(filename, args.header)
def isSource(args, filename):
if 'source' in args and args.source != None:
return endsWithExtension(filename, args.source)
return True
def isCmakeLists(filename):
return filename == constants.kCMakeListsFile
def verifyDictPath(inOutDirectoryContents, categoryKey, root, cwd):
if not root in inOutDirectoryContents:
inOutDirectoryContents[root] = {}
if not cwd in inOutDirectoryContents[root]:
inOutDirectoryContents[root][cwd] = {}
if not categoryKey in inOutDirectoryContents[root][cwd]:
inOutDirectoryContents[root][cwd][categoryKey] = []
def addToDict(inOutDirectoryContents, categoryKey, root, cwd, filename):
verifyDictPath(inOutDirectoryContents, categoryKey, root, cwd)
inOutDirectoryContents[root][cwd][categoryKey].append(filename)
def appendListItems(theList, anotherList):
for i in anotherList:
theList.append(i)
return theList
def flattenList(theList):
flat = []
for i in theList:
if isinstance(i, list):
flat = appendListItems(flat, flattenList(i))
else:
flat.append(i)
return flat
def filenameMatchesMaskList(filename, masklist):
import fnmatch
for pattern in flattenList(masklist):
if fnmatch.fnmatch(filename, pattern):
return True
return False
def ignoreFile(args, filename):
if 'exclude_file' in args and args.exclude_file != None:
return filenameMatchesMaskList(filename, args.exclude_file)
return False
def ignoreDirectory(args, directoryname):
if 'exclude_dir' in args and args.exclude_dir != None:
return filenameMatchesMaskList(directoryname, args.exclude_dir)
return False
def collectFile(inOutDirectoryContents, args, root, cwd, filename):
if ignoreFile(args, filename):
return
if isCmakeLists(filename):
addToDict(inOutDirectoryContents, constants.kCMakeListsKey, root, cwd, filename)
elif isHeader(args, filename):
addToDict(inOutDirectoryContents, constants.kHeaderKey, root, cwd, filename)
elif isSource(args, filename):
addToDict(inOutDirectoryContents, constants.kSourceKey, root, cwd, filename)
def collectSourceFiles(inOutDirectoryContents, args, root, cwd, recurse):
import os
normCwd = os.path.normpath(cwd)
if os.path.exists(os.path.join(root, normCwd, constants.kCMakeListsFile)):
collectFile(inOutDirectoryContents, args, root, cwd, constants.kCMakeListsFile)
return
for d in os.scandir(os.path.join(root, normCwd)):
if d.name.startswith('.'):
continue
if d.is_file():
collectFile(inOutDirectoryContents, args, root, cwd, d.name)
elif d.is_dir() and recurse and not ignoreDirectory(args, d.name):
addToDict(inOutDirectoryContents, constants.kDirectoryKey, root, cwd, d.name)
nextCwd = os.path.normpath(os.path.join(cwd, d.name))
collectSourceFiles(inOutDirectoryContents, args, root, nextCwd, True)