-
Notifications
You must be signed in to change notification settings - Fork 0
/
burner.py
327 lines (278 loc) · 11.8 KB
/
burner.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
# -*- coding: utf-8 -*-
"""Custom Burner client main module
Copyright 2008 Arrigo Marchiori
This program is distributed under the terms of the GNU General Public
License, as specified in the COPYING file.
This file is part of Custom Burner.
Custom Burner is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Custom Burner is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Custom Burner; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
import sys
import os
import os.path
import logging
import socket
import SocketServer
import optparse
import common
quitting = False
def handshake(connection):
"""Handshake to a server.
This function throws a BurnerException or socket.error in case of error"""
data = connection.readLine()
if data != common.MSG_SERVER_GREETING:
raise common.BurnerException, "Strange data received: \"%s\"" % data
connection.request.send(common.MSG_CLIENT_GREETING + "\n")
data = connection.readLine()
if data != common.version:
raise common.BurnerException, "Server version mismatch: \"%s\"" % data
connection.request.send(common.version + "\n")
class CustomBurnerClient:
"""A burner."""
# TCP port to listen on
port = None
# Server IP address
serverIP = None
# Server TCP port
serverPort = None
# Our TCP server object
tcpServer = None
# Our logger
logger = None
# Our name
name = None
# List of the isos we have
isos = []
# Directory containing our isos
isoDirectory = None
def registerToServer(self):
"""Register to a burner server.
All the information about the server and this burner are taken from
object attributes."""
try:
self.logger.info("Connecting to %s:%d" % \
(self.serverIP, self.serverPort))
connection = common.RequestMaker(self.serverIP, self.serverPort)
handshake(connection)
connection.send(common.MSG_CLIENT_REGISTER + "\n")
data = connection.readLine()
if data != common.MSG_ACK:
raise common.BurnerException, \
"Server doesn't want to register us: \"%s\"" % data
connection.send(self.name + "\n")
connection.send(str(self.port) + "\n")
data = connection.readLine()
if data != common.MSG_ACK:
raise common.BurnerException, \
"Server doesn't want to accept our registration: \"%s\"" \
% data
connection.send(common.MSG_CLIENT_HAS_ISOS + "\n")
connection.send(str(len(self.isos)) + "\n")
for iso in self.isos:
connection.send(iso + "\n")
data = connection.readLine()
if data != common.MSG_ACK:
raise common.BurnerException, \
"Server doesn't like our isos: \"%s\"" % data
self.logger.info("Registered to server.")
connection.close()
except common.BurnerException, e:
self.logger.error(e)
sys.exit(1)
except socket.error, e:
self.logger.error(e)
sys.exit(1)
def hasIso(self, name):
"""Return True if this burner has a copy of an iso file."""
if name in self.isos:
return True
else:
self.logger.debug("Server asked for ISO %s, that I don't have." %
name)
return False
def queue(self, date, iso, committer):
"""Get ready to burn."""
self.isoToBurn = iso
self.isoDate = date
self.isoCommitter = committer
def live(self):
"""Waits for jobs and does them."""
while not quitting:
self.logger.info("Waiting for server request...")
self.tcpServer.handle_request()
if self.isoToBurn:
# Burn!
self.logger.info("Burning %s for %s. "
"Please insert disc and press ENTER" % \
(self.isoToBurn, self.isoCommitter))
sys.stdin.readline()
a = os.system(self.burnCmd % os.path.join(self.isoDirectory,
self.isoToBurn))
try:
connection = common.RequestMaker(self.serverIP,
self.serverPort)
handshake(connection)
if a == 0:
# Report succesful job
self.logger.info("ISO %s for %s burnt successfully." %
(self.isoToBurn, self.isoCommitter))
connection.send(common.MSG_BURN_SUCCESS + "\n")
else:
# Report error
self.logger.error("Error while burning %s for %s!" %
(self.isoToBurn, self.isoCommitter))
connection.send(common.MSG_BURN_ERROR + "\n")
connection.send("%s\n%s\n%s\n" %
(self.name, self.isoToBurn,
self.isoCommitter))
data = connection.readLine()
if data != common.MSG_ACK:
raise BurnerException, \
"Strange data from server: \"%s\"" % data
connection.close()
except common.BurnerException, e:
self.logger.error(e)
except socket.error, e:
self.logger.error(e)
self.isoToBurn = False
def sayGoodbye(self):
"""Say good bye to server.
You should call this if this client wants to quit before the
server is closed."""
try:
self.logger.info("Saying goodbye to server.")
connection = common.RequestMaker(self.serverIP,
self.serverPort)
handshake(connection)
connection.send(common.MSG_CLOSING + "\n")
connection.send(self.name + "\n")
data = connection.readLine()
if data != common.MSG_ACK:
self.logger.warning("Server didn't answer Ok to our goodbye, "
"but \"%s\" instead" % data)
connection.close()
except common.BurnerException, e:
self.logger.error(e)
except socket.error, e:
self.logger.error(e)
def __findImages(self):
"""Scans self.isodirectory for image files.
The local variable isos is populated."""
self.isos = os.listdir(os.path.expanduser(self.isoDirectory))
self.logger.debug("I can burn the following isos:" + str(self.isos))
def __init__(self, name, isoDirectory, burnCmd, port, serverIP,
serverPort=1234):
"""Initializes the client.
isoDirectory: path to the directory containing the ISO images.
burnCmd: the command to burn an iso named '%s' (could contain spaces)
"""
self.name = name
self.isoDirectory = os.path.expanduser(isoDirectory)
self.port = port
self.serverIP = serverIP
self.serverPort = serverPort
self.logger = logging.getLogger("CustomBurnerClient")
self.logger.info("Starting")
self.__findImages()
self.registerToServer()
self.logger.debug("Starting to listen on port %d" % self.port)
self.tcpServer = TCPServer(("", self.port),
RequestHandler)
self.burnCmd = burnCmd
self.isoToBurn = False
class TCPServer(SocketServer.TCPServer):
"""Our TCP server.
This class just inherits from the SocketServer module.
"""
# We allow reusing an address
allow_reuse_address = True
class RequestHandler(common.RequestHandler):
"""Handles network requests from the server.
Part of the client side protocol is implemented here.
"""
def handle(self):
"""Handle the connection: receive a command from the server."""
global quitting, burner
try:
handshake(self)
data = self.readLine()
if data == common.MSG_CLOSING:
self.logger.info("Server is closing.")
self.request.send(common.MSG_ACK + "\n")
quitting = True
elif data == common.MSG_REQUEST_BURN:
date = self.readLine()
iso = self.readLine()
committer = self.readLine()
if burner.hasIso(iso):
burner.queue(date, iso, committer)
self.request.send(common.MSG_ACK + "\n")
else:
self.request.send(common.MSG_NO_SUCH_ISO + "\n")
else:
raise common.BurnerException, \
"Strange data from server: \"%s\"" % data
except common.BurnerException, e:
self.logger.error(e)
except socket.error, e:
self.logger.error(e)
############
def BurnerMain():
"""Main"""
global burner
# Cmd-line arguments
parser = optparse.OptionParser()
# Default values
parser.set_defaults(name="Toaster",
command="wodim driveropts=burnfree -data %s",
server="127.0.0.1",
directory=".",
port=1235,
serverport=1234)
parser.add_option("-n", "--name", dest="name", help="sets the burner name")
parser.add_option("-d", "--dir", dest="directory",
help="specifies the directory containing the isos")
parser.add_option("-c", "--cmd", dest="command",
help="specifies the command to burn an iso named %s")
parser.add_option("-p", "--port", dest="port", type="int",
help="specifies the TCP port for listening")
parser.add_option("-s", "--server", dest="server",
help="specifies the hostname or IP address of the server")
parser.add_option("-t", "--serverport", dest="serverport", type="int",
help="specifies the server'sTCP port")
parser.add_option("-v", "--verbose", dest="verbosity",
action="count", help="increase verbosity")
(opts, args) = parser.parse_args()
if len(args) > 0:
# We don't want cmdline arguments
parser.print_help()
sys.exit(-1)
# Setup logger
if opts.verbosity == None or opts.verbosity == 0:
loglevel = logging.INFO
else:
loglevel = logging.DEBUG
logging.basicConfig(level=loglevel,
format='%(asctime)s %(name)-18s %(levelname)-8s %(message)s',
datefmt='%d %b %Y %H:%M:%S')
try:
burner = CustomBurnerClient(opts.name, opts.directory, opts.command,
opts.port, opts.server, opts.serverport)
except socket.error, e:
# This may occur during server start
sys.stderr.write("Socket error: %s\n" % str(e))
sys.exit(-1)
try:
burner.live()
except KeyboardInterrupt:
# User killed this application, but the server is still running.
burner.sayGoodbye()