-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·155 lines (130 loc) · 5.04 KB
/
Copy pathinstall.py
File metadata and controls
executable file
·155 lines (130 loc) · 5.04 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/python3
import os
import sys
import argparse
import time
import datetime
import json
# DEFAULT CONFIG
LOG_FILE = "setup.log"
DOTFILE = "default.json"
BACKUP_SUFFIX = "__backup"
PRESET = "default"
home = os.path.expanduser("~")
# ------------------------------------------
# CLI Args
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", dest="filename", default=DOTFILE,
help="read from FILE", metavar="FILE")
parser.add_argument("-o", "--option", dest="option", required=False,
help="set OPTION without propting for one", metavar="OPTION")
args = parser.parse_args()
# ------------------------------------------
def correct_path(key, value):
path = key.replace("~", home)
return path, value
def source_correction(value: str, dotfiles_path: str):
path = value
if path.split('/')[0] == ".":
base_array = dotfiles_path.split("/")
base_array.pop()
base = "/".join(base_array)
path = value.replace(".", base)
return path
def read_config(dotfile: str):
with open(dotfile) as json_data:
data = json.load(json_data)
return data
def print_notifier():
print("----------------------------------------------------------------")
print(
f"Script output will be logged in {LOG_FILE} for future reference")
print("----------------------------------------------------------------")
class Logger():
def __init__(self, filename=LOG_FILE):
self.file = open(filename, 'a')
def log(self, string: str):
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
print(string)
string = "["+st+"] "+string+"\n"
self.file.write(string)
def destroy(self):
self.file.close()
class Installer():
def __init__(self, backup_suffix=BACKUP_SUFFIX, logger: Logger = None):
self.logger = logger
self.dotfile = args.filename
option = args.option
if option is None:
option = input("[i]nstall [r]ecover [e]xit:\n")
data = read_config(self.dotfile)
try:
links = data['links']
deps = data['dependencies']
except:
print('Did you specify an existing preset?')
sys.exit()
if option == "i":
self.logger.log("# INSTALLING")
print_notifier()
self.install(links)
self.logger.log(
"----------------------------------------------------------------")
self.logger.log("Installation complete!\n")
if option == "r":
self.logger.log("# REMOVING")
print_notifier()
self.uninstall(links)
self.logger.log(
"----------------------------------------------------------------")
self.logger.log("Removal complete!\n")
if option == "e":
sys.exit()
def install(self, list: []):
# Get name to folder to look at
dotfile = self.dotfile.split('/')[-1]
for item in list:
path, source = correct_path(item, list[item])
if os.path.exists(path+BACKUP_SUFFIX):
self.logger.log(
f"{path}{BACKUP_SUFFIX} exists, script will stop to prevent file mangling")
exit('Script Exited!')
if os.path.exists(path) and not os.path.islink(path):
self.logger.log(f"{path} - Path exists!")
self.logger.log(f"Renaming: {path} to {path}{BACKUP_SUFFIX}")
os.rename(path, path+BACKUP_SUFFIX)
try:
source = source_correction(source, self.dotfile)
self.logger.log(f"Linking: {dotfile} | {source} to {path}")
os.symlink(source, path)
except FileExistsError:
self.logger.log("Link already exists!")
except FileNotFoundError:
if not os.path.exists(path):
pwd = path.split('/')
pwd.pop()
pwd = '/'.join(pwd)
print(f"Creating path: {pwd}")
os.makedirs(pwd)
os.symlink(source, path)
def uninstall(self, list: []):
for item in list:
path, source = correct_path(item, list[item])
if os.path.exists(path+BACKUP_SUFFIX):
if os.path.islink(path):
self.logger.log(f"Removing: {path}")
os.remove(path)
try:
self.logger.log(
f"Renaming: {path}{BACKUP_SUFFIX} to {path}")
os.rename(path+BACKUP_SUFFIX, path)
except FileNotFoundError:
self.logger.log(
f"{path}{BACKUP_SUFFIX} not found, likely deleted")
else:
self.logger.log("Not a link or missing, skipping!")
else:
self.logger.log(f"Removing: {path}")
os.remove(path)
Installer(logger=Logger())