forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.py
executable file
·277 lines (233 loc) · 9.05 KB
/
package.py
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
272
273
274
275
276
#!/usr/bin/env python
"""Packaging script for Robot Framework
Usage: package.py command version_number [release_tag]
Argument 'command' can have one of the following values:
- sdist : create source distribution
- wininst : create Windows installer
- all : create both packages
- version : update only version information in 'src/robot/version.py'
- jar : create stand-alone jar file containing RF and Jython
'version_number' must be a version number in format '2.x(.y)', 'trunk' or
'keep'. With 'keep', version information is not updated.
'release_tag' must be either 'a<num>', 'b<num>', 'rc<num>' or 'final', where
'<num>' is an integer like 'a1' or 'rc2'. When 'version_number' is 'trunk',
'release_tag' is automatically assigned to the current date.
When creating the jar distribution, jython.jar must be placed in 'ext-lib'
directory, under the project root.
This script uses 'setup.py' internally. Distribution packages are created
under 'dist' directory, which is deleted initially.
Examples:
package.py sdist 2.0 final
package.py wininst keep
package.py all 2.1.13 a2
package.py sdist trunk
package.py version trunk
"""
from __future__ import with_statement
import sys
import os
from os.path import abspath, dirname, exists, join
import shutil
import re
import time
import subprocess
import zipfile
from glob import glob
import urllib
ROOT_PATH = abspath(dirname(__file__))
DIST_PATH = join(ROOT_PATH, 'dist')
BUILD_PATH = join(ROOT_PATH, 'build')
ROBOT_PATH = join(ROOT_PATH, 'src', 'robot')
JAVA_SRC = join(ROOT_PATH, 'src', 'java', 'org', 'robotframework')
JYTHON_VERSION = '2.5.3'
SETUP_PATH = join(ROOT_PATH, 'setup.py')
BITMAP = join(ROOT_PATH, 'robot.bmp')
INSTALL_SCRIPT = 'robot_postinstall.py'
VERSION_PATH = join(ROBOT_PATH, 'version.py')
POM_PATH = join(ROOT_PATH, 'pom.xml')
VERSIONS = [re.compile('^2\.\d+(\.\d+)?$'), 'trunk', 'keep']
RELEASES = [re.compile('^a\d+$'), re.compile('^b\d+$'),
re.compile('^rc\d+$'), 'final']
VERSION_CONTENT = """# Automatically generated by 'package.py' script.
import sys
VERSION = '%(version_number)s'
RELEASE = '%(release_tag)s'
TIMESTAMP = '%(timestamp)s'
def get_version(sep=' '):
if RELEASE == 'final':
return VERSION
return VERSION + sep + RELEASE
def get_full_version(who=''):
sys_version = sys.version.split()[0]
version = '%%s %%s (%%s %%s on %%s)' \\
%% (who, get_version(), _get_interpreter(), sys_version, sys.platform)
return version.strip()
def _get_interpreter():
if sys.platform.startswith('java'):
return 'Jython'
if sys.platform == 'cli':
return 'IronPython'
if 'PyPy' in sys.version:
return 'PyPy'
return 'Python'
"""
def sdist(*version_info):
version(*version_info)
_clean()
_create_sdist()
_announce()
def wininst(*version_info):
version(*version_info)
_clean()
if _verify_platform(*version_info):
_create_wininst()
_announce()
def all(*version_info):
version(*version_info)
_clean()
_create_sdist()
if _verify_platform(*version_info):
_create_wininst()
_announce()
def version(version_number, release_tag=None):
_verify_version(version_number, VERSIONS)
if version_number == 'keep':
_keep_version()
elif version_number =='trunk':
_update_version(version_number, '%d%02d%02d' % time.localtime()[:3])
else:
_update_version(version_number, _verify_version(release_tag, RELEASES))
sys.path.insert(0, ROBOT_PATH)
from version import get_version
return get_version(sep='')
def _verify_version(given, valid):
for item in valid:
if given == item or (hasattr(item, 'search') and item.search(given)):
return given
raise ValueError
def _update_version(version_number, release_tag):
timestamp = '%d%02d%02d-%02d%02d%02d' % time.localtime()[:6]
vfile = open(VERSION_PATH, 'wb')
vfile.write(VERSION_CONTENT % locals())
vfile.close()
# TODO: Fix before next final release
#_update_pom_version(version_number, release_tag)
print 'Updated version to %s %s' % (version_number, release_tag)
def _update_pom_version(version_number, release_tag):
version = '%s-%s' % (version_number, release_tag)
pom_content = open(POM_PATH).read()
with open(POM_PATH, 'w') as pom_file:
pom_file.write(re.sub('(<version>).*(</version>)',
'\\1%s\\2' % version, pom_content))
def _keep_version():
sys.path.insert(0, ROBOT_PATH)
from version import get_version
print 'Keeping version %s' % get_version()
def _clean():
print 'Cleaning up...'
for path in [DIST_PATH, BUILD_PATH]:
if exists(path):
shutil.rmtree(path)
def _verify_platform(version_number, release_tag=None):
if release_tag == 'final' and os.sep != '\\':
print 'Final Windows installers can only be created in Windows.'
print 'Windows installer was not created.'
return False
return True
def _create_sdist():
_create('sdist --force-manifest', 'source distribution')
def _create_wininst():
_create('bdist_wininst --bitmap %s --install-script %s' % (BITMAP, INSTALL_SCRIPT),
'Windows installer')
if os.sep != '\\':
print 'Warning: Windows installers created on other platforms may not'
print 'be exactly identical to ones created in Windows.'
def _create(command, name):
print 'Creating %s...' % name
rc = os.system('%s %s %s' % (sys.executable, SETUP_PATH, command))
if rc != 0:
print 'Creating %s failed.' % name
sys.exit(rc)
print '%s created successfully.' % name.capitalize()
def _announce():
print 'Created:'
for path in os.listdir(DIST_PATH):
print abspath(join(DIST_PATH, path))
def jar(*version_info):
jython_jar = _get_jython_jar()
print 'Using Jython %s' % jython_jar
ver = version(*version_info)
tmpdir = _create_tmpdir()
try:
_compile_java_classes(tmpdir, jython_jar)
_unzip_jython_jar(tmpdir, jython_jar)
_copy_robot_files(tmpdir)
_compile_all_py_files(tmpdir, jython_jar)
_overwrite_manifest(tmpdir, ver)
try:
jar_path = _create_jar_file(tmpdir, ver)
print 'Created %s based on %s' % (jar_path, jython_jar)
except subprocess.CalledProcessError:
print "Unable to create jar! Check for jar command available at the command line."
except subprocess.CalledProcessError:
print "Unable to compile java classes! Check for javac command available at the command line."
shutil.rmtree(tmpdir)
def _get_jython_jar():
lib_dir = join(ROOT_PATH, 'ext-lib')
jar_path = join(lib_dir, 'jython-standalone-%s.jar' % JYTHON_VERSION)
if os.path.exists(jar_path):
return jar_path
if not os.path.exists(lib_dir):
os.mkdir(lib_dir)
dl_url = "http://search.maven.org/remotecontent?filepath=org/python/jython-standalone/%s/jython-standalone-%s.jar" \
% (JYTHON_VERSION, JYTHON_VERSION)
print 'Jython not found, going to download from %s' % dl_url
urllib.urlretrieve(dl_url, jar_path)
return jar_path
def _compile_java_classes(tmpdir, jython_jar):
source_files = [join(JAVA_SRC, f)
for f in os.listdir(JAVA_SRC) if f.endswith('.java')]
print 'Compiling %d source files' % len(source_files)
subprocess.check_call(['javac', '-d', tmpdir, '-target', '1.5', '-source', '1.5',
'-cp', jython_jar] + source_files, shell=os.name=='nt')
def _create_tmpdir():
tmpdir = join(ROOT_PATH, 'tmp-jar-dir')
if exists(tmpdir):
shutil.rmtree(tmpdir)
os.mkdir(tmpdir)
return tmpdir
def _unzip_jython_jar(tmpdir, jython_jar):
zipfile.ZipFile(jython_jar).extractall(tmpdir)
def _copy_robot_files(tmpdir):
# pyc files must be excluded so that compileall works properly.
todir = join(tmpdir, 'Lib', 'robot')
shutil.copytree(ROBOT_PATH, todir, ignore=shutil.ignore_patterns('*.pyc'))
shutil.rmtree(join(todir, 'htmldata', 'testdata'))
def _compile_all_py_files(tmpdir, jython_jar):
subprocess.check_call(['java', '-jar', jython_jar, '-m', 'compileall', tmpdir])
# Jython will not work without its py-files, but robot will
for root, _, files in os.walk(join(tmpdir,'Lib','robot')):
for f in files:
if f.endswith('.py'):
os.remove(join(root, f))
def _overwrite_manifest(tmpdir, version):
with open(join(tmpdir, 'META-INF', 'MANIFEST.MF'), 'w') as mf:
mf.write('''Manifest-Version: 1.0
Main-Class: org.robotframework.RobotFramework
Specification-Version: 2
Implementation-Version: %s
''' % version)
def _create_jar_file(source, version):
path = join(DIST_PATH, 'robotframework-%s.jar' % version)
if not exists(DIST_PATH):
os.mkdir(DIST_PATH)
_fill_jar(source, path)
return path
def _fill_jar(sourcedir, jarpath):
subprocess.check_call(['jar', 'cvfM', jarpath, '.'], cwd=sourcedir,
shell=os.name=='nt')
if __name__ == '__main__':
try:
globals()[sys.argv[1]](*sys.argv[2:])
except (KeyError, IndexError, TypeError, ValueError):
print __doc__