1
0
forked from Mirror/wren
Files
wren/util/util.py
Bob Nystrom f866ee7003 Vendor GYP and libuv into the repo.
Instead of dynamically downloading these as needed during a build, this
checks in those two dependencies directly into the Wren repo. That's a
little lame because users of Wren who aren't building the CLI don't
actually need them, but they aren't too big, so it's not a huge deal.

It makes builds (particularly on Travis) more reliable, because they
don't have to pull down additional content over the network.
2018-03-14 07:27:29 -07:00

78 lines
1.8 KiB
Python

# Utility functions used by other Python files in this directory.
from __future__ import print_function
import os
import os.path
import platform
import shutil
import subprocess
import sys
def python2_binary():
"""Tries to find a python 2 executable."""
# Using [0] instead of .major here to support Python 2.6.
if sys.version_info[0] == 2:
return sys.executable or "python"
else:
return "python2"
def clean_dir(dir):
"""If dir exists, deletes it and recreates it, otherwise creates it."""
if os.path.isdir(dir):
remove_dir(dir)
os.makedirs(dir)
def ensure_dir(dir):
"""Creates dir if not already there."""
if os.path.isdir(dir):
return
os.makedirs(dir)
def remove_dir(dir):
"""Recursively removes dir."""
if platform.system() == "Windows":
# rmtree gives up on readonly files on Windows
# rd doesn't like paths with forward slashes
subprocess.check_call(
['cmd', '/c', 'rd', '/s', '/q', dir.replace('/', '\\')])
else:
shutil.rmtree(dir)
def replace_in_file(path, text, replace):
"""Replaces all occurrences of `text` in the file at `path` with `replace`."""
with open(path) as file:
contents = file.read()
contents = contents.replace(text, replace)
with open(path, "w") as file:
file.write(contents)
def run(args, cwd=None):
"""Spawn a process to invoke [args] and mute its output."""
try:
# check_output() was added in Python 2.7.
has_check_output = (sys.version_info[0] > 2 or
(sys.version_info[0] == 2 and sys.version_info[1] >= 7))
if has_check_output:
subprocess.check_output(args, cwd=cwd, stderr=subprocess.STDOUT)
else:
proc = subprocess.Popen(args, cwd=cwd, stdout=subprocess.PIPE)
proc.communicate()[0].split()
except subprocess.CalledProcessError as error:
print(error.output)
sys.exit(error.returncode)