-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot.py
executable file
·135 lines (111 loc) · 3.65 KB
/
dot.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/python3
import os
from pathlib import Path
from typing import Optional
from subprocess import run
from time import strftime
from sys import argv
import dotlib
MAP_FILE = "dotfiles.json"
APP_NAME = "dot"
@dotlib.cmd(desc="Open a new shell in the same directory as your dotfiles")
def cd():
run([os.environ['SHELL']], cwd=exe)
@dotlib.cmd(desc="Install dotfiles by creating symlinks")
def install():
os.chdir(exe)
for k in cfg:
dotlib.create_link(cfg, k)
os.chdir(cwd)
@dotlib.cmd(desc="Sync dotfiles with remote")
def sync():
date = strftime("%m-%d-%y %H:%M:%S")
print(date)
run(["git", "add", "--all"], cwd=exe)
run(["git", "commit", "-m", date], cwd=exe)
run(["git", "pull"], cwd=exe)
run(["git", "push"], cwd=exe)
@dotlib.cmd(desc="Pull any changes from remote")
def pull():
run(["git", "pull"], cwd=exe)
@dotlib.cmd(desc="Push any local changes to remote")
def push():
date = strftime("%m-%d-%y %H:%M:%S")
print(date)
run(["git", "add", "--all"], cwd=exe)
run(["git", "commit", "-m", date], cwd=exe)
run(["git", "push"], cwd=exe)
@dotlib.cmd(desc="Add a local file/dir to the dotfile repo")
def add(path: str, map_to: Optional[str]):
home = Path.home().absolute()
orig = Path(path).expanduser().absolute()
if not map_to:
dotfile = exe.joinpath(orig.name)
else:
dotfile = exe.joinpath(map_to)
print(f"Map {orig} to {dotfile}")
# Replace /home/$USER with ~
link_to = str(orig)
if link_to.startswith(str(home)):
link_to = link_to.replace(str(home), "~", 1)
if not orig.exists():
print(f"{orig}: Does not exist")
return 1
if orig.is_symlink():
print(f"{orig}: A symlink already exists at this location")
return 1
dotfile = exe.joinpath(orig.name)
is_dir = orig.is_dir()
if dotfile.exists():
print(f"{dotfile}: Already exists")
return 1
print(f"mv {orig} -> {dotfile}")
orig.rename(dotfile)
orig.symlink_to(dotfile, target_is_directory=is_dir)
cfg[dotfile.name] = link_to
print(f"map {dotfile.name} -> {link_to}")
dotlib.write_map(cfg, exe.joinpath(MAP_FILE))
@dotlib.cmd(desc="Unadd a dotfile by returning the file to its original location outside of the repo")
def unadd(name: str):
if name not in cfg:
print(f'"{name}" does not exist in repo')
return 1
local = Path(cfg[name]).expanduser().absolute()
repo = exe.joinpath(name).expanduser().absolute()
if not local.exists() or not local.is_symlink():
print(f'No symlink exists at "{local}"')
return 1
if not repo.exists():
print(f'File does not exist in repo: "{repo}"')
return 1
if local.readlink() != repo:
print(f'The repo file ({repo}) does not link to the local file ({local})')
return 1
if not dotlib.ask(f'Unadd "{name}" ({local})?'):
return 0
local.unlink()
repo.rename(local)
del cfg[name]
dotlib.write_map(cfg, exe.joinpath(MAP_FILE))
@dotlib.cmd(desc="Show unsynced changes")
def diff():
run(["git", "diff"], cwd=exe)
@dotlib.cmd(name="list", desc="List all mapped dotfiles")
def list_map():
print(f"{MAP_FILE}:")
l = 0
for k in cfg.keys():
l = max(l, len(k))
for k, v in cfg.items():
print(f" {k:>{l}} -> {v}")
def main(args):
global cfg, exe, cwd
cwd = Path.cwd().expanduser().absolute()
exe = Path(args[0]).expanduser().absolute()
if exe.is_symlink():
exe = exe.readlink()
exe = exe.parent.absolute()
cfg = dotlib.load_map(exe.joinpath(MAP_FILE))
dotlib.run(APP_NAME, args, help_cmd="help")
if __name__ == "__main__":
main(argv)