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


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:45