PageRenderTime 41ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/linux/createProjects.py

https://github.com/paulobarcelos/openFrameworks
Python | 226 lines | 215 code | 10 blank | 1 comment | 16 complexity | b1d95cd98bfcee22ac204a964f5f7734 MD5 | raw file
  1. #!/usr/bin/python
  2. import os
  3. from lxml import etree
  4. from lxml import objectify
  5. import argparse
  6. import shutil
  7. import glob
  8. of_root = os.path.realpath(__file__)[0:-(len(os.path.join('scripts','linux','createProject.py'))+2)]
  9. platform = 'linux'
  10. arch = 'linux'
  11. uname = os.uname()
  12. for uname_str in uname:
  13. if uname_str=='x86_64':
  14. arch = 'linux64'
  15. templates_path = os.path.join(of_root,'apps','devApps',platform)
  16. template = {'cbp': os.path.join(templates_path , 'emptyExample_' + arch + '.cbp'), 'full_cbp': os.path.join(templates_path , 'emptyExample_' + arch + '_fullCBP.cbp'), 'workspace': os.path.join(templates_path , 'emptyExample_' + arch + '.workspace'),'makefile': os.path.join(templates_path , 'Makefile'), 'config.make': os.path.join(templates_path , 'config.make')}
  17. fullCBP = True
  18. def addCBPIncludePath(project,dirpath):
  19. found=False
  20. if project.find('Compiler') == None:
  21. etree.SubElement(project,"Compiler")
  22. if project.Compiler.find('Add') != None:
  23. for include in project.Compiler.Add:
  24. if str(include.get("directory"))==str(dirpath):
  25. found=True
  26. break
  27. if not found:
  28. include = etree.SubElement(project.Compiler,"Add")
  29. include.set("directory",dirpath)
  30. def addCBPLibrary(project,libpath):
  31. found=False
  32. if project.find('Linker') == None:
  33. etree.SubElement(project,"Linker")
  34. if project.Linker.find('Add') != None:
  35. for lib in project.Linker.Add:
  36. if str(lib.get("library"))==str(libpath):
  37. found=True
  38. break
  39. if not found:
  40. include = etree.SubElement(project.Linker,"Add")
  41. include.set("library",libpath)
  42. def addCBPUnit(project,filepath,basefolder):
  43. found=False
  44. for unit in project.Unit:
  45. if str(unit.get("filename"))==str(filepath):
  46. found=True
  47. break
  48. if not found:
  49. unit = etree.SubElement(project,"Unit")
  50. unit.set("filename",filepath)
  51. virtual_folder = etree.SubElement(unit,"Option")
  52. virtual_folder.set("virtualFolder",basefolder)
  53. def addAddon(project,addon):
  54. if addon == '':
  55. return
  56. if not os.path.exists(os.path.join(of_root,'addons',addon)):
  57. print 'error', addon, 'in addons.make not found'
  58. return
  59. if not os.path.exists(os.path.join(of_root,'addons',addon,'src')):
  60. print 'error', addon, 'has no src folder'
  61. return
  62. if fullCBP:
  63. addon_src = os.path.join('..','..','..',os.path.join(of_root,'addons',addon,'src')[len(of_root)+1:])
  64. addCBPIncludePath(project,addon_src)
  65. for root, dirs, files in os.walk(os.path.join(of_root,'addons',addon,'src')):
  66. for name in files:
  67. basefolder = root[len(of_root)+1:]
  68. filepath = str(os.path.join('..','..','..',basefolder,name))
  69. addCBPUnit(project,filepath,basefolder)
  70. if fullCBP:
  71. for dir in dirs:
  72. basefolder = root[len(of_root)+1:]
  73. dirpath = os.path.join('..','..','..',basefolder,dir)
  74. addCBPIncludePath(project,dirpath)
  75. if fullCBP:
  76. if not os.path.exists(os.path.join(of_root,'addons',addon,'libs')):
  77. return
  78. for libdir in os.listdir(os.path.join(of_root,'addons',addon,'libs')):
  79. if not os.path.isdir(os.path.join(of_root,'addons',addon,'libs',libdir)):
  80. continue
  81. basefolder = os.path.join('addons',addon,'libs',libdir);
  82. if os.path.exists(os.path.join(of_root,basefolder,'include')):
  83. dirpath = os.path.join(of_root,basefolder,'include')
  84. addCBPIncludePath(project,os.path.join('..','..','..',basefolder,'include'))
  85. for root, dirs, files in os.walk(dirpath):
  86. for dir in dirs:
  87. basefolder_addon = root[len(of_root)+1:]
  88. dirpath_addon = os.path.join('..','..','..',basefolder_addon,dir)
  89. addCBPIncludePath(project,dirpath_addon)
  90. basefolder = os.path.join('addons',addon,'libs',libdir);
  91. if os.path.exists(os.path.join(of_root,basefolder,'lib',arch)):
  92. dirpath = os.path.join('..','..','..',basefolder,'lib',arch)
  93. if os.path.exists(os.path.join(of_root,basefolder,'lib',arch,'libsorder.make')):
  94. libsorder = open(os.path.join(of_root,basefolder,'lib',arch,'libsorder.make'))
  95. for lib in libsorder:
  96. if lib[-1]=='\n':
  97. lib = lib[:-1]
  98. addCBPLibrary(project,os.path.join(dirpath,'lib'+lib.strip()+'.a'))
  99. libsorder.close()
  100. else:
  101. for lib in glob.glob(os.path.join(of_root,basefolder,'lib',arch,'*.a')):
  102. baselib = lib[len(of_root)+1:]
  103. addCBPLibrary(project,os.path.join('..','..','..',baselib))
  104. for lib in glob.glob(os.path.join(of_root,basefolder,'lib',arch,'*.so')):
  105. baselib = lib[len(of_root)+1:]
  106. addCBPLibrary(project,os.path.join('..','..','..',baselib))
  107. def addAddons(project,project_path):
  108. if not os.path.exists(os.path.join(project_path,'addons.make')):
  109. return
  110. addons_make = open(os.path.join(project_path,'addons.make'),'r')
  111. for addon in addons_make:
  112. if addon[-1]=='\n':
  113. addon = addon[:-1]
  114. addAddon(project, addon)
  115. def createCBP(project_path):
  116. if os.path.abspath(project_path) == os.path.abspath(templates_path):
  117. return
  118. project_name = os.path.basename(project_path)
  119. cbp = objectify.parse(os.path.join(project_path,project_name+'.cbp'))
  120. root = cbp.getroot()
  121. project = root.Project
  122. for option in project.Option:
  123. if option.get("title")!=None:
  124. option.set("title",project_name)
  125. # add existing files in src/ to the codeblocks project
  126. for root, dirs, files in os.walk(os.path.join(project_path,'src')):
  127. for name in files:
  128. basefolder = root[len(project_path)+1:]
  129. filepath = str(os.path.join(basefolder,name))
  130. addCBPUnit(project,filepath,basefolder)
  131. # add addons from addons.make to the cbp
  132. addAddons(project,project_path)
  133. cbp_file = open(os.path.join(project_path,project_name+'.cbp'),mode='w')
  134. cbp_file.write(etree.tostring(cbp, xml_declaration=True, encoding='UTF-8', pretty_print=True))
  135. cbp_file.close()
  136. def createWorkspace(project_path):
  137. if os.path.abspath(project_path) == os.path.abspath(templates_path):
  138. return
  139. project_name = os.path.basename(project_path)
  140. ws = objectify.parse(os.path.join(project_path,project_name+'.workspace'))
  141. root = ws.getroot()
  142. workspace = root.Workspace
  143. for project in workspace.Project:
  144. if project.get("filename")=="emptyExample.cbp":
  145. project.set("filename",project_name+".cbp")
  146. ws_file = open(os.path.join(project_path,project_name+'.workspace'),mode='w')
  147. ws_file.write(etree.tostring(ws, xml_declaration=True, encoding='UTF-8', pretty_print=True))
  148. ws_file.close()
  149. def createProject(project_path):
  150. print 'generating',project_path
  151. if os.path.abspath(project_path) == os.path.abspath(templates_path):
  152. return
  153. if project_path[-1]==os.sep:
  154. project_path=project_path[:-1]
  155. if not os.path.exists(project_path):
  156. os.mkdir(project_path)
  157. project_name = os.path.basename(project_path)
  158. if fullCBP:
  159. shutil.copyfile(template['full_cbp'],os.path.join(project_path,project_name+'.cbp'))
  160. else:
  161. shutil.copyfile(template['cbp'],os.path.join(project_path,project_name+'.cbp'))
  162. shutil.copyfile(template['workspace'],os.path.join(project_path,project_name+'.workspace'))
  163. if platform == "linux":
  164. shutil.copyfile(template['makefile'],os.path.join(project_path,'Makefile'))
  165. if platform == "linux" and not os.path.exists(os.path.join(project_path, 'config.make')):
  166. shutil.copyfile(template['config.make'],os.path.join(project_path,'config.make'))
  167. if not os.path.exists(os.path.join(project_path,'src')):
  168. os.mkdir(os.path.join(project_path , 'src'))
  169. for file in os.listdir(os.path.join(templates_path , 'src')):
  170. shutil.copyfile(os.path.join(templates_path , 'src' , file), os.path.join(project_path , 'src' , file))
  171. if not os.path.exists(os.path.join(project_path , 'bin')):
  172. os.mkdir(os.path.join(project_path , 'bin'))
  173. if not os.path.exists(os.path.join(project_path , 'bin', 'data')):
  174. os.mkdir(os.path.join(project_path , 'bin','data'))
  175. createCBP(project_path)
  176. createWorkspace(project_path)
  177. parser = argparse.ArgumentParser(description='OF linux project generator')
  178. parser.add_argument('project_path', metavar='project_path', nargs='?')
  179. parser.add_argument('-n', '--not_mk', dest='not_mk', action='store_const',
  180. default=False, const=True, help='create cbp not dependent on Makefile')
  181. project_path = parser.parse_args().project_path
  182. fullCBP = parser.parse_args().not_mk
  183. if project_path==None: #parse all examples
  184. for example in os.listdir(os.path.join(of_root,'apps','examples')):
  185. createProject(os.path.join(of_root,'apps','examples',example))
  186. for example in os.listdir(os.path.join(of_root,'apps','addonsExamples')):
  187. createProject(os.path.join(of_root,'apps','addonsExamples',example))
  188. else:
  189. createProject(project_path)