diff --git a/install.py b/install.py index a1a9c43..26935ce 100755 --- a/install.py +++ b/install.py @@ -46,6 +46,14 @@ class Installer: 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: """Put the ~/ back in the path.""" @@ -90,9 +98,7 @@ class Installer: ui.info_2("Copy", p_src, "->", self.pretty_path(p_src)) p_src.copy(p_dest) - def do_download( - self, *, url: str, dest: str, executable: bool = False - ) -> None: + def do_download(self, *, url: str, dest: str, executable: bool = False) -> None: """Retrieve a file from a url.""" p_dest = Path(dest).expanduser() @@ -108,9 +114,7 @@ class Installer: if executable: p_dest.chmod(0o755) - def do_install( - self, *packages: str, **os_specific_packages: List[str] - ) -> None: + def do_install(self, *packages: str, **os_specific_packages: List[str]) -> None: """Install packages with OS-specific package manager. 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) - def do_symlink_dir( - self, src: str, dest: str, condition: str = "true" - ) -> None: + def do_symlink_dir(self, src: str, dest: str, condition: str = "true") -> None: """Make a symlink to a dir.""" self._do_symlink(src, dest, condition=condition, is_dir=True) - def _do_symlink( - self, src: str, dest: str, *, condition: str, is_dir: bool - ) -> None: + def _do_symlink(self, src: str, dest: str, *, condition: str, is_dir: bool) -> None: p_src = Path(src).expanduser() pretty_src = self.pretty_path(p_src) @@ -334,6 +334,24 @@ def define_os() -> str: 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: """Parse args and instantiate the Installer.""" @@ -341,20 +359,30 @@ def main() -> None: parser.add_argument( "programs", nargs="*", + help="Programs to process, can be none", ) 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", help="Overwrite existing files", ) parser.add_argument( - "-l", - "--list_programs", + "-g", "--generate", 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( - "--update", + "-l", "--list_programs", + action="store_true", + help="List all programs from the configuration file", + ) + parser.add_argument( + "-u", "--update", action="store_true", help="Update git repos", ) @@ -362,19 +390,31 @@ def main() -> None: args = parser.parse_args() programs = args.programs + from_file = args.from_file force = args.force + generate = args.generate list_programs = args.list_programs update = args.update installer = Installer(force=force, update=update) + if generate: + installer.generate_process_list() + return + if list_programs: ui.info("The following items were found in the config file:") for program in installer.conf.keys(): ui.info_1(program) + return - else: - installer.process(programs=programs) + if from_file: + assert not programs, \ + "Can't use 'process_list.txt' if you also specify programs!" + + programs = read_programs_from_process_list() + + installer.process(programs=programs) if __name__ == "__main__":