Skip to content

Commit e889298

Browse files
author
Stewart Miles
committed
Fixed generation of .unitypackage with tarfile.
When using gzip and tarfile python modules to create a .unitypackage, by default the gzip module will place the output filename in the gzip header which results in Unity on Windows (just Windows!) refusing to list the contents of the file. To workaround this issue, this change replaces the output filename in the gzip header with OUTPUT.tar.gz when writing, for example, OUTPUT.unitypackage. Tested: * All unit test pass. * Verified this works with Unity 2018.4.17f1 on Windows and still works in Unity on macOS and Linux. Bug: 165661723 Change-Id: I38580b98e44a23c8eeb71790f57184901f7d46fd
1 parent bd018b2 commit e889298

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

source/ExportUnityPackage/export_unity_package.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,31 +2539,36 @@ def create_archive(archive_filename, input_directory, timestamp):
25392539
finally:
25402540
shutil.rmtree(os.path.dirname(list_filename))
25412541
else:
2542-
with gzip.GzipFile(
2543-
archive_filename, "wb",
2544-
mtime=(timestamp if timestamp >= 0 else None)) as gzip_file:
2545-
with tarfile.open(mode="w|", fileobj=gzip_file,
2546-
format=tarfile.USTAR_FORMAT, dereference=True,
2547-
errorlevel=2) as tar_file:
2548-
def reproducible_tarinfo(tarinfo):
2549-
"""Patch TarInfo so that it generates a reproducible archive.
2550-
2551-
Args:
2552-
tarinfo: TarInfo to modify.
2553-
2554-
Returns:
2555-
Modified tarinfo.
2556-
"""
2557-
tarinfo.mtime = timestamp if timestamp >= 0 else tarinfo.mtime
2558-
tarinfo.uid = 0
2559-
tarinfo.gid = 0
2560-
tarinfo.uname = FLAGS.owner
2561-
tarinfo.gname = FLAGS.group
2562-
return tarinfo
2563-
2564-
for filename in input_filenames:
2565-
tar_file.add(filename, recursive=False,
2566-
filter=reproducible_tarinfo)
2542+
with open(archive_filename, "wb") as gzipped_tar_file:
2543+
with gzip.GzipFile(
2544+
# The filename in the archive must end with .tar or .tar.gz for
2545+
# Unity to open it on Windows.
2546+
os.path.splitext(archive_filename)[0] + ".tar.gz",
2547+
fileobj=gzipped_tar_file, mode="wb",
2548+
mtime=(timestamp if timestamp >= 0 else None)) as gzip_file:
2549+
with tarfile.open(mode="w|", fileobj=gzip_file,
2550+
format=tarfile.USTAR_FORMAT, dereference=True,
2551+
errorlevel=2) as tar_file:
2552+
def reproducible_tarinfo(tarinfo):
2553+
"""Patch TarInfo so that it generates a reproducible archive.
2554+
2555+
Args:
2556+
tarinfo: TarInfo to modify.
2557+
2558+
Returns:
2559+
Modified tarinfo.
2560+
"""
2561+
tarinfo.mtime = timestamp if timestamp >= 0 else tarinfo.mtime
2562+
tarinfo.uid = 0
2563+
tarinfo.gid = 0
2564+
tarinfo.uname = FLAGS.owner
2565+
tarinfo.gname = FLAGS.group
2566+
return tarinfo
2567+
2568+
for filename in input_filenames:
2569+
tar_file.add(filename, recursive=False,
2570+
filter=reproducible_tarinfo)
2571+
25672572
finally:
25682573
os.chdir(cwd)
25692574

0 commit comments

Comments
 (0)