makewxs.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # @brief WiX wxsd file generator
00004 # @date $Date: 2008-02-26 13:58:13 $
00005 # @author Norkai Ando <n-ando@aist.go.jp>
00006 #
00007 # Copyright (C) 2008
00008 #     Noriaki Ando
00009 #     Task-intelligence Research Group,
00010 #     Intelligent Systems Research Institute,
00011 #     National Institute of
00012 #         Advanced Industrial Science and Technology (AIST), Japan
00013 #     All rights reserved.
00014 #
00015 # $Id: makewxs.py 1686 2010-01-18 13:36:16Z n-ando $
00016 #
00017 
00018 import yaml
00019 import yat
00020 import sys
00021 import uuid
00022 import getopt
00023 import os
00024 
00025 def replace_uuid(text):
00026     token0 = text.split("__GUID__")
00027     text0 = token0[0]
00028     for i in range(1, len(token0)):
00029         u = str(uuid.uuid1()).upper()
00030         text0 += u + token0[i]
00031 
00032     token1 = text0.split("__UUID")
00033     text1 = token1[0]
00034     for i in range(1, len(token1)):
00035         u = "_" + str(uuid.uuid1()).replace("-", "")
00036         text1 += u + token1[i]
00037     return text1
00038 
00039 class file_list:
00040     def __init__(self, comp, path, files):
00041         self.comp = comp
00042         self.path = self.check_path(path)
00043         self.files = self.to_basename(files)
00044         self.shortnames = {}
00045         self.shortext = {}
00046         self.output = ""
00047         self.head = """%s:
00048   GUID: %s
00049   Files:
00050 """
00051         self.fitem = """    - Id: %s
00052       ShortName: %s
00053       Name: %s
00054       Source: %s
00055 """
00056         self.count = -1
00057 
00058     def check_path(self, path):
00059         if path[-1] != "\\" or path[-1] != "/":
00060             return path + "\\"
00061 
00062     def to_basename(self, files):
00063         out = []
00064         for f in files:
00065             out.append(os.path.basename(f))
00066         return out
00067 
00068     def to_shortname(self, fname):
00069         try:
00070             name, ext = fname.rsplit(".", 1)
00071         except:
00072             name = fname
00073             ext  = ""
00074         if name != None and len(name) > 8:
00075             short_name = name[:5] + self.sn_num(name[:5])
00076         else:
00077             short_name = name
00078         if ext != None and len(ext) > 3:
00079             short_ext = ext[:3]
00080         else:
00081             short_ext = ext
00082         if short_ext == "":
00083             return short_name
00084         return short_name + "." + short_ext
00085 
00086     def id(self):
00087         self.count += 1
00088         return self.comp + '%04d' % (self.count)
00089         
00090     def sn_num(self, name):
00091         if self.shortnames.has_key(name):
00092             self.shortnames[name] += 1
00093         else:
00094             self.shortnames[name] = 0
00095         return "%03d" % (self.shortnames[name])
00096 
00097     def se_num(self, ext):
00098         if self.shortext.has_key(ext):
00099             self.shortext[ext] += 1
00100         else:
00101             self.shortext[ext] = 0
00102         return "%01d" % (self.shortext[ext])
00103 
00104     def write(self, text):
00105         self.output += text
00106 
00107     def escape(self, text):
00108         return text.replace("\\", "\\\\")
00109 
00110     def yaml(self):
00111         self.write(self.head % (self.comp, str(uuid.uuid1()).upper()))
00112         for fname in self.files:
00113             self.write(self.fitem % (self.escape(self.id()),
00114                                      self.escape(self.to_shortname(fname)),
00115                                      self.escape(fname),
00116                                      self.escape(self.path + fname)))
00117         return self.output
00118 
00119 
00120 class make_wxs:
00121     def __init__(self, outfile, infile, yaml_files):
00122         self.outfile = outfile
00123         self.template = self.load_template(infile)
00124         self.dict = self.load_yaml(yaml_files)
00125 
00126     def load_template(self, template):
00127         fd = open(template, 'r')
00128         t = yat.Template(fd.read(), "{% ", " %}")
00129         fd.close()
00130         return t
00131 
00132     def load_yaml(self, yaml_files):
00133         yaml_text = ""
00134         for f in yaml_files:
00135             fd = open(f, "r")
00136             yaml_text += replace_uuid(fd.read())
00137             fd.close()
00138         return yaml.load(yaml_text)
00139 
00140     def generate(self):
00141         of = open(self.outfile, 'w')
00142         of.write(self.template.generate(self.dict))
00143         of.close()
00144 
00145 
00146 def usage():
00147     print """makewxs.py cmd options
00148 commands:
00149   flist: make file list to be included wxs file
00150   wxs  : make wxs file from a input template file and yaml files 
00151 examples:
00152   makewxs.py flist -c ComponentName -p Path -o OutputFilename file_names...
00153   makewxs.py wxs -o Output.wxs -i InputTempalte input_yaml_files...
00154 """
00155 
00156 
00157 def main(argv):
00158     if len(argv) == 0:
00159         usage()
00160         sys.exit(-1)
00161 
00162     cmd = argv[0]
00163     out = None
00164     if cmd == "flist":
00165         opts, args = getopt.getopt(argv[1:], "c:p:o:", [])
00166         if opts == None:
00167             usage()
00168             sys.exit(-1)
00169             return
00170         for o, a in opts:
00171             if o in ("-c"):
00172                 comp = a
00173             if o in ("-p"):
00174                 path = a
00175             if o in ("-o"):
00176                 out = a
00177         fl = file_list(comp, path, args)
00178         if out == None:
00179             f = sys.stdout
00180         else:
00181             f = open(out, "w")
00182         f.write(fl.yaml())
00183         f.close()
00184     elif cmd == "wxs":
00185         opts, args = getopt.getopt(argv[1:], "o:i:", [])
00186         if opts == None:
00187             usage()
00188             sys.exit(-1)
00189             return
00190         for o, a in opts:
00191             if o in ("-o"):
00192                 outfile = a
00193             if o in ("-i"):
00194                 infile = a
00195         wxs = make_wxs(outfile, infile, args)
00196         wxs.generate()
00197     elif cmd == "":
00198         pass
00199     else:
00200         pass
00201         
00202 
00203 if __name__ == "__main__":
00204     main(sys.argv[1:])


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28