2018-07-08 02:24:29 -04:00
|
|
|
# Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
# All rights reserved. MIT License.
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2018-07-21 19:08:24 -04:00
|
|
|
executable_suffix = ".exe" if os.name == "nt" else ""
|
|
|
|
|
2018-07-08 02:24:29 -04:00
|
|
|
|
2018-07-18 20:00:53 -04:00
|
|
|
def run(args, quiet=False, envs={}):
|
2018-07-16 22:40:42 -04:00
|
|
|
if not quiet:
|
|
|
|
print " ".join(args)
|
2018-07-08 02:24:29 -04:00
|
|
|
env = os.environ.copy()
|
2018-07-18 20:00:53 -04:00
|
|
|
for key in envs.keys():
|
|
|
|
env[key] = envs[key]
|
2018-07-21 19:08:24 -04:00
|
|
|
args[0] = os.path.normpath(args[0])
|
|
|
|
shell = os.name == "nt" # Run through shell to make .bat/.cmd files work.
|
|
|
|
subprocess.check_call(args, env=env, shell=shell)
|
2018-07-08 02:24:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
def remove_and_symlink(target, name, target_is_dir=False):
|
|
|
|
try:
|
2018-07-08 13:14:55 -04:00
|
|
|
# On Windows, directory symlink can only be removed with rmdir().
|
|
|
|
if os.name == "nt" and os.path.isdir(name):
|
|
|
|
os.rmdir(name)
|
|
|
|
else:
|
|
|
|
os.unlink(name)
|
2018-07-08 02:24:29 -04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
symlink(target, name, target_is_dir)
|
|
|
|
|
|
|
|
|
|
|
|
def symlink(target, name, target_is_dir=False):
|
|
|
|
if os.name == "nt":
|
|
|
|
import ctypes
|
|
|
|
CreateSymbolicLinkW = ctypes.windll.kernel32.CreateSymbolicLinkW
|
|
|
|
CreateSymbolicLinkW.restype = ctypes.c_ubyte
|
|
|
|
CreateSymbolicLinkW.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p,
|
|
|
|
ctypes.c_uint32)
|
|
|
|
|
2018-07-20 21:52:00 -04:00
|
|
|
# Replace forward slashes by backward slashes.
|
|
|
|
# Strangely it seems that this is only necessary for symlinks to files.
|
|
|
|
# Forward slashes don't cause any issues when the target is a directory.
|
|
|
|
target = target.replace("/", "\\")
|
2018-07-08 02:24:29 -04:00
|
|
|
flags = 0x02 # SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
|
|
|
|
if (target_is_dir):
|
|
|
|
flags |= 0x01 # SYMBOLIC_LINK_FLAG_DIRECTORY
|
|
|
|
if not CreateSymbolicLinkW(name, target, flags):
|
|
|
|
raise ctypes.WinError()
|
|
|
|
else:
|
|
|
|
os.symlink(target, name)
|
|
|
|
|
|
|
|
|
|
|
|
def touch(fname):
|
|
|
|
if os.path.exists(fname):
|
|
|
|
os.utime(fname, None)
|
|
|
|
else:
|
|
|
|
open(fname, 'a').close()
|