Skip to content

Commit

Permalink
checks for main branch during environmental-check.
Browse files Browse the repository at this point in the history
  • Loading branch information
birkin committed Jan 17, 2025
1 parent 3350f4f commit 24169bc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
58 changes: 42 additions & 16 deletions lib_environment_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@ def determine_project_email_addresses(project_path: Path) -> list[list[str, str]
## end def determine_project_email_addresses()


def check_branch(project_path, project_email_addresses) -> None:
"""
Checks that the project is on the `main` branch.
If not, sends an email to the project sys-admins, then exits.
"""
branch = fetch_branch_data(project_path)
if branch != 'main':
message = f'Error: Project is on branch ``{branch}`` instead of ``main``'
log.exception(message)
## email project sys-admins ---------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)


def fetch_branch_data(project_path: Path) -> str:
"""
Fetches branch-data by reading the `.git/HEAD` file (avoiding calling git via subprocess due to `dubious ownership` issue).
Called by copy_new_compile_to_codebase()
"""
log.debug('starting fetch_branch_data')
git_dir = project_path / '.git'
try:
## read HEAD file to find the project branch ------------
head_file = git_dir / 'HEAD'
ref_line = head_file.read_text().strip()
if ref_line.startswith('ref:'):
project_branch = ref_line.split('/')[-1] # extract the project_branch name
else:
project_branch = 'detached'
except FileNotFoundError:
log.warning('no `.git` directory or HEAD file found.')
project_branch = 'project_branch_not_found'
except Exception:
log.exception('other problem fetching project_branch data')
project_branch = 'project_branch_not_found'
log.debug(f'project_branch: ``{project_branch}``')
return project_branch


def determine_python_version(project_path: Path, project_email_addresses: list[list[str, str]]) -> tuple[str, str, str]:
"""
Determines Python version from the target-project's virtual environment.
Expand Down Expand Up @@ -163,22 +205,6 @@ def determine_environment_type(project_path: Path, project_email_addresses: list
return env_type


# def determine_environment_type() -> str:
# """
# Infers environment type based on the system hostname.
# Returns 'local', 'staging', or 'production'.
# """
# hostname: str = subprocess.check_output(['hostname'], text=True).strip()
# if hostname.startswith('d') or hostname.startswith('q'):
# env_type: str = 'staging'
# elif hostname.startswith('p'):
# env_type: str = 'production'
# else:
# env_type: str = 'local'
# log.debug(f'env_type: {env_type}')
# return env_type


def determine_uv_path() -> Path:
"""
Checks `which` for the `uv` command.
Expand Down
1 change: 1 addition & 0 deletions self_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def manage_update(project_path: str) -> None:
os.chdir(project_path)
## get everything needed up front -------------------------------
project_email_addresses: list[list[str, str]] = lib_environment_checker.determine_project_email_addresses(project_path)
lib_environment_checker.check_branch(project_path, project_email_addresses) # emails admins and exits if not on main
version_info: tuple[str, str, str] = lib_environment_checker.determine_python_version(
project_path, project_email_addresses
) # ie, ('3.12.4', '~=3.12.0', '/path/to/python3.12')
Expand Down

0 comments on commit 24169bc

Please sign in to comment.