File: newrelease.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (259 lines) | stat: -rwxr-xr-x 7,428 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python3

"""Generate new release of ASE.

This script does not attempt to import ASE - then it would depend on
which ASE is installed and how - but assumes that it is run from the
ASE root directory."""

import os
import subprocess
import re
import argparse
from time import strftime
import shutil
from pathlib import Path


os.environ['LANGUAGE'] = 'C'


def runcmd(cmd, output=False, error_ok=False):
    print('Executing:', cmd)
    try:
        if output:
            txt = subprocess.check_output(cmd, shell=True)
            return txt.decode('utf8')
        else:
            return subprocess.check_call(cmd, shell=True)
    except subprocess.CalledProcessError as err:
        if error_ok:
            print('Failed: {}'.format(err))
            print('Continuing...')
        else:
            raise


bash = runcmd


def py(cmd, output=False):
    return runcmd('python3 {}'.format(cmd))


def git(cmd, error_ok=False):
    cmd = 'git {}'.format(cmd)
    return runcmd(cmd, output=True, error_ok=error_ok)


cwd = os.getcwd()
versionfile = 'ase/__init__.py'


def get_version():
    with open(versionfile) as fd:
        return re.search(r"__version__ = '(\S+)'", fd.read()).group(1)


def main():
    p = argparse.ArgumentParser(description='Generate new release of ASE.',
                                epilog='Run from the root directory of ASE.')
    p.add_argument('version', nargs=1,
                   help='version number for new release')
    p.add_argument('nextversion', nargs=1,
                   help='development version after release')
    p.add_argument('--clean', action='store_true',
                   help='delete release branch and tag')
    args = p.parse_args()

    try:
        current_version = get_version()
    except Exception as err:
        p.error('Cannot get version: {}.  Are you in the root directory?'
                .format(err))

    print('Current version: {}'.format(current_version))

    version = args.version[0]
    next_devel_version = args.nextversion[0]

    branchname = 'ase-{}'.format(version)
    current_version = get_version()

    if args.clean:
        print('Cleaning {}'.format(version))
        git('checkout master')
        git('tag -d {}'.format(version), error_ok=True)
        git('branch -D {}'.format(branchname), error_ok=True)
        git('branch -D {}'.format('web-page'), error_ok=True)
        return

    print('New release: {}'.format(version))

    txt = git('status')
    branch = re.match(r'On branch (\S+)', txt).group(1)
    print('Creating new release from branch {}'.format(repr(branch)))
    git('checkout -b {}'.format(branchname))

    def update_version(version):
        print('Editing {}: version {}'.format(versionfile, version))
        new_versionline = "__version__ = '{}'\n".format(version)
        lines = []
        ok = False
        with open(versionfile) as fd:
            for line in fd:
                if line.startswith('__version__'):
                    ok = True
                    line = new_versionline
                lines.append(line)
        assert ok
        with open(versionfile, 'w') as fd:
            for line in lines:
                fd.write(line)

    update_version(version)

    releasenotes = 'doc/releasenotes.rst'
    lines = []

    searchtxt = re.escape("""\
Git master branch
=================

:git:`master <>`.
""")

    replacetxt = """\
Git master branch
=================

:git:`master <>`.

* No changes yet


{header}
{underline}

{date}: :git:`{version} <../{version}>`
"""

    date = strftime('%d %B %Y').lstrip('0')
    header = 'Version {}'.format(version)
    underline = '=' * len(header)
    replacetxt = replacetxt.format(header=header, version=version,
                                   underline=underline, date=date)

    print('Editing {}'.format(releasenotes))
    with open(releasenotes) as fd:
        txt = fd.read()
    txt, n = re.subn(searchtxt, replacetxt, txt, re.MULTILINE)
    assert n == 1

    with open(releasenotes, 'w') as fd:
        fd.write(txt)

    searchtxt = """\
News
====
"""

    replacetxt = """\
News
====

* :ref:`ASE version {version} <releasenotes>` released ({date}).
"""

    replacetxt = replacetxt.format(version=version, date=date)

    frontpage = 'doc/index.rst'
    lines = []
    print('Editing {}'.format(frontpage))
    with open(frontpage) as fd:
        txt = fd.read()
    txt, n = re.subn(searchtxt, replacetxt, txt)
    assert n == 1
    with open(frontpage, 'w') as fd:
        fd.write(txt)

    installdoc = 'doc/install.rst'
    print('Editing {}'.format(installdoc))

    with open(installdoc) as fd:
        txt = fd.read()

    txt, nsub = re.subn(r'ase-\d+\.\d+\.\d+',
                        'ase-{}'.format(version), txt)
    assert nsub > 0
    txt, nsub = re.subn(r'git clone -b \d+\.\d+\.\d+',
                        'git clone -b {}'.format(version), txt)
    assert nsub == 1

    with open(installdoc, 'w') as fd:
        fd.write(txt)

    sphinxconf = 'doc/conf.py'
    print('Editing {}'.format(sphinxconf))
    comment = '# This line auto-edited by newrelease script'
    line1 = "ase_dev_version = '{}'  {}\n".format(next_devel_version, comment)
    line2 = "ase_stable_version = '{}'  {}\n".format(version, comment)
    lines = []
    with open(sphinxconf) as fd:
        for line in fd:
            if re.match('ase_dev_version = ', line):
                line = line1
            if re.match('ase_stable_version = ', line):
                line = line2
            lines.append(line)
    with open(sphinxconf, 'w') as fd:
        fd.write(''.join(lines))

    git('add {}'.format(' '.join([versionfile, sphinxconf, installdoc,
                                  frontpage, releasenotes])))
    git('commit -m "ASE version {}"'.format(version))
    git('tag -s {0} -m "ase-{0}"'.format(version))

    buildpath = Path('build')
    if buildpath.is_dir():
        print('Removing stale build directory, since it exists')
        assert Path('ase/__init__.py').exists()
        assert Path('setup.py').exists()
        shutil.rmtree('build')
    else:
        print('No stale build directory found; proceeding')
    py('setup.py sdist > setup_sdist.log')
    py('setup.py bdist_wheel > setup_bdist_wheel3.log')
    bash('gpg --armor --yes --detach-sign dist/ase-{}.tar.gz'.format(version))
    # git('checkout -b web-page')
    # git('branch --set-upstream-to=origin/web-page')
    # git('checkout {}'.format(branchname))
    # update_version(next_devel_version)
    # git('add {}'.format(versionfile))
    # git('branch --set-upstream-to=master')
    # git('commit -m "bump version number to {}"'.format(next_devel_version))

    print()
    print('Automatic steps done.')
    print()
    print('Now is a good time to:')
    print(' * check the diff')
    print(' * run the tests')
    print(' * verify the web-page build')
    print()
    print('Remaining steps')
    print('===============')
    print('git show {}  # Inspect!'.format(version))
    print('git checkout master')
    print('git merge {}'.format(branchname))
    print('twine upload '
          'dist/ase-{v}.tar.gz '
          'dist/ase-{v}-py3-none-any.whl '
          'dist/ase-{v}.tar.gz.asc'.format(v=version))
    print('git push --tags origin master  # Assuming your remote is "origin"')
    print('git checkout web-page')
    print('git push --force origin web-page')


if __name__ == '__main__':
    main()