-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathfile_finder.py
307 lines (287 loc) · 8.28 KB
/
file_finder.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
import logging
import os
from pathlib import Path
from typing import Iterable, List, Optional, Pattern
import sentry_sdk
from codecov_cli.helpers.folder_searcher import globs_to_regex, search_files
from codecov_cli.helpers.upload_type import ReportType
from codecov_cli.types import UploadCollectionResultFile
logger = logging.getLogger("codecovcli")
coverage_files_patterns = [
"*.clover",
"*.codecov.*",
"*.gcov",
"*.lcov",
"*.lst",
"*coverage*.*",
"*Jacoco*.xml",
"clover.xml",
"cobertura.xml",
"codecov-result.json",
"codecov.*",
"cover.out",
"coverage-final.json",
"excoveralls.json",
"gcov.info",
"jacoco*.xml",
"lcov.dat",
"pylcov.dat",
"lcov.info",
"luacov.report.out",
"naxsi.info",
"nosetests.xml",
"report.xml",
"test_cov.xml",
]
test_results_files_patterns = [
"*junit*.xml",
"*test*.xml",
# the actual JUnit (Java) prefixes the tests with "TEST-"
"*TEST-*.xml",
]
coverage_files_excluded_patterns = [
"*.am",
"*.bash",
"*.bat",
"*.bw",
"*.cfg",
"*.class",
"*.cmake",
"*.conf",
"*.coverage",
"*.cp",
"*.cpp",
"*.crt",
"*.css",
"*.csv",
"*.data",
"*.db",
"*.dox",
"*.ec",
"*.egg",
"*.el",
"*.env",
"*.erb",
"*.exe",
"*.ftl",
"*.gif",
".git*",
"*.gradle",
"*.gz",
"*.h",
"*.html",
"*.in",
"*.jade",
"*.jar*",
"*.jpeg",
"*.jpg",
"*.js",
"*.less",
"*.log",
"*.m4",
"*.mak*",
"*.md",
".nvmrc",
"*.o",
"*.p12",
"*.pem",
"*.png",
"*.pom*",
"*.profdata",
"*.proto",
"*.ps1",
"*.pth",
"*.py",
"*.pyc",
"*.pyo",
"*.rb",
"*.rsp",
"*.rst",
"*.ru",
"*.sbt",
"*.scss",
"*.serialized",
"*.sh",
"*.snapshot",
"*.sql",
"*.svg",
"*.tar.tz",
"*.template",
"*.ts",
"*.whl",
"*.xcconfig",
"*.xcoverage.*",
"*.yml",
"*.yaml",
"*/classycle/report.xml",
"*codecov.yml",
"codecov.yaml",
"*~",
".*coveragerc",
".coverage*",
"coverage-summary.json",
"codecov.SHA256SUM",
"codecov.SHA256SUM.sig",
"createdFiles.lst",
"fullLocaleNames.lst",
"include.lst",
"inputFiles.lst",
"phpunit-code-coverage.xml",
"phpunit-coverage.xml",
"remapInstanbul.coverage*.json",
"scoverage.measurements.*",
"test_*_coverage.txt",
"test-result-*-codecoverage.json",
"testrunner-coverage*",
"*.*js",
"*.map",
"*.egg-info",
".ds_store",
"*.zip",
]
test_results_files_excluded_patterns = (
coverage_files_patterns + coverage_files_excluded_patterns
)
default_folders_to_ignore = [
"vendor",
"bower_components",
".circleci",
"conftest_*.c.gcov",
".egg-info*",
".env",
".envs",
".git",
".go",
".hg",
".map",
".marker",
".tox",
".venv",
".venvs",
".virtualenv",
".virtualenvs",
".yarn",
".yarn-cache",
"__pycache__",
"env",
"envs",
"htmlcov",
"js/generated/coverage",
"node_modules",
"venv",
"venvs",
"virtualenv",
"virtualenvs",
"jspm_packages",
".nyc_output",
]
class FileFinder(object):
def __init__(
self,
search_root: Optional[Path] = None,
folders_to_ignore: Optional[List[Path]] = None,
explicitly_listed_files: Optional[List[Path]] = None,
disable_search: bool = False,
report_type: ReportType = ReportType.COVERAGE,
):
self.search_root = search_root or Path(os.getcwd())
self.folders_to_ignore = list(folders_to_ignore) if folders_to_ignore else []
self.explicitly_listed_files = explicitly_listed_files or None
self.disable_search = disable_search
self.report_type: ReportType = report_type
def find_files(self) -> List[UploadCollectionResultFile]:
with sentry_sdk.start_span(name="find_files"):
if self.report_type == ReportType.COVERAGE:
files_excluded_patterns = coverage_files_excluded_patterns
files_patterns = coverage_files_patterns
elif self.report_type == ReportType.TEST_RESULTS:
files_excluded_patterns = test_results_files_excluded_patterns
files_patterns = test_results_files_patterns
regex_patterns_to_exclude = globs_to_regex(files_excluded_patterns)
assert regex_patterns_to_exclude # this is never `None`
files_paths: Iterable[Path] = []
user_files_paths = []
if self.explicitly_listed_files:
user_files_paths = self.get_user_specified_files(
regex_patterns_to_exclude
)
if not self.disable_search:
regex_patterns_to_include = globs_to_regex(files_patterns)
assert regex_patterns_to_include # this is never `None`
files_paths = search_files(
self.search_root,
default_folders_to_ignore
+ [str(folder) for folder in self.folders_to_ignore],
filename_include_regex=regex_patterns_to_include,
filename_exclude_regex=regex_patterns_to_exclude,
)
result_files = [UploadCollectionResultFile(path) for path in files_paths]
user_result_files = [
UploadCollectionResultFile(path)
for path in user_files_paths
if user_files_paths
]
user_result_files = []
for path in user_files_paths:
if os.path.isfile(path):
user_result_files.append(UploadCollectionResultFile(path))
else:
logger.warning(
f'File "{path}" could not be found or does not exist. Please enter in the full path or from the search root "{self.search_root}"',
)
return list(set(result_files + user_result_files))
def get_user_specified_files(self, regex_patterns_to_exclude: Pattern):
user_filenames_to_include = []
files_excluded_but_user_includes = []
for file in self.explicitly_listed_files:
user_filenames_to_include.append(file.name)
if regex_patterns_to_exclude.match(file.name):
files_excluded_but_user_includes.append(str(file))
if files_excluded_but_user_includes:
logger.warning(
"Some files being explicitly added are found in the list of excluded files for upload. We are still going to search for the explicitly added files.",
extra=dict(
extra_log_attributes=dict(files=files_excluded_but_user_includes)
),
)
regex_patterns_to_include = globs_to_regex(user_filenames_to_include)
multipart_include_regex = globs_to_regex(
[str(path.resolve()) for path in self.explicitly_listed_files]
)
user_files_paths = list(
search_files(
self.search_root,
self.folders_to_ignore,
filename_include_regex=regex_patterns_to_include,
multipart_include_regex=multipart_include_regex,
)
)
not_found_files = []
user_files_paths_resolved = [path.resolve() for path in user_files_paths]
for filepath in self.explicitly_listed_files:
if filepath.resolve() not in user_files_paths_resolved:
## The file given might be linked or in a parent dir, check to see if it exists
if filepath.exists():
user_files_paths.append(filepath)
else:
not_found_files.append(filepath)
if not_found_files:
logger.warning(
"Some files were not found",
extra=dict(extra_log_attributes=dict(not_found_files=not_found_files)),
)
return user_files_paths
def select_file_finder(
root_folder_to_search,
folders_to_ignore,
explicitly_listed_files,
disable_search,
report_type: ReportType = ReportType.COVERAGE,
):
return FileFinder(
root_folder_to_search,
folders_to_ignore,
explicitly_listed_files,
disable_search,
report_type,
)