PageRenderTime 67ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/python-build/build.py

https://code.google.com/p/python-for-android/
Python | 203 lines | 186 code | 3 blank | 14 comment | 0 complexity | 4e6bafff460b74928845e65931e6e6eb MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, GPL-2.0, 0BSD
  1. #!/usr/bin/python
  2. # Copyright (C) 2009 Google Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. # use this file except in compliance with the License. You may obtain a copy of
  6. # the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations under
  14. # the License.
  15. import compileall
  16. import glob
  17. import os
  18. import re
  19. import subprocess
  20. import shutil
  21. import sys
  22. import zipfile
  23. VERSION={
  24. "": "",
  25. "scripts": "",
  26. "extra": "",
  27. "lib": ""
  28. }
  29. for i in ("scripts", "extra", "lib"):
  30. if os.path.isfile("LATEST_VERSION_"+i.upper()):
  31. VERSION[i] = "_"+open("LATEST_VERSION_"+i.upper()).read().strip()
  32. if os.path.isfile("LATEST_VERSION"):
  33. VERSION[""] = "_"+open("LATEST_VERSION").read().strip()
  34. def run(cmd, exit=True, cwd=None):
  35. print cmd
  36. if subprocess.Popen(cmd.split(), cwd=cwd).wait() != 0:
  37. if exit:
  38. print 'Failed!'
  39. sys.exit(1)
  40. else:
  41. print 'Ignoring failure.'
  42. def find(directory, pattern=None, exclude=None):
  43. print 'Looking for paths in %r matching %r' % (directory, pattern)
  44. matches = []
  45. misses = []
  46. if exclude is None:
  47. exclude = []
  48. directory = os.path.abspath(directory)
  49. for root, dirs, files in os.walk(directory):
  50. for basename in dirs + files:
  51. if basename in exclude:
  52. if basename in dirs:
  53. dirs.remove(basename)
  54. continue
  55. path = os.path.join(root, basename)
  56. if pattern is None or re.search(pattern, path):
  57. matches.append(path)
  58. else:
  59. misses.append(path)
  60. print 'Found %d matches and %d misses' % (len(matches), len(misses))
  61. return matches, misses
  62. def rm(path):
  63. print 'Deleting %r' % path
  64. try:
  65. if os.path.isdir(path):
  66. shutil.rmtree(path)
  67. else:
  68. os.remove(path)
  69. except OSError:
  70. pass
  71. def strip(path):
  72. run('arm-linux-androideabi-strip %s' % path)
  73. def zipup(out_path, in_path, top, exclude=None, prefix=''):
  74. zip_file = zipfile.ZipFile(out_path, 'w', compression=zipfile.ZIP_DEFLATED)
  75. for path in find(in_path, exclude=exclude)[0]:
  76. if not os.path.isdir(path):
  77. arcname = prefix + path[len(top):].lstrip('/')
  78. print 'Adding %s to %s' % (arcname, out_path)
  79. zip_file.write(path, arcname)
  80. zip_file.close()
  81. pwd = os.getcwd()
  82. print 'Installing xmppy.'
  83. xmpppy_path = os.path.join(pwd, 'python-libs', 'xmpppy', 'xmpp')
  84. compileall.compile_dir(xmpppy_path)
  85. shutil.copytree(xmpppy_path, 'output/usr/lib/python2.6/xmpp')
  86. print 'Installing BeautifulSoup.'
  87. beautifulsoup_path = os.path.join(pwd, 'python-libs','BeautifulSoup')
  88. compileall.compile_dir(beautifulsoup_path)
  89. shutil.copy(os.path.join(beautifulsoup_path, 'BeautifulSoup.pyc'),
  90. 'output/usr/lib/python2.6/BeautifulSoup.pyc')
  91. print 'Installing gdata.'
  92. gdata_path = os.path.join(pwd, 'python-libs', 'gdata')
  93. run('python setup.py build', cwd=gdata_path)
  94. gdata_build_path = os.path.join(gdata_path, 'build')
  95. gdata_result_path = os.path.join(gdata_build_path,
  96. os.listdir(gdata_build_path)[0])
  97. compileall.compile_dir(gdata_result_path)
  98. shutil.copytree(os.path.join(gdata_result_path, 'gdata'),
  99. 'output/usr/lib/python2.6/gdata')
  100. shutil.copytree(os.path.join(gdata_result_path, 'atom'),
  101. 'output/usr/lib/python2.6/atom')
  102. print 'Installing python-twitter.'
  103. twitter_path = os.path.join(pwd, 'python-libs', 'python-twitter')
  104. compileall.compile_dir(twitter_path)
  105. shutil.copy(os.path.join(twitter_path, 'twitter.pyc'),
  106. 'output/usr/lib/python2.6/twitter.pyc')
  107. print 'Installing simplejson.'
  108. simplejson_path = os.path.join(pwd, 'python-libs', 'python-twitter', 'simplejson')
  109. compileall.compile_dir(simplejson_path)
  110. shutil.copytree(simplejson_path, 'output/usr/lib/python2.6/simplejson')
  111. print 'Installing setuptools.'
  112. setuptools_path = os.path.join(pwd, 'python-libs', 'setuptools')
  113. compileall.compile_dir(setuptools_path)
  114. for i in os.listdir(setuptools_path):
  115. i = os.path.join(setuptools_path, i)
  116. if os.path.isfile(i) and i.endswith(".pyc"):
  117. shutil.copy(i, 'output/usr/lib/python2.6/site-packages')
  118. shutil.copytree(os.path.join(setuptools_path, "setuptools"),
  119. 'output/usr/lib/python2.6/setuptools')
  120. # Remove any existing zip files.
  121. for p in glob.glob(os.path.join(pwd, '*.zip')):
  122. rm(p)
  123. print 'Zipping up Python Libs for deployment.'
  124. output=os.path.join(pwd, 'output')
  125. shutil.copytree(output, 'output.temp')
  126. map(rm, find('output.temp', '\.py$', exclude=['setuptools', 'distutils'])[0])
  127. map(rm, find('output.temp/usr/lib/python2.6', '.*', exclude=['setuptools', 'distutils'])[0])
  128. map(rm, find('output.temp', 'python$', exclude=['setuptools', 'distutils'])[0])
  129. run("mkdir python", cwd="output.temp/usr")
  130. run("cp -r %s/python-libs/py4a python" % pwd, cwd="output.temp/usr")
  131. run("cp %s/setup.cfg ." % pwd, cwd="output.temp/usr")
  132. run("cp %s/prepare_setuptools.sh setup.sh" % pwd, cwd="output.temp/usr")
  133. run("cp %s/standalone_python.sh python.sh" % pwd, cwd="output.temp/usr")
  134. zipup(os.path.join(pwd, 'python-lib%s.zip' % VERSION["lib"]),
  135. os.path.join(pwd, 'output.temp', 'usr'),
  136. os.path.join(pwd, 'output.temp', 'usr'))
  137. rm('output.temp')
  138. print 'Removing unecessary files and directories from installation.'
  139. map(rm, find('output', '\.py$')[0])
  140. map(rm, find('output', '\.c$')[0])
  141. map(rm, find('output', '\.pyo$')[0])
  142. map(rm, find('output','_locale.so$')[0]) # Locale not supported on Android.
  143. rm('output/usr/share')
  144. rm('output/usr/include')
  145. map(strip, find('output', '\.so$')[0])
  146. strip('output/usr/bin/python')
  147. print 'Zipping up standard library.'
  148. libs = os.path.join(pwd, 'output/usr/lib/python2.6')
  149. # Copy in ASE's Android module.
  150. shutil.copy(os.path.join(pwd, 'python-libs', 'ase', 'android.py'),
  151. 'output/usr/lib/python2.6')
  152. zipup(os.path.join(pwd, 'python_extras%s.zip' % VERSION["extra"]), libs, libs,
  153. exclude=['lib-dynload'], prefix='python/')
  154. map(rm, find('output', '\.py$')[0])
  155. map(rm, find('output', '\.pyc$')[0])
  156. map(rm, find('output', '\.doc$')[0])
  157. map(rm, find('output', '\.egg-info$')[0])
  158. def clean_library(lib):
  159. rm(os.path.join(pwd, 'output', 'usr', 'lib', 'python2.6', lib))
  160. map (clean_library, ['ctypes', 'distutils', 'idlelib', 'plat-linux2', 'site-packages'])
  161. print 'Zipping up Python interpreter for deployment.'
  162. zipup(os.path.join(pwd, 'python%s.zip' % VERSION[""]),
  163. os.path.join(pwd, 'output', 'usr'),
  164. os.path.join(pwd, 'output', 'usr'),
  165. exclude=['*.pyc', '*.py'], prefix="python/")
  166. print 'Zipping up Python scripts.'
  167. zipup(os.path.join(pwd, 'python_scripts%s.zip' % VERSION["scripts"]),
  168. os.path.join(pwd, 'python-libs', 'ase', 'scripts'),
  169. os.path.join(pwd, 'python-libs', 'ase', 'scripts'))
  170. print 'Done.'