Add process_file management

This commit is contained in:
flyingscorpio@arch-desktop 2020-05-06 10:34:19 +02:00
parent 2b4aa10ef4
commit bb363abee5

View file

@ -46,6 +46,14 @@ class Installer:
return command.returncode == 0 return command.returncode == 0
def generate_process_list(self) -> None:
"""Use the config file to generate a file with the list of programs."""
ui.info("Generating 'process_list.txt'")
with open("process_list.txt", "w") as process_file:
process_file.write("\n".join(self.conf.keys()) + "\n")
def pretty_path(self, path: Path) -> str: def pretty_path(self, path: Path) -> str:
"""Put the ~/ back in the path.""" """Put the ~/ back in the path."""
@ -90,9 +98,7 @@ class Installer:
ui.info_2("Copy", p_src, "->", self.pretty_path(p_src)) ui.info_2("Copy", p_src, "->", self.pretty_path(p_src))
p_src.copy(p_dest) p_src.copy(p_dest)
def do_download( def do_download(self, *, url: str, dest: str, executable: bool = False) -> None:
self, *, url: str, dest: str, executable: bool = False
) -> None:
"""Retrieve a file from a url.""" """Retrieve a file from a url."""
p_dest = Path(dest).expanduser() p_dest = Path(dest).expanduser()
@ -108,9 +114,7 @@ class Installer:
if executable: if executable:
p_dest.chmod(0o755) p_dest.chmod(0o755)
def do_install( def do_install(self, *packages: str, **os_specific_packages: List[str]) -> None:
self, *packages: str, **os_specific_packages: List[str]
) -> None:
"""Install packages with OS-specific package manager. """Install packages with OS-specific package manager.
Packages can either be in a tuple for non OS-specific packages, or in a Packages can either be in a tuple for non OS-specific packages, or in a
@ -189,16 +193,12 @@ class Installer:
self._do_symlink(src, dest, condition=condition, is_dir=False) self._do_symlink(src, dest, condition=condition, is_dir=False)
def do_symlink_dir( def do_symlink_dir(self, src: str, dest: str, condition: str = "true") -> None:
self, src: str, dest: str, condition: str = "true"
) -> None:
"""Make a symlink to a dir.""" """Make a symlink to a dir."""
self._do_symlink(src, dest, condition=condition, is_dir=True) self._do_symlink(src, dest, condition=condition, is_dir=True)
def _do_symlink( def _do_symlink(self, src: str, dest: str, *, condition: str, is_dir: bool) -> None:
self, src: str, dest: str, *, condition: str, is_dir: bool
) -> None:
p_src = Path(src).expanduser() p_src = Path(src).expanduser()
pretty_src = self.pretty_path(p_src) pretty_src = self.pretty_path(p_src)
@ -334,6 +334,24 @@ def define_os() -> str:
return "Unsupported OS" return "Unsupported OS"
def read_programs_from_process_list() -> List[str]:
"""Read the process_list file to return a list of programs to process."""
ui.info("Found these programs in 'process_list.txt':")
with open("process_list.txt", "r") as process_file:
content = process_file.readlines()
programs = [
line.strip() for line in content
if not line.startswith("#") and len(line) > 0
]
for program in programs:
ui.info_1(program)
return programs
def main() -> None: def main() -> None:
"""Parse args and instantiate the Installer.""" """Parse args and instantiate the Installer."""
@ -341,20 +359,30 @@ def main() -> None:
parser.add_argument( parser.add_argument(
"programs", "programs",
nargs="*", nargs="*",
help="Programs to process, can be none",
) )
parser.add_argument( parser.add_argument(
"--force", "-f", "--from_file",
action="store_true",
help="Use 'process_list.txt' as programs to process",
)
parser.add_argument(
"-F", "--force",
action="store_true", action="store_true",
help="Overwrite existing files", help="Overwrite existing files",
) )
parser.add_argument( parser.add_argument(
"-l", "-g", "--generate",
"--list_programs",
action="store_true", action="store_true",
help="List all programs from the configuration file.", help="Generate a file containing all the programs from the config file"
) )
parser.add_argument( parser.add_argument(
"--update", "-l", "--list_programs",
action="store_true",
help="List all programs from the configuration file",
)
parser.add_argument(
"-u", "--update",
action="store_true", action="store_true",
help="Update git repos", help="Update git repos",
) )
@ -362,18 +390,30 @@ def main() -> None:
args = parser.parse_args() args = parser.parse_args()
programs = args.programs programs = args.programs
from_file = args.from_file
force = args.force force = args.force
generate = args.generate
list_programs = args.list_programs list_programs = args.list_programs
update = args.update update = args.update
installer = Installer(force=force, update=update) installer = Installer(force=force, update=update)
if generate:
installer.generate_process_list()
return
if list_programs: if list_programs:
ui.info("The following items were found in the config file:") ui.info("The following items were found in the config file:")
for program in installer.conf.keys(): for program in installer.conf.keys():
ui.info_1(program) ui.info_1(program)
return
if from_file:
assert not programs, \
"Can't use 'process_list.txt' if you also specify programs!"
programs = read_programs_from_process_list()
else:
installer.process(programs=programs) installer.process(programs=programs)