slntool.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # @brief Visual Studio solution generator
4 # @date $Date: 2008-03-06 06:46:37 $
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: slntool.py 1863 2010-02-11 06:31:11Z n-ando $
16 #
17 
18 import sys
19 import re
20 import yaml
21 import yat
22 
23 #------------------------------------------------------------
24 # Generic vcproj template
25 #------------------------------------------------------------
26 vcversions = {"VC8": {"sln": "9.00", "vc": "2005"},
27  "VC9": {"sln": "10.00", "vc": "2008"},
28  "VC10": {"sln": "11.00", "vc": "2010"}
29  }
30 sln_template = """Microsoft Visual Studio Solution File, Format Version %s
31 # Visual Studio %s
32 [for proj in Projects]
33 Project("{[SolutionGUID]}") = "[proj.Name]", "[proj.FileName]", "{[proj.GUID]}"
34  ProjectSection(ProjectDependencies) = postProject
35 [if-any proj.Depend]
36 [for dep in proj.Depend]
37  {[dep]} = {[dep]}
38 [endfor]
39 [endif]
40  EndProjectSection
41 EndProject
42 [endfor]
43 Global
44  GlobalSection(SolutionConfigurationPlatforms) = preSolution
45 [for conf in Configurations]
46  [conf] = [conf]
47 
48 [endfor]
49  EndGlobalSection
50  GlobalSection(ProjectConfigurationPlatforms) = postSolution
51 [for proj in Projects]
52 [for conf in Configurations]
53  {[proj.GUID]}.[conf].ActiveCfg = [conf]
54 
55  {[proj.GUID]}.[conf].Build.0 = [conf]
56 
57 [endfor]
58 [endfor]
59  EndGlobalSection
60  GlobalSection(SolutionProperties) = preSolution
61  HideSolutionNode = FALSE
62  EndGlobalSection
63 EndGlobal
64 """
65 
66 
67 sln_yaml = """
68 SolutionGUID: 8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942
69 Configurations:
70  - Release|Win32
71  - Debug|Win32
72 """
73 
74 
75 def usage():
76  print """
77 Usage:
78  slntool.py --dep dep_file [--outfile outfile] vcproj_files...
79 
80 Options:
81  --vcversion: Visual C++'s version [VC8|VC9|VC10]
82  --dep: dependency file
83  --out or --output: output file name
84 
85 Example:
86  * make solution from all of found vcprojs
87  slntool.py --dep dep_file --outfile MyApp.sln \
88  `find ./ --name '*.vcproj'`
89  * output solution to stdout
90  slntool.py --dep dep_file *.vcproj
91 
92 Depfile examples: The rule is similar to Makefile's dependency rule
93  All of entries should be Projectname.
94  [target Projectname]: [dependent projectsname...]
95 
96 In the following App.dep file, "App" dpends on "Lib1" and "Lib2".
97 "Lib2" depends on "Lib1", "Lib1" has no dependency.
98 For this solution, "App", "Lib1" and "Lib2"'s project files are necessary.
99 
100 -- App.dep --
101 App: Lib1 Lib2
102 Lib2: Lib1
103 -------------
104 
105 """
106 
107 
108 def get_projinfo(fname):
109  name = None
110  guid = None
111  re_guid = re.compile('^.*?ProjectGUID=\"{(.*)}\"')
112  re_name = re.compile('^.*?Name=\"(.*)\"')
113  fd = open(fname, "r")
114  pj = fd.readlines()
115  for t in pj:
116  n = re_name.match(t)
117  g = re_guid.match(t)
118 
119  if name == None and n:
120  name = n.group(1)
121  if guid == None and g:
122  guid = g.group(1)
123 
124  if name and guid:
125  break
126  fd.close()
127  return {"Name": name, "GUID": guid, "FileName": fname}
128 
129 def get_projinfo(fname,vcversion):
130  name = None
131  guid = None
132  regexs = {"VC8": {"guid":'^.*?ProjectGUID=\"{(.*)}\"',"name":'^.*?Name=\"(.*)\"'},
133  "VC9": {"guid":'^.*?ProjectGUID=\"{(.*)}\"',"name":'^.*?Name=\"(.*)\"'},
134  "VC10": {"guid":'^.*?<ProjectGuid>{(.*)}</ProjectGuid>',"name":'^.*<ProjectName>(.*)</ProjectName>'}
135  }
136  re_guid = re.compile(regexs[vcversion]["guid"])
137  re_name = re.compile(regexs[vcversion]["name"])
138  fd = open(fname, "r")
139  pj = fd.readlines()
140  for t in pj:
141  n = re_name.match(t)
142  g = re_guid.match(t)
143 
144  if name == None and n:
145  name = n.group(1)
146  if guid == None and g:
147  guid = g.group(1)
148 
149  if name and guid:
150  break
151  fd.close()
152  return {"Name": name, "GUID": guid, "FileName": fname}
153 
154 def get_dependencies(fname):
155  if fname == None: return {}
156  depdic = {}
157  fd = open(fname, "r")
158  for l in fd.readlines():
159  (key, val) = l.split(":")
160  vals = val.split()
161  depdic[key] = vals
162  return depdic
163 
164 def parse_args(argv):
165  argc = len(argv)
166  depfile = None
167  outfile = None
168  vcversion = "VC8"
169  flist = []
170  i = 0
171  while i < argc:
172  opt = argv[i]
173  if opt == "--dep":
174  i += 1
175  if i < argc: depfile = argv[i]
176  else: raise InvalidOption(opt + " needs value")
177  elif opt == "--output" or opt == "--out":
178  i += 1
179  if i < argc: outfile = argv[i]
180  else: raise InvalidOption(opt + " needs value")
181  elif opt == "--vcversion":
182  i += 1
183  if i < argc: vcversion = argv[i]
184  else: raise InvalidOption(opt + " needs value")
185  if not vcversions.has_key(vcversion):
186  allowedvers = vcversions.keys().__repr__()
187  raise InvalidOption("allowed vcversions are " + allowedvers)
188  else:
189  while i < argc and argv[i][:2] != "--":
190  flist.append(argv[i])
191  i += 1
192  i += 1
193  return (vcversion, depfile, outfile, flist)
194 
195 def get_slnyaml(depfile, projfiles):
196  depdict = get_dependencies(depfile)
197  projs = []
198  projlist = """Projects:
199 """
200  for f in projfiles:
201  pj = get_projinfo(f)
202  if depdict.has_key(pj["Name"]):
203  pj["Depend"] = depdict[pj["Name"]]
204  projs.append(pj)
205  def depsort(d0, d1):
206  """
207  d0 < d1: return -1
208  d0 == d1: return 0
209  d0 > d1: return 1
210  """
211  d0_depends = d0.has_key("Depend")
212  d1_depends = d1.has_key("Depend")
213  if not d0_depends and not d1_depends:
214  # both d0, d1 has no dependency
215  return 0
216 
217  if not d0_depends and d1_depends:
218  # only "d1" has dependency: d0 < d1
219  return -1
220 
221  if d0_depends and not d1_depends:
222  # only "d0" has dependency: d1 < d0
223  return 1
224 
225  # d0 and d1 has dependency
226  d0_in_dep = depdict.has_key(d0["Name"])
227  d1_in_dep = depdict.has_key(d1["Name"])
228  if not d0_in_dep and not d1_in_dep:
229  return 0
230  if not d0_in_dep and d1_in_dep:
231  return -1
232  if d0_in_dep and not d1_in_dep:
233  return 1
234 
235  # both d0 and d1 have several dependency
236  if depdict[d0["Name"]].count(d1["Name"]) > 0:
237  return 1
238  if depdict[d1["Name"]].count(d0["Name"]) > 0:
239  return -1
240  return 0
241 
242  projs.sort(depsort)
243  for pj in projs:
244  list = """ - Name: %s
245  FileName: %s
246  GUID: &%s %s
247  Depend:
248 """ % (pj["Name"], pj["FileName"], pj["Name"], pj["GUID"])
249  if pj.has_key("Depend"):
250  for dep in pj["Depend"]:
251  dep = """ - *%s
252 """ % (dep)
253  list += dep
254  projlist += list
255  yaml_text = sln_yaml + projlist
256  return yaml_text
257 
258 def get_slnyaml(depfile, projfiles, vcversion):
259  depdict = get_dependencies(depfile)
260  projs = []
261  projlist = """Projects:
262 """
263  for f in projfiles:
264  pj = get_projinfo(f, vcversion)
265  if depdict.has_key(pj["Name"]):
266  pj["Depend"] = depdict[pj["Name"]]
267  projs.append(pj)
268  def depsort(d0, d1):
269  """
270  d0 < d1: return -1
271  d0 == d1: return 0
272  d0 > d1: return 1
273  """
274  d0_depends = d0.has_key("Depend")
275  d1_depends = d1.has_key("Depend")
276  if not d0_depends and not d1_depends:
277  # both d0, d1 has no dependency
278  return 0
279 
280  if not d0_depends and d1_depends:
281  # only "d1" has dependency: d0 < d1
282  return -1
283 
284  if d0_depends and not d1_depends:
285  # only "d0" has dependency: d1 < d0
286  return 1
287 
288  # d0 and d1 has dependency
289  d0_in_dep = depdict.has_key(d0["Name"])
290  d1_in_dep = depdict.has_key(d1["Name"])
291  if not d0_in_dep and not d1_in_dep:
292  return 0
293  if not d0_in_dep and d1_in_dep:
294  return -1
295  if d0_in_dep and not d1_in_dep:
296  return 1
297 
298  # both d0 and d1 have several dependency
299  if depdict[d0["Name"]].count(d1["Name"]) > 0:
300  return 1
301  if depdict[d1["Name"]].count(d0["Name"]) > 0:
302  return -1
303  return 0
304 
305  projs.sort(depsort)
306  for pj in projs:
307  list = """ - Name: %s
308  FileName: %s
309  GUID: &%s %s
310  Depend:
311 """ % (pj["Name"], pj["FileName"], pj["Name"], pj["GUID"])
312  if pj.has_key("Depend"):
313  for dep in pj["Depend"]:
314  dep = """ - *%s
315 """ % (dep)
316  list += dep
317  projlist += list
318  yaml_text = sln_yaml + projlist
319  return yaml_text
320 
321 def gen_solution(version, yaml_text):
322 
323  dict = yaml.load(yaml_text)
324  t = yat.Template(sln_template
325  % (vcversions[version]["sln"],
326  vcversions[version]["vc"]))
327  return t.generate(dict).replace("\r\n", "\n").replace("\n", "\r\n")
328 
329 
331  pass
332 
333 class InvalidOption(SlnToolException):
334  def __init__(self, msg):
335  self.msg = "Error: InvalidOption:\n "
336  self.msg += msg
337 
338 #------------------------------------------------------------
339 # main function
340 #------------------------------------------------------------
341 def main(argv):
342  if len(argv) == 0:
343  usage()
344  sys.exit(-1)
345  try:
346  res = parse_args(argv)
347  except SlnToolException, e:
348  print "\n" + e.msg + "\n"
349  usage()
350  sys.exit(-1)
351 
352  version = res[0]
353  depfile = res[1]
354  outfile = res[2]
355  flist = res[3]
356  #sln_text = gen_solution(version, get_slnyaml(depfile, flist))
357  sln_text = gen_solution(version, get_slnyaml(depfile, flist, version))
358 
359  if outfile == None:
360  fd = sys.stdout
361  else:
362  fd = open(outfile, "wb")
363 
364  fd.write(sln_text)
365 
366 #------------------------------------------------------------
367 # tests
368 #------------------------------------------------------------
370  for f in sys.argv[1:]:
371  print get_projinfo(f)
372 
374  print get_dependencies(sys.argv[1])
375 
377  print gen_solution(get_slnyaml("dep.yaml", sys.argv[1:]))
378 
379 #------------------------------------------------------------
380 # entry point
381 #------------------------------------------------------------
382 if __name__ == "__main__":
383  main(sys.argv[1:])
384 
def usage()
Definition: slntool.py:75
def main(argv)
Definition: slntool.py:341
def parse_args(argv)
Definition: slntool.py:164
def get_dependencies(fname)
Definition: slntool.py:154
def test_getdep()
Definition: slntool.py:373
def test_getslnyaml()
Definition: slntool.py:376
def get_slnyaml(depfile, projfiles)
Definition: slntool.py:195
def __init__(self, msg)
Definition: slntool.py:334
def get_projinfo(fname)
Definition: slntool.py:108
def test_getprojinfo()
Definition: slntool.py:369
def gen_solution(version, yaml_text)
Definition: slntool.py:321


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:56