1
|
|
|
import os |
2
|
|
|
|
3
|
|
|
from coalib.collecting.Importers import iimport_objects |
4
|
|
|
from coalib.misc.Decorators import yield_once |
5
|
|
|
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL |
6
|
|
|
from coalib.parsing.Globbing import iglob, fnmatch |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def _right_kind(bear_class, kinds): |
10
|
|
|
try: |
11
|
|
|
return bear_class.kind() in kinds |
12
|
|
|
except NotImplementedError: |
13
|
|
|
return False |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def _import_bears(file_path, kinds): |
17
|
|
|
# recursive imports: |
18
|
|
|
for bear_list in iimport_objects(file_path, |
19
|
|
|
names='__additional_bears__', |
20
|
|
|
types=list): |
21
|
|
|
for bear_class in bear_list: |
22
|
|
|
if _right_kind(bear_class, kinds): |
23
|
|
|
yield bear_class |
24
|
|
|
# normal import |
25
|
|
|
for bear_class in iimport_objects(file_path, |
26
|
|
|
attributes='kind', |
27
|
|
|
local=True): |
28
|
|
|
if _right_kind(bear_class, kinds): |
29
|
|
|
yield bear_class |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
@yield_once |
33
|
|
|
def icollect(file_paths): |
34
|
|
|
""" |
35
|
|
|
Evaluate globs in file paths and return all matching files. |
36
|
|
|
|
37
|
|
|
:param file_paths: file path or list of such that can include globs |
38
|
|
|
:return: iterator that yields paths of all matching files |
39
|
|
|
""" |
40
|
|
|
if isinstance(file_paths, str): |
41
|
|
|
file_paths = [file_paths] |
42
|
|
|
|
43
|
|
|
for file_path in file_paths: |
44
|
|
|
for match in iglob(file_path): |
45
|
|
|
yield match |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def collect_files(file_paths, ignored_file_paths=None, limit_file_paths=None): |
49
|
|
|
""" |
50
|
|
|
Evaluate globs in file paths and return all matching files |
51
|
|
|
|
52
|
|
|
:param file_paths: file path or list of such that can include globs |
53
|
|
|
:param ignored_file_paths: list of globs that match to-be-ignored files |
54
|
|
|
:param limit_file_paths: list of globs that the files are limited to |
55
|
|
|
:return: list of paths of all matching files |
56
|
|
|
""" |
57
|
|
|
valid_files = list(filter(os.path.isfile, icollect(file_paths))) |
58
|
|
|
valid_files = remove_ignored(valid_files, ignored_file_paths or []) |
59
|
|
|
valid_files = limit_paths(valid_files, limit_file_paths or []) |
60
|
|
|
return valid_files |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def collect_dirs(dir_paths, ignored_dir_paths=None): |
64
|
|
|
""" |
65
|
|
|
Evaluate globs in directory paths and return all matching directories |
66
|
|
|
|
67
|
|
|
:param dir_paths: file path or list of such that can include globs |
68
|
|
|
:param ignored_dir_paths: list of globs that match to-be-ignored dirs |
69
|
|
|
:return: list of paths of all matching directories |
70
|
|
|
""" |
71
|
|
|
valid_dirs = list(filter(os.path.isdir, icollect(dir_paths))) |
72
|
|
|
return remove_ignored(valid_dirs, ignored_dir_paths or []) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
@yield_once |
76
|
|
|
def icollect_bears(bear_dirs, bear_names, kinds, log_printer): |
77
|
|
|
""" |
78
|
|
|
Collect all bears from bear directories that have a matching kind. |
79
|
|
|
|
80
|
|
|
:param bear_dirs: directory name or list of such that can contain bears |
81
|
|
|
:param bear_names: names of bears |
82
|
|
|
:param kinds: list of bear kinds to be collected |
83
|
|
|
:param log_printer: log_printer to handle logging |
84
|
|
|
:return: iterator that yields bear classes |
85
|
|
|
""" |
86
|
|
|
for bear_dir in filter(os.path.isdir, icollect(bear_dirs)): |
87
|
|
|
for bear_name in bear_names: |
88
|
|
|
for matching_file in iglob( |
89
|
|
|
os.path.join(bear_dir, bear_name + '.py')): |
90
|
|
|
|
91
|
|
|
try: |
92
|
|
|
for bear in _import_bears(matching_file, kinds): |
93
|
|
|
yield bear |
94
|
|
|
except BaseException as exception: |
95
|
|
|
log_printer.log_exception( |
96
|
|
|
"Unable to collect bears from {file}. Probably the " |
97
|
|
|
"file is malformed or the module code raises an " |
98
|
|
|
"exception.".format(file=matching_file), |
99
|
|
|
exception, |
100
|
|
|
log_level=LOG_LEVEL.WARNING) |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
def collect_bears(bear_dirs, bear_names, kinds, log_printer): |
104
|
|
|
""" |
105
|
|
|
Collect all bears from bear directories that have a matching kind. |
106
|
|
|
|
107
|
|
|
:param bear_dirs: directory name or list of such that can contain bears |
108
|
|
|
:param bear_names: names of bears |
109
|
|
|
:param kinds: list of bear kinds to be collected |
110
|
|
|
:param log_printer: log_printer to handle logging |
111
|
|
|
:return: list of matching bear classes |
112
|
|
|
""" |
113
|
|
|
return list(icollect_bears(bear_dirs, bear_names, kinds, log_printer)) |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
def remove_ignored(file_paths, ignored_globs): |
117
|
|
|
""" |
118
|
|
|
Removes file paths from list if they are ignored. |
119
|
|
|
|
120
|
|
|
:param file_paths: file path string or list of such |
121
|
|
|
:param ignored_globs: list of globs that match to-be-ignored file paths |
122
|
|
|
:return: list without those items that should be ignored |
123
|
|
|
""" |
124
|
|
|
file_paths = list(set(file_paths)) |
125
|
|
|
reduced_list = file_paths[:] |
126
|
|
|
|
127
|
|
|
for file_path in file_paths: |
128
|
|
|
for ignored_glob in ignored_globs: |
129
|
|
|
if fnmatch(file_path, ignored_glob): |
130
|
|
|
reduced_list.remove(file_path) |
131
|
|
|
break |
132
|
|
|
|
133
|
|
|
return reduced_list |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
def limit_paths(file_paths, limit_globs): |
137
|
|
|
""" |
138
|
|
|
Limits file paths from list based on the given globs. |
139
|
|
|
|
140
|
|
|
:param file_paths: file path string or list of such |
141
|
|
|
:param limit_globs: list of globs to limit the file paths by |
142
|
|
|
:return: list with only those items that in the limited globs |
143
|
|
|
""" |
144
|
|
|
file_paths = list(set(file_paths)) |
145
|
|
|
limited_list = file_paths[:] |
146
|
|
|
|
147
|
|
|
for file_path in file_paths: |
148
|
|
|
for limit_glob in limit_globs: |
149
|
|
|
if not fnmatch(file_path, limit_glob): |
150
|
|
|
limited_list.remove(file_path) |
151
|
|
|
break |
152
|
|
|
|
153
|
|
|
return limited_list |
154
|
|
|
|