Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
signalapp
GitHub Repository: signalapp/Signal-iOS
Path: blob/main/Scripts/sds_codegen/sds_common.py
1 views
1
#!/usr/bin/env python3
2
3
import os
4
import subprocess
5
6
SDS_JSON_FILE_EXTENSION = ".sdsjson"
7
8
9
def fail(*args):
10
error = " ".join(str(arg) for arg in args)
11
raise Exception(error)
12
13
14
git_repo_path = os.path.abspath(
15
subprocess.check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip()
16
)
17
18
19
def sds_to_relative_path(path):
20
path = os.path.abspath(path)
21
if not path.startswith(git_repo_path):
22
fail("Unexpected path:", path)
23
path = path[len(git_repo_path) :]
24
if path.startswith(os.sep):
25
path = path[len(os.sep) :]
26
return path
27
28
29
def sds_from_relative_path(path):
30
return os.path.join(git_repo_path, path)
31
32
33
def clean_up_generated_code(text):
34
# Remove trailing whitespace.
35
lines = text.split("\n")
36
lines = [line.rstrip() for line in lines]
37
text = "\n".join(lines)
38
# Compact newlines.
39
while "\n\n\n" in text:
40
text = text.replace("\n\n\n", "\n\n")
41
# Ensure there's a trailing newline.
42
return text.strip() + "\n"
43
44
45
def clean_up_generated_swift(text):
46
return clean_up_generated_code(text)
47
48
49
def clean_up_generated_objc(text):
50
return clean_up_generated_code(text)
51
52
53
def pretty_module_path(path):
54
path = os.path.abspath(path)
55
if path.startswith(git_repo_path):
56
path = path[len(git_repo_path) :]
57
return path
58
59
60
def write_text_file_if_changed(file_path, text):
61
if os.path.exists(file_path):
62
with open(file_path, "r") as f:
63
oldText = f.read()
64
if oldText == text:
65
return
66
67
with open(file_path, "w") as f:
68
f.write(text)
69
70