-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.py
158 lines (132 loc) · 3.67 KB
/
backup.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
import os
import sys
import subprocess
import string
import re
argv = sys.argv
#Processing backup path
currentPath = os.path.dirname(os.path.abspath(__file__))
cmd = "cat " + currentPath +"/.config"
result = subprocess.check_output(cmd, shell=True)
path = re.split("DESDIR=([^\n]+)", result)
def allBackup():
python()
ruby()
nodejs()
git()
atom()
vim()
def python():
filepath = path[1] + "/python"
try:
print "Start python backup"
cmd = "pip list"
result = subprocess.check_output(cmd, shell=True)
result = result.replace(" (", "%%")
result = result.replace(")"," ")
result = result.replace("\n", "\n##")
except:
print "[Error]Python: you do not install pip"
#try:
f = open(filepath,'wrb')
f.write("##")
f.write(result)
f.close()
print " ..done"
#except:
print "[Error]Python: path error"
def ruby():
filepath = path[1] + "/ruby"
#try:
print "Start Ruby backup",
cmd = "gem list --local"
result = subprocess.check_output(cmd, shell=True)
result = result.replace(" (", "%%")
result = result.replace(")"," ")
result = result.replace("\n", "\n##")
#except:
# print "[Error]Ruby: Gem have some problem"
try:
f = open(filepath, 'wb')
f.write("##")
f.write(result)
f.close()
print " ..done"
except:
print "[Error]Ruby: path error"
def nodejs():
filepath = path[1] + "/nodejs"
try:
print "Start NodeJS backup",
cmd = "npm ls -g"
result = subprocess.check_output(cmd, shell=True)
result = result.replace("@", "%%")
result2 = re.sub(r'((│|├|└)\s(┬|─|└|├|└)*)|((├|└)(┬|─|└|├|└)*)', "##", result)
result = re.sub(r'(##)((\s)^|(##)*)*', "##", result2)
result = result.replace("## ##", "##")
result = result.replace("## ##", "##")
result = result.replace("## ##", "##")
result = result.replace(" ## ", "##")
result = result.replace("## ", "")
result = result.replace(" /usr/local/lib", "")
except:
print "[Error]NodeJS: NPM have some problem"
try:
f = open(filepath, 'wb')
f.write(" ")
f.write(result)
f.close()
print "Start NodeJS backup ..done"
except:
print "[Error]NodeJS: path error"
def git():
try:
print "Start Git backup",
cmd = "cp -p ~/.gitconfig " + path[1] + "/git"
result = subprocess.check_output(cmd, shell=True)
print " ..done"
except:
print "[Error]Git: path error"
def atom():
filepath = path[1] + "/atom"
try:
print "Start Atom backup",
cmd = "apm list --bare"
result = subprocess.check_output(cmd, shell=True)
result = result.replace("@", "%%")
result = result.replace("\n", "\n##")
result = result.replace("####", "")
except:
print "[Error]Atom: APM have some problem"
try:
f = open(filepath, 'wb')
f.write("##")
f.write(result)
f.close()
print " ..done"
except:
print "[Error]Atom: path error"
def vim():
try:
print "Start Vim backup",
cmd = "cp -p ~/.vimrc "+ path[1] + "/vim"
result = subprocess.check_output(cmd, shell=True)
print " ..done"
except:
print "Error: Vim backup fail"
#Main function
if argv[1] == '-all':
allBackup()
elif argv[1] == '-python':
python()
elif argv[1] == '-ruby':
ruby()
elif argv[1] == '-nodejs':
nodejs()
elif argv[1] == '-git':
git()
elif argv[1] == '-atom':
atom()
elif argv[1] == '-vim':
vim()