Rename/refactor command execution funcs

This commit is contained in:
mpeterv 2016-03-28 17:44:33 +03:00
parent 16f7945d7d
commit 097ce9a122

View File

@ -73,17 +73,18 @@ def get_default_cache():
else: else:
return os.path.join(os.getenv("HOME"), ".cache", "hererocks") return os.path.join(os.getenv("HOME"), ".cache", "hererocks")
def exec_command(capture, *args): def run(*args, **kwargs):
"""Execute a command. """Execute a command.
Command can be passed as several arguments, each being a string Command can be passed as several arguments, each being a string
or a list of strings; lists are flattened. or a list of strings; lists are flattened.
If opts.verbose is True, output of the command is shown. If opts.verbose is True, output of the command is shown.
If the command exits with non-zero, print an error message and exit. If the command exits with non-zero, print an error message and exit.
If capture is True, output is returned. Additionally, non-zero exit code If keyward argument get_output is True, output is returned.
with empty output is ignored. Additionally, non-zero exit code with empty output is ignored.
""" """
capture = kwargs.get("get_output", False)
args = [arg for arglist in args for arg in ([arglist] if isinstance(arglist, str) else arglist)] args = [arg for arglist in args for arg in ([arglist] if isinstance(arglist, str) else arglist)]
if opts.verbose: if opts.verbose:
@ -108,10 +109,10 @@ def exec_command(capture, *args):
if opts.verbose and capture: if opts.verbose and capture:
sys.stdout.write(output.decode("UTF-8")) sys.stdout.write(output.decode("UTF-8"))
return capture and output.decode("UTF-8") return capture and output.decode("UTF-8").strip()
def run_command(*args): def get_output(*args):
exec_command(False, *args) return run(get_output=True, *args)
def copy_dir(src, dst): def copy_dir(src, dst):
shutil.copytree(src, dst, ignore=lambda _, __: {".git"}) shutil.copytree(src, dst, ignore=lambda _, __: {".git"})
@ -127,7 +128,7 @@ def set_git_branch_accepts_tags():
global git_branch_accepts_tags global git_branch_accepts_tags
if git_branch_accepts_tags is None: if git_branch_accepts_tags is None:
version_output = exec_command(True, "git", "--version") version_output = get_output("git", "--version")
match = re.search("(\d+)\.(\d+)\.?(\d*)", version_output) match = re.search("(\d+)\.(\d+)\.?(\d*)", version_output)
if match: if match:
@ -211,7 +212,7 @@ class Program(object):
# Have to clone the repo to get the commit ref points to. # Have to clone the repo to get the commit ref points to.
self.fetch_repo(ref) self.fetch_repo(ref)
self.commit = exec_command(True, "git", "rev-parse", "HEAD").strip() self.commit = get_output("git", "rev-parse", "HEAD")
self.version_suffix = " @" + self.commit[:7] self.version_suffix = " @" + self.commit[:7]
else: else:
# Local directory. # Local directory.
@ -243,14 +244,14 @@ class Program(object):
# Sync with origin first. # Sync with origin first.
os.chdir(repo_path) os.chdir(repo_path)
if not exec_command(True, "git", "rev-parse", "--quiet", "--verify", ref): if not get_output("git", "rev-parse", "--quiet", "--verify", ref):
run_command("git", "fetch") run("git", "fetch")
run_command("git", "checkout", ref) run("git", "checkout", ref)
# If HEAD is not detached, we are on a branch that must be synced. # If HEAD is not detached, we are on a branch that must be synced.
if exec_command(True, "git", "symbolic-ref", "-q", "HEAD"): if get_output("git", "symbolic-ref", "-q", "HEAD"):
run_command("git", "pull", "--rebase") run("git", "pull", "--rebase")
return return
else: else:
@ -259,11 +260,11 @@ class Program(object):
print(message) print(message)
clone_command, need_checkout = git_clone_command(self.repo, ref, not self.fetched) clone_command, need_checkout = git_clone_command(self.repo, ref, not self.fetched)
run_command(clone_command, self.repo, repo_path) run(clone_command, self.repo, repo_path)
os.chdir(repo_path) os.chdir(repo_path)
if need_checkout and ref != "master": if need_checkout and ref != "master":
run_command("git", "checkout", ref) run("git", "checkout", ref)
def get_download_name(self): def get_download_name(self):
return self.name + "-" + self.fixed_version + ("-win32" if self.win32_zip else "") return self.name + "-" + self.fixed_version + ("-win32" if self.win32_zip else "")
@ -636,7 +637,7 @@ class RioLua(Lua):
objs.append(obj) objs.append(obj)
cmd_suffix = src if opts.target == "cl" else ["-c", "-o", obj, src] cmd_suffix = src if opts.target == "cl" else ["-c", "-o", obj, src]
run_command(cc, static_cflags if obj in luac_objs else cflags, cmd_suffix) run(cc, static_cflags if obj in luac_objs else cflags, cmd_suffix)
lib_objs = [obj_ for obj_ in objs if obj_ not in luac_objs and (obj_ != "lua" + objext())] lib_objs = [obj_ for obj_ in objs if obj_ not in luac_objs and (obj_ != "lua" + objext())]
luac_objs = ["luac" + objext()] luac_objs = ["luac" + objext()]
@ -645,32 +646,32 @@ class RioLua(Lua):
luac_objs.append("print" + objext()) luac_objs.append("print" + objext())
if opts.target == "cl": if opts.target == "cl":
run_command("link", "/nologo", "/out:luac.exe", luac_objs, lib_objs) run("link", "/nologo", "/out:luac.exe", luac_objs, lib_objs)
if os.path.exists("luac.exe.manifest"): if os.path.exists("luac.exe.manifest"):
run_command("mt", "/nologo", "-manifest", "luac.exe.manifest", "-outputresource:luac.exe") run("mt", "/nologo", "-manifest", "luac.exe.manifest", "-outputresource:luac.exe")
else: else:
run_command("ar", "rcu", self.arch_file, lib_objs) run("ar", "rcu", self.arch_file, lib_objs)
run_command("ranlib", self.arch_file) run("ranlib", self.arch_file)
run_command(cc, "-o", self.luac_file, luac_objs, self.arch_file, lflags) run(cc, "-o", self.luac_file, luac_objs, self.arch_file, lflags)
if opts.target == "mingw": if opts.target == "mingw":
run_command(cc, "-shared", "-o", self.dll_file, lib_objs) run(cc, "-shared", "-o", self.dll_file, lib_objs)
run_command("strip", "--strip-unneeded", self.dll_file) run("strip", "--strip-unneeded", self.dll_file)
run_command(cc, "-o", self.lua_file, "-s", "lua.o", self.dll_file) run(cc, "-o", self.lua_file, "-s", "lua.o", self.dll_file)
elif opts.target == "cl": elif opts.target == "cl":
run_command("link", "/nologo", "/DLL", "/out:" + self.dll_file, lib_objs) run("link", "/nologo", "/DLL", "/out:" + self.dll_file, lib_objs)
if os.path.exists(self.dll_file + ".manifest"): if os.path.exists(self.dll_file + ".manifest"):
run_command("mt", "/nologo", "-manifest", self.dll_file + ".manifest", run("mt", "/nologo", "-manifest", self.dll_file + ".manifest",
"-outputresource:" + self.dll_file) "-outputresource:" + self.dll_file)
run_command("link", "/nologo", "/out:lua.exe", "lua.obj", self.arch_file) run("link", "/nologo", "/out:lua.exe", "lua.obj", self.arch_file)
if os.path.exists("lua.exe.manifest"): if os.path.exists("lua.exe.manifest"):
run_command("mt", "/nologo", "-manifest", "lua.exe.manifest", "-outputresource:lua.exe") run("mt", "/nologo", "-manifest", "lua.exe.manifest", "-outputresource:lua.exe")
else: else:
run_command(cc, "-o", self.lua_file, "lua.o", self.arch_file, lflags) run(cc, "-o", self.lua_file, "lua.o", self.arch_file, lflags)
os.chdir("..") os.chdir("..")
@ -744,7 +745,7 @@ class LuaJIT(Lua):
def make(): def make():
if opts.target == "cl": if opts.target == "cl":
os.chdir("src") os.chdir("src")
run_command("msvcbuild.bat") run("msvcbuild.bat")
os.chdir("..") os.chdir("..")
else: else:
if opts.target == "mingw" and program_exists("mingw32-make"): if opts.target == "mingw" and program_exists("mingw32-make"):
@ -753,9 +754,9 @@ class LuaJIT(Lua):
make = "make" make = "make"
if opts.cflags is None: if opts.cflags is None:
run_command(make) run(make)
else: else:
run_command(make, "XCFLAGS=" + opts.cflags) run(make, "XCFLAGS=" + opts.cflags)
def make_install(self): def make_install(self):
luajit_file = exe("luajit") luajit_file = exe("luajit")
@ -856,12 +857,11 @@ class LuaRocks(Program):
for lua in ("lua5.1", "lua", "luajit"): for lua in ("lua5.1", "lua", "luajit"):
lua_binary = os.path.join(opts.location, "bin", exe(lua)) lua_binary = os.path.join(opts.location, "bin", exe(lua))
if is_executable(lua_binary): if is_executable(lua_binary):
return exec_command(True, lua_binary, "-e", return get_output(lua_binary, "-e", "print(_VERSION:sub(5))")
"print(_VERSION:sub(5))").strip()
raise "Could not locate the LUA binary!" raise "Could not locate the LUA binary!"
def luarocks_help(self): def luarocks_help(self):
return exec_command(True, "install.bat", "/?").strip() return get_output("install.bat", "/?")
def build(self): def build(self):
self.fetch() self.fetch()
@ -882,21 +882,21 @@ class LuaRocks(Program):
# Since LuaRocks 2.1.2 # Since LuaRocks 2.1.2
if "/NOREG" in help_text: if "/NOREG" in help_text:
args += ["/NOREG", "/Q"] args += ["/NOREG", "/Q"]
run_command(args) run(args)
else: else:
print("Building LuaRocks" + self.version_suffix) print("Building LuaRocks" + self.version_suffix)
run_command("./configure", "--prefix=" + opts.location, run("./configure", "--prefix=" + opts.location,
"--with-lua=" + opts.location, "--force-config") "--with-lua=" + opts.location, "--force-config")
if self.is_luarocks_2_0(): if self.is_luarocks_2_0():
run_command("make") run("make")
else: else:
run_command("make", "build") run("make", "build")
def install(self): def install(self):
if not self.win32_zip: if not self.win32_zip:
print("Installing LuaRocks" + self.version_suffix) print("Installing LuaRocks" + self.version_suffix)
run_command("make", "install") run("make", "install")
def get_manifest_name(): def get_manifest_name():
return os.path.join(opts.location, "hererocks.manifest") return os.path.join(opts.location, "hererocks.manifest")