1#!/usr/local/bin/python3.8
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# Authors: Don Welch, Smith Kennedy
21#
22
23__version__ = '5.0'
24__title__ = 'Device URI Creation Utility'
25__mod__ = 'hp-makeuri'
26__doc__ = "Creates device URIs for local and network connected printers for use with CUPS."
27
28# Std Lib
29import sys
30import re
31import getopt
32import os
33
34# Local
35from base.g import *
36from base.codes import *
37from base import device, utils, module
38
39
40USAGE = [ (__doc__, "", "name", True),
41          ("Usage: %s [OPTIONS] [SERIAL NO.|USB ID|IP|DEVNODE]" % __mod__, "", "summary", True),
42          ("[SERIAL NO.|USB ID|IP|DEVNODE]", "", "heading", False),
43          ("USB IDs (usb only):", """"xxx:yyy" where xxx is the USB bus ID and yyy is the USB device ID. The ':' and all leading zeroes must be present.""", 'option', False),
44          ("", """(Use the 'lsusb' command to obtain this information. See Note 1.)""", "option", False),
45          ("IPs (network only):", 'IPv4 address "a.b.c.d" or "hostname"', "option", False),
46          ("DEVNODE (parallel only):", '"/dev/parportX", X=0,1,2,...', "option", False),
47          ("SERIAL NO. (usb and parallel only):", '"serial no."', "option", True),
48          utils.USAGE_OPTIONS,
49          ("To specify the port on a multi-port JetDirect:", "-p<port> or --port=<port> (Valid values are 1\*, 2, and 3. \*default)", "option", False),
50          ("Show the CUPS URI only (quiet mode):", "-c or --cups", "option", False),
51          ("Show the SANE URI only (quiet mode):", "-s or --sane", "option", False),
52          ("Show the HP Fax URI only (quiet mode):", "-f or --fax", "option", False),
53          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
54          utils.USAGE_HELP,
55          utils.USAGE_EXAMPLES,
56          ("USB:", "$ hp-makeuri 001:002", "example", False),
57          ("Network:", "$ hp-makeuri 66.35.250.209", "example", False),
58          ("Parallel:", "$ hp-makeuri /dev/parport0", "example", False),
59          ("USB or parallel (using serial number):", "$ hp-makeuri US123456789", "example", False),
60          utils.USAGE_SPACE,
61          utils.USAGE_NOTES,
62          ("1. Example using 'lsusb' to obtain USB bus ID and USB device ID (example only, the values you obtain will differ) :", "", 'note', False),
63          ("   $ lsusb", "", 'note', False),
64          ("   Bus 003 Device 011: ID 03f0:c202 Hewlett-Packard", "", 'note', False),
65          ("   $ hp-makeuri 003:011", "", 'note', False),
66          ("   (Note: You may have to run 'lsusb' from /sbin or another location. Use '$ locate lsusb' to determine this.)", "", 'note', True),
67          utils.USAGE_SPACE,
68          utils.USAGE_SEEALSO,
69          ("hp-setup", "", "seealso", False),
70        ]
71
72
73mod = module.Module(__mod__, __title__, __version__, __doc__, USAGE,
74                    (INTERACTIVE_MODE,), None, True, True)
75
76opts, device_uri, printer_name, mode, ui_toolkit, lang = \
77    mod.parseStdOpts('p:csf', ['port', 'cups', 'sane', 'fax'],
78                     handle_device_printer=False)
79
80try:
81    cups_quiet_mode = False
82    sane_quiet_mode = False
83    fax_quiet_mode = False
84    jd_port = 1
85
86    for o, a in opts:
87        if o in ('-c', '--cups'):
88            cups_quiet_mode = True
89
90        elif o in ('-s', '--sane'):
91            sane_quiet_mode = True
92
93        elif o in ('-f', '--fax'):
94            fax_quiet_mode = True
95
96        elif o in ('-p', '--port'):
97            try:
98                jd_port = int(a)
99            except ValueError:
100                mod.usage(error_msg=["Invalid port number. Must be between 1 and 3 inclusive."])
101
102        elif o == '-g':
103            log.set_level('debug')
104
105
106    quiet_mode = cups_quiet_mode or sane_quiet_mode or fax_quiet_mode
107    mod.quiet = quiet_mode
108
109    #if quiet_mode:
110    #    log.set_level('warn')
111
112    #utils.log_title(__title__, __version__)
113    mod.showTitle()
114
115    if len(mod.args) != 1:
116        mod.usage(error_msg=["You must specify one SERIAL NO., IP, USB ID or DEVNODE parameter."])
117
118    param = mod.args[0]
119
120    if 'localhost' in param.lower():
121        mod.usage(error_msg=['Invalid hostname'])
122
123    cups_uri, sane_uri, fax_uri = device.makeURI(param, jd_port)
124
125    if not cups_uri:
126        log.error("Device not found")
127        sys.exit(1)
128
129    if cups_quiet_mode:
130        print(cups_uri)
131
132    elif not quiet_mode:
133        print("CUPS URI: %s" % cups_uri)
134
135    if sane_uri:
136        if sane_quiet_mode:
137            print(sane_uri)
138
139        elif not quiet_mode:
140            print("SANE URI: %s" % sane_uri)
141
142    elif not sane_uri and sane_quiet_mode:
143        log.error("Device does not support scan.")
144
145    if fax_uri:
146        if fax_quiet_mode:
147            print(fax_uri)
148
149        elif not quiet_mode:
150            print("HP Fax URI: %s" % fax_uri)
151
152    elif not fax_uri and fax_quiet_mode:
153        log.error("Device does not support fax.")
154
155except KeyboardInterrupt:
156    log.error("User exit")
157
158if not quiet_mode:
159    log.info("")
160    log.info("Done.")
161
162