From 23c1ec8262858ae829ab2cbbbadca567eb5fd484 Mon Sep 17 00:00:00 2001 From: David Sangrey Date: Sun, 9 Nov 2025 18:54:01 -0500 Subject: [PATCH] [Minor] Improve Safety of Subprocess Call --- config/__init__.py | 43 +++++++++++++++++++++---------------------- edshipyard.py | 2 +- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/config/__init__.py b/config/__init__.py index be4841a8..30f15998 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -82,37 +82,36 @@ def git_shorthash_from_head() -> str | None: """ Determine short hash for current git HEAD. - Includes `.DIRTY` if any changes have been made from HEAD + Includes `.DIRTY` if any changes have been made from HEAD. :return: str | None: None if we couldn't determine the short hash. """ - shorthash: str | None = None - try: - git_cmd = subprocess.Popen( - "git rev-parse --short HEAD".split(), - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, + result = subprocess.run( + ["git", "rev-parse", "--short", "HEAD"], + capture_output=True, + text=True, + check=True, ) - out, err = git_cmd.communicate() - + shorthash = result.stdout.strip() except (subprocess.CalledProcessError, FileNotFoundError) as e: logger.info(f"Couldn't run git command for short hash: {e!r}") + return None - else: - shorthash = out.decode().rstrip('\n') - if re.match(r'^[0-9a-f]{7,}$', shorthash) is None: - logger.error(f"'{shorthash}' doesn't look like a valid git short hash, forcing to None") - shorthash = None + if not re.fullmatch(r"[0-9a-f]{7,}", shorthash): + logger.error(f"'{shorthash}' doesn't look like a valid git short hash, forcing to None") + return None - if shorthash is not None: - with contextlib.suppress(Exception): - result = subprocess.run('git diff --stat HEAD'.split(), capture_output=True) - if len(result.stdout) > 0: - shorthash += '.DIRTY' - - if len(result.stderr) > 0: - logger.warning(f'Data from git on stderr:\n{str(result.stderr)}') + with contextlib.suppress(Exception): + diff_result = subprocess.run( + ["git", "diff", "--stat", "HEAD"], + capture_output=True, + check=True + ) + if diff_result.stdout: + shorthash += ".DIRTY" + if diff_result.stderr: + logger.warning(f"Data from git on stderr:\n{diff_result.stderr.decode()}") return shorthash diff --git a/edshipyard.py b/edshipyard.py index 28e6dc2f..f7bceb75 100644 --- a/edshipyard.py +++ b/edshipyard.py @@ -106,7 +106,7 @@ def export(data, filename=None) -> None: # noqa: C901, CCR001 else: name = module['name'] # type: ignore - if name == 'Frame Shift Drive' or name == 'Frame Shift Drive (SCO)': + if name in ['Frame Shift Drive', 'Frame Shift Drive (SCO)']: fsd = module # save for range calculation if mods.get('OutfittingFieldType_FSDOptimalMass'):