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
271
|
#!/usr/bin/python -u
'''
ADOdb release upload script
'''
from distutils.version import LooseVersion
import getopt
import getpass
import glob
import json
import os
from os import path
import re
import requests
import subprocess
import sys
import yaml
# Directories and files to exclude from release tarballs
# for debugging, set to a local dir e.g. "localhost:/tmp/sf-adodb/"
sf_files = "frs.sourceforge.net:/home/frs/project/adodb/"
# SourceForge Release API base URL
# https://sourceforge.net/p/forge/documentation/Using%20the%20Release%20API/
sf_api_url = 'https://sourceforge.net/projects/adodb/files/{}/'
# rsync command template
rsync_cmd = "rsync -vP --rsh ssh {opt} {src} {usr}@{dst}"
# Command-line options
options = "hu:n"
long_options = ["help", "user=", "dry-run"]
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:
release_path Location of the release files to upload
(see buildrelease.py)
Options:
-h | --help Show this usage message
-u | --user <name> SourceForge account (defaults to current user)
-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)
# Create directory if it does not exist
dst_split = dst.rsplit(':')
host = dst_split[0]
dst = dst_split[1]
mkdir = 'ssh {usr}@{host} mkdir -p {dst}'.format(
usr=usr,
host=host,
dst=dst
)
if dry_run:
print mkdir
print command
else:
subprocess.call(mkdir, shell=True)
subprocess.call(command, shell=True)
def get_release_version():
''' Returns the version number (X.Y.Z) from the zip file to upload,
excluding the SemVer suffix
'''
try:
zipfile = glob.glob('adodb-*.zip')[0]
except IndexError:
print "ERROR: release zip file not found in '%s'" % release_path
sys.exit(1)
try:
version = re.search(
"^adodb-([\d]+\.[\d]+\.[\d]+)(-(alpha|beta|rc)\.[\d]+)?\.zip$",
zipfile
).group(1)
except AttributeError:
print "ERROR: unable to extract version number from '%s'" % zipfile
print " Only 3 groups of digits separated by periods are allowed"
sys.exit(1)
return version
def sourceforge_target_dir(version):
''' Returns the SourceForge target directory, relative to the root
directory (defined in sf_files global variable): basedir/subdir, with
basedir:
- for ADOdb version 5: adodb-php5-only
- for newer versions: adodbX (where X is the major version number)
subdir:
- if version >= 5.21: adodb-X.Y
- for older versions: adodb-XYZ-for-php5
'''
major_version = int(version.rsplit('.')[0])
# Base directory
if major_version == 5:
directory = 'adodb-php5-only/'
else:
directory = 'adodb{}/'.format(major_version)
# Keep only X.Y (discard patch number and pre-release suffix)
short_version = version.split('-')[0].rsplit('.', 1)[0]
if LooseVersion(version) >= LooseVersion('5.21'):
directory += "adodb-" + short_version
else:
directory += "adodb-{}-for-php5".format(short_version.replace('.', ''))
return directory
def load_env():
global api_key
# Load the config file
env_file = path.join(path.dirname(path.abspath(__file__)), 'env.yml')
try:
stream = file(env_file, 'r')
y = yaml.safe_load(stream)
except IOError:
print("ERROR: Environment file {} not found".format(env_file))
sys.exit(3)
except yaml.parser.ParserError as e:
print("ERROR: Invalid Environment file")
print(e)
sys.exit(3)
api_key = y['api_key']
def process_command_line():
''' Retrieve command-line options and set global variables accordingly
'''
global dry_run, username, release_path
# 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)
# Default values for flags
username = getpass.getuser()
dry_run = False
for opt, val in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(0)
elif opt in ("-u", "--user"):
username = val
elif opt in ("-n", "--dry-run"):
dry_run = True
# Mandatory parameters
# (none)
# Change to release directory, current if not specified
try:
release_path = args[0]
os.chdir(release_path)
except IndexError:
release_path = os.getcwd()
def upload_release_files():
''' Upload release files from source directory to SourceForge
'''
version = get_release_version()
target = sf_files + sourceforge_target_dir(version)
print
print "Uploading release files..."
print " Source:", release_path
print " Target: " + target
print
call_rsync(
username,
"",
path.join(release_path, "*"),
target
)
def set_sourceforge_file_info():
print("Updating uploaded files information")
base_url = sf_api_url.format(
sourceforge_target_dir(get_release_version())
)
headers = {'Accept': 'application/json"'}
# Loop through uploaded files
for file in glob.glob('adodb-*'):
print(" " + file)
# Determine defaults based on file extension
ext = file.split('.', 3)[3]
if ext == 'zip':
defaults = ['windows']
elif ext == 'tar.gz':
defaults = ['linux', 'mac', 'bsd', 'solaris', 'others']
else:
print("WARNING: Unknown extension for file " + file)
continue
# SourceForge API request
global api_key
url = path.join(base_url, file)
payload = {
'default': defaults,
'api_key': api_key
}
req = requests.put(url, headers=headers, params=payload)
# Print results
if req.status_code == requests.codes.ok:
result = json.loads(req.text)['result']
print(" Download default for: " + result['x_sf']['default'])
else:
if req.status_code == requests.codes.unauthorized:
err = "access denied"
else:
err = "SourceForge API call failed"
print("ERROR: {} - check API key".format(err))
break
def main():
load_env()
process_command_line()
# Start upload process
print "ADOdb release upload script"
upload_release_files()
set_sourceforge_file_info()
# end main()
if __name__ == "__main__":
main()
|