install: Skip templates and raw files that aren't out of date in the destination.
This commit is contained in:
parent
4efb107396
commit
743b4312f7
1 changed files with 37 additions and 16 deletions
23
install.py
23
install.py
|
@ -7,6 +7,7 @@ import socket
|
|||
import sys
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
import mako.lookup
|
||||
import mako.template
|
||||
|
@ -33,6 +34,17 @@ def get_base16(scheme, app, template='default'):
|
|||
return requests.get(base_url + output + '/base16-' + scheme + extension).text
|
||||
|
||||
|
||||
def is_outdated(src: List[Path], dst: Path) -> bool:
|
||||
if not dst.exists():
|
||||
return True
|
||||
|
||||
dst_modified = dst.stat().st_mtime
|
||||
return any(
|
||||
a_src.stat().st_mtime > dst_modified
|
||||
for a_src in src
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Generates and installs dotfiles for this host.',
|
||||
|
@ -56,6 +68,11 @@ def main():
|
|||
type=Path,
|
||||
default=os.environ.get('HOME') or Path.home(),
|
||||
)
|
||||
parser.add_argument(
|
||||
'-f', '--force',
|
||||
help='Force overwrite all files even if they are not considered outdated.',
|
||||
action='store_true'
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
raw_dir = args.dotfiles / 'raw'
|
||||
|
@ -82,6 +99,8 @@ def main():
|
|||
continue
|
||||
rel_path = raw_path.relative_to(raw_dir)
|
||||
output_path = args.home / rel_path
|
||||
|
||||
if args.force or is_outdated([raw_path], output_path):
|
||||
print(rel_path)
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy(raw_path, output_path)
|
||||
|
@ -90,6 +109,9 @@ def main():
|
|||
if not template_path.is_file():
|
||||
continue
|
||||
rel_path = template_path.relative_to(templates_dir)
|
||||
output_path = args.home / template_path.relative_to(templates_dir)
|
||||
|
||||
if args.force or is_outdated([template_path, host_filename], output_path):
|
||||
print(rel_path)
|
||||
template = mako.template.Template(
|
||||
filename=str(template_path),
|
||||
|
@ -100,7 +122,6 @@ def main():
|
|||
host=host_config,
|
||||
get_base16=partial(get_base16, host_config.get('base16-scheme', 'default-dark')),
|
||||
)
|
||||
output_path = args.home / template_path.relative_to(templates_dir)
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(output_path, 'w+') as output_file:
|
||||
output_file.write(output)
|
||||
|
|
Loading…
Add table
Reference in a new issue