Skip to content

Commit

Permalink
Refactor inject_sketch_name.py for improved readability and consisten…
Browse files Browse the repository at this point in the history
…cy in subprocess calls
  • Loading branch information
FredM67 committed Dec 14, 2024
1 parent fc31913 commit 15811e0
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions Mk2_fasterControl_Full/inject_sketch_name.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
from datetime import datetime, timezone
from datetime import datetime
import os
from pathlib import Path
import subprocess
from subprocess import check_output, CalledProcessError
from subprocess import CalledProcessError

def get_git_revision_short_hash() -> str:
try:
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'],shell=True).decode('utf-8').strip()
except CalledProcessError:
return "N/A"
try:
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], shell=True).decode('utf-8').strip()
except CalledProcessError:
return "N/A"

def get_git_current_branch() -> str:
try:
return subprocess.check_output(['git', 'branch', '--show-current'],shell=True).decode('utf-8').strip()
except CalledProcessError:
return "N/A"
try:
return subprocess.check_output(['git', 'branch', '--show-current'], shell=True).decode('utf-8').strip()
except CalledProcessError:
return "N/A"

Import("env")
proj_path = env["PROJECT_DIR"]

proj_path = os.path.join(proj_path, "dummy")

macro_value = r"\"" + os.path.split(os.path.dirname(proj_path))[1] + r"\""
proj_path = os.path.join(env["PROJECT_DIR"], "dummy")

macro_value = f"\"{os.path.split(os.path.dirname(proj_path))[1]}\""
tz_dt = datetime.now().replace(microsecond=0).astimezone().isoformat(' ')

env.Append(CPPDEFINES=[
("PROJECT_PATH", macro_value),
("CURRENT_TIME", "\\\"" + tz_dt + "\\\""),
("BRANCH_NAME", r"\"" + get_git_current_branch() + r"\""),
("COMMIT_HASH", r"\"" + get_git_revision_short_hash() + r"\"")
("PROJECT_PATH", macro_value),
("CURRENT_TIME", f"\"{tz_dt}\""),
("BRANCH_NAME", f"\"{get_git_current_branch()}\""),
("COMMIT_HASH", f"\"{get_git_revision_short_hash()}\"")
])

0 comments on commit 15811e0

Please sign in to comment.