You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.1 KiB
Python
106 lines
3.1 KiB
Python
2 years ago
|
import pathlib
|
||
2 years ago
|
import sys
|
||
2 years ago
|
import json
|
||
2 years ago
|
from functools import cached_property
|
||
|
|
||
2 years ago
|
from . import host
|
||
|
from . import sections
|
||
|
from . import targets
|
||
|
from .options import Options
|
||
2 years ago
|
|
||
2 years ago
|
class Installer:
|
||
|
"""
|
||
|
manages the installation of preferences files
|
||
|
"""
|
||
2 years ago
|
def __init__(self):
|
||
|
self.options = Options.from_cli_args()
|
||
2 years ago
|
|
||
|
def run(self):
|
||
|
"""
|
||
|
runs the install process
|
||
|
"""
|
||
2 years ago
|
if host.is_windows and not host.is_admin:
|
||
|
print("You are not admin: admin is required on Windows")
|
||
2 years ago
|
sys.exit(1)
|
||
2 years ago
|
|
||
2 years ago
|
config = self.options.config
|
||
|
for section_name in config.sections():
|
||
|
try:
|
||
|
T = self.parse_section_name(section_name)
|
||
|
except ValueError as e:
|
||
|
print(f"error reading section: {e}")
|
||
|
continue
|
||
|
section = T.from_section(section_name, **config[section_name])
|
||
|
print(f"Section: {section}")
|
||
|
section.run()
|
||
|
|
||
2 years ago
|
print("linking in home files")
|
||
|
home = self.options.config['home']
|
||
|
home_files = filter(None, home['files'].splitlines())
|
||
|
for fname in home_files:
|
||
|
print(f"\n{fname}")
|
||
|
path = pathlib.Path(fname)
|
||
|
self.map_file(path, path)
|
||
|
|
||
|
if host.is_linux:
|
||
|
self.map_section('map.posix')
|
||
|
self.map_section('map.windows')
|
||
2 years ago
|
|
||
2 years ago
|
if host.is_windows:
|
||
|
self.map_section('map.windows')
|
||
|
|
||
|
@cached_property
|
||
|
def targets(self):
|
||
2 years ago
|
"""
|
||
2 years ago
|
defines all of the places where preferences files will be installed
|
||
2 years ago
|
"""
|
||
2 years ago
|
if host.is_linux:
|
||
2 years ago
|
return [targets.Linux()]
|
||
2 years ago
|
|
||
|
if host.is_windows:
|
||
|
return [targets.Windows()]
|
||
|
|
||
|
return []
|
||
|
|
||
|
def map_file(self, source_path, target_path):
|
||
|
if not source_path.is_absolute():
|
||
|
source_path = host.dotfiles_root / source_path
|
||
2 years ago
|
|
||
2 years ago
|
print(f"source path: {source_path}")
|
||
|
print(f"source drive: {source_path.drive}")
|
||
2 years ago
|
if not source_path.exists():
|
||
2 years ago
|
print("skip: no such file\n")
|
||
2 years ago
|
return
|
||
|
|
||
2 years ago
|
for target in self.targets:
|
||
|
target.map_file(source_path, target_path)
|
||
2 years ago
|
|
||
2 years ago
|
@property
|
||
|
def config_path(self):
|
||
|
# pylint: disable=missing-function-docstring
|
||
|
return self.options.config
|
||
2 years ago
|
|
||
|
@cached_property
|
||
|
def config(self):
|
||
|
"""
|
||
2 years ago
|
the contents of our configuration file
|
||
2 years ago
|
"""
|
||
|
with open(self.config_path, 'r', encoding='utf-8') as config_fp:
|
||
2 years ago
|
log.debug("loading config from path %s", self.config_path)
|
||
2 years ago
|
return json.load(config_fp)
|
||
|
|
||
2 years ago
|
def map_section(self, section_name):
|
||
|
section = self.options.config[section_name]
|
||
|
for source_name in section:
|
||
|
target_name = section[source_name]
|
||
|
source_path = pathlib.Path(source_name)
|
||
|
target_path = pathlib.Path(target_name)
|
||
|
print(f"Map {source_path} to {target_path}")
|
||
|
self.map_file(source_path, target_path)
|
||
2 years ago
|
|
||
|
def parse_section_name(self, section_name):
|
||
|
if section_name == "home":
|
||
|
return sections.Home
|
||
|
raise ValueError(f"unprocessable section name: {section_name}")
|
||
|
|