-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
executable file
·227 lines (192 loc) · 6.64 KB
/
upload.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
from mimetypes import MimeTypes
import cookielib
import mechanize
import os
import re
import sys
import urllib
import urllib2
from datetime import datetime
################################################################################
filenameArch16n = 'english.0.properties'
filenameCss = 'ui_styling.css'
filenameDataSchema = 'data_schema.xml'
filenameUiLogic = 'ui_logic.bsh'
filenameUiSchema = 'ui_schema.xml'
filenameValidation = 'validation.xml'
filenames = [
filenameArch16n,
filenameCss,
filenameDataSchema,
filenameUiLogic,
filenameUiSchema,
filenameValidation
]
username = 'faimsadmin@intersect.org.au'
password = 'Pass.123'
server = 'dev'
url = 'http://%s.fedarch.org' % server
################################################################################
doDelete = False
doReplace = False
hasFlag = False
if len(sys.argv) <= 1: pass
elif sys.argv[1] == '--rm': doDelete = True; hasFlag = True
elif sys.argv[1] == '--replace': doReplace = True; hasFlag = True
else:
sys.stderr.write('USAGE:')
sys.stderr.write(' upload.py [--rm|--replace]\n')
exit()
# The module location can be set manually or be determined automatically
if len(sys.argv) > 1 and not hasFlag:
# Set module location from manually-given argument
moduleLocation = sys.argv[1]
moduleLocation = os.path.abspath(moduleLocation)
else:
# Set module location to that of this script
moduleLocation = os.path.realpath(__file__)
moduleLocation = os.path.dirname(moduleLocation) + os.sep + 'module'
# Determine module name using a heuristic. If the module is in a (parent)
# directory called 'module', it's generally reasonable to assume that
# directory was created by the autogen. In that case, the name of the
# grandparent directory is probably a more apt name for the module. (Though this
# heuristic could fail in a ridiculous way.)
moduleName = moduleLocation.split(os.sep)
if moduleName[-1] == 'module': moduleName = moduleName[-2]
else: moduleName = moduleName[-1]
moduleName += datetime.now().strftime(' %Y-%m-%d')
# Check that all the given paths really exist
if not os.path.exists(moduleLocation):
print 'ERROR: given module location does not exist'
exit()
isMissing = False
for f in filenames:
if not f: continue
if not os.path.exists(moduleLocation + os.sep + f):
print 'ERROR: Cannot find %s' % f
isMissing = True
if isMissing:
exit()
# Initialisation of `magic` python module
mime = MimeTypes()
mime.add_type('text/plain', '.bsh')
mime.add_type('text/plain', '.properties')
################################################################################
def addFile(br, filename, inputName):
if not filename: return
fullFilename = moduleLocation + os.sep + filename
mimetype = mime.guess_type(filename)
mimetype = mimetype[0]
br.form.add_file(open(fullFilename), mimetype, filename, name=inputName)
def addFiles(br, doUploadDataSchema):
addFile (br, filenameArch16n , 'project_module[arch16n]')
addFile (br, filenameCss , 'project_module[css_style]')
if doUploadDataSchema:
addFile(br, filenameDataSchema , 'project_module[data_schema]')
addFile (br, filenameUiLogic , 'project_module[ui_logic]')
addFile (br, filenameUiSchema , 'project_module[ui_schema]')
addFile (br, filenameValidation , 'project_module[validation_schema]')
def login(br):
global username
global password
global url
# Get login form and fill it out
print 'Working on module with the name "%s"...' % moduleName
print
print 'Downloading login form...'
res = br.open(url)
br.select_form(nr=0)
br['user[email]'] = username
br['user[password]'] = password
# Submit yer good ol' form
print 'Submitting login form...'
print
res = br.submit()
def goHome(br):
global url
br.open(url)
def deleteModule(br):
print 'Downloading delete form...'
# Try clicking on link to module config page
try:
linkText = '^%s$' % moduleName
res = br.follow_link(text_regex=linkText)
except:
print 'Cannot delete module; does not exist. Exiting.'
return
# Get URL to delete module
linkText = 'Delete Module'
req = br.click_link(text_regex=linkText)
url = req.get_full_url()
# Search for the stupid CSRF token
token = res.get_data()
token = re.search('"([^"]+)"\s+name="csrf-token"', token)
token = token.group(1)
params = {
u'_method' : 'delete',
u'authenticity_token' : token
}
data = urllib.urlencode(params)
# POST the deletion request
try:
br.open(url, data)
print 'All done!'
except:
print 'Cannot delete module. Exiting.'
def uploadModule(br):
# Determine if module's already been uploaded. (Yes, I know I've misused the
# try-except statement.)
print 'Downloading update/create form...'
try:
# The module's already been uploaded
linkText = '^%s$' % moduleName
res = br.follow_link(text_regex=linkText)
linkText = 'Edit Module'
res = br.follow_link(text_regex=linkText)
print 'Submitting update form...'
print
br.select_form(nr=0)
addFiles(br, doUploadDataSchema=False)
res = br.submit()
except:
# We need to create a new module
linkText = 'Create Module'
res = br.follow_link(text_regex=linkText)
print 'Submitting create form...'
print
br.select_form(nr=0)
br['project_module[name]'] = moduleName
addFiles(br, doUploadDataSchema=True)
res = br.submit()
res = res.get_data()
reg = '<span class="help-inline">([^<]+)</span>'
doesMatch = re.search(reg, res)
Ms = re.finditer(reg, res)
if not doesMatch:
print 'All done!'
return
print 'Completed with errors:'
for m in Ms:
print ' - ', m.group(1)
################################################################################
# Make browser
br = mechanize.Browser()
# Initialisation of `mechanize` module (Some stuff I copied from Stack Overflow)
cookiejar = cookielib.LWPCookieJar()
br.set_cookiejar (cookiejar)
br.set_handle_equiv (True)
br.set_handle_gzip (True)
br.set_handle_redirect(True)
br.set_handle_referer (True)
br.set_handle_robots (False)
# Navigate website and upload module
login(br)
if doReplace:
deleteModule(br)
goHome (br)
uploadModule(br)
elif doDelete:
deleteModule(br)
else:
uploadModule(br)