From 1a31fcc29d4e66827b665a35271ca2775fa33c37 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Fri, 6 Jan 2023 22:04:30 -0600 Subject: [PATCH] i dunno let's try some new shit fuck it we're doing it in python --- .gitignore | 1 + include.json | 15 ++++ install | 7 ++ install.sh | 10 ++- installer.py | 156 ++++++++++++++++++++++++++++++++++++ {windows => wsl}/install.sh | 1 - 6 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 include.json create mode 100755 install create mode 100644 installer.py rename {windows => wsl}/install.sh (99%) diff --git a/.gitignore b/.gitignore index ec76ec2..9dc9bdc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ backup +__pycache__ diff --git a/include.json b/include.json new file mode 100644 index 0000000..e68c258 --- /dev/null +++ b/include.json @@ -0,0 +1,15 @@ +{ + "home_files": [ + ".bash_profile", + ".bashrc", + ".screenrc", + ".tmux.conf", + ".vimrc", + ".config/htop/htoprc", + ".config/lazydocker/config.yml" + ], + "home_dirs": [ + ".vim/pack", + ".vim/ftplugin" + ] +} diff --git a/install b/install new file mode 100755 index 0000000..c0f766c --- /dev/null +++ b/install @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 +""" +installs preferences +""" +from installer import Installer +installer = Installer.from_cli_args() +installer.run() diff --git a/install.sh b/install.sh index a0ce0ce..7c825ef 100755 --- a/install.sh +++ b/install.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash home="$HOME" -base_dir="$home/.dotfiles" -backup_dir="$home/.dotfiles/backup" +base_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +backup_dir="$base_dir/backup" vim_plugins_dir="$home/.vim/bundle" vundle_dir="$vim_plugins_dir/Vundle.vim" @@ -63,7 +63,7 @@ for filename in ${include[@]}; do fi # symlink in the versioned dotfiles. - ln -sv "$source_path" "$dest_path" + ln -fsv "$source_path" "$dest_path" echo "--------------------------------------------------------------------------------" done @@ -79,3 +79,7 @@ fi echo "installing Vim plugins" vim +PluginInstall +qall + +if uname -a | grep -q WSL; then + "$base_dir/wsl/install.sh" -u JordanOrelli +fi diff --git a/installer.py b/installer.py new file mode 100644 index 0000000..3bf709c --- /dev/null +++ b/installer.py @@ -0,0 +1,156 @@ +""" +just exists to define our installer +""" + +import argparse +import json +import logging +import os +import pathlib +import platform +import subprocess +import shutil + +from functools import cached_property + +class Installer: + """ + manages the installation of preferences files + """ + def __init__(self, quiet=False, verbose=False): + log_level = logging.INFO + if verbose: + log_level = logging.DEBUG + if quiet: + log_level = logging.ERROR + logging.basicConfig(level=log_level, format='') + self.log = logging.getLogger('install') + self.config_path = "include.json" + + @classmethod + def from_cli_args(cls): + """ + builds an installer from CLI arguments + """ + args = cls.parse_cli() + installer = cls(quiet=args.quiet, verbose=args.verbose) + return installer + + @classmethod + def parse_cli(cls): + """ + parses our cli arguments + """ + parser = argparse.ArgumentParser( + prog = 'install', + description = 'installs my preferences', + epilog = 'this should work on a bunch of different environments') + parser.add_argument('-v', '--verbose', action='store_true') + parser.add_argument('-q', '--quiet', action='store_true') + return parser.parse_args() + + def run(self): + """ + runs the install process + """ + if home_files := self.config.get('home_files'): + for name in home_files: + self.install_home_file(name) + + def install_home_file(self, name): + """ + installs a given file + """ + self.log.debug("install: %s", name) + + source_path = self.prefs_dir / name + self.log.debug(" source: %s", source_path) + if not source_path.exists(): + self.log.warning("home file source path %s does not exist", source_path) + return + + if not source_path.is_file(): + self.log.warning("home file source path %s is not a file", source_path) + return + + if self.is_linux: + target_path = pathlib.Path.home() / name + self.log.debug(" target: %s", target_path) + if target_path.exists(): + self.log.debug(" target path exists") + if target_path.is_symlink(): + self.log.debug(" is symlink") + if target_path.resolve() == source_path: + self.log.debug(" up to date") + else: + self.log.info("%s -> %s", target_path, source_path) + target_path.symlink_to(source_path) + else: + self.log.info("%s -> %s", target_path, source_path) + target_path.symlink_to(source_path) + else: + self.log.info("%s -> %s", target_path, source_path) + target_path.symlink_to(source_path) + + if self.is_wsl: + target_path = self.windows_home_dir / pathlib.PureWindowsPath(name) + self.log.debug(" target: %s", target_path) + if target_path.exists(): + self.log.debug(" target path exists, will remove") + target_path.unlink() + shutil.copy(source_path, target_path) + + @cached_property + def config(self): + """ + loads the json configuration + """ + with open(self.config_path, 'r', encoding='utf-8') as config_fp: + self.log.debug("loading config from path %s", self.config_path) + return json.load(config_fp) + + @property + def prefs_dir(self): + """ + directory containing our preferences repo + """ + return pathlib.Path(os.path.dirname(os.path.realpath(__file__))) + + @classmethod + @cached_property + def is_linux(cls): + """ + true if we're on linux (including WSL), false otherwise + """ + return platform.system() == 'Linux' + + @classmethod + @cached_property + def is_wsl(cls): + """ + true if we're running Linux on WSL + """ + return 'WSL2' in platform.platform() + + @cached_property + def windows_home_dir(self): + """ + Finds the home directory of the user's Windows home directory and + returns it as a Posix path representing its mount point from the + perspective of WSL. + """ + if not self.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()) + + def setup_file(self, fname): + """ + sets up an individual file + """ + source = self.prefs_dir / fname + print(source) diff --git a/windows/install.sh b/wsl/install.sh similarity index 99% rename from windows/install.sh rename to wsl/install.sh index 31341d6..08f7805 100755 --- a/windows/install.sh +++ b/wsl/install.sh @@ -19,7 +19,6 @@ do done function main() { - if ! [[ $username ]]; then echo "Username is required" 2>&1 usage