-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
66 lines (60 loc) · 2.13 KB
/
common.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
# Copyright 2019 JamesZBL
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pathlib import Path
import os, re
def find_root_by_file_name(base_dir, file_name_sections, reserve_mode='child', reg=True):
result = []
for root, folders, files in os.walk(base_dir):
if reg:
match = 0
for file in files:
for section in file_name_sections:
if re.match(section, file):
match += 1
if len(file_name_sections) == match:
relative_path = str(Path(root).relative_to(base_dir))
result.append(relative_path)
else:
for file in files:
for filename in file_name_sections:
if file == filename:
relative_path = str(Path(root).relative_to(base_dir))
result.append(relative_path)
result = list(set(result))
result.sort()
if 'child' == reserve_mode:
for d in result:
for dd in result:
if d != dd and d.startswith(dd):
result.remove(dd)
elif 'parent' == reserve_mode:
trash = []
for d in result:
for dd in result:
if d != dd and dd.startswith('{}/'.format(d)):
trash.append(dd)
filterd = []
for d in result:
if d not in trash:
filterd.append(d)
result = filterd
return result
def artifact_relative_dirs(artifact_coordinates):
result = []
for artifact in artifact_coordinates:
sections = artifact.split(':')
if 5 < len(sections):
continue
group_id, artifact_id, package, version, scope = tuple(sections)
group_id_dir = group_id.replace('.', '/')
artifact_dir = '{}/{}/{}'.format(group_id_dir, artifact_id, version)
result.append(artifact_dir)
return result