PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ase/utils/build_web_page.py

https://gitlab.com/naagafreeman/ase
Python | 134 lines | 130 code | 4 blank | 0 comment | 8 complexity | b06f5fc9981382529aa08284d24507ec MD5 | raw file
  1. from __future__ import print_function
  2. import glob
  3. import optparse
  4. import os
  5. import shutil
  6. import subprocess
  7. import sys
  8. import time
  9. def git_pull(name='ase'):
  10. os.chdir(name)
  11. try:
  12. sleep = 1 # 1 minute
  13. while True:
  14. try:
  15. output = subprocess.check_output(
  16. 'GIT_HTTP_LOW_SPEED_LIMIT=1000 '
  17. 'GIT_HTTP_LOW_SPEED_TIME=20 ' # make sure we get a timeout
  18. 'git pull 2>> pull.err', shell=True)
  19. except subprocess.CalledProcessError:
  20. if sleep > 16:
  21. raise
  22. time.sleep(sleep * 60)
  23. sleep *= 2
  24. else:
  25. break
  26. finally:
  27. os.chdir('..')
  28. lastline = output.splitlines()[-1]
  29. return not lastline.startswith('Already up-to-date')
  30. def build(force_build, name='ase', env=''):
  31. if not force_build:
  32. return
  33. home = os.getcwd()
  34. os.chdir(name)
  35. # Clean up:
  36. shutil.rmtree('doc')
  37. subprocess.check_call('git checkout .', shell=True)
  38. # Create development snapshot tar-file and install:
  39. try:
  40. shutil.rmtree('dist')
  41. except OSError:
  42. pass
  43. subprocess.check_call('python setup.py sdist install --home=..',
  44. shell=True)
  45. # Build web-page:
  46. os.chdir('doc')
  47. os.makedirs('build/html') # Sphinx-1.1.3 needs this (1.2.2 is OK)
  48. subprocess.check_call(env + ' PYTHONPATH='
  49. '{0}/lib/python:{0}/lib64/python:$PYTHONPATH '
  50. 'PATH={0}/bin:$PATH '.format(home) +
  51. 'make html', shell=True)
  52. # Use https for mathjax:
  53. subprocess.check_call(
  54. 'find build -name "*.html" | '
  55. 'xargs sed -i "s|http://cdn.mathjax.org|https://cdn.mathjax.org|"',
  56. shell=True)
  57. tar = glob.glob('../dist/*.tar.gz')[0].split('/')[-1]
  58. os.rename('../dist/' + tar, 'build/html/' + tar)
  59. # Set correct version of snapshot tar-file:
  60. subprocess.check_call(
  61. 'find build/html -name install.html | '
  62. 'xargs sed -i s/snapshot.tar.gz/{}/g'.format(tar),
  63. shell=True)
  64. os.chdir('..')
  65. if name == 'ase':
  66. output = subprocess.check_output(
  67. 'epydoc --docformat restructuredtext --parse-only '
  68. '--name {0} --url http://wiki.fysik.dtu.dk/{1} '
  69. '--show-imports --no-frames -v {1}'.format(name.upper(), name),
  70. shell=True)
  71. # Check for warnings:
  72. for line in output.splitlines():
  73. if line.startswith('|'):
  74. print(line)
  75. os.rename('html', 'doc/build/html/epydoc')
  76. os.chdir('doc/build')
  77. dir = name + '-web-page'
  78. os.rename('html', dir)
  79. subprocess.check_call('tar -czf {0}.tar.gz {0}'.format(dir),
  80. shell=True)
  81. os.rename('{}.tar.gz'.format(dir), '../../../{}.tar.gz'.format(dir))
  82. os.chdir('../../..')
  83. try:
  84. shutil.rmtree('lib64')
  85. except OSError:
  86. pass
  87. shutil.rmtree('lib')
  88. shutil.rmtree('bin')
  89. def main(build=build):
  90. """Build web-page if there are changes in the source.
  91. The optional build function is used by GPAW to build its web-page.
  92. """
  93. if os.path.isfile('build-web-page.lock'):
  94. print('Locked', file=sys.stderr)
  95. return
  96. try:
  97. home = os.getcwd()
  98. open('build-web-page.lock', 'w').close()
  99. parser = optparse.OptionParser(usage='Usage: %prog [-f]',
  100. description='Build web-page')
  101. parser.add_option('-f', '--force-build', action='store_true',
  102. help='Force build instead of building only when '
  103. 'there are changes to the docs or code.')
  104. opts, args = parser.parse_args()
  105. assert len(args) == 0
  106. changes = git_pull('ase')
  107. build(opts.force_build or changes)
  108. finally:
  109. os.remove(os.path.join(home, 'build-web-page.lock'))
  110. if __name__ == '__main__':
  111. main()