-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwizzy
executable file
·227 lines (197 loc) · 8.43 KB
/
wizzy
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
import yaml,sys,glob,os,shutil,errno,subprocess
from random import choice
from clint.textui import colored
command_names = ['list', 'add', 'check', 'install', 'fix', 'doctor', 'remove', 'track', 'untrack', 'help']
def isACommand(command):
return command in command_names
def configurationFile():
try:
stream = open("wizzy_config.yml", 'r')
configuration_file = yaml.load(stream)
return configuration_file
except IOError:
wizzyCastAndSpeakError("File wizzy_config.yml", " not found! Please fix this!")
exit(0)
def randomMagicWord():
words = [
"Aajaye",
"Abracadabra",
"Alla Peanut Butter Sandwiches",
"Alakazam",
"Cei",
"Hocus pocus",
"Joshikazam",
"Klaatu barada nikto",
"Meeska, Mooska, Mickey Mouse",
"Open sesame",
"Hey Presto",
"Sim Sala Bim",
"Shazam",
"Izzy wizzy",
"Wella Walla Washington",
"Mecca lecca hi, mecca hiney ho"]
return choice(words)
def wizzySpeak(phrase, action=""):
print colored.blue("Wizzy: ") + colored.cyan(action)+ colored.yellow(phrase) + colored.cyan(" ...")
def wizzyCast():
print colored.blue(" *^~.. ") + colored.green(randomMagicWord()+"!") + colored.blue(" ..~^* ")
def wizzyCastAndSpeak(phrase):
print colored.blue("Wizzy: ") + colored.blue(" *^~.. ") + colored.green(randomMagicWord()+"!") + colored.blue(" ..~^* ") + colored.yellow(phrase)
def wizzyCastAndSpeakError(element, phrase):
print colored.red("Wizzy: ") + colored.blue(" *^~.. ") + colored.green(randomMagicWord()+"!") + colored.blue(" ..~^* ") + colored.red(element) + colored.yellow(phrase)
# ---------------------------------------------------
# Configuration file actions
def check():
wizzySpeak("Let me see.. i should have a check spell somewhere...")
wizzyCast()
wizzySpeak("Checking files inside wizzy_config.yml")
errors = 0
for element in configurationFile()['files']:
if not glob.glob(os.path.abspath(element)):
errors += 1
wizzyCastAndSpeakError(element, " not valid. Please use valid file name")
if errors > 0 :
if errors == 1:
wizzySpeak("You have "+str(errors) +" error. Please run wizzy doctor to fix it")
else:
wizzySpeak("You have "+str(errors) +" errors. Please run wizzy doctor to fix them")
else:
wizzySpeak("Ok! It should be all ok ...")
def doctor():
wizzySpeak("Checking files inside wizzy_config.yml")
for element in configurationFile()['files']:
if not glob.glob(os.path.abspath(element)):
remove(element)
def list():
wizzySpeak("Coff Coff.. too much dust!")
for element in configurationFile()['files']:
if not glob.glob(os.path.abspath(element)):
#The file(s) are in the wizzy_config but not on the os !
wizzyCastAndSpeakError(element, " does not exist. Check your wizzy_config file")
for file in glob.glob(os.path.abspath(element)):
print colored.green(file)
def add(file):
if glob.glob(os.path.abspath(file)):
array = configurationFile()['files']
# TODO: Check for duplicates
array.append(file)
configurationFile()['files'].update({'files': array})
stream = open("wizzy_config.yml",'w')
yaml.dump(configurationFile()['files'],stream)
wizzySpeak("File " + file + " added to wizzy_config.yml")
else:
wizzyCastAndSpeakError(file, " does not making any sense. Please insert a valid file name or regex!")
def remove(file):
array = configurationFile()['files']
try:
array.remove(file)
configurationFile()['files'].update({'files': array})
stream = open("wizzy_config.yml",'w')
yaml.dump(configurationFile()['files'],stream)
wizzySpeak("File " + file + " removed from wizzy_config.yml")
except ValueError,e :
wizzyCastAndSpeakError(file, " was not in the config file. I cannot remove what does not exist")
# ---------------------------------------------------
# ---------------------------------------------------
# Basic operations
def help():
span = 10
print colored.cyan("usage: wizzy <spell>")
print colored.yellow("Shhhh! Here you can find the list of the spells :")
print colored.red(command_names[0].ljust(span)) + colored.green("List all the files specified into the wizzy-config")
print colored.red(command_names[1].ljust(span)) + colored.green("Add a file to the wizzy-config")
print colored.red(command_names[2].ljust(span)) + colored.green("Check if wizzy-config contains errors")
print colored.red(command_names[3].ljust(span)) + colored.green("Install wizzy and caches all files specified in the wizzy-config")
print colored.red(command_names[4].ljust(span)) + colored.green("Restores the cached files .. like .. magic ! ")
print colored.red(command_names[5].ljust(span)) + colored.green("Fix the wizzy-config removing invalid files")
print colored.red(command_names[6].ljust(span)) + colored.green("Remove a file from the wizzy-config")
print colored.red(command_names[7].ljust(span)) + colored.green("Set all the files in a assume-unchanged state")
print colored.red(command_names[8].ljust(span)) + colored.green("Set all the files in a no-assume-unchanged state")
print colored.red(command_names[9].ljust(span)) + colored.green("Well.. that's me!")
def install():
wizzySpeak("Creating magic directory")
# Create wizzy cache directory
try:
os.makedirs(configurationFile()['wizzy_cache_directory'])
except OSError, e:
if e.errno == errno.EEXIST:
wizzyCastAndSpeakError(configurationFile()['wizzy_cache_directory'], " directory was already created. Let's skip this ok?")
else:
# If is not an already exists error
raise
# Fill wizzy dir with files specified in .wizzy-config
for element in configurationFile()['files']:
if not glob.glob(os.path.abspath(element)):
#The file(s) are in the .wizzy-config but not on the os !
wizzyCastAndSpeakError(element, " disappeared! Oh snap! Can you please run wizzy check to understand what is missing?")
for file in glob.glob(os.path.abspath(element)):
wizzySpeak(file,"Cached ")
shutil.copy2(file, os.path.abspath(configurationFile()['wizzy_cache_directory']))
def isModified(f):
if not os.path.exists(os.path.abspath(f)) :
# file removed?
return True
if os.stat(f).st_mtime == os.stat(os.path.abspath(configurationFile()['wizzy_cache_directory'])+"/"+os.path.basename(f)).st_mtime :
return False
else :
return True
def fix():
if not glob.glob(os.path.abspath(configurationFile()['wizzy_cache_directory'])):
wizzyCastAndSpeakError(configurationFile()['wizzy_cache_directory'], " directory not found. Have you tried to wizzy install first?")
else:
for element in configurationFile()['files']:
for file in glob.glob(os.path.abspath(element)):
# if the file is modified since last magic, copy the cached version
if isModified(file) :
if os.path.exists(file):
# Override file
os.remove(file)
shutil.copy2(configurationFile()['wizzy_cache_directory']+"/"+os.path.basename(file), os.path.dirname(file))
wizzyCast()
wizzySpeak(file,"Restored ")
else :
wizzySpeak(file,"No magic needed for file: ")
# ---------------------------------------------------
# ---------------------------------------------------
# Git related operations
def execute_process(command):
child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)
while True:
out = child.stderr.read(1)
if out == '' and child.poll() != None:
break
if out != '':
sys.stdout.write(out)
sys.stdout.flush()
def untrack():
for element in configurationFile()['files']:
for file in glob.glob(os.path.abspath(element)):
execute_process('git update-index --assume-unchanged ' + file)
# print getInfoMessage(" "+file+"? ")+ getMagicMessage("Oh snap! My alzheimer!" )
wizzyCastAndSpeak(file+" ? Oh snap! My alzheimer!")
def track():
for element in configurationFile()['files']:
for file in glob.glob(os.path.abspath(element)):
execute_process('git update-index --no-assume-unchanged ' + file)
wizzyCastAndSpeak(file+" ? Oh now i remember!")
# ---------------------------------------------------
def exec_arg(argv):
if len(argv) == 2:
# check if the command is supported
if not argv[1] in command_names:
# not supported
wizzyCastAndSpeakError(argv, " wrong spell! What are you trying to say? Plase type wizzy help")
else:
# calling the command
# print argv
globals()[argv[1]]()
elif len(argv) == 3 :
if not isACommand(argv[1]):
wizzyCastAndSpeakError(argv, " wrong spell! What are you trying to say? Plase type wizzy help")
else:
globals()[argv[1]](argv[2])
else:
# wrong arguments man
wizzyCastAndSpeakError(argv, " wrong arguments! What are you trying to say? Plase type wizzy help")
exec_arg(sys.argv)