Skip to content

Commit

Permalink
Avoid use of chdir()
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-uva committed Nov 18, 2024
1 parent 4e5ef88 commit 9197c99
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions common/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import smtplib
import fnmatch
import socket
import shlex
import copy
import time
import json
Expand Down Expand Up @@ -112,10 +113,8 @@ def get_git_branch():
repository or git is not installed an empty string is returned.
"""
try:
cwd = os.getcwd()
os.chdir(config.get('PATH_ROOT'))
branch = subprocess.run(["git", "branch", "--show-current"], stdout=subprocess.PIPE)
os.chdir(cwd)
root_dir = str(config.get('PATH_ROOT').resolve())
branch = subprocess.run(shlex.split(f"git -C {shlex.quote(root_dir)} branch --show-current"), stdout=subprocess.PIPE)
if branch.returncode != 0:
raise ValueError()
return branch.stdout.decode("utf-8").strip()
Expand Down Expand Up @@ -145,7 +144,6 @@ def get_software_commit(worker=None):
# try git command line within the 4CAT root folder
# if it is a checked-out git repository, it will tell us the hash of
# the currently checked-out commit
cwd = os.getcwd()

# path has no Path.relative()...
relative_filepath = Path(re.sub(r"^[/\\]+", "", worker.filepath)).parent
Expand All @@ -155,24 +153,24 @@ def get_software_commit(worker=None):
# useful version info (since the extension is by definition not in the
# main 4CAT repository) and will return an empty value
if worker and worker.is_extension:
extension_dir = config.get("PATH_ROOT").joinpath(relative_filepath)
os.chdir(extension_dir)
working_dir = str(config.get("PATH_ROOT").joinpath(relative_filepath).resolve())
# check if we are in the extensions' own repo or 4CAT's
repo_level = subprocess.run(["git", "rev-parse", "--show-toplevel"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
git_cmd = f"git -C {shlex.quote(working_dir)} rev-parse --show-toplevel"
repo_level = subprocess.run(shlex.split(git_cmd), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if Path(repo_level.stdout.decode("utf-8")) == config.get("PATH_ROOT"):
# not its own repository
return ("", "")

else:
os.chdir(config.get("PATH_ROOT"))
working_dir = str(config.get("PATH_ROOT").resolve())

show = subprocess.run(["git", "show"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
show = subprocess.run(shlex.split(f"git -C {shlex.quote(working_dir)} show"), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if show.returncode != 0:
raise ValueError()
commit = show.stdout.decode("utf-8").split("\n")[0].split(" ")[1]

# now get the repository the commit belongs to, if we can
origin = subprocess.run(["git", "config", "--get", "remote.origin.url"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
origin = subprocess.run(shlex.split(f"git -C {shlex.quote(working_dir)} config --get remote.origin.url"), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if origin.returncode != 0 or not origin.stdout:
raise ValueError()
repository = origin.stdout.decode("utf-8").strip()
Expand All @@ -182,9 +180,6 @@ def get_software_commit(worker=None):
except (subprocess.SubprocessError, IndexError, TypeError, ValueError, FileNotFoundError) as e:
return ("", "")

finally:
os.chdir(cwd)

return (commit, repository)

def get_software_version():
Expand Down Expand Up @@ -280,7 +275,6 @@ def find_extensions():

# collect metadata for extensions
allowed_metadata_keys = ("name", "version", "url")
cwd = os.getcwd()
for extension in extensions:
extension_folder = extension_path.joinpath(extension)
metadata_file = extension_folder.joinpath("metadata.json")
Expand All @@ -297,8 +291,8 @@ def find_extensions():
if extensions[extension]["is_git"]:
# try to get remote URL
try:
os.chdir(extension_folder)
origin = subprocess.run(["git", "config", "--get", "remote.origin.url"], stderr=subprocess.PIPE,
extension_root = str(extension_folder.resolve())
origin = subprocess.run(shlex.split(f"git -C {shlex.quote(extension_root)} config --get remote.origin.url"), stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
if origin.returncode != 0 or not origin.stdout:
raise ValueError()
Expand All @@ -310,8 +304,6 @@ def find_extensions():
except (subprocess.SubprocessError, IndexError, TypeError, ValueError, FileNotFoundError) as e:
print(e)
pass
finally:
os.chdir(cwd)

return extensions, errors

Expand Down

0 comments on commit 9197c99

Please sign in to comment.