[24249] in Source-Commits

home help back first fref pref prev next nref lref last post

/svn/athena r23847 - in trunk/debathena/config/printing-config: debian files/usr/bin

daemon@ATHENA.MIT.EDU (Geoffrey Thomas)
Sat Jun 13 23:59:30 2009

Date: Sat, 13 Jun 2009 23:59:18 -0400
From: Geoffrey Thomas <geofft@MIT.EDU>
Message-Id: <200906140359.n5E3xI9D004522@drugstore.mit.edu>
To: source-commits@mit.edu
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Author: geofft
Date: 2009-06-13 23:59:18 -0400 (Sat, 13 Jun 2009)
New Revision: 23847

Modified:
   trunk/debathena/config/printing-config/debian/changelog
   trunk/debathena/config/printing-config/files/usr/bin/lpr.debathena
Log:
In printing-config:
  * If the arguments to a printing command imply a desire for a particular
    version of that command (CUPS or LPRng), use that version. (Trac: #251)


Modified: trunk/debathena/config/printing-config/debian/changelog
===================================================================
--- trunk/debathena/config/printing-config/debian/changelog	2009-06-14 03:59:13 UTC (rev 23846)
+++ trunk/debathena/config/printing-config/debian/changelog	2009-06-14 03:59:18 UTC (rev 23847)
@@ -1,3 +1,10 @@
+debathena-printing-config (1.7) unstable; urgency=low
+
+  * If the arguments to a printing command imply a desire for a particular
+    version of that command (CUPS or LPRng), use that version. (Trac: #251)
+
+ -- Geoffrey Thomas <geofft@mit.edu>  Sat, 13 Jun 2009 22:52:24 -0400
+
 debathena-printing-config (1.6) unstable; urgency=low
 
   * Add missing dependency on python-hesiod. (Trac: #204)

Modified: trunk/debathena/config/printing-config/files/usr/bin/lpr.debathena
===================================================================
--- trunk/debathena/config/printing-config/files/usr/bin/lpr.debathena	2009-06-14 03:59:13 UTC (rev 23846)
+++ trunk/debathena/config/printing-config/files/usr/bin/lpr.debathena	2009-06-14 03:59:18 UTC (rev 23847)
@@ -16,13 +16,27 @@
 from subprocess import call, PIPE
 
 opts = {
-    'cancel': 'aD:Eh:P:U:u:',
-    'lp': 'ABcd:D:Ef:Gh:H:i:mn:o:pP:q:rsS:t:T:U:wX:y:',
-    'lpq': 'aAD:Eh:lLP:st:U:Vv',
-    'lpr': '#:1:2:3:4:AbBC:D:EF:GhH:i:J:kK:lmo:pP:qrR:sT:U:Vw:X:YZ:',
-    'lprm': 'aAD:Eh:lLP:st:U:V'
+    'cups': {
+        'cancel': 'EU:ah:u:',
+        'lp': 'EU:cd:h:mn:o:q:st:H:P:i:',
+        'lpq': 'EU:h:P:al',
+        'lpr': 'EH:U:P:#:hlmo:pqrC:J:T:',
+        'lprm': 'EU:h:P:'
+    },
+    'lprng': {
+        'cancel': 'aAD:P:VU:',
+        'lp': 'ckmprswBGYd:D:f:n:q:t:',
+        'lpq': 'aAlLVcvP:st:D:',
+        'lpr': 'ABblC:D:F:Ghi:kJ:K:#:m:P:rR:sT:U:Vw:X:YZ:1:2:3:4:',
+        'lprm': 'aAD:P:VU:'
+    }
 }
 
+preflist = {
+    'cups': ['cups', 'lprng'],
+    'lprng': ['lprng', 'cups']
+}
+
 def error(code, message):
     """Exit out with an error
     """
@@ -72,48 +86,63 @@
         new_command = '/usr/bin/mit-%s' % command
         run(command, new_command, args)
 
-def getPrintQueue(command, args):
+def getPrintQueue(command, args, preference):
     """Given argv, extract the printer name, using knowledge of the possible
     command line options for both the CUPS and LPRng versions of the command
-    that this script was called as.
+    that this script was called as. Also, return whether the command line
+    options imply a particular version of the command.
     """
-    try:
-        # Get the set of options that correspond to the command that this
-        # script was invoked as
-        cmd_opts = opts[command]
-        options, args = getopt.gnu_getopt(args, cmd_opts)
+    for version in preflist[preference]:
+        try:
+            # Get the set of options that correspond to the command that this
+            # script was invoked as
+            cmd_opts = opts[version][command]
+        except KeyError:
+            error(1, """
+Error: this script was called as %s, when it must be called as
+one of lpr, lpq, lprm, lp, or cancel
+
+""" % command)
+
+        # Attempt to parse it with the current version of this command
+        try:
+            options, args = getopt.gnu_getopt(args, cmd_opts)
+        except getopt.GetoptError:
+            # That's the wrong version, so try the next one.
+            continue
+
         for o, a in options:
             if o == '-P':
-                return a
+                return (version, a)
         if command == 'cancel' and len(args) > 0:
-            return args[0]
+            return (version, args[0])
+        # Since we've successfully getopt'd, don't try any other versions,
+        # but do note that we like this version.
+        preference = version
+        break
+
     # This script was invoked with argv[0] set to something it doesn't know
-    # about
-    except KeyError:
-        error(1, """
-Error: this script was called as %s, when it must be called as
-one of lpr, lpq, lprm, lp, or cancel
-
-""" % command)
+    # about. In that case, use our preferred version.
     default = os.getenv('PRINTER')
     if default:
-        return default
+        return (preference, default)
     try:
         h = hesiod.Lookup(os.uname()[1], 'cluster')
         for result in h.results:
             (key, value) = result.split(None, 1)
             if key == 'lpr':
-                return value
+                return (preference, value)
     except IOError, e:
         if e.errno in (errno.ENOENT, errno.EMSGSIZE):
             pass
         else:
             raise
 
+    return (preference, None)
+
 if __name__ == '__main__':
     # Remove the command name from the arguments when we extract it
     command = os.path.basename(sys.argv.pop(0))
-    queue = getPrintQueue(command, sys.argv)
     # Figure out whether to prefer LPRng or CUPS. If we can't decide based on
     # the config file, then we're going with CUPS
     try:
@@ -123,7 +152,11 @@
             strip().lower()
     except IOError:
         preference = 'cups'
-    
+
+    # Determine if the arguments prefer one version of this command,
+    # For instance, "lpq -h cups.csail -P xerox5" wants cups-lpq
+    (preference, queue) = getPrintQueue(command, sys.argv, preference)
+
     fns = {'cups': [useCups, useLprng],
            'lprng': [useLprng, useCups]}
     fn_args = [command, queue, sys.argv]


home help back first fref pref prev next nref lref last post