File: buildrelease.py

package info (click to toggle)
libphp-adodb 5.20.14-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,544 kB
  • sloc: php: 37,579; python: 601; xml: 117; sql: 32; sh: 22; makefile: 5
file content (270 lines) | stat: -rwxr-xr-x 7,666 bytes parent folder | download | duplicates (3)
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
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/python -u
'''
    ADOdb release build script

    - Create release tag if it does not exist
    - Copy release files to target directory
    - Generate zip/tar balls
    -
'''

import errno
import getopt
import re
import os
from os import path
import shutil
import subprocess
import sys
import tempfile

import updateversion


# ADOdb Repository reference
origin_repo = "https://github.com/ADOdb/ADOdb.git"
release_branch = "master"
release_prefix = "adodb"

# Directories and files to exclude from release tarballs
exclude_list = (".git*",
                "replicate",
                "scripts",
                "tests",
                # There are no png files in there...
                # "cute_icons_for_site/*.png",
                "hs~*.*",
                "adodb-text.inc.php",
                # This file does not exist in current repo
                # 'adodb-lite.inc.php'
                )

# Command-line options
options = "hb:dfk"
long_options = ["help", "branch", "debug", "fresh", "keep"]

# Global flags
debug_mode = False
fresh_clone = False
cleanup = True


def usage():
    print '''Usage: %s [options] version release_path

    Parameters:
        version                 ADOdb version to bundle (e.g. v5.19)
        release_path            Where to save the release tarballs

    Options:
        -h | --help             Show this usage message

        -b | --branch <branch>  Use specified branch (defaults to '%s' for '.0'
                                releases, or 'hotfix/<version>' for patches)
        -d | --debug            Debug mode (ignores upstream: no fetch, allows
                                build even if local branch is not in sync)
        -f | --fresh            Create a fresh clone of the repository
        -k | --keep             Keep build directories after completion
                                (useful for debugging)
''' % (
        path.basename(__file__),
        release_branch
    )
#end usage()


def set_version_and_tag(version):
    '''
    '''
    global release_branch, debug_mode, fresh_clone, cleanup

    # Delete existing tag to force creation in debug mode
    if debug_mode:
        try:
            updateversion.tag_delete(version)
        except:
            pass

    # Checkout release branch
    subprocess.call("git checkout %s" % release_branch, shell=True)

    if not debug_mode:
        # Make sure we're up-to-date, ignore untracked files
        ret = subprocess.check_output(
            "git status --branch --porcelain --untracked-files=no",
            shell=True
        )
        if not re.search(release_branch + "$", ret):
            print "\nERROR: branch must be aligned with upstream"
            sys.exit(4)

    # Update the code, create commit and tag
    updateversion.version_set(version)

    # Make sure we don't delete the modified repo
    if fresh_clone:
        cleanup = False


def main():
    global release_branch, debug_mode, fresh_clone, cleanup

   # Get command-line options
    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], options, long_options)
    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(2)

    if len(args) < 2:
        usage()
        print "ERROR: please specify the version and release_path"
        sys.exit(1)

    for opt, val in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(0)

        elif opt in ("-b", "--branch"):
            release_branch = val

        elif opt in ("-d", "--debug"):
            debug_mode = True

        elif opt in ("-f", "--fresh"):
            fresh_clone = True

        elif opt in ("-k", "--keep"):
            cleanup = False

    # Mandatory parameters
    version = updateversion.version_check(args[0])
    release_path = args[1]

    # Default release branch
    if updateversion.version_is_patch(version):
        release_branch = 'hotfix/' + version

    # -------------------------------------------------------------------------
    # Start the build
    #
    global release_prefix

    print "Building ADOdb release %s into '%s'\n" % (
        version,
        release_path
    )

    if debug_mode:
        print "DEBUG MODE: ignoring upstream repository status"

    if fresh_clone:
        # Create a new repo clone
        print "Cloning a new repository"
        repo_path = tempfile.mkdtemp(prefix=release_prefix + "-",
                                     suffix=".git")
        subprocess.call(
            "git clone %s %s" % (origin_repo, repo_path),
            shell=True
        )
        os.chdir(repo_path)
    else:
        repo_path = subprocess.check_output('git root', shell=True).rstrip()
        os.chdir(repo_path)

        # Check for any uncommitted changes
        try:
            subprocess.check_output(
                "git diff --exit-code && "
                "git diff --cached --exit-code",
                shell=True
                )
        except:
            print "ERROR: there are uncommitted changes in the repository"
            sys.exit(3)

        # Update the repository
        if not debug_mode:
            print "Updating repository in '%s'" % os.getcwd()
            try:
                subprocess.check_output("git fetch", shell=True)
            except:
                print "ERROR: unable to fetch\n"
                sys.exit(3)

    # Check existence of Tag for version in repo, create if not found
    try:
        updateversion.tag_check(version)
        if debug_mode:
            set_version_and_tag(version)
    except:
        set_version_and_tag(version)

    # Copy files to release dir
    release_files = release_prefix + version.split(".")[0]
    release_tmp_dir = path.join(release_path, release_files)
    print "Copying release files to '%s'" % release_tmp_dir
    retry = True
    while True:
        try:
            shutil.copytree(
                repo_path,
                release_tmp_dir,
                ignore=shutil.ignore_patterns(*exclude_list)
            )
            break
        except OSError, err:
            # First try and file exists, try to delete dir
            if retry and err.errno == errno.EEXIST:
                print "WARNING: Directory '%s' exists, delete it and retry" % (
                    release_tmp_dir
                )
                shutil.rmtree(release_tmp_dir)
                retry = False
                continue
            else:
                # We already tried to delete or some other error occured
                raise

    # Create tarballs
    print "Creating release tarballs..."
    release_name = release_prefix + '-' + version
    print release_prefix, version, release_name

    os.chdir(release_path)
    print "- tar"
    subprocess.call(
        "tar -czf %s.tar.gz %s" % (release_name, release_files),
        shell=True
    )
    print "- zip"
    subprocess.call(
        "zip -rq %s.zip %s" % (release_name, release_files),
        shell=True
    )

    if cleanup:
        print "Deleting working directories"
        shutil.rmtree(release_tmp_dir)
        if fresh_clone:
            shutil.rmtree(repo_path)
    else:
        print "\nThe following working directories were kept:"
        if fresh_clone:
            print "- '%s' (repo clone)" % repo_path
        print "- '%s' (release temp dir)" % release_tmp_dir
        print "Delete them manually when they are no longer needed."

    # Done
    print "\nADOdb release %s build complete, files saved in '%s'." % (
        version,
        release_path
    )
    print "Don't forget to generate a README file with the changelog"

#end main()

if __name__ == "__main__":
    main()