cleaning up

master
Jordan Orelli 2 months ago
parent b2f4e57538
commit cdbfe35a6f

@ -2,15 +2,16 @@ import subprocess
import shutil import shutil
import re import re
class CargoInstall: class CargoInstall:
resource_name = 'cargo-install' resource_name = "cargo-install"
def __init__(self, label, section): def __init__(self, label, section):
self.label = label or None self.label = label or None
self.parse_crates(section.get('crates', '')) self.parse_crates(section.get("crates", ""))
def run(self): 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") print(f"SKIP [cargo-install {self.label}]: cargo not found")
return return
@ -19,7 +20,8 @@ class CargoInstall:
cmd = ["cargo", "install"] cmd = ["cargo", "install"]
cmd.extend(list(missing)) cmd.extend(list(missing))
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: 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): def parse_crates(self, text):
self.crates = set() self.crates = set()
@ -39,7 +41,7 @@ class CargoInstall:
# line is empty # line is empty
continue continue
line = line.decode("utf-8") line = line.decode("utf-8")
if re.match(r'^\s', line): if re.match(r"^\s", line):
# line starts with whitespace # line starts with whitespace
continue continue
try: try:

@ -1,14 +1,15 @@
import subprocess import subprocess
from pathlib import Path from pathlib import Path
from . import log from .log import log
class Repo: class Repo:
resource_name = 'git' resource_name = "git"
def __init__(self, label, section): def __init__(self, label, section):
self.label = label or None self.label = label or None
self.repo = section['repo'] self.repo = section["repo"]
self.target = Path(section['target']).expanduser() self.target = Path(section["target"]).expanduser()
def run(self): def run(self):
if self.target.exists(): if self.target.exists():

@ -1,12 +1,13 @@
from . import host from .host import host
from .linker import Linker from .linker import Linker
class Home: class Home:
resource_name = 'home' resource_name = "home"
def __init__(self, label, section): def __init__(self, label, section):
self.label = label or None self.label = label or None
self.parse_files(section.get('files', '')) self.parse_files(section.get("files", ""))
def parse_files(self, text): def parse_files(self, text):
self.files = [] self.files = []
@ -21,8 +22,8 @@ class Home:
def __repr__(self): def __repr__(self):
if self.label: if self.label:
return f'<Home: {self.label}>' return f"<Home: {self.label}>"
return '<Home>' return "<Home>"
def run(self): def run(self):
linker = Linker(host.dotfiles_root, host.home) linker = Linker(host.dotfiles_root, host.home)

@ -6,7 +6,6 @@ running. On WSL, that means we're on Linux
import ctypes import ctypes
import os import os
import platform import platform
import sys
import pathlib import pathlib
import shutil import shutil
from functools import cached_property from functools import cached_property
@ -54,7 +53,7 @@ class Host:
try: try:
return os.getuid() == 0 return os.getuid() == 0
except AttributeError: except AttributeError:
return ctypes.windll.shell32.IsUserAnAdmin() != 0 return ctypes.windll.shell32.IsUserAnAdmin() != 0 # type: ignore
@property @property
def dotfiles_root(self): def dotfiles_root(self):
@ -83,4 +82,4 @@ class Host:
return shutil.which(cmd) is not None return shutil.which(cmd) is not None
sys.modules[__name__] = Host() host = Host()

