File: release.py

package info (click to toggle)
lapackpp 2024.10.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,500 kB
  • sloc: cpp: 80,181; ansic: 27,660; python: 4,838; xml: 182; perl: 99; makefile: 53; sh: 23
file content (255 lines) | stat: -rw-r--r-- 8,093 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
# Copyright (c) 2017-2023, University of Tennessee. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
# This program is free software: you can redistribute it and/or modify it under
# the terms of the BSD 3-Clause license. See the accompanying LICENSE file.

'''
Tags project with version based on current date, and creates tar file.
Tag is yyyy.mm.dd, based on current year, month, and day.
Version is an integer yyyymmdd, to allow simple comparisons.

Requires Python >= 3.7.

Usage:

    #!/usr/bin/env python3
    import release
    release.copyright()
    release.make( 'project', 'version.h', 'version.c' )

'project' is the name of the project, used for the tar filename.

'version.h' is a header containing the following #define for the version,
with PROJECT changed to the project's name.

    // Version is updated by make_release.py; DO NOT EDIT.
    // Version 2020.02.00
    #define PROJECT_VERSION 20200200

'version.c' is a source file containing the following #define for the id:

    // PROJECT_ID is the Mercurial or git commit hash ID, either
    // defined by `hg id` or `git rev-parse --short HEAD` in Makefile,
    // or defined here by make_release.py for release tar files. DO NOT EDIT.
    #ifndef PROJECT_ID
    #define PROJECT_ID "unknown"
    #endif

    const char* id() {
        return PROJECT_ID;
    }

    int version() {
        return PROJECT_VERSION;
    }

Steps this takes:

1. Marks version in repo.
   - Saves the Version to version.h.
   - Updates copyright year in all files.
   - Commits that change.
   - Tags that commit.
2. Prepares archive in directory project-tag.
   - Saves the `git rev-parse --short HEAD` to version.c.
   - Generates Doxygen docs.
3. Generates tar file project-tag.tar.gz
'''

from __future__ import print_function

import sys
MIN_PYTHON = (3, 7)
assert sys.version_info >= MIN_PYTHON, "requires Python >= %d.%d" % MIN_PYTHON

import os
import datetime
import re
import subprocess
from   subprocess import PIPE

#-------------------------------------------------------------------------------
def myrun( cmd, **kwargs ):
    '''
    Simple wrapper around subprocess.run(), with check=True.
    If cmd is a str, it is split on spaces before being passed to run.
    Prints cmd.
    kwargs are passed to run(). Set `stdout=PIPE, text=True` if you want the
    output returned.
    '''
    if (type(cmd) is str):
        cmd = cmd.split(' ')
    print( '\n>>', ' '.join( cmd ) )
    return subprocess.run( cmd, check=True, **kwargs ).stdout
# end

#-------------------------------------------------------------------------------
def file_sub( filename, search, replace, **kwargs ):
    '''
    Replaces search regexp with replace in file filename.
    '''
    #print( 'reading', filename )
    txt = open( filename ).read()
    txt2 = re.sub( search, replace, txt, **kwargs )
    if (txt != txt2):
        #print( 'writing', filename )
        open( filename, mode='w' ).write( txt2 )
    else:
        print( '##### Warning: no change in', filename, '#####' )
# end

#-------------------------------------------------------------------------------
def copyright():
    '''
    Update copyright in all files.
    '''
    today = datetime.date.today()
    year  = today.year

    files = myrun( 'git ls-tree -r master --name-only',
                   stdout=PIPE, text=True ).rstrip().split( '\n' )
    print( '\n>> Updating copyright in:', end=' ' )
    for file in files:
        if (re.search( r'^(old/|src/hip/)', file ) or os.path.isdir( file )):
            continue

        print( file, end=', ' )
        file_sub( file,
                  r'Copyright \(c\) (\d+)(-\d+)?, University of Tennessee',
                  r'Copyright (c) \1-%04d, University of Tennessee' % (year) )
    # end
    print()

    myrun( 'git diff' )
    print( '>> Commit changes [yn]? ', end='' )
    response = input()
    if (response != 'y'):
        print( '>> Release aborted. Please revert changes as desired.' )
        exit(1)

    myrun( ['git', 'commit', '-m', 'copyright '+ str(year), '.'] )
# end

