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 / testpage < prev    next >
Encoding:
Text File  |  2007-04-04  |  7.9 KB  |  280 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. __version__ = '4.1'
  24. __title__ = 'Testpage Print Utility'
  25. __doc__ = "Print a tespage to a printer. Prints a summary of device information and shows the printer's margins."
  26.  
  27. # Std Lib
  28. import sys
  29. import os
  30. import getopt
  31. import re
  32. import time
  33.  
  34. # Local
  35. from base.g import *
  36. from base import device, utils
  37. from prnt import cups
  38.  
  39. USAGE = [(__doc__, "", "name", True),
  40.          ("Usage: hp-testpage [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  41.          utils.USAGE_ARGS,
  42.          utils.USAGE_DEVICE,
  43.          utils.USAGE_PRINTER,
  44.          utils.USAGE_SPACE,
  45.          utils.USAGE_OPTIONS,
  46.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  47.          ("Don't wait for printout to complete:", "-x", "option", True),
  48.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  49.          utils.USAGE_HELP,
  50.          utils.USAGE_SPACE,
  51.          utils.USAGE_NOTES,
  52.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  53.         ]
  54.  
  55. def usage(typ='text'):
  56.     if typ == 'text':
  57.         utils.log_title(__title__, __version__)
  58.  
  59.     utils.format_text(USAGE, typ, __title__, 'hp-testpage', __version__)
  60.     sys.exit(0)
  61.  
  62.  
  63. log.set_module('hp-testpage')
  64.  
  65. try:
  66.     opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:gx',
  67.                                ['printer=', 'device=', 'help', 'help-rest', 
  68.                                 'help-man', 'logging=', 'bus=', 'help-desc'])
  69. except getopt.GetoptError:
  70.     usage()
  71.  
  72. printer_name = None
  73. device_uri = None
  74. bus = 'cups'
  75. log_level = logger.DEFAULT_LOG_LEVEL
  76. wait_for_printout = True
  77.  
  78. if os.getenv("HPLIP_DEBUG"):
  79.     log.set_level('debug')
  80.  
  81. for o, a in opts:
  82.     if o in ('-h', '--help'):
  83.         usage()
  84.  
  85.     elif o == '--help-rest':
  86.         usage('rest')
  87.  
  88.     elif o == '--help-man':
  89.         usage('man')
  90.  
  91.     elif o == '--help-desc':
  92.         print __doc__,
  93.         sys.exit(0)
  94.  
  95.     elif o in ('-p', '--printer'):
  96.         printer_name = a
  97.  
  98.     elif o in ('-d', '--device'):
  99.         device_uri = a
  100.  
  101.     elif o in ('-b', '--bus'):
  102.         bus = a.lower().strip()
  103.         if not device.validateBusList(bus):
  104.             usage()
  105.  
  106.     elif o in ('-l', '--logging'):
  107.         log_level = a.lower().strip()
  108.         if not log.set_level(log_level):
  109.             usage()
  110.  
  111.     elif o == '-g':
  112.         log.set_level('debug')
  113.  
  114.     elif o == '-x':
  115.         wait_for_printout = False
  116.  
  117.  
  118. if device_uri and printer_name:
  119.     log.error("You may not specify both a printer (-p) and a device (-d).")
  120.     usage()
  121.  
  122. if device_uri and printer_name:
  123.     log.error("You may not specify both a printer (-p) and a device (-d).")
  124.     usage()
  125.  
  126. utils.log_title(__title__, __version__)
  127.  
  128. if not device_uri and not printer_name:
  129.     try:
  130.         device_uri = device.getInteractiveDeviceURI(bus)
  131.         if device_uri is None:
  132.             sys.exit(0)
  133.     except Error:
  134.         log.error("Error occured during interactive mode. Exiting.")
  135.         sys.exit(0)
  136.  
  137. try:
  138.     d = device.Device(device_uri, printer_name)
  139. except Error, e:
  140.     log.error("Device error (%s)." % e.msg)
  141.     sys.exit(1)
  142.  
  143. if d.device_uri is None and printer_name:
  144.     log.error("Printer '%s' not found." % printer_name)
  145.     sys.exit(1)
  146.  
  147. if d.device_uri is None and device_uri:
  148.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  149.     sys.exit(1)
  150.  
  151. user_cfg.last_used.device_uri = d.device_uri
  152.  
  153. try:
  154.     try:
  155.         d.open()
  156.     except Error:
  157.         log.error("Unable to print to printer. Please check device and try again.")
  158.         sys.exit(1)
  159.  
  160.     if len(d.cups_printers) == 0:
  161.         log.error("No printer queues found for device.")
  162.         sys.exit(1)
  163.  
  164.     elif len(d.cups_printers) > 1:
  165.         log.info("\nMultiple printers (queues) found in CUPS for device.")
  166.         log.info(utils.bold("\nPlease choose the printer (queue) to use for the test page:\n"))
  167.  
  168.         max_name = 24
  169.         for q in d.cups_printers:
  170.             max_name = max(max_name, len(q))
  171.  
  172.         formatter = utils.TextFormatter(
  173.             (
  174.                 {'width': 4, 'margin': 2},
  175.                 {'width': max_name, 'margin': 2},
  176.             )
  177.         )
  178.  
  179.         log.info(formatter.compose(("Num.", "CUPS printer (queue)")))
  180.         log.info(formatter.compose(('-'*4, '-'*(max_name))))
  181.  
  182.         x = 0
  183.         for q in d.cups_printers:
  184.             log.info(formatter.compose((str(x), d.cups_printers[x])))
  185.             x += 1
  186.  
  187.         while 1:
  188.             user_input = raw_input(utils.bold("\nEnter number 0...%d for printer (q=quit) ?" % (x-1)))
  189.  
  190.             if user_input == '':
  191.                 log.warn("Invalid input - enter a numeric value or 'q' to quit.")
  192.                 continue
  193.  
  194.             if user_input.strip()[0] in ('q', 'Q'):
  195.                 sys.exit(0)
  196.  
  197.             try:
  198.                 i = int(user_input)
  199.             except ValueError:
  200.                 log.warn("Invalid input - enter a numeric value or 'q' to quit.")
  201.                 continue
  202.  
  203.             if i < 0 or i > (x-1):
  204.                 log.warn("Invalid input - enter a value between 0 and %d or 'q' to quit." % (x-1))
  205.                 continue
  206.  
  207.             break
  208.  
  209.         printer_name = d.cups_printers[i]
  210.  
  211.     else:
  212.         printer_name = d.cups_printers[0]
  213.  
  214.     log.info("")
  215.  
  216.     if d.isIdleAndNoError():
  217.         d.close()
  218.         log.info( "Printing test page to printer %s..." % printer_name)
  219.         try:
  220.             d.printTestPage(printer_name)
  221.         except Error, e:
  222.             if e.opt == ERROR_NO_CUPS_QUEUE_FOUND_FOR_DEVICE:
  223.                 log.error("No CUPS queue found for device. Please install the printer in CUPS and try again.")
  224.             else:
  225.                 log.error("An error occured (code=%d)." % e.opt)
  226.         else:
  227.             if wait_for_printout:
  228.                 log.info("Test page has been sent to printer. Waiting for printout to complete...")
  229.  
  230.                 time.sleep(5)
  231.                 i = 0
  232.  
  233.                 while True:
  234.                     time.sleep(5)
  235.  
  236.                     try:
  237.                         d.queryDevice(quick=True)
  238.                     except Error, e:
  239.                         log.error("An error has occured.")
  240.  
  241.                     if d.error_state == ERROR_STATE_CLEAR:
  242.                         break
  243.  
  244.                     elif d.error_state == ERROR_STATE_ERROR:
  245.                         cleanup_spinner()
  246.                         log.error("An error has occured (code=%d). Please check the printer and try again." % d.status_code)
  247.                         break
  248.  
  249.                     elif d.error_state == ERROR_STATE_WARNING:
  250.                         cleanup_spinner()
  251.                         log.warning("There is a problem with the printer (code=%d). Please check the printer." % d.status_code)
  252.  
  253.                     else: # ERROR_STATE_BUSY
  254.                         update_spinner()
  255.  
  256.                     i += 1
  257.  
  258.                     if i > 24:  # 2min
  259.                         break
  260.  
  261.                 cleanup_spinner()
  262.  
  263.             else:
  264.                 log.info("Test page has been sent to printer.")
  265.  
  266.     else:
  267.         log.error("Device is busy or in an error state. Please check device and try again.")
  268.         sys.exit(1)
  269.  
  270.  
  271. finally:
  272.     d.close()
  273.  
  274.     log.info("")
  275.     log.notice("If an error occured, or the test page failed to print, refer to the HPLIP website")
  276.     log.notice("at: http://hplip.sourceforge.net for troubleshooting and support.")
  277.     log.info("")
  278.  
  279. log.info("Done.")
  280.