@ -35,7 +35,7 @@ class Installer:
def when(self, section: SectionProxy) -> bool: def when(self, section: SectionProxy) -> bool:
if clause := section.get("when", None): if clause := section.get("when", None):
from . import host from .host import host
given_locals = { given_locals = {
"host": host, "host": host,

@ -1,8 +1,7 @@
import pathlib import pathlib
import shutil import shutil
import os
from . import host from .host import host
class Linker: class Linker:

@ -3,11 +3,11 @@ a logging object
""" """
import logging import logging
import sys
class Log: class Log:
def __init__(self): def __init__(self):
logging.basicConfig(level=logging.INFO, format='') logging.basicConfig(level=logging.INFO, format="")
self._target = logging.getLogger() self._target = logging.getLogger()
def __getattr__(self, name): def __getattr__(self, name):
@ -16,4 +16,5 @@ class Log:
def __dir__(self): def __dir__(self):
return dir(self._target) return dir(self._target)
sys.modules[__name__] = Log()
log = Log()

@ -6,13 +6,15 @@ import argparse
import pathlib import pathlib
from functools import cached_property from functools import cached_property
from . import log from .log import log
from . import host from .host import host
class Options: class Options:
""" """
turns the cli arguments into an object with nice fields and the like turns the cli arguments into an object with nice fields and the like
""" """
@classmethod @classmethod
def from_cli_args(cls): def from_cli_args(cls):
""" """
@ -33,14 +35,15 @@ class Options:
@classmethod @classmethod
def create_cli_parser(cls): def create_cli_parser(cls):
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog = 'install', prog="install",
description = """ description="""
installs preferences files. installs preferences files.
""") """,
parser.add_argument('-v', '--verbose', action='store_true') )
parser.add_argument('-q', '--quiet', action='store_true') parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument('-c', '--config', help="path to config file") parser.add_argument("-q", "--quiet", action="store_true")
parser.add_argument('-x', action='store_true') parser.add_argument("-c", "--config", help="path to config file")
parser.add_argument("-x", action="store_true")
return parser return parser
@property @property

@ -1,5 +1,5 @@
import subprocess import subprocess
from . import host from .host import host
class Run: class Run:

@ -2,21 +2,25 @@
sections defines our various config sections sections defines our various config sections
""" """
import pathlib from configparser import SectionProxy
from typing import Sequence
class Home: class Home:
""" """
the [home] section maps files within the home directory the [home] section maps files within the home directory
""" """
def __init__(self, files: Sequence[str]):
self.files = files
@classmethod @classmethod
def from_section(Cls, name, **section): def from_section(cls, name, section: SectionProxy):
v = Cls() if raw_files := section.get("files"):
if raw_files := section.get('files'): files = list(filter(None, raw_files.splitlines()))
v.files = filter(None, raw_files.splitlines())
else: else:
v.files = None files = []
return v return cls(files)
def run(self): def run(self):
pass pass

@ -8,12 +8,14 @@ import shutil
import os import os
from functools import cached_property from functools import cached_property
from . import host from .host import host
class Target: class Target:
""" """
base class of all target platforms base class of all target platforms
""" """
def target_path(self, relpath): def target_path(self, relpath):
""" """
computes the path of a file in the home directory computes the path of a file in the home directory
@ -79,32 +81,36 @@ class Target:
target_path.symlink_to(source_path) target_path.symlink_to(source_path)
print("") print("")
class Linux(Target): class Linux(Target):
""" """
defines a local Linux target: the local machine when the script is run on defines a local Linux target: the local machine when the script is run on
Linux Linux
""" """
class Windows(Target): class Windows(Target):
""" """
defines a local Windows target: the local machine when the script is run on defines a local Windows target: the local machine when the script is run on
Windows Windows
""" """
class WSLHost(Target): class WSLHost(Target):
""" """
defines the Windows machine on which the WSL instance is hosted defines the Windows machine on which the WSL instance is hosted
""" """
@cached_property @cached_property
def target_root(self): def target_root(self):
if not host.is_wsl: if not host.is_wsl:
raise Exception("cannot get windows home dir from anything other than wsl") raise Exception("cannot get windows home dir from anything other than wsl")
res = subprocess.run(['wslvar', 'USERPROFILE'], check=False, res = subprocess.run(
capture_output=True) ["wslvar", "USERPROFILE"], check=False, capture_output=True
winpath = res.stdout.decode('utf-8').strip() )
res = subprocess.run(['wslpath', winpath], check=False, winpath = res.stdout.decode("utf-8").strip()
capture_output=True) res = subprocess.run(["wslpath", winpath], check=False, capture_output=True)
return pathlib.Path(res.stdout.decode('utf-8').strip()) return pathlib.Path(res.stdout.decode("utf-8").strip())
def map_file(self, source_path, target_path): def map_file(self, source_path, target_path):
""" """

Loading…
Cancel
Save