Skip to content

Commit

Permalink
Merge pull request #1 from kiviktnm/improve-file-install
Browse files Browse the repository at this point in the history
Better printing for file install
  • Loading branch information
kiviktnm committed May 24, 2024
2 parents 61a0666 + e93eed4 commit 4d940ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
12 changes: 7 additions & 5 deletions src/decman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ def __init__(
if group is not None:
self.gid = grp.getgrnam(group).gr_gid

def copy_to(
self,
target_directory: str,
variables: typing.Optional[dict[str, str]] = None) -> list[str]:
def copy_to(self,
target_directory: str,
variables: typing.Optional[dict[str, str]] = None,
only_print: bool = False) -> list[str]:
"""
Copies the files in this directory to the target directory.
Expand All @@ -242,7 +242,9 @@ def copy_to(
target = os.path.normpath(
os.path.join(target_directory, src_path))
created.append(target)
file.copy_to(target, variables)

if not only_print:
file.copy_to(target, variables)
finally:
os.chdir(original_wd)
return created
Expand Down
24 changes: 9 additions & 15 deletions src/decman/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ def main():
help="python file containing configuration")
parser.add_argument(
"--print",
"--dry-run",
action="store_true",
default=False,
help=
"print what would happen as a result of running decman (doesn't print removed files)"
)
help="print what would happen as a result of running decman")
parser.add_argument("--debug",
action="store_true",
default=False,
Expand Down Expand Up @@ -263,23 +262,18 @@ def _install_pkgs(self):

def _create_and_remove_files(self):
l.print_summary("Installing files.")
l.print_list("Files to install:",
self.source.all_file_targets(),
elements_per_line=1,
level=l.INFO)
l.print_list("Directories to install:",
self.source.all_directory_targets(),
elements_per_line=1,
level=l.INFO)

if self.only_print:
return

all_created = self.source.create_all_files()
all_created = self.source.create_all_files(self.only_print)
to_remove = self.source.files_to_remove(self.store, all_created)

l.print_list("Ensured files are up to date:",
all_created,
elements_per_line=1)
l.print_list("Removing files:", to_remove, elements_per_line=1)

if self.only_print:
return

for file in to_remove:
try:
os.remove(file)
Expand Down
11 changes: 8 additions & 3 deletions src/decman/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def run_after_version_change(self, store: Store):
elif module.enabled and module.name not in store.enabled_modules:
module.after_version_change()

def create_all_files(self) -> list[str]:
def create_all_files(self, only_print: bool) -> list[str]:
"""
Creates all files and returns them. The files created are based on the specified files,
directories and modules.
Expand All @@ -470,9 +470,13 @@ def install_files(files: dict[str, decman.File],
variables: typing.Optional[dict[str, str]] = None):
for target, file in files.items():
created_files.append(target)

if only_print:
continue

try:
file.copy_to(target, variables)
print_debug(f"Installing file to {target}.")
file.copy_to(target, variables)
except OSError as e:
print_error(f"{e}")
raise err.UserFacingError(
Expand All @@ -483,7 +487,8 @@ def install_dirs(dirs: dict[str, decman.Directory],
for target, directory in dirs.items():
try:
print_debug(f"Installing directory to {target}.")
created_files.extend(directory.copy_to(target, variables))
created_files.extend(
directory.copy_to(target, variables, only_print))
except OSError as e:
print_error(f"{e}")
raise err.UserFacingError(
Expand Down

0 comments on commit 4d940ff

Please sign in to comment.