forked from Peter-Schorn/SpotifyAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenable_testing.py
More file actions
58 lines (43 loc) · 1.73 KB
/
Copy pathenable_testing.py
File metadata and controls
58 lines (43 loc) · 1.73 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
54
55
import sys, os, re
use_test = sys.argv[1].lower()
project_directory = os.path.dirname(__file__)
# ensure the working directory is the SpotifyAPI package
package_file = os.path.join(project_directory, "Package.swift")
if not os.path.exists(package_file):
print(
"Expected to find Package.swift file in the working direcory. "
"The working directory must be the root directory of the SpotifyAPI "
f"package: {project_directory}"
)
exit(1)
use_test_flag = "#if TEST"
dont_use_test_flag = "#if !TEST"
if use_test == "true":
flags = [use_test_flag, dont_use_test_flag]
elif use_test == "false":
flags = [dont_use_test_flag, use_test_flag]
else:
exit("first argument must be either 'true' or 'false'")
print(f"will replace {flags[0]} with {flags[1]} in {project_directory}")
sources_directory = os.path.join(project_directory, "Sources")
tests_directory = os.path.join(project_directory, "Tests")
# the full paths to all of the swift source code files
# in the Sources and Tests directory, and the package.swift file
swift_files: [str] = []
swift_files.append(package_file)
# search for all swift source code files
for directory in (sources_directory, tests_directory):
for root, dirs, files in os.walk(directory):
for file in files:
if not file.endswith(".swift"):
continue
full_path = os.path.join(root, file)
swift_files.append(full_path)
pattern = rf"^(\s*){flags[0]}\s*$"
replacement = rf"\1{flags[1]}"
for file in swift_files:
with open(file, encoding='utf-8') as f:
text = f.read()
new_text = re.sub(pattern, replacement, text, flags=re.MULTILINE)
with open(file, "w", encoding='utf-8') as f:
f.write(new_text)