-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyxfer.py
executable file
·334 lines (256 loc) · 8.06 KB
/
pyxfer.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Python version of the XFER tool
"""
# imports
import requests
import os
from docopt import docopt
__doc__ = """
Python xfer.
Usage:
pyxfer -s <ip>
pyxfer -r <ip>
pyxfer -u <ip> <file> [<path>] [<opt>]
pyxfer -d <ip> <cpcfile> [<opt>]
pyxfer -x <ip> <cpcfile>
pyxfer -y <ip> <file> [<opt>]
pyxfer -m <ip> <folder>
pyxfer --cd <ip> <cpcfolder>
pyxfer --rm <ip> <cpcfile>
pyxfer --delRom <ip> <romid>
pyxfer -f <ip> <file> <romid> <name>
pyxfer -p <ip>
pyxfer --ls <ip> [<folder>]
Options:
-s Reset CPC.
-r Reboot M4.
-u Upload a file on the SD card of the M4.
-d Download a file from the SD card of the M4.
-x Execute the program on the SD card of the M4.
-y Upload and execute the program on the /tmp of SD card of the M4.
-m Create directory on the SD card.
--cd Change current directory in CPC.
--rm Delete file or empty directory on CPC.
-f Install ROM <file> at position <romid>.
--delRom Delete the ROM.
-p Put CPC in pause.
--ls List files on CPC.
"""
class XFER(object):
def __init__(self, ip):
self._ip = ip
def _getUrl(self, path):
return "http://%s/%s" % (self._ip, path)
def _getHeaders(self):
return {"user-agent": "cpcxfer"}
def _upload_file(self, path, destination, opt):
assert os.path.isfile(path)
if opt!=0:
raise "Need to add an amsdos header"
print "Upload " + path + " in " + destination
url = self._getUrl("upload.html")
r = requests.post(
url,
files={"upfile" : (
destination + "/" + os.path.basename(path),
open(path, 'rb'),
"Content-Type: application/octet-stream",
{'Expires': '0'}
)}
)
assert r.status_code == 200
def _upload_dir(self, path, destination, opt):
assert os.path.isdir(path)
removed_base = path[:-len(os.path.basename(path))]
def _treat_dir(current_dir):
current_base = (destination + "/" + current_dir[len(removed_base):]).replace("//", "/").replace(' ', '_')
self.mkdir(current_base)
for filename in os.listdir(current_dir):
filename = os.path.join( path, filename)
print ">", filename, current_base, opt
self.upload(filename, current_base, opt)
_treat_dir(path)
def resetM4(self):
url = self._getUrl("config.cgi?mres")
print "Reset M4"
r= requests.get(url, headers=self._getHeaders())
assert r.status_code == 200
def resetCPC(self):
url = self._getUrl("config.cgi?cres")
print "Reset CPC"
r= requests.get(url, headers=self._getHeaders())
assert r.status_code == 200
def upload(self, path, destination, opt):
assert(os.path.exists(path))
if os.path.isdir(path):
return self._upload_dir(path, destination, opt)
else:
return self._upload_file(path, destination, opt)
def download(self, path, opt):
print "Download" + path
url = self._getUrl("sd/%s" % path)
r = requests.get( url)
assert r.status_code == 200
with open(os.path.basename(path), 'wb') as f:
for c in r.text:
f.write(chr(ord(c)))
def execute(self, cpcfile):
url = self._getUrl("config.cgi")
print "Execute " + cpcfile
r = requests.get(
url,
params = {
"run2": cpcfile
}
)
assert r.status_code == 200
def mkdir(self, folder):
# TODO Manage the fact that several repositories are asked
if folder[0] != "/":
folder = "/" + folder
print "Create directory " + folder
url = self._getUrl("config.cgi")
r= requests.get(
url,
params = {"mkdir": folder},
headers=self._getHeaders())
assert r.status_code == 200
def cd(self, cpcfolder):
url = self._getUrl("config.cgi")
r = requests.get(
url,
params = {
"cd": cpcfolder
}
)
assert r.status_code == 200
def rm(self, cpcfile):
url = self._getUrl("config.cgi")
r = requests.get(
url,
params = {
"rm": cpcfile
}
)
assert r.status_code == 200
def delRom(self, romid):
assert 0 <= int(romid) <= 31
url = self._getUrl("roms.shtml")
r = requests.get(
url,
params = {
"rmsl": romid
}
)
assert r.status_code == 200
def putRom(self, fname, romid, name):
assert 0 <= int(romid) <= 31
assert os.path.isfile(fname)
print "Put ROM " + fname + " in " + romid + ' as ' + name
url = self._getUrl("roms.shtml")
r = requests.post(
url,
data = {
"slotnum": romid,
"slotname": name,
},
files = {
"uploadedfile": (
"rom.bin",
open(fname, 'rb'),
"Content-Type: application/octet-stream",
{'Expires': '0'}
)}
)
assert r.status_code == 200
def pause(self):
url = self._getUrl("config.cgi")
r = requests.get(
url,
params = {
"chlt": "CPC+Pause"
}
)
assert r.status_code == 200
def ls(self, cpcfolder):
print "LS in " + cpcfolder
url = self._getUrl("config.cgi")
r = requests.get(
url,
params = {
"ls": cpcfolder
}
)
assert r.status_code == 200
url = self._getUrl("sd/m4/dir.txt")
r = requests.get(
url
)
assert r.status_code == 200
print r.text
# code
if __name__ == '__main__':
arguments = docopt(__doc__, version='Python XFER 0.1')
xfer = XFER(arguments['<ip>'])
shared = {
'dest' : "/",
'opt' : 0
}
def upload():
fname = arguments["<file>"]
if arguments["<path>"]:
shared['dest'] = arguments["<path>"]
if arguments["<opt>"]:
shared["opt"] = arguments["<opt>"]
xfer.upload(fname, shared["dest"], shared["opt"])
def download():
fname = arguments["<cpcfile>"]
if arguments["<opt>"]:
shared["opt"] = arguments["<opt>"]
xfer.download(fname, shared["opt"])
def execute():
xfer.execute(arguments["<cpcfile>"])
if arguments["-s"]:
xfer.resetCPC()
elif arguments["-r"]:
xfer.resetM4()
elif arguments["-u"]:
upload()
elif arguments["-d"]:
shared["opt"] = 1
download()
elif arguments["-x"]:
execute()
elif arguments["-y"]:
arguments["<path>"] = "/tmp"
upload()
arguments["<cpcfile>"] = "/tmp/" + os.path.basename(arguments["<file>"])
execute()
elif arguments["-m"]:
xfer.mkdir(arguments["<folder>"])
elif arguments["--cd"]:
xfer.cd(arguments["<cpcfolder>"])
elif arguments["--rm"]:
xfer.rm(arguments["<cpcfile>"])
elif arguments["--delRom"]:
xfer.delRom(arguments["<romid>"])
elif arguments["-f"]:
xfer.putRom(arguments['<file>'], arguments["<romid>"], arguments["<name>"])
elif arguments["-p"]:
xfer.pause()
elif arguments["--ls"]:
if not arguments["<folder>"]:
arguments["<folder>"] = ""
xfer.ls(arguments["<folder>"])
else:
print "Option unknown"
# metadata
__author__ = 'Romain Giot'
__copyright__ = 'Copyright 2017'
__credits__ = ['Romain Giot']
__licence__ = 'GPL'
__version__ = '0.1'
__maintainer__ = 'Romain Giot'
__email__ = 'giot.romain@gmail.com'
__status__ = 'Prototype'