Skip to content

Commit 112d536

Browse files
authored
Merge pull request #1 from cloudflare/ggu/tar-gz
Convert wheels to tar.gz format
2 parents 473a517 + b9b8cee commit 112d536

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

packages/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.venv
2+
.pyodide-xbuildenv-0.26.0a3
3+
dist
4+
emsdk
5+
packages
6+
pyodide

packages/script.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ def make_bundle(tag, dist = Path("dist")):
5050
json.dump(lock, file)
5151
for package in lock["packages"].values():
5252
name = normalize(package["name"])
53+
print("untarring " + name)
54+
os.mkdir(tempdir / name)
5355
if name.endswith("-tests") or name == "test":
54-
os.mkdir(tempdir / name)
5556
continue
5657
file = dist / package["file_name"]
57-
with zipfile.ZipFile(file, "r") as zip:
58+
with tarfile.open(file, "r:gz") as zip:
5859
zip.extractall(tempdir / name)
5960
# create temp tarfile from tempdir
6061
with tarfile.open(tempdir / "pyodide_packages.tar", "w") as tar:
@@ -87,17 +88,49 @@ def upload_to_r2(tag, dist = Path("dist")):
8788
print(f"uploading {path} to {key}")
8889
s3.upload_file(str(path), "python-package-bucket", key)
8990

91+
# converts all the .zip wheels into .tar.gz format (destructively)
92+
def convert_wheels_to_tar_gz(dist = Path("dist")):
93+
with open(dist / "pyodide-lock.json", "r") as file:
94+
lock = json.load(file)
95+
96+
for package in lock["packages"].values():
97+
name = normalize(package["name"])
98+
file = dist / package["file_name"]
99+
# check file ends with .zip or .whl
100+
if not (file.name.endswith(".zip") or file.name.endswith(".whl")):
101+
continue
102+
new_file = file.with_suffix(".tar.gz")
103+
print("Converting zip file " + str(file) + " to .tar.gz format")
104+
with zipfile.ZipFile(file, "r") as zip:
105+
with tempfile.TemporaryDirectory() as t:
106+
tempdir = Path(t)
107+
zip.extractall(tempdir)
108+
# create tar.gz file from tempdir
109+
with tarfile.open(new_file, "w:gz") as tar:
110+
tar.add(tempdir, arcname="./")
111+
os.remove(file)
112+
package["file_name"] = new_file.name
113+
# update sha256 hash
114+
new_file_bytes = new_file.read_bytes()
115+
new_file_hash = hashlib.sha256(new_file_bytes).hexdigest()
116+
package["sha256"] = new_file_hash
117+
118+
with open(dist / "pyodide-lock.json", "w") as file:
119+
json.dump(lock, file)
120+
90121
if __name__ == "__main__":
122+
if len(sys.argv) != 2:
123+
print("Usage: python script.py <tag>")
124+
sys.exit(1)
125+
tag = sys.argv[1]
126+
91127
with open("required_packages.txt", "r") as file:
92128
required_packages = file.read().split("\n")
93129
status = os.system(f"pyodide build-recipes --install {' '.join(required_packages)}")
94130
if status != 0:
95131
raise Exception("Failed to build recipes")
96-
97-
if len(sys.argv) != 2:
98-
print("Usage: python script.py <tag>")
99-
sys.exit(1)
100-
tag = sys.argv[1]
132+
133+
convert_wheels_to_tar_gz()
101134

102135
make_bundle(tag)
103136
upload_to_r2(tag)

0 commit comments

Comments
 (0)