summaryrefslogtreecommitdiff
path: root/tools/util.py
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2018-07-25 10:17:44 +0200
committerBert Belder <bertbelder@gmail.com>2018-07-25 20:13:17 +0200
commit4d08bb85a4276292c2121b221b4840865a13cd42 (patch)
treed7da20a39432ddcfb9e43c4e1ad010d8db310230 /tools/util.py
parent0875411267d62a0fdcaf762657f19082b440fe36 (diff)
Clean up and fix tools
* Make sync_third_party work in general * Un-break build.py and run_hooks.py on windows * Partially fix format.py on windows * Reduce code duplication between run_hooks and sync_third_party
Diffstat (limited to 'tools/util.py')
-rw-r--r--tools/util.py33
1 files changed, 27 insertions, 6 deletions
diff --git a/tools/util.py b/tools/util.py
index 4938c5157..436630401 100644
--- a/tools/util.py
+++ b/tools/util.py
@@ -1,20 +1,30 @@
# Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
# All rights reserved. MIT License.
import os
+import shutil
+import stat
import subprocess
executable_suffix = ".exe" if os.name == "nt" else ""
+root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
-def run(args, quiet=False, envs={}):
+def make_env(merge_env={}, env=None):
+ if env is None:
+ env = os.environ
+ env = env.copy()
+ for key in merge_env.keys():
+ env[key] = merge_env[key]
+ return env
+
+
+def run(args, quiet=False, cwd=None, env=None, merge_env={}):
+ args[0] = os.path.normpath(args[0])
if not quiet:
print " ".join(args)
- env = os.environ.copy()
- for key in envs.keys():
- env[key] = envs[key]
- args[0] = os.path.normpath(args[0])
+ env = make_env(env=env, merge_env=merge_env)
shell = os.name == "nt" # Run through shell to make .bat/.cmd files work.
- subprocess.check_call(args, env=env, shell=shell)
+ subprocess.check_call(args, cwd=cwd, env=env, shell=shell)
def remove_and_symlink(target, name, target_is_dir=False):
@@ -68,3 +78,14 @@ def find_exts(directory, *extensions):
matches.append(os.path.join(root, filename))
break
return matches
+
+
+# The Python equivalent of `rm -rf`.
+def rmtree(directory):
+ # On Windows, shutil.rmtree() won't delete files that have a readonly bit.
+ # Git creates some files that do. The 'onerror' callback deals with those.
+ def rm_readonly(func, path, _):
+ os.chmod(path, stat.S_IWRITE)
+ func(path)
+
+ shutil.rmtree(directory, onerror=rm_readonly)