Refactorings related to subprocess

Use tuples instead of lists where possible.

Prepend commands with "/usr/bin/env".
This commit is contained in:
Stephen Seo 2022-09-07 16:53:21 +09:00
parent d51430cc10
commit c8f0bd6f06

137
update.py
View file

@ -64,7 +64,13 @@ def ensure_pkg_dir_exists(pkg, pkg_state, other_state):
pkg_state[pkg]["repo_path"] = AUR_GIT_REPO_PATH_TEMPLATE.format(pkg) pkg_state[pkg]["repo_path"] = AUR_GIT_REPO_PATH_TEMPLATE.format(pkg)
try: try:
subprocess.run( subprocess.run(
["git", "clone", pkg_state[pkg]["repo_path"], pkgdir], (
"/usr/bin/env",
"git",
"clone",
pkg_state[pkg]["repo_path"],
pkgdir,
),
check=True, check=True,
) )
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
@ -104,7 +110,7 @@ def update_pkg_dir(pkg, pkg_state, other_state):
# fetch all # fetch all
try: try:
subprocess.run( subprocess.run(
["git", "fetch", "-p", "--all"], ("/usr/bin/env", "git", "fetch", "-p", "--all"),
check=True, check=True,
cwd=pkgdir, cwd=pkgdir,
) )
@ -118,7 +124,7 @@ def update_pkg_dir(pkg, pkg_state, other_state):
remotes = [] remotes = []
try: try:
result = subprocess.run( result = subprocess.run(
["git", "remote"], ("/usr/bin/env", "git", "remote"),
check=True, check=True,
cwd=pkgdir, cwd=pkgdir,
capture_output=True, capture_output=True,
@ -145,7 +151,7 @@ def update_pkg_dir(pkg, pkg_state, other_state):
selected_remote = None selected_remote = None
try: try:
result = subprocess.run( result = subprocess.run(
["git", "status", "-sb", "--porcelain"], ("/usr/bin/env", "git", "status", "-sb", "--porcelain"),
check=True, check=True,
cwd=pkgdir, cwd=pkgdir,
capture_output=True, capture_output=True,
@ -172,7 +178,7 @@ def update_pkg_dir(pkg, pkg_state, other_state):
current_branch_hash = None current_branch_hash = None
try: try:
result = subprocess.run( result = subprocess.run(
["git", "log", "-1", "--format=format:%H"], ("/usr/bin/env", "git", "log", "-1", "--format=format:%H"),
check=True, check=True,
cwd=pkgdir, cwd=pkgdir,
capture_output=True, capture_output=True,
@ -194,7 +200,14 @@ def update_pkg_dir(pkg, pkg_state, other_state):
remote_branch_hash = None remote_branch_hash = None
try: try:
result = subprocess.run( result = subprocess.run(
["git", "log", "-1", "--format=format:%H", selected_remote], (
"/usr/bin/env",
"git",
"log",
"-1",
"--format=format:%H",
selected_remote,
),
check=True, check=True,
cwd=pkgdir, cwd=pkgdir,
capture_output=True, capture_output=True,
@ -215,16 +228,18 @@ def update_pkg_dir(pkg, pkg_state, other_state):
# update current branch if not same commit # update current branch if not same commit
if current_branch_hash != remote_branch_hash: if current_branch_hash != remote_branch_hash:
try: try:
subprocess.run(["git", "pull"], check=True, cwd=pkgdir) subprocess.run(
("/usr/bin/env", "git", "pull"), check=True, cwd=pkgdir
)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
try: try:
subprocess.run( subprocess.run(
["git", "restore", "."], ("/usr/bin/env", "git", "restore", "."),
check=True, check=True,
cwd=pkgdir, cwd=pkgdir,
) )
subprocess.run( subprocess.run(
["git", "pull"], ("/usr/bin/env", "git", "pull"),
check=True, check=True,
cwd=pkgdir, cwd=pkgdir,
) )
@ -248,7 +263,9 @@ def check_pkg_build(pkg, pkg_state, other_state, editor):
pkgdir = os.path.join(other_state["clones_dir"], pkg) pkgdir = os.path.join(other_state["clones_dir"], pkg)
log_print('Checking PKGBUILD for "{}"...'.format(pkg)) log_print('Checking PKGBUILD for "{}"...'.format(pkg))
try: try:
subprocess.run([editor, "PKGBUILD"], check=True, cwd=pkgdir) subprocess.run(
("/usr/bin/env", editor, "PKGBUILD"), check=True, cwd=pkgdir
)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
log_print('ERROR: Failed checking PKGBUILD for "{}"'.format(pkg)) log_print('ERROR: Failed checking PKGBUILD for "{}"'.format(pkg))
return "abort" return "abort"
@ -383,6 +400,7 @@ def get_pkgbuild_version(pkg, force_check_srcinfo, pkg_state, other_state):
'Running "makechrootpkg ... --nobuild" to ensure pkgver in PKGBUILD is updated...' 'Running "makechrootpkg ... --nobuild" to ensure pkgver in PKGBUILD is updated...'
) )
command_list = [ command_list = [
"/usr/bin/env",
"makechrootpkg", "makechrootpkg",
"-c", "-c",
"-r", "-r",
@ -394,8 +412,8 @@ def get_pkgbuild_version(pkg, force_check_srcinfo, pkg_state, other_state):
if not dep_fullpath: if not dep_fullpath:
log_print('ERROR: Failed to get dep "{}"'.format(dep)) log_print('ERROR: Failed to get dep "{}"'.format(dep))
sys.exit(1) sys.exit(1)
command_list.insert(1, "-I") command_list.insert(2, "-I")
command_list.insert(2, dep_fullpath) command_list.insert(3, dep_fullpath)
for aur_dep in pkg_state[pkg]["aur_deps"]: for aur_dep in pkg_state[pkg]["aur_deps"]:
aur_dep_fullpath = get_latest_pkg( aur_dep_fullpath = get_latest_pkg(
aur_dep, other_state["pkg_out_dir"] aur_dep, other_state["pkg_out_dir"]
@ -405,8 +423,8 @@ def get_pkgbuild_version(pkg, force_check_srcinfo, pkg_state, other_state):
'ERROR: Failed to get aur_dep "{}"'.format(aur_dep) 'ERROR: Failed to get aur_dep "{}"'.format(aur_dep)
) )
sys.exit(1) sys.exit(1)
command_list.insert(1, "-I") command_list.insert(2, "-I")
command_list.insert(2, aur_dep_fullpath) command_list.insert(3, aur_dep_fullpath)
subprocess.run( subprocess.run(
command_list + post_command_list, command_list + post_command_list,
check=True, check=True,
@ -428,11 +446,12 @@ def get_pkgbuild_version(pkg, force_check_srcinfo, pkg_state, other_state):
# TODO maybe sandbox sourcing the PKGBUILD # TODO maybe sandbox sourcing the PKGBUILD
pkgbuild_output = subprocess.run( pkgbuild_output = subprocess.run(
[ (
"/usr/bin/env",
"bash", "bash",
"-c", "-c",
f"source {os.path.join(pkgdir, 'PKGBUILD')}; echo \"pkgver=$pkgver\"; echo \"pkgrel=$pkgrel\"; echo \"epoch=$epoch\"", f"source {os.path.join(pkgdir, 'PKGBUILD')}; echo \"pkgver=$pkgver\"; echo \"pkgrel=$pkgrel\"; echo \"epoch=$epoch\"",
], ),
capture_output=True, capture_output=True,
text=True, text=True,
) )
@ -558,13 +577,17 @@ def get_pkg_current_version(pkg, pkg_state, repo):
current_version = None current_version = None
try: try:
result = subprocess.run( result = subprocess.run(
(
"/usr/bin/env",
"bash",
"-c"
"tar -tf {} | grep '{}.*/$'".format( "tar -tf {} | grep '{}.*/$'".format(
repo, pkg_state[pkg]["pkg_name"] repo, pkg_state[pkg]["pkg_name"]
), ),
),
check=True, check=True,
capture_output=True, capture_output=True,
encoding="UTF-8", encoding="UTF-8",
shell=True,
) )
reprog = re.compile( reprog = re.compile(
"^{}-(?P<epoch>[0-9]+:)?(?P<version>[^-/: ]*-[0-9]+)/$".format( "^{}-(?P<epoch>[0-9]+:)?(?P<version>[^-/: ]*-[0-9]+)/$".format(
@ -600,11 +623,11 @@ def get_sudo_privileges():
if not SUDO_PROC: if not SUDO_PROC:
log_print("sudo -v") log_print("sudo -v")
try: try:
subprocess.run(["sudo", "-v"], check=True) subprocess.run(("/usr/bin/env", "sudo", "-v"), check=True)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return False return False
SUDO_PROC = subprocess.Popen( SUDO_PROC = subprocess.Popen(
["while true; do sudo -v; sleep 2m; done"], shell=True "while true; do sudo -v; sleep 2m; done", shell=True
) )
atexit.register(cleanup_sudo, sudo_proc=SUDO_PROC) atexit.register(cleanup_sudo, sudo_proc=SUDO_PROC)
return True return True
@ -631,32 +654,32 @@ def create_executable_script(dest_filename, script_contents):
tempf_name = tempf.name tempf_name = tempf.name
try: try:
subprocess.run( subprocess.run(
[ (
"/usr/bin/env", "/usr/bin/env",
"sudo", "sudo",
"cp", "cp",
tempf_name, tempf_name,
dest_filename, dest_filename,
], ),
check=True, check=True,
) )
subprocess.run( subprocess.run(
[ (
"/usr/bin/env", "/usr/bin/env",
"sudo", "sudo",
"chmod", "chmod",
"a+rx", "a+rx",
dest_filename, dest_filename,
], ),
check=True, check=True,
) )
subprocess.run( subprocess.run(
[ (
"/usr/bin/env", "/usr/bin/env",
"rm", "rm",
"-f", "-f",
tempf_name, tempf_name,
], ),
check=True, check=True,
) )
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
@ -673,13 +696,14 @@ def setup_ccache(chroot):
# set up ccache stuff # set up ccache stuff
try: try:
subprocess.run( subprocess.run(
[ (
"/usr/bin/env",
"sudo", "sudo",
"sed", "sed",
"-i", "-i",
"/^BUILDENV=/s/!ccache/ccache/", "/^BUILDENV=/s/!ccache/ccache/",
f"{chroot}/root/etc/makepkg.conf", f"{chroot}/root/etc/makepkg.conf",
], ),
check=True, check=True,
) )
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
@ -693,13 +717,14 @@ def cleanup_ccache(chroot):
# cleanup ccache stuff # cleanup ccache stuff
try: try:
subprocess.run( subprocess.run(
[ (
"/usr/bin/env",
"sudo", "sudo",
"sed", "sed",
"-i", "-i",
"/^BUILDENV=/s/ ccache/ !ccache/", "/^BUILDENV=/s/ ccache/ !ccache/",
f"{chroot}/root/etc/makepkg.conf", f"{chroot}/root/etc/makepkg.conf",
], ),
check=True, check=True,
) )
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
@ -750,7 +775,8 @@ def cleanup_sccache(chroot):
# cleanup sccache stuff # cleanup sccache stuff
try: try:
subprocess.run( subprocess.run(
[ (
"/usr/bin/env",
"sudo", "sudo",
"rm", "rm",
"-f", "-f",
@ -762,7 +788,7 @@ def cleanup_sccache(chroot):
f"{chroot}/root/usr/local/bin/clang", f"{chroot}/root/usr/local/bin/clang",
f"{chroot}/root/usr/local/bin/clang++", f"{chroot}/root/usr/local/bin/clang++",
f"{chroot}/root/usr/local/bin/rustc", f"{chroot}/root/usr/local/bin/rustc",
], ),
check=False, check=False,
) )
except BaseException: except BaseException:
@ -798,6 +824,7 @@ def update_pkg_list(
cleanup_sccache(other_state["chroot"]) cleanup_sccache(other_state["chroot"])
command_list = [ command_list = [
"/usr/bin/env",
"makechrootpkg", "makechrootpkg",
"-c", "-c",
"-r", "-r",
@ -815,8 +842,8 @@ def update_pkg_list(
if not dep_fullpath: if not dep_fullpath:
log_print('ERROR: Failed to get dep "{}"'.format(dep)) log_print('ERROR: Failed to get dep "{}"'.format(dep))
sys.exit(1) sys.exit(1)
command_list.insert(1, "-I") command_list.insert(2, "-I")
command_list.insert(2, dep_fullpath) command_list.insert(3, dep_fullpath)
for aur_dep in pkg_state[pkg]["aur_deps"]: for aur_dep in pkg_state[pkg]["aur_deps"]:
aur_dep_fullpath = get_latest_pkg( aur_dep_fullpath = get_latest_pkg(
aur_dep, other_state["pkg_out_dir"] aur_dep, other_state["pkg_out_dir"]
@ -824,15 +851,15 @@ def update_pkg_list(
if not aur_dep_fullpath: if not aur_dep_fullpath:
log_print('ERROR: Failed to get aur_dep "{}"'.format(aur_dep)) log_print('ERROR: Failed to get aur_dep "{}"'.format(aur_dep))
sys.exit(1) sys.exit(1)
command_list.insert(1, "-I") command_list.insert(2, "-I")
command_list.insert(2, aur_dep_fullpath) command_list.insert(3, aur_dep_fullpath)
if "ccache_dir" in pkg_state[pkg]: if "ccache_dir" in pkg_state[pkg]:
command_list.insert(1, "-d") command_list.insert(2, "-d")
command_list.insert(2, f'{pkg_state[pkg]["ccache_dir"]}:/ccache') command_list.insert(3, f'{pkg_state[pkg]["ccache_dir"]}:/ccache')
post_command_list.insert(1, "CCACHE_DIR=/ccache") post_command_list.insert(1, "CCACHE_DIR=/ccache")
elif "sccache_dir" in pkg_state[pkg]: elif "sccache_dir" in pkg_state[pkg]:
command_list.insert(1, "-d") command_list.insert(2, "-d")
command_list.insert(2, f'{pkg_state[pkg]["sccache_dir"]}:/sccache') command_list.insert(3, f'{pkg_state[pkg]["sccache_dir"]}:/sccache')
post_command_list.insert(1, "SCCACHE_DIR=/sccache") post_command_list.insert(1, "SCCACHE_DIR=/sccache")
post_command_list.insert( post_command_list.insert(
2, f'SCCACHE_CACHE_SIZE={pkg_state[pkg]["sccache_cache_size"]}' 2, f'SCCACHE_CACHE_SIZE={pkg_state[pkg]["sccache_cache_size"]}'
@ -918,7 +945,7 @@ def update_pkg_list(
log_print(f'Signing "{other_state["repo"]}"...') log_print(f'Signing "{other_state["repo"]}"...')
try: try:
subprocess.run( subprocess.run(
[ (
"/usr/bin/rm", "/usr/bin/rm",
"-f", "-f",
str( str(
@ -927,11 +954,12 @@ def update_pkg_list(
f"{other_state['repo']}.sig", f"{other_state['repo']}.sig",
) )
), ),
] )
) )
subprocess.run( subprocess.run(
[ (
"/usr/bin/gpg", "/usr/bin/env",
"gpg",
"--batch", "--batch",
"--passphrase-fd", "--passphrase-fd",
"0", "0",
@ -942,10 +970,10 @@ def update_pkg_list(
"--detach-sign", "--detach-sign",
str( str(
os.path.join( os.path.join(
other_state["pkg_out_dir"], f"{other_state['repo']}" other_state["pkg_out_dir"], other_state["repo"]
) )
), ),
], ),
check=True, check=True,
input=signing_gpg_pass, input=signing_gpg_pass,
text=True, text=True,
@ -955,17 +983,18 @@ def update_pkg_list(
if repo_sig_name.rfind("/") != -1: if repo_sig_name.rfind("/") != -1:
repo_sig_name = repo_sig_name.rsplit(sep="/", maxsplit=1)[1] repo_sig_name = repo_sig_name.rsplit(sep="/", maxsplit=1)[1]
subprocess.run( subprocess.run(
[ (
"/usr/bin/ln", "/usr/bin/env",
"ln",
"-sf", "-sf",
repo_sig_name, repo_sig_name,
str( str(
os.path.join( os.path.join(
other_state["pkg_out_dir"], f"{other_state['repo']}" other_state["pkg_out_dir"], other_state["repo"]
) )
).removesuffix(".tar") ).removesuffix(".tar")
+ ".sig", + ".sig",
] )
) )
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
log_print(f'WARNING: Failed to sign "{other_state["repo"]}"') log_print(f'WARNING: Failed to sign "{other_state["repo"]}"')
@ -1067,7 +1096,8 @@ def test_gpg_passphrase(signing_gpg_dir, signing_key_fp, passphrase):
tempnf.flush() tempnf.flush()
try: try:
subprocess.run( subprocess.run(
[ (
"/usr/bin/env",
"gpg", "gpg",
"--batch", "--batch",
"--passphrase-fd", "--passphrase-fd",
@ -1078,7 +1108,7 @@ def test_gpg_passphrase(signing_gpg_dir, signing_key_fp, passphrase):
signing_key_fp, signing_key_fp,
"--detach-sign", "--detach-sign",
tempnf.name, tempnf.name,
], ),
check=True, check=True,
input=passphrase, input=passphrase,
text=True, text=True,
@ -1320,12 +1350,13 @@ if __name__ == "__main__":
log_print("Updating the chroot...") log_print("Updating the chroot...")
try: try:
subprocess.run( subprocess.run(
[ (
"/usr/bin/env",
"arch-nspawn", "arch-nspawn",
"{}/root".format(other_state["chroot"]), "{}/root".format(other_state["chroot"]),
"pacman", "pacman",
"-Syu", "-Syu",
], ),
check=True, check=True,
) )
except subprocess.CalledProcessError: except subprocess.CalledProcessError: