diff --git a/prefs/cargo.py b/prefs/cargo.py index 903b1fe..01a6c0f 100644 --- a/prefs/cargo.py +++ b/prefs/cargo.py @@ -2,15 +2,16 @@ import subprocess import shutil import re + class CargoInstall: - resource_name = 'cargo-install' + resource_name = "cargo-install" def __init__(self, label, section): self.label = label or None - self.parse_crates(section.get('crates', '')) + self.parse_crates(section.get("crates", "")) def run(self): - if shutil.which('cargo') is None: + if shutil.which("cargo") is None: print(f"SKIP [cargo-install {self.label}]: cargo not found") return @@ -19,7 +20,8 @@ class CargoInstall: cmd = ["cargo", "install"] cmd.extend(list(missing)) with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: - print(proc.stdout.read()) + if proc.stdout: + print(proc.stdout.read()) def parse_crates(self, text): self.crates = set() @@ -39,7 +41,7 @@ class CargoInstall: # line is empty continue line = line.decode("utf-8") - if re.match(r'^\s', line): + if re.match(r"^\s", line): # line starts with whitespace continue try: diff --git a/prefs/git.py b/prefs/git.py index 8cd86c9..f0df42c 100644 --- a/prefs/git.py +++ b/prefs/git.py @@ -1,14 +1,15 @@ import subprocess from pathlib import Path -from . import log +from .log import log + class Repo: - resource_name = 'git' + resource_name = "git" def __init__(self, label, section): self.label = label or None - self.repo = section['repo'] - self.target = Path(section['target']).expanduser() + self.repo = section["repo"] + self.target = Path(section["target"]).expanduser() def run(self): if self.target.exists(): diff --git a/prefs/home.py b/prefs/home.py index bd80805..d5ab013 100644 --- a/prefs/home.py +++ b/prefs/home.py @@ -1,12 +1,13 @@ -from . import host +from .host import host from .linker import Linker + class Home: - resource_name = 'home' + resource_name = "home" def __init__(self, label, section): self.label = label or None - self.parse_files(section.get('files', '')) + self.parse_files(section.get("files", "")) def parse_files(self, text): self.files = [] @@ -21,8 +22,8 @@ class Home: def __repr__(self): if self.label: - return f'' - return '' + return f"" + return "" def run(self): linker = Linker(host.dotfiles_root, host.home) diff --git a/prefs/host.py b/prefs/host.py index 65915d0..4151c79 100644 --- a/prefs/host.py +++ b/prefs/host.py @@ -6,7 +6,6 @@ running. On WSL, that means we're on Linux import ctypes import os import platform -import sys import pathlib import shutil from functools import cached_property @@ -54,7 +53,7 @@ class Host: try: return os.getuid() == 0 except AttributeError: - return ctypes.windll.shell32.IsUserAnAdmin() != 0 + return ctypes.windll.shell32.IsUserAnAdmin() != 0 # type: ignore @property def dotfiles_root(self): @@ -83,4 +82,4 @@ class Host: return shutil.which(cmd) is not None -sys.modules[__name__] = Host() +host = Host() diff --git a/prefs/installer.py b/prefs/installer.py index 34e0876..e830e2d 100644 --- a/prefs/installer.py +++ b/prefs/installer.py @@ -35,7 +35,7 @@ class Installer: def when(self, section: SectionProxy) -> bool: if clause := section.get("when", None): - from . import host + from .host import host given_locals = { "host": host, diff --git a/prefs/linker.py b/prefs/linker.py index 4a512c7..9736d22 100644 --- a/prefs/linker.py +++ b/prefs/linker.py @@ -1,8 +1,7 @@ import pathlib import shutil -import os -from . import host +from .host import host class Linker: diff --git a/prefs/log.py b/prefs/log.py index 7db668b..7b5e85a 100644 --- a/prefs/log.py +++ b/prefs/log.py @@ -3,11 +3,11 @@ a logging object """ import logging -import sys + class Log: def __init__(self): - logging.basicConfig(level=logging.INFO, format='') + logging.basicConfig(level=logging.INFO, format="") self._target = logging.getLogger() def __getattr__(self, name): @@ -16,4 +16,5 @@ class Log: def __dir__(self): return dir(self._target) -sys.modules[__name__] = Log() + +log = Log() diff --git a/prefs/options.py b/prefs/options.py index 7ef9e70..0f03aaf 100644 --- a/prefs/options.py +++ b/prefs/options.py @@ -6,13 +6,15 @@ import argparse import pathlib from functools import cached_property -from . import log -from . import host +from .log import log +from .host import host + class Options: """ turns the cli arguments into an object with nice fields and the like """ + @classmethod def from_cli_args(cls): """ @@ -33,14 +35,15 @@ class Options: @classmethod def create_cli_parser(cls): parser = argparse.ArgumentParser( - prog = 'install', - description = """ + prog="install", + description=""" installs preferences files. - """) - parser.add_argument('-v', '--verbose', action='store_true') - parser.add_argument('-q', '--quiet', action='store_true') - parser.add_argument('-c', '--config', help="path to config file") - parser.add_argument('-x', action='store_true') + """, + ) + parser.add_argument("-v", "--verbose", action="store_true") + parser.add_argument("-q", "--quiet", action="store_true") + parser.add_argument("-c", "--config", help="path to config file") + parser.add_argument("-x", action="store_true") return parser @property diff --git a/prefs/run.py b/prefs/run.py index 55690d1..791c928 100644 --- a/prefs/run.py +++ b/prefs/run.py @@ -1,5 +1,5 @@ import subprocess -from . import host +from .host import host class Run: diff --git a/prefs/sections.py b/prefs/sections.py index 8c05642..06f2f2f 100644 --- a/prefs/sections.py +++ b/prefs/sections.py @@ -2,21 +2,25 @@ sections defines our various config sections """ -import pathlib +from configparser import SectionProxy +from typing import Sequence + class Home: """ the [home] section maps files within the home directory """ + + def __init__(self, files: Sequence[str]): + self.files = files + @classmethod - def from_section(Cls, name, **section): - v = Cls() - if raw_files := section.get('files'): - v.files = filter(None, raw_files.splitlines()) + def from_section(cls, name, section: SectionProxy): + if raw_files := section.get("files"): + files = list(filter(None, raw_files.splitlines())) else: - v.files = None - return v + files = [] + return cls(files) def run(self): pass - diff --git a/prefs/targets.py b/prefs/targets.py index 50e46e0..b892ccc 100644 --- a/prefs/targets.py +++ b/prefs/targets.py @@ -8,12 +8,14 @@ import shutil import os from functools import cached_property -from . import host +from .host import host + class Target: """ base class of all target platforms """ + def target_path(self, relpath): """ computes the path of a file in the home directory @@ -79,32 +81,36 @@ class Target: target_path.symlink_to(source_path) print("") + class Linux(Target): """ defines a local Linux target: the local machine when the script is run on Linux """ + class Windows(Target): """ defines a local Windows target: the local machine when the script is run on Windows """ + class WSLHost(Target): """ defines the Windows machine on which the WSL instance is hosted """ + @cached_property def target_root(self): if not host.is_wsl: raise Exception("cannot get windows home dir from anything other than wsl") - res = subprocess.run(['wslvar', 'USERPROFILE'], check=False, - capture_output=True) - winpath = res.stdout.decode('utf-8').strip() - res = subprocess.run(['wslpath', winpath], check=False, - capture_output=True) - return pathlib.Path(res.stdout.decode('utf-8').strip()) + res = subprocess.run( + ["wslvar", "USERPROFILE"], check=False, capture_output=True + ) + winpath = res.stdout.decode("utf-8").strip() + res = subprocess.run(["wslpath", winpath], check=False, capture_output=True) + return pathlib.Path(res.stdout.decode("utf-8").strip()) def map_file(self, source_path, target_path): """