makewxs.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # @brief WiX wxsd file generator
4 # @date $Date: 2008-02-26 13:58:13 $
5 # @author Norkai Ando <n-ando@aist.go.jp>
6 #
7 # Copyright (C) 2008
8 # Noriaki Ando
9 # Task-intelligence Research Group,
10 # Intelligent Systems Research Institute,
11 # National Institute of
12 # Advanced Industrial Science and Technology (AIST), Japan
13 # All rights reserved.
14 #
15 # $Id: makewxs.py 1686 2010-01-18 13:36:16Z n-ando $
16 #
17 
18 import yaml
19 import yat
20 import sys
21 import uuid
22 import getopt
23 import os
24 
25 def replace_uuid(text):
26  token0 = text.split("__GUID__")
27  text0 = token0[0]
28  for i in range(1, len(token0)):
29  u = str(uuid.uuid1()).upper()
30  text0 += u + token0[i]
31 
32  token1 = text0.split("__UUID")
33  text1 = token1[0]
34  for i in range(1, len(token1)):
35  u = "_" + str(uuid.uuid1()).replace("-", "")
36  text1 += u + token1[i]
37  return text1
38 
39 class file_list:
40  def __init__(self, comp, path, files):
41  self.comp = comp
42  self.path = self.check_path(path)
43  self.files = self.to_basename(files)
44  self.shortnames = {}
45  self.shortext = {}
46  self.output = ""
47  self.head = """%s:
48  GUID: %s
49  Files:
50 """
51  self.fitem = """ - Id: %s
52  ShortName: %s
53  Name: %s
54  Source: %s
55 """
56  self.count = -1
57 
58  def check_path(self, path):
59  if path[-1] != "\\" or path[-1] != "/":
60  return path + "\\"
61 
62  def to_basename(self, files):
63  out = []
64  for f in files:
65  out.append(os.path.basename(f))
66  return out
67 
68  def to_shortname(self, fname):
69  try:
70  name, ext = fname.rsplit(".", 1)
71  except:
72  name = fname
73  ext = ""
74  if name != None and len(name) > 8:
75  short_name = name[:5] + self.sn_num(name[:5])
76  else:
77  short_name = name
78  if ext != None and len(ext) > 3:
79  short_ext = ext[:3]
80  else:
81  short_ext = ext
82  if short_ext == "":
83  return short_name
84  return short_name + "." + short_ext
85 
86  def id(self):
87  self.count += 1
88  return self.comp + '%04d' % (self.count)
89 
90  def sn_num(self, name):
91  if self.shortnames.has_key(name):
92  self.shortnames[name] += 1
93  else:
94  self.shortnames[name] = 0
95  return "%03d" % (self.shortnames[name])
96 
97  def se_num(self, ext):
98  if self.shortext.has_key(ext):
99  self.shortext[ext] += 1
100  else:
101  self.shortext[ext] = 0
102  return "%01d" % (self.shortext[ext])
103 
104  def write(self, text):
105  self.output += text
106 
107  def escape(self, text):
108  return text.replace("\\", "\\\\")
109 
110  def yaml(self):
111  self.write(self.head % (self.comp, str(uuid.uuid1()).upper()))
112  for fname in self.files:
113  self.write(self.fitem % (self.escape(self.id()),
114  self.escape(self.to_shortname(fname)),
115  self.escape(fname),
116  self.escape(self.path + fname)))
117  return self.output
118 
119 
120 class make_wxs:
121  def __init__(self, outfile, infile, yaml_files):
122  self.outfile = outfile
123  self.template = self.load_template(infile)
124  self.dict = self.load_yaml(yaml_files)
125 
126  def load_template(self, template):
127  fd = open(template, 'r')
128  t = yat.Template(fd.read(), "{% ", " %}")
129  fd.close()
130  return t
131 
132  def load_yaml(self, yaml_files):
133  yaml_text = ""
134  for f in yaml_files:
135  fd = open(f, "r")
136  yaml_text += replace_uuid(fd.read())
137  fd.close()
138  return yaml.load(yaml_text)
139 
140  def generate(self):
141  of = open(self.outfile, 'w')
142  of.write(self.template.generate(self.dict))
143  of.close()
144 
145 
146 def usage():
147  print """makewxs.py cmd options
148 commands:
149  flist: make file list to be included wxs file
150  wxs : make wxs file from a input template file and yaml files
151 examples:
152  makewxs.py flist -c ComponentName -p Path -o OutputFilename file_names...
153  makewxs.py wxs -o Output.wxs -i InputTempalte input_yaml_files...
154 """
155 
156 
157 def main(argv):
158  if len(argv) == 0:
159  usage()
160  sys.exit(-1)
161 
162  cmd = argv[0]
163  out = None
164  if cmd == "flist":
165  opts, args = getopt.getopt(argv[1:], "c:p:o:", [])
166  if opts == None:
167  usage()
168  sys.exit(-1)
169  return
170  for o, a in opts:
171  if o in ("-c"):
172  comp = a
173  if o in ("-p"):
174  path = a
175  if o in ("-o"):
176  out = a
177  fl = file_list(comp, path, args)
178  if out == None:
179  f = sys.stdout
180  else:
181  f = open(out, "w")
182  f.write(fl.yaml())
183  f.close()
184  elif cmd == "wxs":
185  opts, args = getopt.getopt(argv[1:], "o:i:", [])
186  if opts == None:
187  usage()
188  sys.exit(-1)
189  return
190  for o, a in opts:
191  if o in ("-o"):
192  outfile = a
193  if o in ("-i"):
194  infile = a
195  wxs = make_wxs(outfile, infile, args)
196  wxs.generate()
197  elif cmd == "":
198  pass
199  else:
200  pass
201 
202 
203 if __name__ == "__main__":
204  main(sys.argv[1:])
def __init__(self, outfile, infile, yaml_files)
Definition: makewxs.py:121
def escape(self, text)
Definition: makewxs.py:107
def yaml(self)
Definition: makewxs.py:110
def uuid1(node=None, clock_seq=None)
def sn_num(self, name)
Definition: makewxs.py:90
def __init__(self, comp, path, files)
Definition: makewxs.py:40
def replace_uuid(text)
Definition: makewxs.py:25
def generate(self)
Definition: makewxs.py:140
def se_num(self, ext)
Definition: makewxs.py:97
def load_template(self, template)
Definition: makewxs.py:126
def check_path(self, path)
Definition: makewxs.py:58
def id(self)
Definition: makewxs.py:86
def write(self, text)
Definition: makewxs.py:104
def usage()
Definition: makewxs.py:146
def to_basename(self, files)
Definition: makewxs.py:62
def load_yaml(self, yaml_files)
Definition: makewxs.py:132
def to_shortname(self, fname)
Definition: makewxs.py:68
def main(argv)
Definition: makewxs.py:157


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:34