Skip to content

Commit

Permalink
neatens log entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
birkin committed Jan 27, 2025
1 parent d57e56b commit ba7f036
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 40 deletions.
7 changes: 4 additions & 3 deletions lib_call_runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def run_initial_tests(uv_path: Path, project_path: Path, project_email_addresses
- Emails project-admins
- Raises an exception
"""
log.debug('starting run_initial_tests()')
log.info('::: running initial tests ----------')
## set the venv -------------------------------------------------
venv_tuple: tuple[Path, Path] = lib_common.determine_venv_paths(project_path) # these are resolved-paths
(venv_bin_path, venv_path) = venv_tuple
Expand All @@ -28,6 +28,7 @@ def run_initial_tests(uv_path: Path, project_path: Path, project_email_addresses
## run the command ----------------------------------------------
try:
subprocess.run(command, check=True, env=local_scoped_env)
log.info('ok / initial tests passed')
except Exception as e:
message = f'Error on initial run_tests() call: ``{e}``. Halting self-update.'
log.exception(message)
Expand All @@ -50,7 +51,7 @@ def run_followup_tests(uv_path: Path, project_path: Path, project_email_addresse
- returns "tests failed" message (to be add to the diff email)
- does not exit, so that diffs can be emailed and permissions updated
"""
log.debug('starting run_followup_tests()')
log.info('::: running followup tests ----------')
## set the venv -------------------------------------------------
venv_tuple: tuple[Path, Path] = lib_common.determine_venv_paths(project_path) # these are resolved-paths
(venv_bin_path, venv_path) = venv_tuple
Expand All @@ -61,7 +62,7 @@ def run_followup_tests(uv_path: Path, project_path: Path, project_email_addresse
## run the command ----------------------------------------------
try:
subprocess.run(command, check=True, env=local_scoped_env)
log.debug('followup tests passed')
log.info('ok / followup tests passed')
return_val = None
except subprocess.CalledProcessError as e:
message = f'Error on followup run_tests() call: ``{e}``.'
Expand Down
16 changes: 10 additions & 6 deletions lib_compilation_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def compare_with_previous_backup(
Returns False if there are no changes, True otherwise.
(Currently the manager-script just passes in the new_path, and the old_path is determined.)
"""
log.debug('starting compare_with_previous_backup()')
log.info('::: starting compare to check for changes ----------')
changes = True
## try to get the old-path --------------------------------------
if not old_path:
Expand All @@ -49,7 +49,7 @@ def compare_with_previous_backup(
if curr_lines_filtered == prev_lines_filtered:
log.debug('no differences found in dependencies.')
changes = False
log.debug(f'changes: ``{changes}``')
log.info(f'ok / changes, ``{changes}``')
return changes # just the boolean

def filter_initial_comments(self, lines: list[str]) -> list[str]:
Expand All @@ -70,7 +70,7 @@ def make_diff_text(self, project_path: Path) -> str:
Creates a diff from the two most recent requirements files.
Called by send_email_of_diffs().
"""
log.debug('starting make_diff_text()')
log.info('::: making diff-text ----------')
## get the two most recent backup files -------------------------
backup_dir: Path = project_path.parent / 'requirements_backups'
log.debug(f'backup_dir: ``{backup_dir}``')
Expand All @@ -90,7 +90,7 @@ def make_diff_text(self, project_path: Path) -> str:
diff_lines = [f'--- {previous_file.name}\n', f'+++ {current_file.name}\n']
diff_lines.extend(difflib.unified_diff(prev_lines_filtered, curr_lines_filtered))
diff_text = ''.join(diff_lines)
log.debug(f'diff_text: ``{diff_text}``')
log.info(f'ok / diff_text, ``{diff_text}``')
return diff_text

def copy_new_compile_to_codebase(self, compiled_requirements: Path, project_path: Path, environment_type: str) -> str:
Expand All @@ -102,7 +102,7 @@ def copy_new_compile_to_codebase(self, compiled_requirements: Path, project_path
Called by self_updater.py.
"""
log.debug('starting copy_new_compile_to_codebase()')
log.info('::: copying new compile to codebase ----------')
## copy new requirements file to project --------------------
problem_message = ''
try:
Expand All @@ -113,7 +113,7 @@ def copy_new_compile_to_codebase(self, compiled_requirements: Path, project_path
compiled_requirements_lines = compiled_requirements.read_text().splitlines()
compiled_requirements_lines = [line for line in compiled_requirements_lines if not line.startswith('#')]
save_path.write_text('\n'.join(compiled_requirements_lines))
log.debug('new requirements file copied to project.')
log.info('ok / new requirements file copied to project.')
except Exception as e:
problem_message = f'Error copying new requirements file to project; error: ``{e}``'
log.exception(problem_message)
Expand All @@ -123,13 +123,17 @@ def copy_new_compile_to_codebase(self, compiled_requirements: Path, project_path
lib_git_handler.run_git_add(save_path, project_path)
try:
## run a git-commit via subprocess ------------------------
log.info('::: running git-commit ----------')
command = ['git', 'commit', '-m', 'auto-update of requirements']
log.debug(f'git-commit-command, ``{command}``')
subprocess.run(command, cwd=project_path, check=True, capture_output=True, text=True)
log.info('ok / git-commit successful')
## run a git-push via subprocess ------------------------
log.info('::: running git-push ----------')
command = ['git', 'push', 'origin', 'main']
log.debug(f'git-push command, ``{command}``')
subprocess.run(command, cwd=project_path, check=True)
log.info('ok / git-push successful')
except Exception as e:
log.debug(f'e.returncode, ``{e.returncode}``')
stderr_output = e.stderr or '' # safeguard against None
Expand Down
38 changes: 22 additions & 16 deletions lib_environment_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def validate_project_path(project_path: Path) -> None:
- Sends an email to the self-updater sys-admins
- Exits the script
"""
log.debug('starting validate_project_path()')
log.debug(f'project_path: ``{project_path}``')
log.info('::: validating project_path ----------')
# log.debug(f'project_path: ``{project_path}``')
if not project_path.exists():
message = f'Error: The provided project_path ``{project_path}`` does not exist. Halting self-update.'
log.exception(message)
Expand All @@ -35,7 +35,7 @@ def validate_project_path(project_path: Path) -> None:
## raise exception -----------------------------------------
raise Exception(message)
else:
log.debug('project_path valid')
log.info(f'ok / project_path, ``{project_path}``')
return


Expand All @@ -54,14 +54,12 @@ def determine_project_email_addresses(project_path: Path) -> list[list[str, str]
- Sends an email to the self-updater sys-admins
- Exits the script
"""
log.debug('starting determine_project_email_addresses()')
log.info('::: determining email addresses ----------')
try:
settings: dict = dotenv.dotenv_values('../.env')
# email_addresses: list[list[str, str]] = settings['ADMINS_JSON']
email_addresses_json: str = settings['ADMINS_JSON']
email_addresses: list[list[str, str]] = json.loads(email_addresses_json)
log.debug(f'email_addresses: {email_addresses}')
log.debug(f'type(email_addresses): {type(email_addresses)}')
log.info(f'ok / email_addresses: {email_addresses}')
return email_addresses
except Exception as e:
message = f'Error determining email addresses: {e}'
Expand All @@ -81,6 +79,7 @@ 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.
"""
log.info('::: checking branch ----------')
branch = fetch_branch_data(project_path)
if branch != 'main':
message = f'Error: Project is on branch ``{branch}`` instead of ``main``'
Expand All @@ -91,14 +90,17 @@ def check_branch(project_path, project_email_addresses) -> None:
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)
else:
log.info(f'ok / branch, ``{branch}``')
return


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 check_branch()
"""
log.debug('starting fetch_branch_data')
# log.debug('starting fetch_branch_data')
git_dir = project_path / '.git'
try:
## read HEAD file to find the project branch ------------
Expand All @@ -114,7 +116,7 @@ def fetch_branch_data(project_path: Path) -> str:
except Exception:
log.exception('other problem fetching project_branch data')
project_branch = 'project_branch_not_found'
log.debug(f'project_branch: ``{project_branch}``')
# log.debug(f'project_branch: ``{project_branch}``')
return project_branch


Expand All @@ -128,7 +130,7 @@ def check_git_status(project_path: Path, project_email_addresses: list[list[str,
Note: just looking for the word 'clean' because one version of git says "working tree clean"
and another says "working directory clean". TODO: consider just checking the ok boolean.
"""
log.debug('starting check_git_status()')
log.info('::: checking git status ----------')
## check for uncommitted changes --------------------------
call_result: tuple[bool, dict] = lib_git_handler.run_git_status(project_path)
(ok, output) = call_result
Expand All @@ -141,6 +143,8 @@ def check_git_status(project_path: Path, project_email_addresses: list[list[str,
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)
else:
log.info('ok / git status is clean')
return


Expand All @@ -163,7 +167,7 @@ def determine_python_version(project_path: Path, project_email_addresses: list[l
TODO: eventually remove the unneed code.
"""
log.debug('starting determine_python_version()')
log.info('::: determining python version ----------')
## get env_python_path ------------------------------------------
env_python_path: Path = project_path.parent / 'env/bin/python3'
log.debug(f'env_python_path before resolve: ``{env_python_path}``')
Expand Down Expand Up @@ -196,6 +200,7 @@ def determine_python_version(project_path: Path, project_email_addresses: list[l
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)
log.info(f'ok / python_version, ``{python_version}``')
return (python_version, tilde_notation, env_python_path_resolved)

## end def determine_python_version()
Expand All @@ -206,6 +211,7 @@ def determine_environment_type(project_path: Path, project_email_addresses: list
Infers environment type based on the system hostname.
Returns 'local', 'staging', or 'production'.
"""
log.info('::: determining environment type ----------')
## ensure all .in files exist -----------------------------------
for filename in ['local.in', 'staging.in', 'production.in']:
full_path: Path = project_path / 'requirements' / filename
Expand All @@ -228,7 +234,7 @@ def determine_environment_type(project_path: Path, project_email_addresses: list
env_type: str = 'production'
else:
env_type: str = 'local'
log.debug(f'env_type: {env_type}')
log.info(f'ok / env_type, ``{env_type}``')
return env_type


Expand All @@ -238,15 +244,15 @@ def determine_uv_path() -> Path:
If that fails, gets path from this script's venv.
Used for compile and sync.
"""
log.debug('starting determine_uv_path()')
log.info('::: determining uv path ----------')
try:
uv_initial_path: str = subprocess.check_output(['which', 'uv'], text=True).strip()
uv_path = Path(uv_initial_path).resolve() # to ensure an absolute-path
except subprocess.CalledProcessError:
log.debug("`which` unsuccessful; accessing this script's venv")
initial_uv_path: Path = Path(__file__).parent.parent / 'env' / 'bin' / 'uv'
uv_path = initial_uv_path.resolve()
log.debug(f'determined uv_path: ``{uv_path}``')
log.info(f'ok / uv_path, ``{uv_path}``')
return uv_path


Expand All @@ -259,12 +265,12 @@ def determine_group(project_path: Path, project_email_addresses: list[list[str,
- Sends an email to the project sys-admins
- Exits the script
"""
log.debug('starting infer_group()')
log.info('::: determining group ----------')
try:
group_list: list[str] = subprocess.check_output(['ls', '-l', str(project_path)], text=True).splitlines()
groups = [line.split()[3] for line in group_list if len(line.split()) > 3]
most_common_group: str = max(set(groups), key=groups.count)
log.debug(f'most_common_group: {most_common_group}')
log.info(f'ok / most_common_group, ``{most_common_group}``')
return most_common_group
except Exception as e:
message = f'Error inferring group: {e}'
Expand Down
8 changes: 7 additions & 1 deletion lib_git_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ def run_git_pull(project_path: Path) -> tuple[bool, dict]:
Note to self: subprocess.run's `cwd` param changes the current-working-directory before the command is run,
and leaves it there.
"""
log.info('::: running git pull ----------')
command = ['git', 'pull']
result: subprocess.CompletedProcess = subprocess.run(command, cwd=str(project_path), capture_output=True, text=True)
log.debug(f'result: {result}')
ok = True if result.returncode == 0 else False
if ok is True:
log.info('ok / git pull successful')
output = {'stdout': f'{result.stdout}', 'stderr': f'{result.stderr}'}
return_val = (ok, output)
log.debug(f'return_val: {return_val}')
Expand All @@ -40,11 +43,14 @@ def run_git_add(requirements_path: Path, project_path: Path) -> tuple[bool, dict
"""
Runs `git add` and return the output.
"""
log.info('::: running git add ----------')
command = ['git', 'add', str(requirements_path)]
result: subprocess.CompletedProcess = subprocess.run(command, cwd=str(project_path), capture_output=True, text=True)
log.debug(f'result: {result}')
ok = True if result.returncode == 0 else False
if ok is True:
log.info('ok / git add successful')
output = {'stdout': f'{result.stdout}', 'stderr': f'{result.stderr}'}
return_val = (ok, output)
log.debug(f'return_val: {return_val}')
return return_val
return return_val
Loading

0 comments on commit ba7f036

Please sign in to comment.