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
|
import os
import sys
import shutil
from logging import Logger, getLogger
log: Logger = getLogger(__name__)
def get_file_list(path: str,
exts: list[str],
exclude: list[str]=None) -> list[str]:
log.debug('retrieving file list in path "%s" that contain file'
' extensions (%s) except (%s)',
path, ', '.join(exts),
', '.join(exclude if exclude is not None else []))
out: list[str] = []
for root, dirs, files in os.walk(path):
if exclude is not None:
log.debug('removing excludes from list')
dirs[:] = [d for d in dirs if d not in exclude]
for f in files:
if f.endswith(tuple(exts)):
stripped_f: str = os.path.join(root, f).replace(path, '')[1:]
out.append(stripped_f)
log.debug('added file "%s" without "%s" part: "%s"',
f, path, stripped_f)
else:
log.debug('ignoring file "%s" as it doesn\'t contain'
' any of the extensions (%s)', f, ', '.join(exts))
return out
def get_dir_structure(path: str,
exclude: list[str]=None) -> list[str]:
log.debug('retrieving dir structure in path "%s" except (%s)',
path, ', '.join(exclude if exclude is not None else []))
out: list[str] = []
for root, dirs, files in os.walk(path):
if exclude is not None:
log.debug('removing excludes from list')
dirs[:] = [d for d in dirs if d not in exclude]
for d in dirs:
if root in out:
out.remove(root)
log.debug('removed dir "%s" as it already is in the list', root)
joined_dir: str = os.path.join(root, d)
out.append(joined_dir)
log.debug('added dir "%s" to the list', joined_dir)
log.debug('removing "%s" from all dirs in list', path)
return [o.replace(path, '')[1:] for o in out]
def create_dir(path: str, p: bool=False) -> None:
try:
if p:
os.makedirs(path)
else:
os.mkdir(path)
log.info('created directory "%s"', path)
except FileExistsError:
log.info('directory "%s" already exists, ignoring', path)
def copy_file(src: str, dst: str) -> None:
if not os.path.exists(dst):
shutil.copy2(src, dst)
log.info('copied file "%s" to "%s"', src, dst)
else:
log.info('file "%s" already exists, ignoring', dst)
def sanity_check_path(path: str) -> None:
if '$' in path:
log.error('"$" character found in path "%s";'
' could be due to non-existant env var.', path)
sys.exit(1)
|