forked from OpenRefine/OpenRefine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refine.py
211 lines (167 loc) · 6.31 KB
/
refine.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
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
import sys, os, zipfile, shutil, urllib.request
from urllib.request import urlopen
from urllib.request import urlretrieve
from xml.dom import minidom
class Launcher:
_path = "workspace/"
_version = "0.1"
_remote = "https://gist.github.com/luiseduardobrito/5750844/raw/49d8ad6266739ad0deefeaecfca7023af0b1dac0/config.xml"
_local = "config.xml"
_local_workspace = os.path.join(_path, "workspace.zip")
_main_workspace = "main"
def unzip(self, zipFilePath, destDir):
zfile = zipfile.ZipFile(zipFilePath)
for name in zfile.namelist():
(dirName, fileName) = os.path.split(name)
if fileName == '':
# directory
newDir = destDir + '/' + dirName
if not os.path.exists(newDir):
os.mkdir(newDir)
else:
# file
fd = open(destDir + '/' + name, 'wb')
fd.write(zfile.read(name))
fd.close()
zfile.close()
def getText(self, nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
def handleTok(self, tokenlist):
texts = ""
for token in tokenlist:
texts += " "+ self.getText(token.childNodes)
return texts
def _log(self, msg):
print(msg)
def check_updates(self, config_url = ''):
self._log("Getting update information from server...")
self._version = self.get_local_version()
(v, w) = self.get_remote_info(self._remote)
self._log("Local workspace version: %s" % self._version)
if(self._version == v):
self._log("Client already in latest version.")
return
else:
self._log("Workspace needs to be updated. Remote workspace version: %s" % v)
self.update_projects(w)
self.purge()
self._log("Client updated successfuly! \n")
def update_projects(self, workspace):
self._log("Recreating local workspace...")
shutil.rmtree(self._path)
if not os.path.exists(self._path):
os.makedirs(self._path)
self._log("Downloading projects files. Please, be a little patient, it may take some time...")
urlretrieve (workspace, self._local_workspace)
self._log("Inflating workspace files...")
self.unzip(self._local_workspace, self._path)
os.remove(self._local_workspace)
self._log("Workspace created and populated successfully!")
def purge(self):
self._log("Purging configuration files...")
os.remove(self._local)
self._log("Downloading untouched configuration file from server...")
urlretrieve (self._remote, self._local)
def get_remote_info(self, input, w = _main_workspace):
xmldoc = minidom.parse(urlopen(input))
config = xmldoc.getElementsByTagName("config")[0]
if(len(xmldoc.getElementsByTagName("workspaces")) < 1
or len(xmldoc.getElementsByTagName("workspaces")[0].getElementsByTagName(w)) < 1):
self._log("\nERROR: workspace not found in remote repository.")
exit(0)
return (str(self.handleTok(config.getElementsByTagName("version"))).strip().capitalize(),
str(self.handleTok(xmldoc.getElementsByTagName(w))).strip().capitalize())
def get_remote_version(self, input):
(v, w) = self.get_remote_info(input)
return v
def get_local_version(self):
xmldoc = minidom.parse(open(self._local, "r"))
config = xmldoc.getElementsByTagName("config")[0]
if(config):
return str(self.handleTok(config.getElementsByTagName("version"))).strip().capitalize()
else:
self._log("No configuration information found, downloading new one...");
self.purge()
check_updates()
def _info(self):
self._log("\nGPNX Refine - Customized large data manipulated solution")
self._log("Copyright 2013 - GPNX Group")
self._log("Redesigned by Luis Eduardo Brito <luis@gpnxgroup.com>\n")
return
def _help(self):
self._info()
self._howto()
self._log("")
return
def _howto(self):
self._log("How to:")
self._log(" python refine.py [options]\n")
self._log("Available options:")
self._log(" --version get local version")
self._log(" --skip-update-check skip remote check, just take me to the grefine")
self._log(" --purge-installation purge all stuff and redownload from remote")
self._log(" --workspace [name] specify remote workspace")
self._log(" --directory [name] specify local workspace")
self._log(" --help SOS: help me")
return
def version(self):
self._info()
self._log("Local version: %s" % self.get_local_version())
self._log("")
return
def remote_version(self):
self._info()
self._log("Local version: %s" % self.get_local_version())
self._log("Remote version: %s" % self.get_remote_version(self._remote))
self._log(("Client needs to be updated.", "Client is updated!")[self.get_local_version() == self.get_remote_version(self._remote)])
self._log("")
return
def run(self):
os.system('./launcher -d %s'%self._path)
return
def __init__(self):
# --help
if(len(sys.argv) > 1 and (sys.argv[1] == "--help" or sys.argv[1] == "-h" or sys.argv[1] == "help")):
self._help()
return
# --version
if(len(sys.argv) > 1 and (sys.argv[1] == "--version" or sys.argv[1] == "-v" or sys.argv[1] == "version")):
self.version()
return
# --remote-version
if(len(sys.argv) > 1 and (sys.argv[1] == "--remote-version" or sys.argv[1] == "-r" or sys.argv[1] == "remote")):
self.remote_version()
return
self._info()
# --purge-installation
if(len(sys.argv) > 1 and (sys.argv[1] == "--purge-installation" or sys.argv[1] == "-p" or sys.argv[1] == "purge")):
(v,w) = self.get_remote_info(self._remote)
self.update_projects(w)
self.purge()
return
# --workspace
if(len(sys.argv) > 1 and (sys.argv[1] == "--workspace" or sys.argv[1] == "-w" or sys.argv[1] == "workspace")):
if(len(sys.argv) < 2):
self._log("You need to specify the workspace you want to use.")
exit()
(v,w) = self.get_remote_info(self._remote, sys.argv[2])
self.update_projects(w)
self.purge()
self.run()
return
if(len(sys.argv) > 1 and sys.argv[1] == "--skip-update-check"):
self.run()
return
if(len(sys.argv) > 1 and (sys.argv[1] == "--directory" or sys.argv[1] == "-d" or sys.argv[1] == "directory")):
if(len(sys.argv) < 2):
self._log("You need to specify the local directory you want to work on.")
exit()
self._path = sys.argv[2]
return
self.check_updates()
self.run()
l = Launcher()