Repository navigation
Expand file tree
/
Copy pathmake_dist.in
More file actions
executable file
·236 lines (198 loc) · 8.09 KB
/
Copy pathmake_dist.in
File metadata and controls
executable file
·236 lines (198 loc) · 8.09 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/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
# oof_manager@nist.gov.
# Script for building a distribution of OOFCanvas. Derived from the
# same script in OOF2.
import getopt
import os
import os.path
import shutil
import subprocess
import sys
import tempfile
import time
#=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=##=--=#
## Utility function for the simplest subprocess invocation
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 branch, not master. Implies --dryrun and --noclean
Real distributions are always made without --branch.
--help Print this
""", file=sys.stderr)
sys.exit()
version = "@oofcanvas_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]
dryrun = True
noclean = True
elif opt[0] == '--help':
state_options_and_quit()
startdir = os.getcwd()
# Create a temp working directory.
tempdir = tempfile.mkdtemp(prefix="oofcanvas-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 whose name is given by the OOFCanvas
# version number: oofcanvas-version.
distdir = "oofcanvas-" + 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
commit_msg = f"Building oofcanvas release version {version}"
if comment:
commit_msg += " -- " + comment
tag = "oofcanvas-" + version
if comment:
oldtag = tag
comment.replace(' ', '-')
tag += "--" + comment
# Check and 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 master 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 branch 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("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 "oofcanvas-[version]/",
# which is nice and modular.
print("Changing directory to", tempdir, file=sys.stderr)
os.chdir(tempdir)
# Directories that are in git but should not be distributed
excludeDirs = [".git", "TEST"]
# Files that are in in git but should not be distributed
excludeFiles = ["make_dist.in", ".gitignore", "TODO.txt"]
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 fname not in excludeFiles):
print(fname, file=manifest)
if (os.path.isdir(fname) 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)
osCall("openssl", "dgst", "-md5", finaldistfilename)
osCall("openssl", "dgst", "-rmd160", finaldistfilename)
osCall("openssl", "dgst", "-sha256", finaldistfilename)
osCall("wc", "-c", finaldistfilename)