-
Notifications
You must be signed in to change notification settings - Fork 1
/
samsungScannerServer.py
executable file
·1531 lines (1320 loc) · 58.3 KB
/
samsungScannerServer.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# samsungScannerServer.py
# Tool to interact with the "scan to PC" option in Samsung MFP like the CLX 3300
#
# Copyright (C) 2022-2023 Steffen Klee
# Copyright (C) 2012-2013 angelnu & Totally King
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = "0.6.1"
import atexit
import datetime
import errno # t-k: needed for error handling in TCP proxy
import io
import logging
import logging.handlers
import multiprocessing # t-k: need subprocesses for TCP and UDP proxy
import os
import os.path
import platform
import pwd # t-k: for automatically configured OUTPUT_PREFIX and OWNER(_UID)
import queue
import re
import signal # t-k: for correct handling of SIGTERM and so on (which atexit can't handle)
import socket # t-k: needed for TCP and UDP proxy to interfere with scanner commands needed for multipage
import sys
import time
import traceback
import xml.etree.ElementTree as ET
from optparse import OptionGroup, OptionParser
from string import Template
from urllib import request
import sane
from PIL import Image
from pypdf import PdfReader, PdfWriter
from six.moves import http_client
"""
Summary of messages exchanged in order to scan
register server: server -> scanner (HTTP POST) with:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<S2PC_Regi UserID="Server-XP" UniqueID="ac16b1c1824380e7" RegiType="ADD" />
</root>
scanner answer:
<?xml version="1.0" encoding="UTF-8"?><root><S2PC_Regi UserID ="Server-XP" Result="ADD_OK" InstanceID="27" /></root>
query SNMP 1,3,6,1,4,1,236,11,5,11,81,11,7,2,1,2,<InstanceID> until is we get "1" in the first byte (user selected the
server to scan).
Samsung Windows driver does this every 1/2 second
send configuration options to scan: sever -> scanner (HTTP Post) with:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<S2PC_AppList>
<List>
<AppIndex Value="1" />
<AppName Value="My Documents" />
<AppType Value="MAC" />
<Resolution Value="DPI_300" />
<Color Value="COLOR_GRAY" />
<FileFormat Value="FORMAT_M_PDF" />
<ScanSize Value="SIZE_A4" />
<DuplexScan Value="DUPLEX_OFF" />
<Orientation Value="ORIENTATION_SIDEWAY" />
</List>
</S2PC_AppList>
</root>
scanner answer:
closes connection
query SNMP 1,3,6,1,4,1,236,11,5,11,81,11,7,2,1,2,<InstanceID> until is we get "2" in the first byte (user has selected
scan options based on the offered template).
start a scan using SANE
register server again
if we want to unregister the server: sever -> scanner (HTTP Post) with:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<S2PC_Regi UserID="Server-XP" UniqueID="ac16b1c1824380e7" RegiType="DELETE" />
</root>
"""
# ############################# FUNCTIONS ###############################
# HTTP Post functions
def post_multipart(host, selector, fields, files, exact_response=True):
"""
Post fields and files to an http host as multipart/form-data.
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be uploaded as files
Return the server's response page.
"""
# t-k: print a better error message to the log
try:
content_type, body = encode_multipart_formdata(fields, files)
h = http_client.HTTPConnection(host)
# h.set_debuglevel(1)
h.putrequest('POST', selector)
h.putheader('content-type', content_type)
h.putheader('content-length', str(len(body)))
h.endheaders()
h.send(body)
if exact_response:
response = h.getresponse()
return response.read()
else:
return None
except Exception as e:
raise Exception('Problem contacting Scanner over network: %s' % e)
def encode_multipart_formdata(fields, files):
"""
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be uploaded as files
Return (content_type, body) ready for httplib.HTTP instance
"""
boundary = b'----------ThIs_Is_tHe_bouNdaRY_$'
crlf = b'\r\n'
stream = []
for (key, value) in fields:
stream.append(b'--' + boundary)
stream.append(b'Content-Disposition: form-data; name="%d"' % key)
stream.append(b'')
if type(value) != bytes:
value = value.encode("utf-8")
stream.append(value)
for (key, filename, value) in files:
stream.append(b'--' + boundary)
stream.append(b'Content-Disposition: form-data; name="%d"; filename="%b"' % (key, filename.encode("utf-8")))
stream.append(b'Content-Type: application/octet-stream')
stream.append(b'')
if type(value) != bytes:
value = value.encode("utf-8")
stream.append(value)
stream.append(b'--' + boundary + b'--')
stream.append(b'')
body = crlf.join(stream)
content_type = b'multipart/form-data; boundary=%b' % boundary
return content_type, body
def server_register(printing=True):
msg = '<?xml version="1.0" encoding="UTF-8" ?>'
msg += '<root>'
msg += '<S2PC_Regi UserID="' + SERVER_NAME + '" UniqueID="' + SERVER_UID + '" RegiType="ADD" />'
msg += '</root>'
result = str(post_multipart(SCANNER_IP, '/IDS/ScanFaxToPC.cgi', [], [(1, "c:\\IDS.XML", msg)]))
# print result
# <?xml version="1.0" encoding="UTF-8"?><root><S2PC_Regi UserID ="W510" Result="ADD_OK" InstanceID="29" /></root>
m = re.match(r'.*Result="ADD_OK" InstanceID="(\d+)"', result)
if not m:
raise NameError("Error registering server: " + result)
else:
if printing:
print("Newly registered server '%(SERVER_NAME)s' with UniqueID '%(SERVER_UID)s' has got" % globals())
print(" InstanceID '" + m.group(1) + "'.") # t-k: better readability and understanding
return int(m.group(1))
# t-k: restructered function to be real refresh
def server_refresh():
global SERVER_INSTANCE_ID
old_instance_id = SERVER_INSTANCE_ID
SERVER_INSTANCE_ID = server_register(printing=False)
if SERVER_INSTANCE_ID != old_instance_id:
print("Refreshed server '%(SERVER_NAME)s' with UniqueID '%(SERVER_UID)s' has got" % globals())
print(" new InstanceID '" + str(SERVER_INSTANCE_ID) + "'.")
return SERVER_INSTANCE_ID
# t-k: new function = easier to understand
def server_unregister():
unique_id = SERVER_UID
msg = '<?xml version="1.0" encoding="UTF-8" ?>'
msg += '<root>'
msg += '<S2PC_Regi UserID="' + SERVER_NAME + '" UniqueID="' + unique_id + '" RegiType="DELETE" />'
msg += '</root>'
result = post_multipart(SCANNER_IP, '/IDS/ScanFaxToPC.cgi', [], [(1, "c:\\IDS.XML", msg)])
# print result
# <?xml version="1.0" encoding="UTF-8"?>
# <root><S2PC_Regi UserID ="server" Result="DELETE_OK" InstanceID="140" /></root>
m = re.match(b'.*Result="DELETE_OK"', result)
if not m:
raise NameError("Error unregistering server: " + result)
else:
print("Unregistered server '%(SERVER_NAME)s' with UniqueID '%(SERVER_UID)s'." % globals())
def push_server_options():
"""<?xml version="1.0" encoding="UTF-8" ?>
<root>
<S2PC_AppList>
<List>
<AppIndex Value="1" />
<AppName Value="Gray default" />
<AppType Value="MAC" />
<Resolution Value="DPI_300" />
<Color Value="COLOR_GRAY" />
<FileFormat Value="FORMAT_M_PDF" />
<ScanSize Value="SIZE_A4" />
<DuplexScan Value="DUPLEX_OFF" />
<Orientation Value="ORIENTATION_SIDEWAY" />
</List>
</S2PC_AppList>
</root>"""
root = ET.Element('root')
app_list = ET.SubElement(root, 'S2PC_AppList')
index = 0
for option in OPTIONS:
index += 1
list_element = ET.SubElement(app_list, 'List')
ET.SubElement(list_element, 'AppIndex').attrib['Value'] = str(index)
ET.SubElement(list_element, 'AppName').attrib['Value'] = option["name"]
ET.SubElement(list_element, 'AppType').attrib['Value'] = 'MAC'
ET.SubElement(list_element, 'Resolution').attrib['Value'] = option["resolution"]
ET.SubElement(list_element, 'Color').attrib['Value'] = option["color"]
ET.SubElement(list_element, 'FileFormat').attrib['Value'] = option["format"]
ET.SubElement(list_element, 'ScanSize').attrib['Value'] = option["size"]
ET.SubElement(list_element, 'DuplexScan').attrib['Value'] = "DUPLEX_OFF"
ET.SubElement(list_element, 'Orientation').attrib['Value'] = "ORIENTATION_SIDEWAY"
msg = b'<?xml version="1.0" encoding="UTF-8" ?>\r\n' + ET.tostring(root)
# msg=ET.tostring(root, encoding="UTF-8")
post_multipart(SCANNER_IP, '/IDS/ScanFaxToPC.cgi', [], [(1, "scantopc", msg)], False)
def query_user_options():
result = post_multipart(SCANNER_IP, '/IDS/UserSelect.xml', [], [(1, "scantopc", "")])
# {'name':'Gray-S_PDF-75','color':'GRAY','resolution':'75','format':'S_PDF','size','a4'}
# result='<?xml version="1.0" encoding="UTF-8"?><root><S2PC_Select><AppIndex Value="1"/>
# <Resolution Value="DPI_300"/><Color Value="COLOR_GRAY"/><FileFormat Value="FORMAT_M_PDF"/>
# <ScanSize Value="SIZE_A4"/></S2PC_Select></root>'
# print result
root = ET.fromstring(result).find('S2PC_Select')
index = root.find('AppIndex').attrib["Value"]
user_options = OPTIONS[int(index) - 1] # t-k: added '-1'
user_options['color'] = root.find('Color').attrib["Value"]
user_options['resolution'] = root.find('Resolution').attrib["Value"]
user_options['format'] = root.find('FileFormat').attrib["Value"]
user_options['size'] = root.find('ScanSize').attrib["Value"]
return user_options
# SNMP queries
def query_snmp_variable(ip, oid):
from pysnmp.entity.rfc3413.oneliner import cmdgen
error_indication, error_status, error_index, var_binds = cmdgen.CommandGenerator().getCmd(
cmdgen.CommunityData('my-agent', 'public', 0),
cmdgen.UdpTransportTarget((ip, 161)),
oid)
return_value = None
if error_indication:
raise NameError('Error indication in SNMP query: %s' % error_indication) # t-k: %s to avoid TypeError
elif error_status:
raise NameError('Error status in SNMP query: %s' % error_status) # t-k: %s to avoid TypeError
else:
return_value = var_binds
return return_value
def query_printer_scan_status(instance_id):
# t-k: more descriptive Error handling and logging
try:
result = query_snmp_variable(SCANNER_IP, (1, 3, 6, 1, 4, 1, 236, 11, 5, 11, 81, 11, 7, 2, 1, 2, instance_id))
# (ObjectName('1.3.6.1.4.1.236.11.5.11.81.11.7.2.1.2.29'), OctetString('\x00\x00\x00\x00'))
return result[0][1][0]
except Exception as e:
if 'result' not in locals():
result = None
raise Exception(("Could not query printer scan status.\n" + ' ' * 4 +
"Result was '%(result)s'.\n" + ' ' * 4 +
"Error message: %(e)s.") % locals())
# Function for a single scan task
def scann_worker():
server_refresh()
# t-k: a little more descriptive logging
print("Waiting for scan job ...")
# print ' '*4 + 'printer scan status: ' + str(query_printer_scan_status(SERVER_INSTANCE_ID)) + ' -- waiting for 1
# ...'
i = 0
while query_printer_scan_status(SERVER_INSTANCE_ID) != 1:
i += 1
if i % 300 == 0: # t-k: refresh every > 5 mins (server get's auto. unregistered after ~30 mins)
server_refresh()
time.sleep(1)
print(' ' * 4 + 'Got it!')
push_server_options()
# t-k: a little more descriptive logging
print("Waiting for user selection ...")
# print ' '*4 + 'printer scan status: ' + str(query_printer_scan_status(SERVER_INSTANCE_ID)) + ' -- waiting for 2
# ...'
# t-k: may be canceled by user: check if status changes back to 1
i = 0
while True:
pps = query_printer_scan_status(SERVER_INSTANCE_ID)
if pps == 2:
break
elif pps == 1:
push_server_options()
print('Reconnected, waiting for user selection ...')
# print ' '*4 + 'printer scan status: ' + str(query_printer_scan_status(SERVER_INSTANCE_ID)) + ' --
# waiting for 2 ...'
continue
i += 1
if i % 300 == 0:
server_refresh()
time.sleep(1)
print(' ' * 4 + 'Got it!')
user_selection = query_user_options()
print('Options selected by user:', user_selection)
scan_and_save(user_selection)
# t-k: method to automatically determine translation from scanner command (received by server) to sane command
# was written for sizes but may be adapted to other translations
def autoconfig_dic(dic_name, xml_key, preferred):
if dic_name not in globals():
try:
# t-k: get available options from XML file that may be received by server
capxmlfile = request.urlopen('http://%s/IDS/CAP.XML' % REAL_SCANNER_IP)
capxmldata = capxmlfile.read()
capxmlfile.close()
xmlroot = ET.fromstring(capxmldata)
sizes = []
for size in xmlroot.iter(xml_key):
sizes.append(size.attrib['ID'])
# t-k: get available size options for SANE device
sane_sizes = saneSingleton["page_format"].constraint
# t-k: match these two sets together and save as dic_name (e.g. SIZE2SANE)
dic = {}
for sizeID in sizes:
size_lst = sizeID.split('_')[1:] # t-k: may be > 1 part, e.g. ['B5', 'JIS']
candidates = []
for saneSize in sane_sizes:
flag = True
for sizeElm in size_lst:
flag = flag and sizeElm.lower() in saneSize.lower()
# t-k: save combination if all parts match sane size and
# rotated takes precedence over non-rotated
if flag and (sizeID not in dic or preferred in saneSize.lower()):
dic[sizeID] = saneSize
if dic == {}:
raise ValueError('%(dic_name)s dictionary must not be empty!' % locals())
globals()[dic_name] = dic
print_autoconfig(dic, dic_name, no_quotes=True)
except Exception as e:
print('Error while trying to configure scanning options:', file=sys.stderr)
print(' %s: %s' % (type(e).__name__, e), file=sys.stderr)
print("You should manually configure %s in '%s'." % (dic_name, CONFIG_FILE), file=sys.stderr)
sys.exit(1)
# angelnu: my scanner takes very long to find -> cache
saneSingleton = None
def get_sane_instance():
global saneSingleton
if saneSingleton:
return saneSingleton
else:
print("Init SANE ...")
sane.init()
print("Connecting to scanner ...")
while True:
try:
# t-k: use modified open method to use modified sane classes
if MODIFIED_SANE:
saneSingleton = modsaneopen(SCANNER_SANE_NAME)
else:
saneSingleton = sane.open(SCANNER_SANE_NAME)
except Exception as e:
if MODIFIED_SANE and e.message.startswith('no such scan device'):
print("Proxy scan 'device' not found, restarting proxies and trying again ...", file=sys.stderr)
# t-k: restart proxies
exit_proxies()
start_proxies()
else:
print('Problem connecting to scanner, trying again in 10s ...', file=sys.stderr)
traceback.print_exc(file=sys.stderr)
time.sleep(10)
else:
# t-k: if SIZE2SANE / ... haven't been given in config file, try to automatically configure them
# t-k: f.l.t.r. -> name of dict, xml key, preferred sane option
autoconfig_dic('SIZE2SANE', 'Size', 'rotated')
break
print("Connected to scanner.")
return saneSingleton
def scan_and_save(user_selection, imgs=None):
global saneSingleton # t-k: if cache needs to be reset
# t-k: to raise file index independent of file extension
# so no more same file names with different extensions
def exists_file_with_other_extension(base_filename):
from glob import glob
search_pattern = base_filename + '.*'
return bool(glob(search_pattern))
# t-k: change ownership of scan file
def chown_file(filename):
if 'OWNER_UID' in globals():
uid = int(OWNER_UID)
gid = pwd.getpwuid(uid).pw_gid
os.chown(filename, uid, gid)
# print 'Device options: ', s.get_options()
# print 'Device parameters:', s.get_parameters()
mode = MODES2SANE[user_selection["color"]]
print("MODE: " + mode)
dpi = int(user_selection["resolution"].replace('DPI_', ''))
print("DPI: " + str(dpi))
size = SIZE2SANE[user_selection["size"]]
print("SIZE: " + size)
# Initialize scan
def init_scan():
print("Scanning ...")
s = get_sane_instance()
s.mode = mode
s.resolution = dpi
s.page_format = size # t-k: bugfix page_format is correct (not page-format)
imgs = s.multi_scan()
return imgs, s
if not imgs:
imgs, s = init_scan()
# Process images
output_files = []
index = 1
date = datetime.datetime.now().strftime("%Y-%m-%d")
while True:
try:
for im in imgs:
file_exists = True
while file_exists:
base_filename = Template(user_selection["output"])\
.safe_substitute(date=date, uid="%02d" % index, # t-k: index formatted with padding zero
homedir=HOME_DIR) # t-k: automatically detect home dir ('~')
filename = base_filename + '.' + EXTENSIONS[user_selection["format"]] # t-k: seperate base_filename
file_exists = exists_file_with_other_extension(
base_filename) # t-k: raise index independent of file extension
index += 1
# t-k: rotate image if necessary
if re.match('.*rotate', size, re.IGNORECASE):
im = im.rotate(270)
# t-k: print log of applying user filters only if there are any
if len(user_selection['filters']):
print("Applying user filters to " + filename + " ...")
for userFilter in user_selection['filters']:
im = userFilter(im) # t-k: replaced img with im
print("Saving " + filename + " ...")
im.info['dpi'] = (dpi, dpi)
im.info['resolution'] = (dpi, dpi)
im.save(filename, dpi=(dpi, dpi), resolution=dpi)
chown_file(filename) # t-k: change ownership of scan file
print("Done.")
output_files.append(filename)
except Exception as e:
if e == 'Error during device I/O':
if MODIFIED_SANE:
print('SANE ' + e + '. Restarting proxies and retrying ...', file=sys.stderr)
exit_proxies()
start_proxies()
else:
print('SANE ' + e + '. Retrying ...', file=sys.stderr)
# s.close() # <- this causes seg fault
saneSingleton = None
imgs, s = init_scan()
else:
print('Whoops! Problem scanning (maybe version Samsung device driver >= 4.1 and multi-scan?):',
file=sys.stderr)
traceback.print_exc(file=sys.stderr)
break
else:
break
# t-k: if more than one server is used on the same scanner
# does not yet work, since closing the session raises a seg fault
# maybe poor programming of the C based sane extension?
# (not incrementing reference count when new references to scanner object are created?)
if not SCANNER_CACHING:
# t-k: end session with scanner and reset cache
# s.close() # <- this causes seg fault
saneSingleton = None
# print('Scanner session closed and cache reset.')
print('Scanner cache reset.')
# Concat PDFs and delete temp ones
if (user_selection["format"] == "FORMAT_M_PDF" or user_selection["format"] == "FORMAT_PDF") and (
len(output_files) > 1):
print("Concatenating PDF files ...")
output = PdfWriter()
for aFile in output_files:
input_pdf = PdfReader(file(aFile, "rb"))
output.addPage(input_pdf.getPage(0))
output_stream = io.BytesIO() # file(output_files[0], "wb")
output.write(output_stream)
# output_stream.close()
for aFile in output_files:
print("Deleting " + aFile + " ...")
os.remove(aFile)
print("Writing final PDF " + output_files[0] + " ...")
output_file = open(output_files[0], "wb")
output_file.write(output_stream.getvalue())
output_file.close()
chown_file(output_files[0]) # t-k: change ownership of scan file
print("Done.")
def del_pid_file():
# t-k: delete PID only if it exists and if not caught signal SIGQUIT (3, with Strg+\)
if CAUGHT_SIGQUIT:
print('Did not remove PID file: ' + options.pidfile + '\n' + ' ' * 4 +
'because of the SIGQUIT signal caught.')
else:
if os.path.exists(options.pidfile):
os.remove(options.pidfile)
print('Removed PID file: ' + options.pidfile)
else:
print('Could not remove PID file: ' + options.pidfile + '\n' + ' ' * 4 +
"because it was already deleted (probably by 'sudo service samsungScannerServer stop').")
def server_uid_gen():
"""
generate a UniqueID for this server based on SERVER_NAME and hostname using md5 as hash method
"""
from hashlib import md5
servername = SERVER_NAME
hostname = platform.node()
def hash2half_length2int(hash_string):
"""
convert hash string to half its length
watch out: returns int (not str)
"""
half_length = int(len(hash_string) / 2)
part1 = hash_string[:half_length]
part2 = hash_string[half_length:]
half_hash_int = int(part1, 16) + int(part2, 16)
# restrict to max. 16 (hex) characters
half_hash_int %= (256 ** 8)
return half_hash_int
# use md5 as hash method -> length 32
server_hash = md5(servername.encode('utf-8')).hexdigest()
host_hash = md5(hostname.encode('utf-8')).hexdigest()
server_hash_half_int = hash2half_length2int(server_hash)
host_hash_half_int = hash2half_length2int(host_hash)
server_uid_int = server_hash_half_int + host_hash_half_int
# restrict to max. 16 (hex) characters
server_uid_int %= (256 ** 8)
# convert to hex
return hex(server_uid_int).replace("0x", "").replace("L", "")
# ########################## SIGNAL HANDLING ############################
# t-k: handle some signals to trigger normal exit (and atexit then triggers its own stuff (delPID, server_unregister))
def sig_handler(signum, stack=None):
sig = convSignum2Sig[signum]
if sig in ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGTERM']:
exit_code = convSig2exitCode.get(sig, 1)
print("Caught signal %d '%s', exiting with code %d ..." % (signum, sig, exit_code))
if sig == 'SIGQUIT':
global CAUGHT_SIGQUIT
CAUGHT_SIGQUIT = True
sys.exit(exit_code)
# for other signals
else:
pass
# t-k: set up dictionary to convert from signal number (e.g. 15) to signal (e.g. 'SIGTERM')
convSignum2Sig = {}
# t-k: set up dictionary to convert from signal (e.g. 'SIGTERM') to proper exit code (e.g. 143)
convSig2exitCode = {
'SIGINT': 1,
'SIGQUIT': 131,
'SIGTERM': 143,
'SIGHUP': 129,
}
# t-k: handle SIG... signals correctly (like SIGTERM)
# thus also handles 'sudo service samsungScannerServer stop'
# ~ for i in [x for x in dir(signal) if x.startswith("SIG") and not x.startswith('SIG_')]:
for i in ['SIGINT', 'SIGQUIT', 'SIGTERM', 'SIGHUP']:
try:
signum = getattr(signal, i)
signal.signal(signum, sig_handler)
convSignum2Sig[signum] = i
except RuntimeError as m:
pass # t-k: do not consider signals like SIGKILL, which cannot be handled (by definition)
# t-k: keep track of a caught SIGQUIT, so temp. files (PID file) will not be removed
CAUGHT_SIGQUIT = False
# ################### OPTIONS AND CONFIGURATION FILE ####################
# Parse options
parser = OptionParser(usage="usage: %prog [options]",
version="%prog " + __version__)
parser.add_option("-d", "--daemon", action='store_true', dest="daemon",
help="Fork a daemon")
parser.add_option("-p", "--pidfile", dest="pidfile",
help="File to write the daemon PID")
group = OptionGroup(parser, "Debug Options",
"Caution: use these options at your own risk. "
"These options are expected to be only for debugging. ")
group.add_option("--imageFiles", action="append", dest="imageFiles",
help="Image files to process instead of scanning. When this option is used the program " +
"will apply the selected filters, store the result and terminate.")
group.add_option("--optionsIndex", type="int", dest="optionsIndex", default=0,
help="What of the OPTIONS[] to use for processing the --imageFiles.")
parser.add_option_group(group)
(options, args) = parser.parse_args()
if len(args) != 0:
parser.error("incorrect number of arguments")
# Read configuration
HOME_DIR=os.getenv("HOME", os.path.expanduser("~"))
XDG_CONFIG_HOME=os.getenv("XDG_CONFIG_HOME", os.path.join(HOME_DIR, ".config"))
PATHS = ['.', XDG_CONFIG_HOME, '/etc']
CONFIG_FILENAME = 'samsungScannerServer.conf'
CONFIG_FILE = None
for path in PATHS:
try:
filePath = path + '/' + CONFIG_FILENAME
exec(compile(open(filePath).read(), filePath, 'exec'))
CONFIG_FILE = filePath
break
except IOError:
""
if not CONFIG_FILE:
print("Could not find config file (" + CONFIG_FILENAME + ") in " + str(PATHS), file=sys.stderr)
sys.exit(1)
# ############################## LOGGING ################################
class LogFile(object):
def __init__(self, name=None):
self.logger = logging.getLogger(name)
self.buffer = ""
# StringIO.StringIO()
def write(self, msg, level=logging.INFO):
self.buffer += msg
lines = self.buffer.splitlines()
# print>>sys.stderr,lines
if self.buffer.count('\n') == len(lines):
self.buffer = ""
else:
# Last line was not \n terminated
self.buffer = lines.pop()
for line in lines:
self.logger.log(level, line)
def flush(self):
for handler in self.logger.handlers:
handler.flush()
class FilterEmptyLines(logging.Filter):
def filter(self, record):
return len(record.msg) != 0
# t-k: classes that handle logging from multiple processes
# (supporting rotating log file)
class QueueHandler(logging.Handler):
"""
This handler sends events to a queue. Typically, it would be used together
with a multiprocessing Queue to centralise logging to file in one process
(in a multi-process application), so as to avoid file write contention
between processes.
This code is new in Python 3.2, but this class can be copy pasted into
user code for use with earlier Python versions.
"""
def __init__(self, queue):
"""
Initialise an instance, using the passed queue.
"""
logging.Handler.__init__(self)
self.queue = queue
def enqueue(self, record):
"""
Enqueue a record.
The base implementation uses put_nowait. You may want to override
this method if you want to use blocking, timeouts or custom queue
implementations.
"""
self.queue.put_nowait(record)
def prepare(self, record):
"""
Prepares a record for queuing. The object returned by this method is
enqueued.
The base implementation formats the record to merge the message
and arguments, and removes unpickleable items from the record
in-place.
You might want to override this method if you want to convert
the record to a dict or JSON string, or send a modified copy
of the record while leaving the original intact.
"""
# The format operation gets traceback text into record.exc_text
# (if there's exception data), and also puts the message into
# record.message. We can then use this to replace the original
# msg + args, as these might be unpickleable. We also zap the
# exc_info attribute, as it's no longer needed and, if not None,
# will typically not be pickleable.
self.format(record)
record.msg = record.message
record.args = None
record.exc_info = None
return record
def emit(self, record):
"""
Emit a record.
Writes the LogRecord to the queue, preparing it for pickling first.
"""
try:
self.enqueue(self.prepare(record))
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
def listener_configurer():
if not options.daemon:
logging.basicConfig(level=logging.INFO)
else:
logging.basicConfig(level=logging.INFO, filename='/dev/null')
if LOG_NAME:
root = logging.getLogger()
h = logging.handlers.RotatingFileHandler(filename=LOG_NAME, maxBytes=LOG_MAXBYTES, backupCount=LOG_BACKUPCOUNT)
f = logging.Formatter(fmt='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d-%y %H:%M:%S')
fil = FilterEmptyLines()
h.setFormatter(f)
h.addFilter(fil)
root.addHandler(h)
# This is the listener process top-level loop: wait for logging events
# (LogRecords)on the queue and handle them, quit when you get a None for a
# LogRecord.
def listener_process(queue, configurer):
configurer()
while True:
try:
record = queue.get()
if record is None: # We send this as a sentinel to tell the listener to quit.
break
logger = logging.getLogger(record.name)
logger.handle(record) # No level or filter logic applied - just do it!
except (KeyboardInterrupt, SystemExit):
# raise
pass # handled by signal and atexit
except:
import sys
import traceback
print('Whoops! Problem:', file=sys.stderr)
traceback.print_exc(file=sys.stderr)
def worker_configurer(queue):
h = QueueHandler(queue) # Just the one handler needed
root = logging.getLogger()
root.addHandler(h)
root.setLevel(logging.INFO)
if __name__ == '__main__':
# Daemon mode
if options.daemon:
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError as e:
logging.exception("Error forking daemon: %s" % e)
sys.exit(1)
# t-k: Logging supporting multiprocessing
logQ = multiprocessing.Queue()
listener = multiprocessing.Process(target=listener_process,
args=(logQ, listener_configurer))
listener.start()
worker_configurer(logQ)
sys.stdout = LogFile('stdout')
sys.stderr = LogFile('stderr')
def exit_listener():
logQ.put_nowait(None)
# Print version
print("###########################")
print("# Initiating version " + __version__)
print("###########################")
print('At program termination joining log listener process with:\n' + ' ' * 4 +
str(atexit.register(exit_listener)))
# Logging configuration file
print("Used '%s' as configuration file." % CONFIG_FILE)
print('Below is what was configured with it.')
f = open(CONFIG_FILE)
for line in f:
# t-k: do not print empty lines to logfile
if line.strip() == '':
continue
# t-k: do not print lines that are commented out to logfile
noindentLine = line.lstrip()
if noindentLine[0] == '#':
continue
# t-k: remove remaining comments from line
if '#' in line:
line = line.split('#')[0] + '\n'
sys.stdout.write("CONFIG: " + line)
f.close()
# Debug mode
if options.imageFiles:
print("Running in debug mode!")
HOME_DIR = "/tmp/"
imgs = []
for imageFile in options.imageFiles:
imgs.append(Image.open(imageFile))
scan_and_save(OPTIONS[options.optionsIndex], imgs)
sys.exit(0)
# Daemon mode
if options.daemon and options.pidfile:
print("Write PID to file: " + options.pidfile)
print('At program termination removing PID file (if it still exists and not caught SIGQUIT) with:\n' +
' ' * 4 + str(atexit.register(del_pid_file)))
pid = str(os.getpid())
file(options.pidfile, 'w+').write("%s\n" % pid)
# ######################### AUTO CONFIGURATION ##########################
# t-k: some automatic configuration providing default values
# which takes place if values were not given in conf file
print('The following was automatically configured.')
# t-k: Automatic configuration -> Log
def print_autoconfig(variable, variable_name, no_quotes=False):
"""
add automatically configured VARIABLE to a list that is later printed to log
"""
if no_quotes:
quote = ""
else:
quote = "'"
print("AUTOCONFIG: %(variable_name)s = %(quote)s%(variable)s%(quote)s" % locals())
# t-k: function to extract valid IPv4s
def extractIPs(file_content):
re_ip = r"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([ (\[]?(\.|dot)[ )\]]?(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})"
ips = [each[0] for each in re.findall(re_ip, file_content)]
# print(ips)
for item in ips:
location = ips.index(item)
ip = re.sub(r"[ ()\[\]]", "", item)
ip = re.sub("dot", ".", ip)
ips.remove(item)
ips.insert(location, ip)
return ips
# t-k: Get scanner name automatically, try again if nothing found (e.g. no network connection)
if 'SCANNER_SANE_NAME' not in globals():
while True:
print("Init SANE ...")
sane.init() # t-k: bugfix, can't find any devs without init
devs = sane.get_devices()
for dev in devs:
if dev[1].upper() == 'SAMSUNG':
SCANNER_SANE_NAME = dev[0]
print_autoconfig(SCANNER_SANE_NAME, 'SCANNER_SANE_NAME')
break
if 'SCANNER_SANE_NAME' not in globals():
if devs:
tmpinsert = ' SAMSUNG'
else:
tmpinsert = ''
sys.stderr.write('No%s Scanner found. Trying again in 30s.\n' % tmpinsert)
time.sleep(30)
else:
break
if 'SERVER_NAME' not in globals():
SERVER_NAME = platform.node() # t-k: = hostname
print_autoconfig(SERVER_NAME, 'SERVER_NAME')
if 'OWNER_UID' in globals():
if 'OWNER' in globals():
pass
else:
OWNER = pwd.getpwuid(OWNER_UID).pw_name
print_autoconfig(OWNER, 'OWNER')
else:
if 'OWNER' in globals():
OWNER_UID = pwd.getpwnam(OWNER).pw_uid
print_autoconfig(OWNER_UID, 'OWNER_UID')
else:
OWNER_UID = 1000 # t-k: first ubuntu user as default
print_autoconfig(OWNER_UID, 'OWNER_UID')
OWNER = pwd.getpwuid(OWNER_UID).pw_name
print_autoconfig(OWNER, 'OWNER')
# t-k: always automatically retrieve home dir and extract IP
HOME_DIR = pwd.getpwuid(OWNER_UID).pw_dir
print_autoconfig(HOME_DIR, 'HOME_DIR')
# t-k: updated IP extraction method (thanks to frankentux)
try:
SCANNER_IP = extractIPs(SCANNER_SANE_NAME)[0]
except IndexError: # regex failed?
print("Couldn't recognize IPv4 of scanner '%s'." % SCANNER_SANE_NAME, file=sys.stderr)
sys.exit(1)
print_autoconfig(SCANNER_IP, 'SCANNER_IP')
REAL_SCANNER_IP = SCANNER_IP # t-k: preserve IP if changed by MODIFIED_SANE method
# t-k: get own server IP and change scanner name to include that