#-------------------------------------------------------------------------------
def make( project, version_h, version_c ):
    '''
    Makes project release.
    '''
    today = datetime.date.today()
    year  = today.year
    month = today.month
    mday  = today.day

    top_dir = os.getcwd()

    tag = '%04d.%02d.%02d' % (year, month, mday)
    vtag = 'v' + tag
    version = '%04d%02d%02d' % (year, month, mday)
    print( '\n>> Tag '+ tag +', Version '+ version )

    #--------------------
    # Check change log
    txt = open( 'CHANGELOG.md' ).read()
    if (not re.search( tag, txt )):
        print( '>> Add', tag, 'to CHANGELOG.md. Release aborted.' )
        exit(1)

    #--------------------
    # Update version in version_h.
    print( '\n>> Updating version in:', version_h )
    file_sub( version_h,
              r'// Version \d\d\d\d.\d\d.\d\d\n(#define \w+_VERSION) \d+',
              r'// Version %s\n\1 %s' % (tag, version), count=1 )

    print( '\n>> Updating version in: GNUmakefile' )
    file_sub( 'GNUmakefile',
              r'(VERSION.)\d\d\d\d.\d\d.\d\d',
              r'\g<1>%s' % (tag), count=1 )

    print( '\n>> Updating version in: CMakeLists.txt' )
    file_sub( 'CMakeLists.txt',
              r'VERSION \d\d\d\d.\d\d.\d\d',
              r'VERSION %s' % (tag), count=1 )

    print( '\n>> Updating version in: doxyfile.conf' )
    file_sub( 'docs/doxygen/doxyfile.conf',
              r'(PROJECT_NUMBER *=) *"\d+\.\d+\.\d+"',
              r'\1 "%s"' % (tag), count=1 )

    myrun( 'git diff' )
    myrun( 'git diff --staged' )
    print( '>> Do changes look good? Continue building release [yn]? ', end='' )
    response = input()
    if (response != 'y'):
        print( '>> Release aborted. Please revert changes as desired.' )
        exit(1)

    myrun( ['git', 'commit', '-m', 'Version '+ tag, '.'] )
    myrun( ['git', 'tag', vtag, '-a', '-m', 'Version '+ tag] )

    #--------------------
    # Prepare tar file.
    dir = project +'-'+ tag
    print( '\n>> Preparing files in', dir )

    # Move any existing dir to dir-#; maximum # is 100.
    if (os.path.exists( dir )):
        for index in range( 1, 100 ):
            backup = '%s-%d' % (dir, index)
            if (not os.path.exists( backup )):
                os.rename( dir, backup )
                print( 'backing up', dir, 'to', backup )
                break
    # end

    os.mkdir( dir )
    subprocess.run( 'git archive ' + vtag + ' | tar -x -C ' + dir, shell=True )
    os.chdir( dir )

    # Update hash ID in version_c.
    id = myrun( 'git rev-parse --short HEAD', stdout=PIPE, text=True ).strip()
    print( '\n>> Setting ID in:', version_c )
    file_sub( version_c,
              r'^(#define \w+_ID) "unknown"',
              r'\1 "'+ id +'"', count=1, flags=re.M )

    # Build Doxygen docs. Create dummy 'make.inc' to avoid 'make config'.
    open( 'make.inc', mode='a' ).close()
    myrun( 'make docs' )
    os.unlink( 'make.inc' )

    os.chdir( '..' )

    tar = dir + '.tar.gz'
    print( '\n>> Creating tar file', tar )
    myrun( 'tar -zcvf '+ tar +' '+ dir )

    #--------------------
    # Update online docs.
    myrun( ['rsync', '-av', '--delete',
            '--exclude', 'artwork',  # keep artwork on icl.bitbucket.io
            dir + '/docs/html/',
            'icl.bitbucket.io/' + project + '/'] )

    os.chdir( 'icl.bitbucket.io' )
    myrun( 'git add ' + project )
    myrun( 'git status' )
    print( '>> Do changes look good? Commit docs [yn]? ', end='' )
    response = input()
    if (response != 'y'):
        print( '>> Doc update aborted. Please revert changes as desired.' )
        exit(1)

    # Commit staged files.
    myrun( ['git', 'commit', '-m', project + ' version ' + tag] )

    print( '>> Run `git push` to make changes live [yn]? ', end='' )
    response = input()
    if (response == 'y'):
        myrun( 'git push' )
    else:
        print( '>> Doc update aborted. Please revert changes as desired.' )
        exit(1)

    os.chdir( top_dir )
# end