home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / makeuri < prev    next >
Encoding:
Text File  |  2007-04-04  |  6.3 KB  |  201 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard 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__ = '4.3'
  24. __title__ = 'Device URI Creation Utility'
  25. __doc__ = "Creates device URIs for local and network connected printers for use with CUPS."
  26.  
  27. # Std Lib
  28. import sys
  29. import re
  30. import getopt
  31. import socket
  32.  
  33. # Local
  34. from base.g import *
  35. from base.codes import *
  36. from base import device, utils
  37.  
  38.  
  39. USAGE = [ (__doc__, "", "name", True),
  40.           ("Usage: hp-makeuri [OPTIONS] [SERIAL NO.|USB ID|IP|DEVNODE]", "", "summary", True),
  41.           ("[SERIAL NO.|USB ID|IP|DEVNODE]", "", "heading", False),
  42.           ("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),
  43.           ("", """(Use the 'lsusb' command to obtain this information. See Note 1.)""", "option", False),
  44.           ("IPs (network only):", 'IPv4 address "a.b.c.d" or "hostname"', "option", False),
  45.           ("DEVNODE (parallel only):", '"/dev/parportX", X=0,1,2,...', "option", False),
  46.           ("SERIAL NO. (usb and parallel only):", '"serial no."', "option", True),
  47.           utils.USAGE_OPTIONS,
  48.           ("To specify the port on a multi-port JetDirect:", "-p<port> or --port=<port> (Valid values are 1\*, 2, and 3. \*default)", "option", False),
  49.           ("Show the CUPS URI only (quiet mode):", "-c or --cups", "option", False),
  50.           ("Show the SANE URI only (quiet mode):", "-s or --sane", "option", False),
  51.           ("Show the HP Fax URI only (quiet mode):", "-f or --fax", "option", False),
  52.           utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  53.           utils.USAGE_HELP,
  54.           utils.USAGE_EXAMPLES,
  55.           ("USB:", "$ hp-makeuri 001:002", "example", False),
  56.           ("Network:", "$ hp-makeuri 66.35.250.209", "example", False),
  57.           ("Parallel:", "$ hp-makeuri /dev/parport0", "example", False),
  58.           ("USB or parallel (using serial number):", "$ hp-makeuri US123456789", "example", False),
  59.           utils.USAGE_SPACE,
  60.           utils.USAGE_NOTES,
  61.           ("1. Example using 'lsusb' to obtain USB bus ID and USB device ID (example only, the values you obtain will differ) :", "", 'note', False),
  62.           ("   $ lsusb", "", 'note', False),
  63.           ("   Bus 003 Device 011: ID 03f0:c202 Hewlett-Packard", "", 'note', False),
  64.           ("   $ hp-makeuri 003:011", "", 'note', False),
  65.           ("   (Note: You may have to run 'lsusb' from /sbin or another location. Use '$ locate lsusb' to determine this.)", "", 'note', True),
  66.           utils.USAGE_SPACE,
  67.           utils.USAGE_SEEALSO,
  68.           ("hp-setup", "", "seealso", False),
  69.         ]
  70.  
  71. def usage(typ='text'):
  72.     if typ == 'text':
  73.         utils.log_title(__title__, __version__)
  74.  
  75.     utils.format_text(USAGE, typ, __title__, 'hp-makeuri', __version__)
  76.     sys.exit(0)
  77.  
  78.  
  79.  
  80. log.set_module('hp-makeuri')
  81.  
  82. try:
  83.     opts, args = getopt.getopt(sys.argv[1:], 
  84.                                 'hl:csfp:g', 
  85.                                 ['help', 'help-rest', 'help-man', 'help-desc',
  86.                                   'logging=',
  87.                                   'cups',
  88.                                   'sane',
  89.                                   'fax',
  90.                                   'port=',
  91.                                 ] 
  92.                               ) 
  93. except getopt.GetoptError:
  94.     usage()
  95.     sys.exit(1)
  96.  
  97. if os.getenv("HPLIP_DEBUG"):
  98.     log.set_level('debug')
  99.  
  100. log_level = 'info'
  101. cups_quiet_mode = False
  102. sane_quiet_mode = False
  103. fax_quiet_mode = False
  104. jd_port = 1
  105.  
  106. for o, a in opts:
  107.  
  108.     if o in ('-h', '--help'):
  109.         usage()
  110.  
  111.     elif o == '--help-rest':
  112.         usage('rest')
  113.  
  114.     elif o == '--help-man':
  115.         usage('man')
  116.  
  117.     elif o == '--help-desc':
  118.         print __doc__,
  119.         sys.exit(0)
  120.  
  121.     elif o in ('-l', '--logging'):
  122.         log_level = a.lower().strip()
  123.         if not log.set_level(log_level):
  124.             usage()
  125.  
  126.     elif o in ('-c', '--cups'):
  127.         cups_quiet_mode = True
  128.  
  129.     elif o in ('-s', '--sane'):
  130.         sane_quiet_mode = True
  131.  
  132.     elif o in ('-f', '--fax'):
  133.         fax_quiet_mode = True
  134.  
  135.     elif o in ('-p', '--port'):
  136.         try:
  137.             jd_port = int(a)
  138.         except ValueError:
  139.             log.error("Invalid port number. Must be between 1 and 3 inclusive.")
  140.             usage()
  141.  
  142.     elif o == '-g':
  143.         log.set_level('debug')
  144.  
  145.  
  146. quiet_mode = cups_quiet_mode or sane_quiet_mode or fax_quiet_mode
  147.  
  148. if quiet_mode:
  149.     log.set_level('warn')
  150.  
  151. utils.log_title(__title__, __version__)    
  152.  
  153. if len(args) != 1:
  154.     log.error("You must specify one SERIAL NO., IP, USB ID or DEVNODE on the command line.")
  155.     usage()
  156.  
  157. param = args[0]
  158.  
  159. if 'localhost' in param.lower():
  160.     log.error("Invalid hostname")
  161.     usage()
  162.  
  163. hpiod_sock = None
  164. try:
  165.     hpiod_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  166.     hpiod_sock.connect((prop.hpiod_host, prop.hpiod_port))
  167. except socket.error:
  168.     log.error("Unable to connect to hpiod.")
  169.     sys.exit(1)
  170.  
  171. cups_uri, sane_uri, fax_uri = device.makeURI(hpiod_sock, param, jd_port)
  172.  
  173. if not cups_uri:
  174.     log.error("Device not found")
  175.     sys.exit(1)
  176.  
  177. if cups_quiet_mode:
  178.     print cups_uri
  179. elif not quiet_mode:    
  180.     print "CUPS URI:", cups_uri
  181.  
  182. if sane_uri:
  183.     if sane_quiet_mode:
  184.         print sane_uri
  185.     elif not quiet_mode:
  186.         print "SANE URI:", sane_uri
  187. elif not sane_uri and sane_quiet_mode:
  188.     log.error("Device does not support scan.")
  189.  
  190. if fax_uri:
  191.     if fax_quiet_mode:
  192.         print fax_uri
  193.     elif not quiet_mode:
  194.         print "HP Fax URI:", fax_uri
  195. elif not fax_uri and fax_quiet_mode:
  196.     log.error("Device does not support fax.")
  197.  
  198. hpiod_sock.close()
  199.  
  200. sys.exit(0)
  201.