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 / probe < prev    next >
Encoding:
Text File  |  2007-04-04  |  7.7 KB  |  233 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. # Author: Don Welch
  21. #
  22.  
  23.  
  24. __version__ = '3.2'
  25. __title__ = 'Printer Discovery Utility'
  26. __doc__ = "Discover USB, parallel, and network printers."
  27.  
  28.  
  29. # Std Lib
  30. import sys, getopt
  31.  
  32. # Local
  33. from base.g import *
  34. from base import device, utils
  35.  
  36.  
  37. USAGE = [(__doc__, "", "name", True),
  38.          ("Usage: hp-probe [OPTIONS]", "", "summary", True),
  39.          utils.USAGE_OPTIONS,
  40.          ("Bus to probe:", "-b<bus> or --bus=<bus>", "option", False),
  41.          ("", "<bus>: cups, usb\*, net, bt, fw, par (\*default) (Note: bt and fw not supported in this release.)", "option", False),
  42.          ("Set Time to Live (TTL):", "-t<ttl> or --ttl=<ttl> (Default is 4).", "option", False),
  43.          ("Set timeout:", "-o<timeout in secs.> or --timeout=<timeout is secs.>", "option", False),
  44.          ("Filter by functionality:", "-e<filter list> or --filter=<filter list>", "option", False),
  45.          ("", "<filter list>: comma separated list of one or more of: scan, pcard, fax, copy, or none\*. (\*none is the default)", "option", False),
  46.          ("Search:", "-s<search re> or --search=<search re>", "option", False),
  47.          ("", "<search re> must be a valid regular expression (not case sensitive)", "option", False),
  48.          ("Network discovery method:", "-m<method> or --method=<method>: <method> is 'slp'* or 'mdns'.", "option", False),
  49.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  50.          utils.USAGE_HELP,
  51.          utils.USAGE_SPACE,
  52.          utils.USAGE_EXAMPLES,
  53.          ("Find all devices on the network:", "hp-probe -bnet", "example", False),
  54.          ("Find all devices on USB that support scanning:", "hp-probe -busb -escan", "example", False),
  55.          ("Find all networked devices that contain the name 'lnx' and that support photo cards or scanning:", "hp-probe -bnet -slnx -escan,pcard", "example", False),
  56.          ("Find all devices that have queues installed in CUPS:", "hp-probe -bcups", "example", False),
  57.          ("Find all devices on the USB bus:", "hp-probe", "example", False),
  58.          ]
  59.  
  60. def usage(typ='text'):
  61.     if typ == 'text':
  62.         utils.log_title(__title__, __version__)
  63.  
  64.     utils.format_text(USAGE, typ, __title__, 'hp-probe', __version__)
  65.     sys.exit(0)
  66.  
  67.  
  68. log.set_module('hp-probe')
  69.  
  70.  
  71.  
  72. try:
  73.     opts, args = getopt.getopt(sys.argv[1:],
  74.                                 'hl:b:t:o:e:s:gm:',
  75.                                 ['help', 'help-rest', 'help-man',
  76.                                   'help-desc',
  77.                                   'logging=',
  78.                                   'bus=',
  79.                                   'event=',
  80.                                   'ttl=',
  81.                                   'timeout=',
  82.                                   'filter=',
  83.                                   'search=',
  84.                                   'method=',
  85.                                 ]
  86.                               )
  87. except getopt.GetoptError:
  88.     usage()
  89.  
  90. log_level = logger.DEFAULT_LOG_LEVEL
  91. bus = "usb"
  92. align_debug = False
  93. timeout=10
  94. ttl=4
  95. filter = 'none'
  96. search = ''
  97. method = 'slp'
  98.  
  99. if os.getenv("HPLIP_DEBUG"):
  100.     log.set_level('debug')
  101.  
  102. for o, a in opts:
  103.  
  104.     if o in ('-h', '--help'):
  105.         usage()
  106.  
  107.     elif o == '--help-rest':
  108.         usage('rest')
  109.  
  110.     elif o == '--help-man':
  111.         usage('man')
  112.  
  113.     elif o == '--help-desc':
  114.         print __doc__,
  115.         sys.exit(0)
  116.  
  117.     elif o == '-g':
  118.         log.set_level('debug')
  119.  
  120.     elif o in ('-b', '--bus'):
  121.         try:
  122.             bus = a.lower().strip()
  123.         except:
  124.             bus = 'usb'
  125.  
  126.         if not device.validateBusList(bus):
  127.             usage()
  128.  
  129.     elif o in ('-l', '--logging'):
  130.         log_level = a.lower().strip()
  131.         if not log.set_level(log_level):
  132.             usage()
  133.  
  134.     elif o in ('-m', '--method'):
  135.         method = a.lower().strip()
  136.  
  137.         if method not in ('slp', 'mdns', 'bonjour'):
  138.             log.error("Invalid network search protocol: %s (must be 'slp' or 'mdns')" % method)
  139.             method = 'slp'
  140.  
  141.         else:
  142.             bus = 'net'
  143.  
  144.     elif o in ('-t', '--ttl'):
  145.         try:
  146.             ttl = int(a)
  147.         except ValueError:
  148.             ttl = 4
  149.             log.note("TTL value error. TTL set to default of 4 hops.")
  150.  
  151.     elif o in ('-o', '--timeout'):
  152.         try:
  153.             timeout = int(a)
  154.             if timeout > 45:
  155.                 log.note("Timeout > 45secs. Setting to 45secs.")
  156.                 timeout = 45
  157.         except ValueError:
  158.             timeout = 5
  159.             log.note("Timeout value error. Timeout set to default of 5secs.")
  160.  
  161.         if timeout < 0:
  162.             log.error("You must specify a positive timeout in seconds.")
  163.             usage()
  164.  
  165.     elif o in ('-e', '--filter'):
  166.         filter = a.lower().strip()
  167.         if not device.validateFilterList(filter):
  168.             usage()
  169.  
  170.     elif o in ('-s', '--search'):
  171.         search = a.lower().strip()
  172.  
  173. utils.log_title(__title__, __version__)
  174.  
  175. buses = bus
  176. for bus in buses.split(','):
  177.     if bus == 'net':
  178.         log.info(utils.bold("Probing network for printers. Please wait, this will take approx. %d seconds...\n" % timeout))
  179.  
  180.     devices = device.probeDevices(None, bus, timeout, ttl, filter, search, method)
  181.     cleanup_spinner()
  182.  
  183.     max_c1, max_c2, max_c3, max_c4 = 0, 0, 0, 0
  184.  
  185.     if devices:
  186.         for d in devices:
  187.             max_c1 = max(len(d), max_c1)
  188.             max_c3 = max(len(devices[d][0]), max_c3)
  189.             max_c4 = max(len(devices[d][2]), max_c4)
  190.  
  191.         if bus == 'net':
  192.             formatter = utils.TextFormatter(
  193.                         (
  194.                             {'width': max_c1, 'margin' : 2},
  195.                             {'width': max_c3, 'margin' : 2},
  196.                             {'width': max_c4, 'margin' : 2},
  197.                         )
  198.                     )
  199.  
  200.             log.info(formatter.compose(("Device URI", "Model", "Name")))
  201.             log.info(formatter.compose(('-'*max_c1, '-'*max_c3, '-'*max_c4)))
  202.             for d in devices:
  203.                 log.info(formatter.compose((d, devices[d][0], devices[d][2])))
  204.  
  205.         elif bus in ('usb', 'par', 'cups'):
  206.             formatter = utils.TextFormatter(
  207.                         (
  208.                             {'width': max_c1, 'margin' : 2},
  209.                             {'width': max_c3, 'margin' : 2},
  210.                         )
  211.                     )
  212.  
  213.             log.info(formatter.compose(("Device URI", "Model")))
  214.             log.info(formatter.compose(('-'*max_c1, '-'*max_c3)))
  215.             for d in devices:
  216.                 log.info(formatter.compose((d, devices[d][0])))
  217.  
  218.         else:
  219.             log.error("Invalid bus: %s" % bus)
  220.  
  221.         log.info("\nFound %d printer(s) on the '%s' bus.\n" % (len(devices), bus))
  222.  
  223.     else:
  224.         log.warn("No devices found on the '%s' bus. If this isn't the result you are expecting," % bus)
  225.  
  226.         if bus == 'net':
  227.             log.warn("check your network connections and make sure your internet")
  228.             log.warn("firewall software is disabled.")
  229.         else:
  230.             log.warn("check to make sure your devices are properly connected and powered on.")
  231.  
  232.  
  233.