Skip to content

Commit cef4992

Browse files
committed
Add --top-dir-name arg to allow renaming top level dir of zip file
1 parent c5cc75c commit cef4992

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ optional arguments:
105105
against the manifest. (default: False)
106106
--exclude EXCLUDE Exclude this filename from the archive. May be
107107
repeated. (default: None)
108+
--top-dir-name TOP_DIR_NAME, -t TOP_DIR_NAME
109+
Top dir of the resulting zip: Default=None means use
110+
the full name of the zip. A string means use that name
111+
as the top dir. '.' means no top dir; put the manifest
112+
and contents at top level. (default: None)
108113
```
109114
The following arguments are required: `--base-version`/`-B`, `--name`/`-n`, `dir`.
110115

build-binary-artifact.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,36 +110,36 @@ def hash_dir_contents(dirs, ignore_pattern=None, verbose=False):
110110
raise RuntimeError("hash_dir_contents: exception '%s' processing '%s'"%(e, d))
111111
return SHAhash.hexdigest()[0:16]
112112

113-
def make_tarfile(dirs, manifest, manifest_name, top_level_name, outdir, exclude=None):
114-
"""Make a tarfile named <top_level_name>.tgz from all the files in dir
113+
def make_tarfile(dirs, manifest, manifest_name, outname, top_level_name, outdir, exclude=None):
114+
"""Make a tarfile named <outname>.tgz from all the files in dir
115115
Include the manifest, so the result looks like:
116-
tar filename: <top_level_name>.tgz
116+
tar filename: <outname>.tgz
117117
contents:
118118
top_level_name/
119119
<manifest>
120120
dir/
121121
...
122122
Note: exclude is not yet implemented.
123123
"""
124-
tarfilename = os.path.join(outdir, "%s.tgz" % top_level_name)
124+
tarfilename = os.path.join(outdir, "%s.tgz" % outname)
125125
with tarfile.open(tarfilename, 'w:gz') as f:
126126
f.add(manifest, '%s/%s' % (top_level_name, manifest_name))
127127
for dir in dirs:
128128
f.add(dir, '%s/%s' % (top_level_name, dir))
129129
return tarfilename
130130

131-
def make_zipfile(dirs, manifest, manifest_name, top_level_name, outdir, exclude=None):
132-
"""Make a zipfile named <top_level_name>.zip from all the files in dir
131+
def make_zipfile(dirs, manifest, manifest_name, outname, top_level_name, outdir, exclude=None):
132+
"""Make a zipfile named <outname>.zip from all the files in dir
133133
Include the manifest, so the result looks like:
134-
zip filename: <top_level_name>.zip
134+
zip filename: <outname>.zip
135135
contents:
136-
top_level_name/
136+
top_level_name/ (if --add-top-dir specified)
137137
<manifest>
138138
dir/
139139
...
140140
Exclude any filename in the exclude list
141141
"""
142-
zipfilename = os.path.join(outdir, "%s.zip" % top_level_name)
142+
zipfilename = os.path.join(outdir, "%s.zip" % outname)
143143
with zipfile.ZipFile(zipfilename, 'w', zipfile.ZIP_DEFLATED) as f:
144144
f.write(manifest, '%s/%s' % (top_level_name, manifest_name))
145145
for dir in dirs:
@@ -170,6 +170,12 @@ def create_artifact(args):
170170
os.chdir(args.chdir)
171171
hash = hash_dir_contents(sorted(args.dir), ignore_pattern="*-manifest.txt")
172172
outname = fullname(args, hash)
173+
if args.top_dir_name is None:
174+
top_dir_name = outname
175+
elif args.top_dir_name in ('', '.'):
176+
top_dir_name = '.'
177+
else:
178+
top_dir_name = args.top_dir_name
173179
manifest_name = '%s-manifest.txt' % args.name
174180
file, manifest = tempfile.mkstemp(prefix='%s-manifest'%args.name)
175181
os.close(file)
@@ -182,9 +188,11 @@ def create_artifact(args):
182188
print("WARNING: %s already exists in source dir %s; deleting from source!" % (manifest_name, d))
183189
os.unlink(existing_manifest)
184190
if args.tar:
185-
resultfile = make_tarfile(args.dir, manifest, manifest_name, outname, outdir_path, args.exclude)
191+
resultfile = make_tarfile(args.dir, manifest, manifest_name, outname, top_dir_name,
192+
outdir_path, args.exclude)
186193
else:
187-
resultfile = make_zipfile(args.dir, manifest, manifest_name, outname, outdir_path, args.exclude)
194+
resultfile = make_zipfile(args.dir, manifest, manifest_name, outname, top_dir_name,
195+
outdir_path, args.exclude)
188196
os.unlink(manifest)
189197
if not args.silent:
190198
print("Wrote %s"%resultfile)
@@ -310,6 +318,12 @@ class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter,
310318
help="""Exclude this filename from the archive. May be repeated.""")
311319
parser.add_argument('dir', nargs='+',
312320
help="""dirs to collect into the binary artifact""")
321+
parser.add_argument('--top-dir-name', '-t',
322+
default=None,
323+
help="""Top dir of the resulting zip:\n"""
324+
"""\tDefault=None means use the full name of the zip."""
325+
"""\tA string means use that name as the top dir."""
326+
"""\t'.' means no top dir; put the manifest and contents at top level.""")
313327
args = parser.parse_args(argv)
314328

315329
if args.build_branch is None:

0 commit comments

Comments
 (0)