home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / makeuri < prev    next >
Encoding:
Text File  |  2006-08-30  |  6.5 KB  |  205 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2006 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.2'
  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.  
  34. # Local
  35. from base.g import *
  36. from base.codes import *
  37. from base import device, utils
  38.  
  39.  
  40. USAGE = [ (__doc__, "", "name", True),
  41.           ("Usage: hp-makeuri [OPTIONS] [SERIAL NO.|USB ID|IP|DEVNODE]", "", "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. def usage(typ='text'):
  73.     if typ == 'text':
  74.         utils.log_title(__title__, __version__)
  75.         
  76.     utils.format_text(USAGE, typ, __title__, 'hp-makeuri', __version__)
  77.     sys.exit(0)
  78.  
  79.  
  80.  
  81. try:
  82.     opts, args = getopt.getopt(sys.argv[1:], 
  83.                                 'hl:csfp:g', 
  84.                                 ['help', 'help-rest', 'help-man',
  85.                                   'logging=',
  86.                                   'cups',
  87.                                   'sane',
  88.                                   'fax=',
  89.                                   'port=',
  90.                                 ] 
  91.                               ) 
  92. except getopt.GetoptError:
  93.     usage()
  94.     sys.exit(1)
  95.  
  96. if os.getenv("HPLIP_DEBUG"):
  97.     log.set_level('debug')
  98.  
  99. log_level = 'info'
  100. cups_quiet_mode = False
  101. sane_quiet_mode = False
  102. fax_quiet_mode = False
  103. jd_port = 1
  104.  
  105. for o, a in opts:
  106.  
  107.     if o in ('-h', '--help'):
  108.         usage()
  109.         
  110.     elif o == '--help-rest':
  111.         usage('rest')
  112.         
  113.     elif o == '--help-man':
  114.         usage('man')
  115.  
  116.     elif o in ('-l', '--logging'):
  117.         log_level = a.lower().strip()
  118.         if not log.set_level(log_level):
  119.             usage()
  120.         
  121.     elif o in ('-c', '--cups'):
  122.         cups_quiet_mode = True
  123.         
  124.     elif o in ('-s', '--sane'):
  125.         sane_quiet_mode = True
  126.         
  127.     elif o in ('-f', '--fax'):
  128.         fax_quiet_mode = True
  129.  
  130.     elif o in ('-p', '--port'):
  131.         try:
  132.             jd_port = int(a)
  133.         except ValueError:
  134.             log.error("Invalid port number. Must be between 1 and 3 inclusive.")
  135.             usage()
  136.             
  137.     elif o == '-g':
  138.         log.set_level('debug')
  139.         
  140.  
  141. quiet_mode = cups_quiet_mode or sane_quiet_mode or fax_quiet_mode
  142.  
  143. if quiet_mode:
  144.     log.set_level('warn')
  145.     
  146. utils.log_title(__title__, __version__)    
  147.  
  148. if len(args) != 1:
  149.     log.error("You must specify one SERIAL NO., IP, USB ID or DEVNODE on the command line.")
  150.     usage()
  151.     
  152. param = args[0]
  153.  
  154. if 'localhost' in param.lower():
  155.     log.error("Invalid hostname")
  156.     usage()
  157.  
  158. hpiod_sock = None
  159. try:
  160.     hpiod_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  161.     hpiod_sock.connect((prop.hpiod_host, prop.hpiod_port))
  162. except socket.error:
  163.     log.error("Unable to connect to hpiod.")
  164.     sys.exit(1)
  165.  
  166. hpssd_sock = None
  167. try:
  168.     hpssd_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  169.     hpssd_sock.connect((prop.hpssd_host, prop.hpssd_port))
  170. except socket.error:
  171.     log.error("Unable to connect to hpssd.")
  172.     sys.exit(1)
  173.     
  174. cups_uri, sane_uri, fax_uri = device.makeuri(hpiod_sock, hpssd_sock, param, jd_port)
  175.  
  176. if not cups_uri:
  177.     log.error("Device not found")
  178.     sys.exit(1)
  179.  
  180. if cups_quiet_mode:
  181.     print cups_uri
  182. elif not quiet_mode:    
  183.     print "CUPS URI:", cups_uri
  184.  
  185. if sane_uri:
  186.     if sane_quiet_mode:
  187.         print sane_uri
  188.     elif not quiet_mode:
  189.         print "SANE URI:", sane_uri
  190. elif not sane_uri and sane_quiet_mode:
  191.     log.error("Device does not support scan.")
  192.     
  193. if fax_uri:
  194.     if fax_quiet_mode:
  195.         print fax_uri
  196.     elif not quiet_mode:
  197.         print "HP Fax URI:", fax_uri
  198. elif not fax_uri and fax_quiet_mode:
  199.     log.error("Device does not support fax.")
  200.  
  201. hpiod_sock.close()
  202. hpssd_sock.close()
  203.  
  204. sys.exit(0)
  205.