aboutsummaryrefslogtreecommitdiff
path: root/scripts/formatcode.py
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-05-11 13:05:39 +0200
committerStefan Boberg <[email protected]>2021-05-11 13:05:39 +0200
commitf8d9ac5d13dd37b8b57af0478e77ba1e75c813aa (patch)
tree1daf7621e110d48acd5e12e3073ce48ef0dd11b2 /scripts/formatcode.py
downloadzen-f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa.tar.xz
zen-f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa.zip
Adding zenservice code
Diffstat (limited to 'scripts/formatcode.py')
-rw-r--r--scripts/formatcode.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/scripts/formatcode.py b/scripts/formatcode.py
new file mode 100644
index 000000000..1a214380d
--- /dev/null
+++ b/scripts/formatcode.py
@@ -0,0 +1,49 @@
+import os
+import fileinput
+
+def is_header_missing(f):
+ with open(f) as reader:
+ lines = reader.read().lstrip().splitlines()
+ if len(lines) > 0: return not lines[0].startswith("// ")
+ return True
+
+def add_headers(files, header):
+ for line in fileinput.input(files, inplace=True):
+ if fileinput.isfirstline():
+ [ print(h) for h in header.splitlines() ]
+ print(line, end="")
+
+def scan_tree(root):
+ files = []
+ header_files = []
+ with os.scandir(root) as dirs:
+ for entry in dirs:
+ if entry.is_dir():
+ scan_tree(os.path.join(root, entry.name))
+ elif entry.name.endswith(".cpp") or entry.name.endswith(".h"):
+ print("... formatting: %s"%(entry.name))
+ full_path = os.path.join(root, entry.name)
+ files.append(full_path)
+ if is_header_missing(full_path):
+ header_files.append(full_path)
+ args = ""
+ if files:
+ os.system("clang-format -i " + " ".join(files))
+ if header_files:
+ add_headers(header_files, "// Copyright Epic Games, Inc. All Rights Reserved.\n\n")
+
+def scan_zen(root):
+ with os.scandir(root) as dirs:
+ for entry in dirs:
+ if entry.is_dir() and entry.name.startswith("zen"):
+ scan_tree(os.path.join(root, entry.name))
+
+while True:
+ if (os.path.isfile(".clang-format")):
+ scan_zen(".")
+ quit()
+ else:
+ cwd = os.getcwd()
+ if os.path.dirname(cwd) == cwd:
+ quit()
+ os.chdir("..")