b99885
#!/usr/bin/python
b99885
import os
b99885
import re
b99885
import sys
b99885
if len (sys.argv) < 3:
b99885
    print "Specify hpcups.drv and hpijs.drv pathnames"
b99885
    sys.exit (1)
b99885
b99885
hpcups_drv = sys.argv[1]
b99885
hpijs_drv = sys.argv[2]
b99885
b99885
# Match e.g.      Model "ModelName"
b99885
# and catch 'ModelName' in group 0
b99885
mn_re = re.compile ('^\s*ModelName\s+"(.*)"\s*$')
b99885
b99885
# Match e.g.      Attribute "1284DeviceID" "" "blah"
b99885
# and catch everything before 'blah' in group 0, 'blah' in group 1,
b99885
# trailing characters in group 2
b99885
devid_re = re.compile ('^(\s*Attribute\s+"1284DeviceID"\s+""\s+")(.*)("\s*)$')
b99885
b99885
# Match e.g.   }
b99885
end_re = re.compile ('^\s*}')
b99885
b99885
devid_by_mn = dict()
b99885
b99885
hpcups_lines = file (hpcups_drv, "r").readlines ()
b99885
current_mn = None
b99885
for line in hpcups_lines:
b99885
    if current_mn == None:
b99885
        match = mn_re.match (line)
b99885
        if match == None:
b99885
            continue
b99885
b99885
        current_mn = match.groups ()[0]
b99885
    else:
b99885
        match = devid_re.match (line)
b99885
        if match:
b99885
            devid_by_mn[current_mn] = match.groups ()[1]
b99885
            continue
b99885
b99885
    if end_re.match (line):
b99885
        current_mn = None
b99885
b99885
print >>sys.stderr, \
b99885
    "%d IEEE 1284 Device IDs loaded from %s" % (len (devid_by_mn),
b99885
                                                os.path.basename (hpcups_drv))
b99885
b99885
replaced = 0
b99885
hpijs_lines = file (hpijs_drv, "r").readlines ()
b99885
current_mn = None
b99885
for line in hpijs_lines:
b99885
    if current_mn == None:
b99885
        match = mn_re.match (line)
b99885
        if match:
b99885
            current_mn = match.groups ()[0]
b99885
            if current_mn.endswith (" hpijs"):
b99885
                current_mn = current_mn[:-6]
b99885
    else:
b99885
        match = devid_re.match (line)
b99885
        if match:
b99885
            devid = devid_by_mn.get (current_mn)
b99885
            if devid:
b99885
                line = (match.groups ()[0] + devid + match.groups ()[2])
b99885
                replaced += 1
b99885
            else:
b99885
                print >>sys.stderr, "Not matched: %s" % current_mn
b99885
b99885
    if end_re.match (line):
b99885
        current_mn = None
b99885
b99885
    print line.rstrip ("\n")
b99885
b99885
print >>sys.stderr, \
b99885
    "%d IEEE 1284 Device IDs replaced in %s" % (replaced,
b99885
                                                os.path.basename (hpijs_drv))