-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotty
More file actions
executable file
·136 lines (123 loc) · 3.91 KB
/
dotty
File metadata and controls
executable file
·136 lines (123 loc) · 3.91 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
#!/usr/bin/env python3
import yaml
import os
import argparse
import shutil
from subprocess import call
dotty = """ .---. ,,
,, / \ ;,,'
;, ; ( o o ) ; ;
;,';,,, \ \/ / ,; ;
,,, ;,,,,;;,` '-,;'''',,,'
;,, ;,, ,,,, ,; ,,,'';;,,;''';
;,,,; ~~' '';,,''',,;''''
'''"""
conf = {
'root': '',
'repo': None,
'commit': 'individual'
}
def main():
global conf
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--init', action='store_true', dest='init', help='initialize dotty directory')
parser.add_argument('-u', '--update', action='store_true', dest='update', help='update dotfiles')
parser.add_argument('-r', '--restore', action='store_true', dest='restore', help='restore dotfiles')
parser.add_argument('-p', '--push', action='store_true', dest='push', help='push to repo')
parser.add_argument('-v', '--version', action='version', version='doTTy v.0.1')
args = parser.parse_args()
print('doTTy v.0.1')
print(dotty)
dottconf = read_conf('dotty.yml')
if (args.init):
init_repo()
if (args.update):
for service in dottconf:
clone(dottconf[service], '', conf['root'] + '/' + service)
print_success('Cloned {}'.format(service))
if conf['commit'] == 'individual':
commit('Update {}'.format(service))
print_success('Committed {}'.format(service))
if conf['commit'] == 'group':
commit('Update dotfiles')
print_success('Committed group')
if (args.restore):
for service in dottconf:
restore(dottconf[service], service)
print_success('Restored {}'.format(service))
if (args.push):
push()
def commit(msg):
call(['git', 'add', '.'])
call(['git', 'commit', '-m', msg])
def push():
call(['git', 'push', '-f', 'origin', 'master'])
print_success('Pushed to repo')
def init_repo():
call(['git', 'init'])
call(['git', 'add', '.'])
commit('Initial commit')
try:
call(['git', 'remote', 'add', 'origin', conf['repo']])
except TypeError as tpe:
print_error('Remote repo not set')
def restore(service, label):
for folder in service:
if not os.path.exists(os.path.expanduser(next(iter(folder)))):
os.makedirs(os.path.expanduser(next(iter(folder))))
copytree(os.getcwd() + '/' + label, os.path.expanduser(next(iter(folder))))
def copytree(src, dst):
for item in os.listdir(src):
if not os.path.exists(dst):
os.makedirs(dst)
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d)
else:
if os.path.exists(d):
os.remove(d)
shutil.copy2(s, d)
# Recursively clone a service
def clone(service, dir, label):
if type(service) is dict:
for folder in service:
if dir == '':
clone(service[folder], dir + folder, label)
else:
clone(service[folder], dir + '/' + folder, label + '/' + folder)
elif type(service) is list:
for dot in service:
clone(dot, dir, label)
elif service in [ '$all', '$rall' ]:
if service == '$all':
for file in [f for f in os.listdir(os.path.expanduser(dir)) if os.path.isfile(os.path.expanduser(dir + '/' + f))]:
if not os.path.exists(os.getcwd() + label):
os.makedirs(os.getcwd() + label)
shutil.copy2(os.path.expanduser(dir + '/' + file), os.getcwd() + label + '/' + file)
else:
copytree(os.path.expanduser(dir), os.getcwd() + label)
else:
if not os.path.exists(os.getcwd() + label):
os.makedirs(os.getcwd() + label)
shutil.copy2(os.path.expanduser(dir + service), os.getcwd() + label + '/' + service)
def print_error(str):
print('\033[31m' + '>> ' + '\x1B[0m' + str)
def print_success(str):
print('\033[32m' + '>> ' + '\x1B[0m' + str)
def read_conf(filename):
global conf
try:
dottconf = yaml.load(open(filename))
except FileNotFoundError as fnf:
print_error('dotty.yml not found')
except yaml.YAMLError as yme:
print_error('dotty.yml formatting error')
user = dottconf['dotty']
for key in user:
if key in conf:
conf[key] = user[key]
del dottconf['dotty']
return dottconf
if __name__ == "__main__":
main()