cleaning up

master
Jordan Orelli 4 months ago
parent b2f4e57538
commit cdbfe35a6f

@ -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,6 +20,7 @@ class CargoInstall:
cmd = ["cargo", "install"]
cmd.extend(list(missing))
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
if proc.stdout:
print(proc.stdout.read())
def parse_crates(self, text):
@ -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:

@ -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():

@ -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'<Home: {self.label}>'
return '<Home>'
return f"<Home: {self.label}>"
return "<Home>"
def run(self):
linker = Linker(host.dotfiles_root, host.home)

@ -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()

@ -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,

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

@ -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()

@ -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

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

@ -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

@ -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):
"""

Loading…
Cancel
Save