-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
53 lines (50 loc) · 1.97 KB
/
util.py
File metadata and controls
53 lines (50 loc) · 1.97 KB
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
#!/usr/bin/env python3
import os
import hashlib
import shutil
import fnmatch
def collectDirectoryDataAsText(path : str, excludepatterns : list[str] | None = None) -> str:
"""Dump all contents in a directory as a single string to ease comparison"""
result = []
rootpath = os.path.abspath(path)
for root, subdirs, files in os.walk(rootpath):
subdirs.sort()
files.sort()
for file in files:
filePath = os.path.join(root, file)
printPath = os.path.relpath(filePath, rootpath)
if excludepatterns is not None:
is_excluded = False
for excludepattern in excludepatterns:
if fnmatch.fnmatch(printPath, excludepattern):
is_excluded = True
break
if is_excluded:
continue
result.append(printPath + ":")
with open(filePath, "rb") as f:
bin = f.read()
isString = True
try:
fileString = bin.decode("utf-8")
except:
isString = False
if isString:
result.append(fileString)
else:
result.append("<Binary file; MD5=" + hashlib.md5(bin).hexdigest() + ">")
return "\n".join(result)
def copyTestDirIfRequested(srcdir: str, testname: str) -> None:
"""Sometimes we want to preserve the temporary files from tests (for manual inspection and testing)
To preserve the output file, the unittest should be invoked with the following two environment variables set:
- PREPPIPE_TEST_EXPORT_WRITE_DIR: the directory where the files will be copied to
- PREPPIPE_TEST_EXPORT_TEST_NAME: the test name who can write to this directory
"""
enabledExportTest = os.environ.get("PREPPIPE_TEST_EXPORT_TEST_NAME")
if enabledExportTest == testname:
# do the copy
copyDest = os.environ.get("PREPPIPE_TEST_EXPORT_WRITE_DIR")
if copyDest is None:
raise RuntimeError("PREPPIPE_TEST_EXPORT_WRITE_DIR not specified")
print("Copying " + srcdir + " to " + copyDest)
shutil.copytree(srcdir, copyDest, dirs_exist_ok=True)