~mimose/+junk/hplip-3.16.11

« back to all changes in this revision

Viewing changes to .pc/hpfax-bug-function-used-before-importing-log.patch/fax/backend/hpfax.py

  • Committer: guoyalong
  • Date: 2017-09-21 00:44:54 UTC
  • Revision ID: guoyalong@kylinos.cn-20170921004454-3uje9gvw40x0wurj
Retrieve .pc directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# (c) Copyright 2003-2015 HP Development Company, L.P.
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 
19
#
 
20
# Author: Don Welch
 
21
#
 
22
 
 
23
__version__ = '4.1'
 
24
__title__ = 'CUPS Fax Backend (hpfax:)'
 
25
__doc__ = "CUPS backend for PC send fax. Generally this backend is run by CUPS, not directly by a user. To send a fax as a user, run hp-sendfax or print to the device's CUPS fax queue."
 
26
 
 
27
# StdLib
 
28
import sys
 
29
import getopt
 
30
import os.path, os
 
31
import syslog
 
32
import time
 
33
import operator
 
34
import tempfile
 
35
 
 
36
if sys.version_info[0] == 3:
 
37
    import configparser
 
38
else:
 
39
    import ConfigParser as configparser
 
40
 
 
41
CUPS_BACKEND_OK = 0 # Job completed successfully
 
42
CUPS_BACKEND_FAILED = 1 # Job failed, use error-policy
 
43
CUPS_BACKEND_AUTH_REQUIRED = 2 # Job failed, authentication required
 
44
CUPS_BACKEND_HOLD = 3 # Job failed, hold job
 
45
CUPS_BACKEND_STOP = 4 #  Job failed, stop queue
 
46
CUPS_BACKEND_CANCEL = 5 # Job failed, cancel job
 
47
 
 
48
PIPE_BUF = 4096
 
49
 
 
50
job_id = 0
 
51
pid = os.getpid()
 
52
config_file = '/etc/hp/hplip.conf'
 
53
home_dir = ''
 
54
 
 
55
 
 
56
def bug(msg):
 
57
    syslog.syslog("hpfax[%d]: error: %s\n" % (pid, msg))
 
58
    log.stderr("ERROR: %s\n" % msg)
 
59
 
 
60
 
 
61
if os.path.exists(config_file):
 
62
    config = configparser.ConfigParser()
 
63
    config.read(config_file)
 
64
 
 
65
    try:
 
66
        home_dir = config.get('dirs', 'home')
 
67
    except:
 
68
        bug("Error setting home directory: home= under [dirs] not found.")
 
69
        sys.exit(1)
 
70
else:
 
71
    bug("Error setting home directory: /etc/hp/hplip.conf not found")
 
72
    sys.exit(1)
 
73
 
 
74
if not home_dir or not os.path.exists(home_dir):
 
75
    bug("Error setting home directory: Home directory %s not found." % home_dir)
 
76
    sys.exit(1)
 
77
 
 
78
sys.path.insert(0, home_dir)
 
79
os.chdir(home_dir)
 
80
 
 
81
# HPLIP
 
82
try:
 
83
    from base.g import *
 
84
    from base.codes import *
 
85
    from base import device
 
86
    from base import utils
 
87
    from prnt import cups
 
88
except ImportError as e:
 
89
    bug("Error importing HPLIP modules: %s\n" % (pid, e))
 
90
    sys.exit(1)
 
91
 
 
92
def handle_sigpipe():
 
93
    syslog.syslog("SIGPIPE!")
 
94
 
 
95
 
 
96
USAGE = [(__doc__, "", "para", True),
 
97
         ("Usage: hpfax [job_id] [username] [title] [copies] [options]", "", "summary", True),
 
98
         utils.USAGE_OPTIONS,
 
99
         utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
 
100
         utils.USAGE_HELP,
 
101
        ]
 
102
 
 
103
def usage(typ='text'):
 
104
    if typ == 'text':
 
105
        utils.log_title(__title__, __version__)
 
106
 
 
107
    utils.format_text(USAGE, typ, title=__title__, crumb='hpfax:')
 
108
    sys.exit(CUPS_BACKEND_OK)
 
109
 
 
110
# Send dbus event to hpssd on dbus system bus
 
111
def send_message(device_uri, printer_name, event_code, username, job_id, title, pipe_name=''):
 
