-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·65 lines (49 loc) · 1.73 KB
/
install.py
File metadata and controls
executable file
·65 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/env python3
import argparse
import json
import os
from pathlib import Path
BACKUP_DIR = Path.home() / "dotfiles_old"
def move_to_backup(target: Path) -> None:
print(f"Moving {target} ==> {BACKUP_DIR/target.name}")
if not args.dry_run:
target.rename(BACKUP_DIR / target.name)
def make_symlink(src: Path, dst: Path) -> None:
print(f"linking {src} ==> {dst}")
if not args.dry_run:
dst.parent.mkdir(parents=True, exist_ok=True)
dst.symlink_to(src)
def main(locations: dict[str, str], args):
BACKUP_DIR.mkdir(exist_ok=True)
location_paths: dict[Path, Path] = {
Path(__file__).with_name(src): Path(dst.format(**os.environ))
for src, dst in locations.items()
}
print("\nCreating backups of existing files...")
for src, dst in location_paths.items():
if dst.exists():
move_to_backup(dst)
print("\nInstalling files...")
for src, dst in location_paths.items():
make_symlink(src, dst)
if not any(BACKUP_DIR.iterdir()):
BACKUP_DIR.rmdir()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="install.py",
description="Installs dotfiles into the correct locations by symlink",
)
parser.add_argument("-d", "--dry-run", action="store_true")
parser.add_argument(
"locations_file",
type=Path,
nargs="?",
default=Path(__file__).with_name("locations.json"),
help="a json file describing which files to symlink, and where to place them.",
)
args = parser.parse_args()
if args.dry_run:
print("starting dry run. Not moving any files...")
with args.locations_file.open() as f:
locations = json.load(f)
main(locations, args)