-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmake_dist.in
executable file
·279 lines (236 loc) · 9.34 KB
/
make_dist.in
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
277
278
279
#!/usr/bin/env python
# This software was produced by NIST, an agency of the U.S. government,
# and by statute is not subject to copyright in the United States.
# Recipients of this software assume all responsibilities associated
# with its operation, modification and maintenance. However, to
# facilitate maintenance we ask that before distributing modified
# versions of this software, you first contact the authors at
# Script for building a distribution of OOF2 or OOF3D. Takes care of
# git stuff, and sets the version number in the packaged source code.
# The script creates a git branch for the release. The automatically
# created release branches should never be merged back into master,
# because the modified version numbers will create conflicts. There
# should be no need to merge them anyway.
# This script is created by running "make make_dist" after creating
# the build tools by running cmake.
# The options are:
# --comment a string containing no dots (optional)
# --branch Use the given branch, instead of master. (optional)
# The branch is still retrieved from github, so it must
# be committed and pushed.
import getopt
import os
import os.path
import shutil
import subprocess
import sys
import tempfile
import time
#=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=#
## Utilities
def osCall(*args):
print("--->", ' '.join(args), file=sys.stderr)
proc = subprocess.run(args)
if proc.returncode != 0:
print("Failed to execute", ' '.join(args), file=sys.stderr)
print("Aborting!", file=sys.stderr)
sys.exit(proc.returncode)
#=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=#
options = ['comment=', 'dryrun', 'help', 'noclean', 'branch=']
def state_options_and_quit():
print("""Options are:
--comment=<comment> Optional comment, cannot contain dots or spaces
Debugging options are:
--noclean Don't remove temp directory
--dryrun Don't actually commit, tag, or push in git
--branch=<branchname> Use the given branch, not master.
--help Print this
""", file=sys.stderr)
sys.exit()
version = "@oof2_VERSION@"
comment = None
branch = None
dryrun = False
noclean = False
try:
optlist, args = getopt.getopt(sys.argv[1:], '', options)
except getopt.error as message:
print(message)
sys.exit()
for opt in optlist:
if opt[0] == '--comment':
comment = opt[1]
elif opt[0] == '--noclean':
noclean = True
elif opt[0] == '--dryrun':
dryrun = True
elif opt[0] == '--branch':
branch = opt[1]
elif opt[0] == '--help':
state_options_and_quit()
startdir = os.getcwd()
print(f"Building oof2 distribution named {version}", file=sys.stderr)
# Create a temp directory.
tempdir = tempfile.mkdtemp(prefix='oof-tempdir-'+version+'-')
print("Using temp directory", tempdir, file=sys.stderr)
try:
# Get the git repository location
proc = subprocess.run(['git', 'remote', 'get-url', 'origin'],
capture_output=True, text=True)
if proc.stderr:
print("*** Failed to get git repository!", \
proc.stderr.strip(), file=sys.stderr)
sys.exit(1)
giturl = proc.stdout.strip()
# cd to the temp directory
print("Changing directory to", tempdir, file=sys.stderr)
os.chdir(tempdir)
# Clone the git repository into a directory whose name is given by
# the version number.
distdir = "oof2-" + version
if branch is None:
cmd = ['git', 'clone', giturl, distdir]
else:
cmd = ['git', 'clone', '--branch', branch, giturl, distdir]
osCall(*cmd)
# cd to the cloned source code directory
print("Changing directory to", distdir, file=sys.stderr)
os.chdir(distdir)
# Construct the commit message and new branch name (aka tag)
commit_msg = f"Building oof2 release version {version}"
if comment:
commit_msg += " -- " + comment
tag = "oof2-" + version
if comment:
oldtag = tag
comment.replace(' ', '-')
tag += "--" + comment
# Check and/or fix the tag if it's not legal
cmd = ['git', 'check-ref-format', '--normalize', '--allow-onelevel', tag]
proc = subprocess.run(cmd, capture_output=True, text=True)
if proc.returncode == 0: # success
tag = proc.stdout.strip()
else:
print("Failed to convert '%s' to a legal tag." % tag, file=sys.stderr)
print("Using '%s' instead." % oldtag, file=sys.stderr)
tag = oldtag
# Create a branch for the release. This is done in the cloned
# repository in the temp directory, so if dryrun==True it has no
# long lasting effects.
# First see if the branch already exists. "git ls-remote" returns
# 2 if the branch doesn't exist, when given the --exit-code
# argument.
cmd = ['git', 'ls-remote', '--exit-code', 'origin', tag]
if subprocess.run(cmd).returncode == 2:
# The branch doesn't already exist.
newbranch = True
osCall('git', 'checkout', '-b', tag)
else:
# The branch already exists. Check it out, and merge the
# current base branch into it. There *should* be no conflicts in
# the merge.
newbranch = False
osCall('git', 'checkout', tag)
mbranch = branch if branch else 'master'
print(f"Merging {mbranch} into {tag}", file=sys.stderr)
proc = subprocess.run(['git', 'merge', '--no-edit', mbranch],
text=True, capture_output=True)
print(proc.stdout, file=sys.stderr)
if proc.returncode != 0:
print(proc.stderr, file=sys.stderr)
print(f"Failed to merge master into {tag}", file=sys.stderr)
sys.exit(1)
# Push the branch to the server, unless this is just a dry run.
if not newbranch:
cmd = ['git', 'push', 'origin', tag]
else:
cmd = ['git', 'push', '--set-upstream', 'origin', tag]
if not dryrun:
osCall(*cmd)
else:
print("Dry run! Not running:", file=sys.stderr)
print(" ".join(cmd), file=sys.stderr)
# The distribution includes two files, MANIFEST and package_date,
# that aren't in git, and are therefore constructed only now that
# all the git manipulations are complete.
# Make a timestamp file for the distribution.
timefile = open("package_date", "w")
print(time.ctime(), file=timefile)
timefile.close()
# Make the MANIFEST file after moving back up to the temp
# directory. Moving up to the temp directory means that the path
# names in the file will all start with "oof2-version/",
# which is nice and modular.
print("Changing directory to", tempdir, file=sys.stderr)
os.chdir(tempdir)
globalExcludeDirs = [
".git"
]
excludeDirs = [
"math",
"NOTES",
"3DSandbox",
"SRC/DOC",
"SRC/TEST-DATA", "SRC/TEST-SRC",
"SRC/common/EXTRA",
"SRC/engine/EXTRA",
"SRC/engine/PETSc",
"SRC/image/GRAINBDY",
"SRC/image/SEGMENTATION",
"SRC/image/imagemanip",
"TEST/BENCHMARK"
]
globalExcludeFiles = [
".gitignore"
]
excludeFiles = [
"make_dist.in",
"oof2-build", "oof2-clean",
"SRC/header", "SRC/header.py",
"SRC/maketags", "SRC/switchboard.txt",
"SRC/TODO", "SRC/commentline",
"SRC/OPTIMIZATION"
]
excludeDirs = [os.path.join(distdir, f) for f in excludeDirs]
excludeFiles = [os.path.join(distdir, f) for f in excludeFiles]
def getFiles(path, manifest):
if os.path.isdir(path):
files = os.listdir(path) # just file name, no path components
for f in files:
if path != ".":
fname = os.path.join(path, f)
else:
fname = f
if (os.path.isfile(fname) and f not in globalExcludeFiles and
fname not in excludeFiles) :
print(fname, file=manifest)
if (os.path.isdir(fname) and f not in globalExcludeDirs and
fname not in excludeDirs):
getFiles(fname, manifest)
print("Building MANIFEST", file=sys.stderr)
manifest = open(os.path.join(distdir, "MANIFEST"), "w")
getFiles(distdir, manifest)
manifest.close()
# Build the distribution.
distfilename = distdir + ".tar.gz"
print("Distribution file is", distfilename, file=sys.stderr)
cmd = ['tar', '-T', os.path.join(distdir, 'MANIFEST'), '-czf', distfilename]
osCall(*cmd)
print("Moving", distfilename, "to", startdir, file=sys.stderr)
finaldistfilename = os.path.join(startdir, distfilename)
os.rename(distfilename, finaldistfilename)
finally:
if not noclean:
print("Removing", tempdir, file=sys.stderr)
shutil.rmtree(tempdir)
else:
print("Not removing", tempdir, file=sys.stderr)
print("Done.", distfilename, "is ready.", file=sys.stderr)
print("""To publish it, copy it to WEBPAGES/oof2/source and edit
WEBPAGES/oof2/index.html. Remember to check that the README file in
WEBPAGES/oof2/source is up to date.""", file=sys.stderr)
osCall("openssl", "dgst", "-md5", finaldistfilename)
osCall("openssl", "dgst", "-rmd160", finaldistfilename)
osCall("openssl", "dgst", "-sha256", finaldistfilename)
osCall("wc", "-c", finaldistfilename)