112
    args = [device_uri, printer_name, event_code, username, job_id, title, pipe_name]
 
113
    msg = lowlevel.SignalMessage('/', 'com.hplip.StatusService', 'Event')
 
114
    msg.append(signature='ssisiss', *args)
 
115
 
 
116
    SystemBus().send_message(msg)
 
117
 
 
118
 
 
119
try:
 
120
    opts, args = getopt.getopt(sys.argv[1:], 'l:hg', ['level=', 'help', 'help-rest', 'help-man'])
 
121
 
 
122
except getopt.GetoptError:
 
123
    usage()
 
124
 
 
125
for o, a in opts:
 
126
 
 
127
    if o in ('-l', '--logging'):
 
128
        log_level = a.lower().strip()
 
129
        log.set_level(log_level)
 
130
 
 
131
    elif o == '-g':
 
132
        log.set_level('debug')
 
133
 
 
134
    elif o in ('-h', '--help'):
 
135
        usage()
 
136
 
 
137
    elif o == '--help-rest':
 
138
        usage('rest')
 
139
 
 
140
    elif o == '--help-man':
 
141
        usage('man')
 
142
 
 
143
 
 
144
if len( args ) == 0:
 
145
    cups11 = utils.to_bool(sys_conf.get('configure', 'cups11', '0'))
 
146
 
 
147
    try:
 
148
        probed_devices = device.probeDevices(['usb', 'par'], filter={'fax-type': (operator.gt, 0)})
 
149
    except Error:
 
150
        sys.exit(CUPS_BACKEND_FAILED)
 
151
 
 
152
    good_devices = 0
 
153
    for uri in probed_devices:
 
154
        try:
 
155
            back_end, is_hp, bus, model, serial, dev_file, host, zc, port = \
 
156
                device.parseDeviceURI(uri)
 
157
        except Error:
 
158
            continue
 
159
 
 
160
        mq = device.queryModelByModel(model)
 
161
 
 
162
        if mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_MARVELL,):
 
163
            # HP Fax 3
 
164
            if bus == 'usb':
 
165
                print('direct %s "HP Fax 3" "%s USB %s HP Fax HPLIP" "MFG:HP;MDL:Fax 3;DES:HP Fax 3;"' % \
 
166
                    (uri.replace("hp:", "hpfax:"), model.replace('_', ' '), serial))
 
167
 
 
168
            else: # par
 
169
                print('direct %s "HP Fax 3" "%s LPT HP Fax HPLIP" "MFG:HP;MDL:Fax 3;DES:HP Fax 3;"' % \
 
170
                    (uri.replace("hp:", "hpfax:"), model.replace('_', ' ')))
 
171
 
 
172
        elif mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_SOAP,) or mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_LEDMSOAP,):
 
173
            # HP Fax 2
 
174
            if bus == 'usb':
 
175
                print('direct %s "HP Fax 2" "%s USB %s HP Fax HPLIP" "MFG:HP;MDL:Fax 2;DES:HP Fax 2;"' % \
 
176
                    (uri.replace("hp:", "hpfax:"), model.replace('_', ' '), serial))
 
177
 
 
178
            else: # par
 
179
                print('direct %s "HP Fax 2" "%s LPT HP Fax HPLIP" "MFG:HP;MDL:Fax 2;DES:HP Fax 2;"' % \
 
180
                    (uri.replace("hp:", "hpfax:"), model.replace('_', ' ')))
 
181
        elif mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_LEDM,):
 
182
            # HP Fax 4
 
183
            if bus == 'usb':
 
184
                print('direct %s "HP Fax 4" "%s USB %s HP Fax HPLIP" "MFG:HP;MDL:Fax 4;DES:HP Fax 4;"' % \
 
185
                    (uri.replace("hp:", "hpfax:"), model.replace('_', ' '), serial))
 
186
 
 
187
            else: # par
 
188
                print('direct %s "HP Fax 4" "%s LPT HP Fax HPLIP" "MFG:HP;MDL:Fax 4;DES:HP Fax 4;"' % \
 
189
                    (uri.replace("hp:", "hpfax:"), model.replace('_', ' ')))
 
190
 
 
191
        else:
 
192
            # HP Fax
 
193
            if bus == 'usb':
 
194
                print('direct %s "HP Fax" "%s USB %s HP Fax HPLIP" "MFG:HP;MDL:Fax;DES:HP Fax;"' % \
 
195
                    (uri.replace("hp:", "hpfax:"),  model.replace('_', ' '), serial))
 
