/
structure.py
155 lines (134 loc) · 4.13 KB
/
structure.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
import re
import os
import shutil
from collections import namedtuple
from autohdl import instance
from autohdl import hdlGlobals
from autohdl import progressBar
from hdlLogger import log_call, logging
log = logging.getLogger(__name__)
def generate(path = ''):
"""
Could be
empty
simple name
path
"""
root = os.path.abspath(path)
if not os.path.exists(root):
os.makedirs(root)
log.info('Design root: '+ root)
for i in hdlGlobals.predefDirs:
path = os.path.join(root,i)
if not os.path.exists(path):
os.mkdir(path)
pathData = os.path.join(os.path.dirname(__file__), 'data')
Copy = namedtuple('Copy', ['src', 'dst'])
listToCopy = (
Copy(os.path.join(pathData,'build.yaml'), os.path.join(root, 'resource', 'build.yaml')),
Copy(os.path.join(pathData,'run_avhdl.bat'), os.path.join(root,'script', 'run_avhdl.bat')),
Copy(os.path.join(pathData,'kungfu.py'), os.path.join(root, 'script', 'kungfu.py'))
)
for i in listToCopy:
if not os.path.exists(i.dst):
shutil.copy(i.src, i.dst)
return get(root)
def get(path = '', ignore = ('.git', '.svn')):
root = os.path.abspath(path)
return tree(directory = root, ignore = ignore)
def tree(directory, padding = ' ', _res = [], ignore = []):
_res.append(padding[:-1] + '+-' + os.path.basename(os.path.abspath(directory)) + os.path.sep)
padding += ' '
files = os.listdir(directory)
count = 0
for file in files:
if file in ignore:
continue
count += 1
_res.append(padding + '|')
path = directory + os.path.sep + file
if os.path.isdir(path):
if count == len(files):
tree(directory= path, padding = padding + ' ')
else:
tree(directory= path, padding = padding + '|')
else:
_res.append(padding + '+-' + file)
return '\n'.join(_res)
def pathOk(path, ignore, only):
if ignore:
for ignoreItem in ignore:
if re.search(ignoreItem, path):
return False
if only:
for onlyItem in only:
if re.search(onlyItem, path):
return True
else:
return True
def _convertToSet(arg):
arg = arg or set()
if type(arg) is str:
arg = [arg]
return set(arg)
@log_call
def search(directory = '.',
ignoreDir = None, ignoreExt = None, onlyExt = None):
"""
Recursively search files by pattern.
Input: directory - start point to search,
ignore and only - filter patterns for directory and files (should be lists)
Returns list of files.
"""
ignoreDir = _convertToSet(ignoreDir)
ignoreExt = _convertToSet(ignoreExt)
onlyExt = _convertToSet(onlyExt)
resFiles = []
for root, dirs, files in os.walk(os.path.abspath(directory)):
for i in set(dirs) & ignoreDir:
log.debug( 'ignore directory: ' + i)
dirs.remove(i)
for f in files[:]:
log.debug( 'file: ' + f)
ext = os.path.splitext(f)[1]
if ext in ignoreExt:
files.remove(f)
log.debug( 'ignore file (ignore list) ' + f)
elif onlyExt and (ext not in onlyExt):
files.remove(f)
log.debug( 'ignore file (only list) ' + f)
else:
log.debug( 'add file ' + f)
resFiles.append(os.path.join(root, f))
return resFiles
########################################################################
@log_call
def setMainSrc(config):
config['mainSrc'] = search('../src',
onlyExt=hdlGlobals.hdlFileExt,
ignoreDir=hdlGlobals.ignoreRepoDir)
config['mainSrcParsed'] = instance.get_instances(config['mainSrc'])
def setSrc(config):
log.info('Analyzing dependences...')
progressBar.run()
setMainSrc(config)
setDepSrc(config)
progressBar.stop()
@log_call
def setDepSrc(config):
parsed = config['mainSrcParsed']
del config['mainSrcParsed']
while True:
new = instance.analyze(parsed, config)
if new:
parsed.update(new)
else:
break
config['parsed'] = parsed
allSrcFiles = {os.path.abspath(val['path']) for val in parsed.values()}
config['depSrc'] = list(allSrcFiles - set(config['mainSrc']))
if __name__ == '__main__':
res = []
res += [search(directory=i) for i in ['verilog', 'programmator']]
# print '\n'.join(res)
print res