Skip to content

Commit

Permalink
refactors call to filter_initial_comments().
Browse files Browse the repository at this point in the history
  • Loading branch information
birkin committed Dec 19, 2024
1 parent 7e35020 commit 3579412
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions self_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def compile_requirements(project_path: Path, python_version: str, environment_ty
raise Exception(message)
return compiled_filepath

## end def compile_requirements()


def remove_old_backups(project_path: Path, keep_recent: int = 30) -> None:
"""
Expand Down Expand Up @@ -168,20 +170,7 @@ def compare_with_previous_backup(project_path: Path, backup_file: Path) -> bool:
log.debug(f'backup_dir: ``{backup_dir}``')
previous_files: list[Path] = sorted([f for f in backup_dir.iterdir() if f.suffix == '.txt' and f != backup_file])

def filter_initial_comments(lines: list[str]) -> list[str]:
"""
Filters out initial lines starting with '#' from a list of lines.
The reason for this is that:
- one of the first line of the backup file includes a timestamp, which would always be different.
- if a generated `.txt` file is used to update the venv, the string `# ACTIVE`
is added to the top of the file, which would always be different from a fresh compile.
"""
log.debug('starting filter_initial_comments()')
non_comment_index = next((i for i, line in enumerate(lines) if not line.startswith('#')), len(lines))
return lines[non_comment_index:]

if previous_files:
log.debug('hereC')
previous_file_path: Path = previous_files[-1]
with previous_file_path.open() as prev, backup_file.open() as curr:
prev_lines = prev.readlines()
Expand Down Expand Up @@ -238,6 +227,8 @@ def sync_dependencies(project_path: Path, backup_file: Path, uv_path: Path) -> N
log.exception(message)
raise Exception(message)

## end def sync_dependencies()


def update_permissions_and_mark_active(project_path: Path, backup_file: Path) -> None:
"""
Expand Down Expand Up @@ -270,6 +261,20 @@ def update_permissions_and_mark_active(project_path: Path, backup_file: Path) ->
## ------------------------------------------------------------------


def filter_initial_comments(lines: list[str]) -> list[str]:
"""
Filters out initial lines starting with '#' from a list of lines.
The reason for this is that:
- one of the first line of the backup file includes a timestamp, which would always be different.
- if a generated `.txt` file is used to update the venv, the string `# ACTIVE`
is added to the top of the file, which would always be different from a fresh compile.
Called by `compare_with_previous_backup()`.
"""
log.debug('starting filter_initial_comments()')
non_comment_index = next((i for i, line in enumerate(lines) if not line.startswith('#')), len(lines))
return lines[non_comment_index:]


def infer_group(project_path: Path) -> str:
"""
Infers the group by examining existing files.
Expand Down Expand Up @@ -327,6 +332,8 @@ def manage_update(project_path: str) -> None:
update_permissions_and_mark_active(project_path, compiled_requirements)
return

## end def manage_update()


if __name__ == '__main__':
log.debug('starting dundermain')
Expand Down

0 comments on commit 3579412

Please sign in to comment.