196
 
 
197
            else: # par
 
198
                print('direct %s "HP Fax" "%s LPT HP Fax HPLIP" "MFG:HP;MDL:Fax;DES:HP Fax;"' % \
 
199
                    (uri.replace("hp:", "hpfax:"),  model.replace('_', ' ')))
 
200
 
 
201
        good_devices += 1
 
202
 
 
203
    if good_devices == 0:
 
204
        if cups11:
 
205
            print('direct hpfax:/no_device_found "HP Fax" "no_device_found" ""')
 
206
        else:
 
207
            print('direct hpfax "Unknown" "HP Fax (HPLIP)" ""')
 
208
 
 
209
    sys.exit(CUPS_BACKEND_OK)
 
210
 
 
211
else:
 
212
    try:
 
213
        # dBus
 
214
        import dbus
 
215
        from dbus import SystemBus, lowlevel
 
216
    except ImportError:
 
217
        bug("HPLIP pc send fax requires dbus and python-dbus")
 
218
        sys.exit(CUPS_BACKEND_FAILED)
 
219
 
 
220
    import warnings
 
221
    # Ignore: .../dbus/connection.py:242: DeprecationWarning: object.__init__() takes no parameters
 
222
    # (occurring on Python 2.6/dBus 0.83/Ubuntu 9.04)
 
223
    warnings.simplefilter("ignore", DeprecationWarning)
 
224
 
 
225
    # CUPS provided environment
 
226
    try:
 
227
        device_uri = os.environ['DEVICE_URI']
 
228
        printer_name = os.environ['PRINTER']
 
229
    except KeyError:
 
230
        bug("Improper environment: Must be run by CUPS.")
 
231
        sys.exit(CUPS_BACKEND_FAILED)
 
232
 
 
233
    log.debug(args)
 
234
 
 
235
    try:
 
236
        job_id, username, title, copies, options = args[0:5]
 
237
        job_id = int(job_id)
 
238
    except IndexError:
 
239
        bug("Invalid command line: invalid arguments.")
 
240
        sys.exit(CUPS_BACKEND_FAILED)
 
241
 
 
242
    send_message(device_uri, printer_name, EVENT_START_FAX_PRINT_JOB, username, job_id, title)
 
243
 
 
244
    try:
 
245
        input_fd = open(args[5], 'r')
 
246
    except IndexError:
 
247
        input_fd = 0
 
248
 
 
249
    if os.path.exists("/home/%s/.hplip"%username):
 
250
        tmp_dir = "/home/%s/.hplip"%username
 
251
    else:
 
252
        tmp_dir = "/tmp"
 
253
 
 
254
    pipe_name = os.path.join(tmp_dir, "hp_fax-pipe-%d" % job_id)
 
255
 
 
256
    # Create the named pipe. Make sure it exists before sending
 
257
    # message to hppsd.
 
258
    os.umask(0o111)
 
259
    try:
 
260
        os.mkfifo(pipe_name)
 
261
    except OSError:
 
262
        os.unlink(pipe_name)
 
263
        os.mkfifo(pipe_name)
 
264
 
 
265
    # Send dbus event to hpssd
 
266
    send_message(device_uri, printer_name, EVENT_FAX_RENDER_COMPLETE, username, job_id, title, pipe_name)
 
267
 
 
268
    # REVISIT:
 
269
    pipe = os.open(pipe_name, os.O_WRONLY)
 
270
 
 
271
    bytes_read = 0
 
272
    while True:
 
273
        data = os.read(input_fd, PIPE_BUF)
 
274
 
 
275
        if not data:
 
276
            break
 
277
 
 
278
        os.write(pipe, data)
 
279
        #syslog.syslog("Writing %d to pipe..." % len(data))
 
280
        bytes_read += len(data)
 
281
 
 
282
    if not bytes_read:
 
283
        bug("No data on input file descriptor.")
 
284
        sys.exit(CUPS_BACKEND_FAILED)
 
285
 
 
286
    os.close(input_fd)
 
287
    os.close(pipe)
 
288
    os.unlink(pipe_name)
 
289
 
 
290
    send_message(device_uri, printer_name, EVENT_END_FAX_PRINT_JOB, username, job_id, title)
 
291
 
 
292
    sys.exit(CUPS_BACKEND_OK)