-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmprint
More file actions
executable file
·203 lines (161 loc) · 6.39 KB
/
dmprint
File metadata and controls
executable file
·203 lines (161 loc) · 6.39 KB
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
#!/usr/bin/python
'''
dmprint
A simple script to print document
with dm's printer
'''
import sys, paramiko, select, time, re, os
import getpass
from optparse import OptionParser
# from DrPrintBackend import Backend
if __name__ == "__main__":
## Analyze comand line options
usage = "usage: %prog [options] [file]"
parser = OptionParser(usage=usage)
parser.add_option("-u", "--user", dest="user",default='brocchi',
help = "set username to use for the ssh connection",
metavar="user")
parser.add_option("-P", "--printer", dest="printer",default='cdcpt',
help = "set printer to use to print",
metavar="printer")
# parser.add_option("-q", "--queue", dest="queue",default=None,
# help = "get and list print queue")
(option, args) = parser.parse_args()
try:
filename = args[0]
except:
print ("\t...cosa volevi stampare?")
sys.exit()
print (option)
print ("\t...stampo come %s" % option.user)
remote_host = "ssh.dm.unipi.it"
p = getpass.getpass()
get_queue(option.printer, remote_host, option.user)
def get_queue(printer, remote_host, username, password):
"""
Obtain the queue of jobs on selected printer. It opens an SSH
connection to the server and parse lpq -Pprinter output
"""
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys() # importo le host key dal sistema
except:
raise RuntimeError('Impossibile inizializzare paramiko')
try:
client.connect(remote_host,
port = 22,
username = username,
password = password)
except:
raise RuntimeError('Impossibile connettersi a %s' % remote_host)
stdin, stdout, stderr = client.exec_command("lpq -P%s" % printer)
output = stdout.read()
# Parse output
jobs = []
for line in re.findall(r"active\s+(\w+)\s+(\d+)\s+(.+)\s+(\d+) bytes", output):
job = {
'position': 1,
'user': line[0],
'id': line[1],
'filename': line[2],
'size': line[3],
}
jobs.append (job)
for line in re.findall(r"(\d)\w*\s+(\w+)\s+(\d+)\s+(.+)\s+(\d+) bytes",
output):
pos = int(line[0]) + 1
job = {
'position': pos,
'user': line[1],
'id': line[2],
'filename': line[3].strip(),
'size': line[4]
}
jobs.append(job)
print jobs
# def cancel_transfer(self, widget):
# self.__abort_transfer = True
def send_print(printer, username, password, page_per_page,
filename, page_range, copies, orientation, sides, remote_host):
self.__abort_transfer = False
# Get connection
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
except:
raise PrintingError('Impossibili inizializzare paramiko')
try:
client.connect(remote_host,
port=22,
username=username,
password=password)
except paramiko.AuthenticationException, e:
raise PrintingError('Autenticazione fallita')
except Exception, e:
raise PrintingError('Connessione fallita (%s)' % e)
t = client.get_transport()
sftp = paramiko.SFTPClient.from_transport(t)
# Questo è inevitabile.. :)
cmd = "lpr -P%s " % printer
# Numero di pagine
try:
copies = int(float(copies))
except ValueError:
copies = 1
if copies is not 1:
cmd = cmd + "-# %s " % copies
cmd_opts = ""
## Pagine logiche per pagine
if not page_per_page == 1:
cmd_opts += "-o number-up=%s " % str(page_per_page)
## Da a
if not page_range == None:
cmd_opts += "-o page-ranges=%s " % page_range
## Orientazione (se è vuoto è verticale)
if not orientation == "":
cmd_opts += "-o %s " % orientation
## Long edge, short edge ed amici vari
cmd_opts += "-o sides=%s " % sides
## Se ci sono opzioni dai il -o e specificale
if not cmd_opts == "":
cmd = cmd + "%s" % cmd_opts + " /tmp/drprint_tmp_%s" % username
try:
self.emit('transfer-started')
buf_size = 4096 # 4 Kb
open_file = sftp.file("/tmp/drprint_tmp_%s" % username, "w")
bytes_read = 0
file_size = os.path.getsize(filename)
with open(filename, "r") as local_handle:
buf = None
while(buf != ""):
buf = local_handle.read(buf_size)
bytes_read = bytes_read + len(buf)
if self.__abort_transfer:
return self.__abort_transfer
open_file.write(buf)
self.emit('transfer-progress', bytes_read, file_size)
except OSError:
raise PrintingError('Errore nel trasferimento del file')
else:
pass
self.emit('transfer-finished')
# Apriamo la sessione.
chan = t.open_session()
# Diamo il comando sul canale
chan.exec_command(cmd)
exit_status = chan.recv_exit_status()
chan.close()
if exit_status == 0:
sftp.remove("/tmp/drprint_tmp_%s" % username)
if exit_status != 0:
raise PrintingError('Il comando <b>lpr</b> non e\' andato a buon fine (Exit status = %d)' % exit_status)
return self.__abort_transfer
# ssh = paramiko.SSHClient()
# if file_to_print=="casa":
# ssh.load_system_host_keys() # importo le host key dal sistema
# ssh.connect('192.168.1.95', username=login) # mi collego in locale
# ssh.invoke_shell # apre una shell remota (?)
# stdin, stdout, stderr = ssh.exec_command("ls") # lanciamo un comando di prova
# else:
# print "Mmm..."