PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/setup.py

https://github.com/andrewrk/MonsterMechanics
Python | 273 lines | 259 code | 6 blank | 8 comment | 0 complexity | 1dad5e22594f4d2b66117f504e221d81 MD5 | raw file
  1. import os
  2. # usage: python setup.py command
  3. #
  4. # sdist - build a source dist
  5. # py2exe - build an exe
  6. # py2app - build an app
  7. # cx_freeze - build a linux binary (not implemented)
  8. #
  9. # the goods are placed in the dist dir for you to .zip up or whatever...
  10. APP_NAME = 'MonsterMechanics'
  11. DESCRIPTION = open('README').read()
  12. METADATA = {
  13. 'name':APP_NAME,
  14. 'version': '0.0.1',
  15. 'license': 'short_licence',
  16. 'description': '',
  17. 'author': '',
  18. #'author_email': '',
  19. 'url': 'http://pyweek.org/e/MonsterMechanics',
  20. 'classifiers': [
  21. 'Development Status :: 4 - Beta',
  22. 'Intended Audience :: End Users/Desktop',
  23. 'Intended Audience :: Information Technology',
  24. 'License :: OSI Approved :: BSD License',
  25. 'Operating System :: OS Independent',
  26. 'Programming Language :: Python :: 2',
  27. 'Programming Language :: Python :: 2.5',
  28. 'Programming Language :: Python :: 2.6',
  29. 'Programming Language :: Python :: 2.7',
  30. 'Programming Language :: Python :: 3',
  31. 'Programming Language :: Python :: 3.0',
  32. 'Programming Language :: Python :: 3.1',
  33. 'Programming Language :: Python :: 3.2',
  34. 'Topic :: Software Development :: Libraries :: pygame',
  35. 'Topic :: Games/Entertainment :: Real Time Strategy',
  36. ],
  37. 'py2exe.target':'',
  38. #'py2exe.icon':'icon.ico', #64x64
  39. 'py2exe.binary':APP_NAME, #leave off the .exe, it will be added
  40. 'py2app.target':APP_NAME,
  41. 'py2app.icon':'icon.icns', #128x128
  42. #'cx_freeze.cmd':'~/src/cx_Freeze-3.0.3/FreezePython',
  43. 'cx_freeze.cmd':'cxfreeze',
  44. 'cx_freeze.target':'%s_linux' % APP_NAME,
  45. 'cx_freeze.binary':APP_NAME,
  46. }
  47. files_to_remove = ['tk84.dll',
  48. '_ssl.pyd',
  49. 'tcl84.dll',
  50. os.path.join('numpy','core', '_dotblas.pyd'),
  51. os.path.join('numpy', 'linalg', 'lapack_lite.pyd'),
  52. ]
  53. directories_to_remove = [os.path.join('numpy', 'distutils'),
  54. 'distutils',
  55. 'tcl',
  56. ]
  57. cmdclass = {}
  58. PACKAGEDATA = {
  59. 'cmdclass': cmdclass,
  60. 'package_dir': {'MonsterMechanics': 'MonsterMechanics',
  61. },
  62. 'packages': ['MonsterMechanics', ],
  63. }
  64. PACKAGEDATA.update(METADATA)
  65. from distutils.core import setup, Extension
  66. try:
  67. import py2exe
  68. except:
  69. pass
  70. import sys
  71. import glob
  72. import os
  73. import shutil
  74. try:
  75. cmd = sys.argv[1]
  76. except IndexError:
  77. print 'Usage: setup.py install|py2exe|py2app|cx_freeze'
  78. raise SystemExit
  79. # utility for adding subdirectories
  80. def add_files(dest,generator):
  81. for dirpath, dirnames, filenames in generator:
  82. for name in 'CVS', '.svn':
  83. if name in dirnames:
  84. dirnames.remove(name)
  85. for name in filenames:
  86. if '~' in name: continue
  87. suffix = os.path.splitext(name)[1]
  88. if suffix in ('.pyc', '.pyo'): continue
  89. if name[0] == '.': continue
  90. filename = os.path.join(dirpath, name)
  91. dest.append(filename)
  92. # define what is our data
  93. _DATA_DIR = os.path.join('MonsterMechanics', 'data')
  94. data = []
  95. add_files(data,os.walk(_DATA_DIR))
  96. #data_dirs = [os.path.join(f2.replace(_DATA_DIR, 'data'), '*') for f2 in data]
  97. data_dirs = [os.path.join(f2.replace(_DATA_DIR, 'data')) for f2 in data]
  98. PACKAGEDATA['package_data'] = {'MonsterMechanics': data_dirs}
  99. data.extend(glob.glob('*.txt'))
  100. #data.append('MANIFEST.in')
  101. # define what is our source
  102. src = []
  103. add_files(src,os.walk('MonsterMechanics'))
  104. src.extend(glob.glob('*.py'))
  105. # build the sdist target
  106. if cmd not in "py2exe py2app cx_freeze".split():
  107. f = open("MANIFEST.in","w")
  108. for l in data: f.write("include "+l+"\n")
  109. for l in src: f.write("include "+l+"\n")
  110. f.close()
  111. setup(**PACKAGEDATA)
  112. # build the py2exe target
  113. if cmd in ('py2exe',):
  114. dist_dir = os.path.join('dist',METADATA['py2exe.target'])
  115. data_dir = dist_dir
  116. src = 'run_game.py'
  117. dest = METADATA['py2exe.binary']+'.py'
  118. shutil.copy(src,dest)
  119. setup(
  120. options={'py2exe':{
  121. 'dist_dir':dist_dir,
  122. 'dll_excludes':['_dotblas.pyd','_numpy.pyd', 'numpy.linalg.lapack_lite.pyd', 'numpy.core._dotblas.pyd'] + files_to_remove,
  123. 'excludes':['matplotlib', 'tcl', 'OpenGL'],
  124. 'ignores':['matplotlib', 'tcl', 'OpenGL'],
  125. 'bundle_files':1,
  126. }},
  127. # windows=[{
  128. console=[{
  129. 'script':dest,
  130. #'icon_resources':[(1,METADATA['py2exe.icon'])],
  131. }],
  132. )
  133. # build the py2app target
  134. if cmd == 'py2app':
  135. dist_dir = os.path.join('dist',METADATA['py2app.target']+'.app')
  136. data_dir = os.path.join(dist_dir,'Contents','Resources')
  137. from setuptools import setup
  138. src = 'run_game.py'
  139. dest = METADATA['py2app.target']+'.py'
  140. shutil.copy(src,dest)
  141. APP = [dest]
  142. DATA_FILES = []
  143. OPTIONS = {'argv_emulation': True,
  144. #'iconfile':METADATA['py2app.icon']
  145. }
  146. setup(
  147. app=APP,
  148. data_files=DATA_FILES,
  149. options={'py2app': OPTIONS},
  150. setup_requires=['py2app'],
  151. )
  152. # make the cx_freeze target
  153. if cmd == 'cx_freeze':
  154. app_dist_dir = METADATA['cx_freeze.target'] + "_" + METADATA['version']
  155. dist_dir = os.path.join('dist', app_dist_dir)
  156. data_dir = dist_dir
  157. modules_exclude = "tcl,tk"
  158. cmd_args = (METADATA['cx_freeze.cmd'], dist_dir, METADATA['cx_freeze.binary'], modules_exclude)
  159. sys_cmd = '%s --install-dir=%s --target-name=%s --exclude-modules=%s run_game.py' % cmd_args
  160. print sys_cmd
  161. os.system(sys_cmd)
  162. import shutil
  163. if os.path.exists(os.path.join(data_dir, "tcl")):
  164. shutil.rmtree( os.path.join(data_dir, "tcl") )
  165. if os.path.exists(os.path.join(data_dir, "tk")):
  166. shutil.rmtree( os.path.join(data_dir, "tk") )
  167. # recursively make a bunch of folders
  168. def make_dirs(dname_):
  169. parts = list(os.path.split(dname_))
  170. dname = None
  171. while len(parts):
  172. if dname == None:
  173. dname = parts.pop(0)
  174. else:
  175. dname = os.path.join(dname,parts.pop(0))
  176. if not os.path.isdir(dname):
  177. os.mkdir(dname)
  178. # copy data into the binaries
  179. if cmd in ('py2exe','cx_freeze','py2app'):
  180. dest = data_dir
  181. for fname in data:
  182. dname = os.path.join(dest,os.path.dirname(fname))
  183. make_dirs(dname)
  184. if not os.path.isdir(fname):
  185. #print (fname,dname)
  186. shutil.copy(fname,dname)
  187. # make a tgz files.
  188. if cmd == 'cx_freeze':
  189. sys_cmd = "cd dist; tar -vczf %s.tgz %s/" % (app_dist_dir,app_dist_dir)
  190. os.system(sys_cmd)
  191. # remove files from the zip.
  192. if 0 and cmd in ('py2exe'):
  193. import shutil
  194. #shutil.rmtree( os.path.join('dist') )
  195. #shutil.rmtree( os.path.join('build') )
  196. os.system("unzip dist/library.zip -d dist\library")
  197. for fn in files_to_remove:
  198. os.remove( os.path.join('dist', 'library', fn) )
  199. for d in directories_to_remove:
  200. if os.path.exists( os.path.join('dist', 'library', d) ):
  201. shutil.rmtree( os.path.join('dist', 'library', d) )
  202. os.remove( os.path.join('dist', 'library.zip') )
  203. os.chdir("dist")
  204. os.chdir("library")
  205. os.system("zip -r -9 ..\library.zip .")
  206. os.chdir("..")
  207. os.chdir("..")