summaryrefslogtreecommitdiff
path: root/scripts/uploadrelease.py
blob: 54f7a6d6b5d2ae26dac7686b19700f9218731e74 (plain)
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
#!/usr/bin/python -u
'''
    ADOdb release upload script
'''

import getopt
import glob
import os
from os import path
import re
import subprocess
import sys


# Directories and files to exclude from release tarballs
sf_files = "frs.sourceforge.net:/home/frs/project/adodb" \
           "/adodb-php5-only/adodb-{ver}-for-php5/"
sf_doc = "web.sourceforge.net:/home/project-web/adodb/htdocs/"
rsync_cmd = "rsync -vP --rsh ssh {opt} {src} {usr}@{dst}"

# Command-line options
options = "hfdn"
long_options = ["help", "files", "doc", "dry-run"]

upload_files = True
upload_doc = True
dry_run = False


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

    This script will upload the files in the given directory (or the
    current one if unspecified) to Sourceforge.

    Parameters:
        username                Sourceforge user account
        release_path            Location of the release files to upload
                                (see buildrelease.py)

    Options:
        -h | --help             Show this usage message
        -f | --files            Upload release files only
        -d | --doc              Upload documentation only
        -n | --dry-run          Do not upload the files
''' % (
        path.basename(__file__)
    )
#end usage()


def call_rsync(usr, opt, src, dst):
    ''' Calls rsync to upload files with given parameters
        usr = ssh username
        opt = options
        src = source directory
        dst = target directory
    '''
    global dry_run

    command = rsync_cmd.format(usr=usr, opt=opt, src=src, dst=dst)

    if dry_run:
        print command
    else:
        subprocess.call(command, shell=True)


def get_release_version():
    ''' Get the version number from the zip file to upload
    '''
    try:
        zipfile = glob.glob('adodb-*.zip')[0]
    except IndexError:
        print "ERROR: release zip file not found in '%s'" % release_path
        sys.exit(1)

    version = re.search("^adodb-([\d]+\.[\d]+\.[\d]+)\.zip$", zipfile).group(1)

    return version


def main():
    # 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) < 1:
        usage()
        print "ERROR: please specify the Sourceforge user and release_path"
        sys.exit(1)

    global upload_files, upload_doc, dry_run, username, release_path

    upload_files = True
    upload_doc = True
    dry_run = False

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

        elif opt in ("-f", "--files"):
            upload_doc = False

        elif opt in ("-d", "--doc"):
            upload_files = False

        elif opt in ("-n", "--dry-run"):
            dry_run = True

    # Mandatory parameters
    username = args[0]

    # Change to release directory, current if not specified
    try:
        release_path = args[1]
        os.chdir(release_path)
    except IndexError:
        release_path = os.getcwd()

    # Upload release files
    if upload_files:
        version = get_release_version()

        # Start upload process
        print "ADOdb release upload script"

        target = sf_files.format(ver=version)
        print
        print "Uploading release files..."
        print "  Target: " + target
        print
        call_rsync(
            username,
            "--exclude=docs",
            path.join(release_path, "*"),
            target
        )

    # Upload documentation
    if upload_doc:
        print
        print "Uploading documentation..."
        print
        call_rsync(
            username,
            "",
            path.join(release_path, "docs", "*"),
            sf_doc
        )

#end main()

if __name__ == "__main__":
    main()