File: build_web_page.py

package info (click to toggle)
python-ase 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,192 kB
  • ctags: 8,112
  • sloc: python: 93,375; sh: 99; makefile: 94
file content (120 lines) | stat: -rw-r--r-- 3,546 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from __future__ import print_function
import glob
import optparse
import os
import shutil
import subprocess
import sys
import time


def git_pull(name='ase'):
    os.chdir(name)
    try:
        sleep = 1  # 1 minute
        while True:
            try:
                output = subprocess.check_output(
                    'GIT_HTTP_LOW_SPEED_LIMIT=1000 '
                    'GIT_HTTP_LOW_SPEED_TIME=20 '  # make sure we get a timeout
                    'git pull 2>> pull.err', shell=True)
            except subprocess.CalledProcessError:
                if sleep > 16:
                    raise
                time.sleep(sleep * 60)
                sleep *= 2
            else:
                break
    finally:
        os.chdir('..')
    lastline = output.splitlines()[-1]
    return not lastline.startswith('Already up-to-date')


def build(force_build, name='ase', env=''):
    if not force_build:
        return

    home = os.getcwd()

    os.chdir(name)

    # Clean up:
    shutil.rmtree('doc')
    subprocess.check_call('git checkout .', shell=True)

    # Create development snapshot tar-file and install:
    try:
        shutil.rmtree('dist')
    except OSError:
        pass
    subprocess.check_call('python setup.py sdist install --home=..',
                          shell=True)

    # Build web-page:
    os.chdir('doc')
    os.makedirs('build/html')  # Sphinx-1.1.3 needs this (1.2.2 is OK)
    subprocess.check_call(env + ' PYTHONPATH='
                          '{0}/lib/python:{0}/lib64/python:$PYTHONPATH '
                          'PATH={0}/bin:$PATH '.format(home) +
                          'make html', shell=True)

    # Use https for mathjax:
    subprocess.check_call(
        'find build -name "*.html" | '
        'xargs sed -i "s|http://cdn.mathjax.org|https://cdn.mathjax.org|"',
        shell=True)

    tar = glob.glob('../dist/*.tar.gz')[0].split('/')[-1]
    os.rename('../dist/' + tar, 'build/html/' + tar)

    # Set correct version of snapshot tar-file:
    subprocess.check_call(
        'find build/html -name install.html | '
        'xargs sed -i s/snapshot.tar.gz/{}/g'.format(tar),
        shell=True)

    os.chdir('..')

    os.chdir('doc/build')
    dir = name + '-web-page'
    os.rename('html', dir)
    subprocess.check_call('tar -czf {0}.tar.gz {0}'.format(dir),
                          shell=True)
    os.rename('{}.tar.gz'.format(dir), '../../../{}.tar.gz'.format(dir))
    os.chdir('../../..')
    try:
        shutil.rmtree('lib64')
    except OSError:
        pass
    shutil.rmtree('lib')
    shutil.rmtree('bin')


def main(build=build):
    """Build web-page if there are changes in the source.

    The optional build function is used by GPAW to build its web-page.
    """
    if os.path.isfile('build-web-page.lock'):
        print('Locked', file=sys.stderr)
        return
    try:
        home = os.getcwd()
        open('build-web-page.lock', 'w').close()

        parser = optparse.OptionParser(usage='Usage: %prog [-f]',
                                       description='Build web-page')
        parser.add_option('-f', '--force-build', action='store_true',
                          help='Force build instead of building only when '
                          'there are changes to the docs or code.')
        opts, args = parser.parse_args()
        assert len(args) == 0
        changes = git_pull('ase')
        build(opts.force_build or changes)
    finally:
        os.remove(os.path.join(home, 'build-web-page.lock'))


if __name__ == '__main__':
    main()