-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReleaseHelper.py
More file actions
executable file
·220 lines (167 loc) · 7.46 KB
/
Copy pathReleaseHelper.py
File metadata and controls
executable file
·220 lines (167 loc) · 7.46 KB
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
#!/usr/bin/env python2
"""Helper for cutting Python releases.
This script creates a compressed tar file named Webware-VER.tar.gz
in the parent directory of Webware. The package can be built either
from the current Git workspace or from a tag in the Git repository.
The current Git workspace will be used if you call the script without
any arguments, like that:
> bin/ReleaseHelper.py
Since the release will be created from the files in the live workspace,
it should be clean and up to date and not have had install.py run on it,
or your distro will end up with generated docs. However, the versions
should have been set with the bin/setversion script.
The workspace will not be touched in the process.
The version number for the release tarball is taken from
Webware/Properties.py like you would expect.
Instead of using the workspace, you can also extract the release directly
from a tag in the Git repository, for instance:
> bin/ReleaseHelper.py tag=1.1.2
This will extract the files tagged with 1.1.2 and build the
tarball Webware-1.1.2.tar.gz in Webware's parent directory.
This means the release will match exactly what is in the repository,
reducing the risk of local changes, modified files, or new files
which are not in the repository from showing up in the release.
This script only works on Posix. Releases are not created on Windows
because permissions and EOLs can be problematic for other platforms.
Use the option pgk=zip to create a zip archive instead of a tarball.
For more information, see the Release Procedures in the Webware docs.
"""
import os
import sys
import time
class ReleaseHelper(object):
def main(self):
self.writeHello()
self.checkPlatform()
self.readArgs()
self.buildRelease()
def buildRelease(self):
"""Prepare a release by extracting files from the repository.
You can specify a tag name on the command line, like
> bin/ReleaseHelper.py tag=1.1.2
This will extract the files tagged with 1.1.2 and build the
tarball Webware-1.1.2.tar.gz in your parent directory.
This means the release will match exactly what is in the repository,
and reduces the risk of local changes, modified files, or new
files which are not in the repository from showing up in the release.
"""
tag = self._args.get('tag')
pkg = self._args.get('pkg')
pkgType = 'zip archive' if pkg == 'zip' else 'tarball'
if tag:
print "Creating %s from tag %s ..." % (pkgType, tag)
else:
print "Creating %s from current workspace..." % pkgType
# the location of this script:
progPath = os.path.join(os.getcwd(), sys.argv[0])
# we assume this script is located in Webware/bin/
webwarePath = os.path.dirname(os.path.dirname(progPath))
# make the Webware directory our current directory
self.chdir(webwarePath)
# the tarball will land in its parent directory:
tarDir = os.pardir
if webwarePath not in sys.path:
sys.path.insert(1, webwarePath)
from MiscUtils.PropertiesObject import PropertiesObject
target = 'Release'
try:
os.mkdir(target)
except OSError:
print "Staging directory already exists:", target
print "Please remove this directory."
return
cleanup = [target]
source = 'tags/%s' % tag if tag else 'HEAD'
try:
self.run('git archive %s | tar -x -C %s' % (source, target))
if not os.path.exists(target):
print "Unable to extract from %r" % source
if tag:
print "Perhaps the tag %r does not exist." % tag
self.error()
propertiesFile = os.path.join(target, 'Properties.py')
if not os.path.exists(propertiesFile):
self.error('Properties.py not found.')
props = PropertiesObject(propertiesFile)
if props.get('name') != 'Webware for Python':
self.error('This is not a Webware package.')
ver = props['versionString']
print "Webware version is:", ver
if not tag:
# timestamp for time of release used to in versioning the file
year, month, day = time.localtime(time.time())[:3]
datestamp = "%04d%02d%02d" % (year, month, day)
# drop leading 2 digits from year. (Ok, itn's not Y2K but it
# is short and unique in a narrow time range of 100 years.)
datestamp = datestamp[2:]
ver += "-" + datestamp
print "Packaged release will be:", ver
pkgDir = "Webware-%s" % ver
if os.path.exists(pkgDir):
self.error("%s is in the way, please remove it." % pkgDir)
# rename the target to the pkgDir so the extracted parent
# directory from the tarball will be unique to this package.
self.run("mv %s %s" % (target, pkgDir))
cleanup.append(pkgDir)
pkgExt = '.zip' if pkg == 'zip' else '.tar.gz'
pkgName = os.path.join(pkgDir + pkgExt)
# cleanup .git files
self.run("find %s -name '.git*' -exec rm {} \;" % pkgDir)
# We could cleanup any other files not part of this release here.
# (For instance, we could create releases without documentation).
# We could also create additional files to be part of this release
# without being part of the repository, for instance documentation
# that is automatically created from markup.
pkgPath = os.path.join(tarDir, pkgName)
if os.path.exists(pkgPath):
self.error("%s is in the way, please remove it." % pkgPath)
tarCmd = 'zip -qr' if pkg == 'zip' else 'tar -czf'
self.run('%s %s %s' % (tarCmd, pkgPath, pkgDir))
if not os.path.exists(pkgPath):
self.error('Could not create %s.' % pkgType)
finally: # Clean up
for path in cleanup:
if os.path.exists(path):
self.run('rm -rf ' + path)
print
print "file:", pkgName
print "dir:", os.path.abspath(tarDir)
print "size:", os.path.getsize(pkgPath), 'Bytes'
print
print 'Success.'
print
def writeHello(self):
print
print 'Webware for Python'
print 'Release Helper'
print
def checkPlatform(self):
if os.name != 'posix':
print 'This script only runs on Posix. Your op sys is %s.' % os.name
print 'Webware releases are always created on Posix machines.'
print 'These releases work on both Posix and MS Windows.'
self.error()
def readArgs(self):
args = {}
for arg in sys.argv[1:]:
try:
name, value = arg.split('=', 1)
except ValueError:
self.error('Invalid argument: %s' % arg)
args[name] = value
self._args = args
def error(self, msg=''):
if msg:
print 'ERROR: %s' % msg
sys.exit(1)
def chdir(self, path):
print 'cmd> chdir %s' % path
os.chdir(path)
def run(self, cmd):
"""Runs an arbitrary Unix command."""
print 'cmd>', cmd
results = os.popen(cmd).read()
if results:
print results
if __name__ == '__main__':
ReleaseHelper().main()