def main(args): global errors global model_dat line_num = 0 log.set_module("dat2drv.py") cur_path = os.path.realpath(os.path.normpath(os.getcwd())) dat_path = os.path.join(cur_path, 'data', 'models') model_dat = models.ModelData(dat_path) load_models() verbose = False quiet = False try: opts, args = getopt.getopt(args, 'd:l:ho:vq', ['logging=', 'help', 'help-rest', 'help-man', 'drv=', 'output=', 'verbose', 'quiet']) except getopt.GetoptError as e: log.error(e.msg) usage() sys.exit(0) log_level = 'info' if os.getenv("HPLIP_DEBUG"): log.set_level('debug') for o, a in opts: if o in ('-h', '--help'): usage() elif o == '--help-rest': usage('rest') elif o == '--help-man': usage('man') elif o in ('-v', '--verbose'): verbose = True elif o in ('-q', '--quiet'): quiet = True elif o in ('-l', '--logging'): log.set_level(a.lower().strip()) if not quiet: utils.log_title(__title__, __version__) drv_dir = os.path.join(cur_path, 'prnt', 'drv') errors = [] warns = [] notes = [] for template_file in utils.walkFiles(drv_dir, recurse=False, abs_paths=True, return_folders=False, pattern='*.in.template'): basename = os.path.basename(template_file).split('.')[0] # Output drv_in_file = os.path.join(cur_path, 'prnt', 'drv', '%s.drv.in' % basename) # XML output (per model) output_path = os.path.join(cur_path, 'prnt', 'drv', 'foomatic_xml', basename) # XML Output (master driver list) driver_path = os.path.join(cur_path, 'prnt', 'drv', 'foomatic_xml', basename, '%s.xml' % basename) log.info("Working on %s file..." % basename) log.info("Input file: %s" % template_file) log.info("Output file: %s" % drv_in_file) log.info("Output XML directory: %s" % output_path) log.info("Output driver XML file: %s" % driver_path) # CREATE DRV.IN FILE log.info("Processing %s.drv.in.template..." % basename) tui.update_spinner() template_classes = [] template_file_f = open(template_file, 'r') drv_in_file_f = open(drv_in_file, 'w') models_placement = {} for m in models_dict: models_placement[m] = 0 line = 0 for x in template_file_f: if verbose: log.info(x.strip()) line += 1 tui.update_spinner() drv_in_file_f.write(x) match = pat_template.match(x) if match is not None: matches = [] indent = match.group(1) indent2 = ' '*(len(indent)+2) classes = match.group(2).split(':') tech_class = classes[0] if tech_class not in models.TECH_CLASSES: errors.append("(%s:line %d) Invalid tech-class (%s): %s" % (basename, line, tech_class, x.strip())) continue template_classes.append(tech_class) tech_subclass = classes[1:] ok = True for sc in tech_subclass: if sc not in models.TECH_SUBCLASSES: errors.append("(%s:line %d) Invalid tech-subclass (%s): %s" % (basename, line, sc, x.strip())) ok = False if not ok: continue for m in models_dict: include = False if tech_class in models_dict[m]['tech-class'] and \ len(models_dict[m]['tech-subclass']) == len(tech_subclass): for msc in models_dict[m]['tech-subclass']: if msc not in tech_subclass: break else: include = True if include: models_placement[m] += 1 matches.append(m) if matches: try: matches.sort(key=lambda y: pat_prod_num.search(y).group(1)) except: matches.sort(key=str.lower) for p in matches: if verbose: log.info("(%s) Adding section for model: %s" % (basename, p)) drv_in_file_f.write("%s{\n" % indent) if basename == 'hpcups': model_name = models_dict[p]['norm_model'] else: model_name = models_dict[p]['norm_model'] + " %s" % basename orig_model_name = model_name while True: if len(model_name) > 31: for k in SHORTENING_REPLACEMENTS: if k in model_name.lower(): model_name = utils.ireplace(model_name, k, SHORTENING_REPLACEMENTS[k]) model_name = model_name.replace(' ', ' ') if len(model_name) < 32: warns.append('len("%s")>31, shortened to len("%s")=%d using sub-brand shortening replacements.' % (orig_model_name, model_name, len(model_name))) break if len(model_name) < 32: break if "series" in model_name.lower(): model_name = utils.ireplace(model_name, "series", "Ser.") model_name = model_name.replace(' ', ' ') if len(model_name) < 32: warns.append('len("%s")>31, shortened to len("%s")=%d using "series" to "ser." replacement.' % (orig_model_name, model_name, len(model_name))) break if "ser." in model_name.lower(): model_name = utils.ireplace(model_name, "ser.", "") model_name = model_name.replace(' ', ' ') if len(model_name) < 32: warns.append('len("%s")>31, shortened to len("%s")=%d using "ser." removal.' % (orig_model_name, model_name, len(model_name))) break if len(model_name) > 31: model_name = model_name[:31] errors.append('len("%s")>31 chars, could not shorten to <32. Truncating to 31 chars (%s).' % (orig_model_name, model_name)) break drv_in_file_f.write('%sModelName "%s"\n' % (indent2, orig_model_name)) if len(models_dict[p]['tech-class']) > 1: if basename == "hpcups": drv_in_file_f.write('%sAttribute "NickName" "" "%s %s, %s $Version' % (indent2, orig_model_name, models.TECH_CLASS_PDLS[tech_class],basename)) else: drv_in_file_f.write('%sAttribute "NickName" "" "%s %s, $Version' % (indent2, orig_model_name, models.TECH_CLASS_PDLS[tech_class])) else: if basename == "hpcups": drv_in_file_f.write('%sAttribute "NickName" "" "%s, %s $Version' % (indent2, orig_model_name, basename)) else: drv_in_file_f.write('%sAttribute "NickName" "" "%s, $Version' % (indent2, orig_model_name)) if models_dict[p]['plugin'] in (1, 2): if (models_dict[p]['plugin-reason'] & 15 ) in (1, 2, 3, 4, 5, 6, 8, 9, 10, 12): drv_in_file_f.write(', requires proprietary plugin') drv_in_file_f.write('"\n') drv_in_file_f.write('%sAttribute "ShortNickName" "" "%s"\n' % (indent2, model_name)) pp = p.replace('_', ' ') if 'apollo' in p.lower(): devid = "MFG:Apollo;MDL:%s;DES:%s;" % (pp, pp) elif 'laserjet' in p.lower() or 'designjet' in p.lower(): devid = "MFG:Hewlett-Packard;MDL:%s;DES:%s;" % (pp, pp) else: devid = "MFG:HP;MDL:%s;DES:%s;" % (pp, pp) drv_in_file_f.write('%sAttribute "1284DeviceID" "" "%s"\n' % (indent2, devid)) if len(models_dict[p]['tech-class']) > 1: if basename == 'hpcups': drv_in_file_f.write('%sPCFileName "%s-%s.ppd"\n' % (indent2, fixFileName(p), models.TECH_CLASS_PDLS[tech_class])) else: drv_in_file_f.write('%sPCFileName "%s-%s-%s.ppd"\n' % (indent2, fixFileName(p), basename, models.TECH_CLASS_PDLS[tech_class])) elif tech_class != 'Postscript': if basename == 'hpcups': drv_in_file_f.write('%sPCFileName "%s.ppd"\n' % (indent2, fixFileName(p))) else: drv_in_file_f.write('%sPCFileName "%s-%s.ppd"\n' % (indent2, fixFileName(p), basename)) else: drv_in_file_f.write('%sPCFileName "%s-ps.ppd"\n' % (indent2, fixFileName(p))) for c in models_dict[p]['case_models']: drv_in_file_f.write('%sAttribute "Product" "" "(%s)"\n' % (indent2, c)) drv_in_file_f.write("%s}\n" % indent) else: errors.append("(%s:line %d) No models matched the specified classes on line: %s" % (basename, line, x.strip())) else: match = pat_template2.match(x) if match is not None: errors.append("(%s:line %d) Malformed line: %s (missing initial //)" % (basename, line, x.strip())) template_file_f.close() drv_in_file_f.close() tui.cleanup_spinner() for tc in models.TECH_CLASSES: if tc.lower() in ('undefined', 'postscript', 'unsupported'): continue if tc not in template_classes: warns.append("(%s) Section <%%%s:...%%> not found." % (basename, tc)) # OUTPUT XML FILES if not os.path.exists(output_path): os.makedirs(output_path) if os.path.exists(driver_path): os.remove(driver_path) files_to_delete = [] for f in utils.walkFiles(output_path, recurse=True, abs_paths=True, return_folders=False, pattern='*'): files_to_delete.append(f) for f in files_to_delete: os.remove(f) driver_f = open(driver_path, 'w') driver_doc = XMLDocument("driver", id="driver/hplip") name_node = driver_doc.add("name") name_node.addText("hplip") url_node = driver_doc.add("url") url_node.addText("http://hplipopensource.com") supplier_node = driver_doc.add("supplier") supplier_node.addText("Hewlett-Packard") mfg_node = driver_doc.add("manufacturersupplied") mfg_node.addText("HP|Apollo") lic_node = driver_doc.add("license") lic_node.addText("BSD/GPL/MIT") driver_doc.add("freesoftware") support_node = driver_doc.add("supportcontact", level="voluntary", url="https://launchpad.net/hplip") support_node.addText("HPLIP Support at Launchpad.net") shortdesc_node = driver_doc.add("shortdescription") shortdesc_en_node = shortdesc_node.add("en") shortdesc_en_node.addText("HP's driver suite for printers and multi-function devices") func_node = driver_doc.add("functionality") maxresx_node = func_node.add("maxresx") maxresx_node.addText("1200") maxresy_node = func_node.add("maxresy") maxresy_node.addText("1200") func_node.add("color") exec_node = driver_doc.add("execution") exec_node.add("nopjl") exec_node.add("ijs") proto_node = exec_node.add("prototype") #proto_node.addText("gs -q -dBATCH -dPARANOIDSAFER -dQUIET -dNOPAUSE -sDEVICE=ijs -sIjsServer=hpijs%A%B%C -dIjsUseOutputFD%Z -sOutputFile=- -") comments_node = driver_doc.add("comments") comments_en_node = comments_node.add("en") comments_en_node.addText("") printers_node = driver_doc.add("printers") for m in models_dict: if models_dict[m]['support-type'] == SUPPORT_TYPE_NONE: continue if 'apollo' in m.lower(): make = 'Apollo' else: make = 'HP' if 'apollo' in m.lower(): ieee1284 = "MFG:Apollo;MDL:%s;DES:%s;" % (m, m) else: ieee1284 = "MFG:HP;MDL:%s;DES:%s;" % (m, m) postscriptppd = '' if 'Postscript' in models_dict[m]['tech-class']: postscriptppd = "%s-ps.ppd" % fixFileName(m) stripped_model = m if stripped_model.startswith('hp_'): stripped_model = stripped_model.replace('hp_', '').capitalize() elif stripped_model.startswith('apollo_'): stripped_model = stripped_model.replace('apollo_', '').capitalize() fixed_model = stripped_model.replace('_', ' ').capitalize() # Output to the per-model XML file outputModel(m, fixed_model, stripped_model, make, postscriptppd, ieee1284, output_path, verbose) # Output to driver master XML file outputDriver(m, fixed_model, stripped_model, make, printers_node, verbose) driver_f.write(str(driver_doc)) driver_f.close() # Make sure all models ended up in drv.in file log.info("Checking for errors...") tui.update_spinner() for m in models_dict: tui.update_spinner() tc = models_dict[m]['tech-class'] st = models_dict[m]['support-type'] if not tc or 'Undefined' in tc: if st: errors.append('(%s) Invalid tech-class for model %s ("Undefined" or missing)' % (basename, m)) #else: # warns.append('(%s) Invalid tech-class for unsupported model %s ("Undefined" or missing)' % (basename, m)) else: if not models_placement[m] and st and \ len(tc) == 1 and 'Postscript' not in tc: sects = [] for tc in models_dict[m]['tech-class']: for sc in models_dict[m]['tech-subclass']: sects.append(sc) errors.append("(%s) Model '%s' did not have a matching section. Needed section: <%%%s:%s%%>" % (basename, m, tc, ':'.join(sects))) if len(tc) == 1 and 'Postscript' in tc: notes.append("(%s) Postscript-only model %s was not included in DRV file." % (basename, m)) tui.cleanup_spinner() # end for if not quiet or verbose: if notes: tui.header("NOTES") for n in notes: log.note(n) if warns: tui.header("WARNINGS") for w in warns: log.warn(w) if errors: tui.header("ERRORS") for e in errors: log.error(e) else: if warns: log.warn("%d warnings" % len(warns)) if errors: log.error("%d errors" % len(errors))
drv_in_file_f.write("%s{\n" % indent) if basename == 'hpcups': model_name = models_dict[p]['norm_model'] else: model_name = models_dict[p][ 'norm_model'] + " %s" % basename orig_model_name = model_name while True: if len(model_name) > 31: for k in SHORTENING_REPLACEMENTS: if k in model_name.lower(): model_name = utils.ireplace( model_name, k, SHORTENING_REPLACEMENTS[k]) model_name = model_name.replace( ' ', ' ') if len(model_name) < 32: warns.append( 'len("%s")>31, shortened to len("%s")=%d using sub-brand shortening replacements.' % (orig_model_name, model_name, len(model_name))) break if len(model_name) < 32: break if "series" in model_name.lower():
log.info("(%s) Adding section for model: %s" % (basename, p)) drv_in_file_f.write("%s{\n" % indent) if basename == 'hpcups': model_name = models_dict[p]['norm_model'] else: model_name = models_dict[p]['norm_model'] + " %s" % basename orig_model_name = model_name while True: if len(model_name) > 31: for k in SHORTENING_REPLACEMENTS: if k in model_name.lower(): model_name = utils.ireplace(model_name, k, SHORTENING_REPLACEMENTS[k]) model_name = model_name.replace(' ', ' ') if len(model_name) < 32: warns.append('len("%s")>31, shortened to len("%s")=%d using sub-brand shortening replacements.' % (orig_model_name, model_name, len(model_name))) break if len(model_name) < 32: break if "series" in model_name.lower(): model_name = utils.ireplace(model_name, "series", "Ser.") model_name = model_name.replace(' ', ' ') if len(model_name) < 32: warns.append('len("%s")>31, shortened to len("%s")=%d using "series" to "ser." replacement.' % (orig_model_name, model_name, len(model_name)))