Skip to content

Commit cdf3c0e

Browse files
committed
[GR-41622] Simplify and reduce the resources we store for standalone/python embeddings.
PullRequest: graalpython/2754
2 parents 1444ee3 + 41a1af0 commit cdf3c0e

File tree

1 file changed

+14
-16
lines changed
  • graalpython/lib-graalpython/modules/standalone

1 file changed

+14
-16
lines changed

graalpython/lib-graalpython/modules/standalone/__main__.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,14 @@ def bundle_python_resources(
138138
zf,
139139
__graalpython__.capi_home,
140140
f"{vfs_prefix}/{home_prefix}/lib-graalpython",
141-
data_only=True,
141+
path_filter=lambda file=None, dir=None: file and file.endswith(".py"),
142142
)
143143
write_folder_to_zipfile(
144-
zf, __graalpython__.stdlib_home, f"{vfs_prefix}/{home_prefix}/lib-python/3"
144+
zf,
145+
__graalpython__.stdlib_home,
146+
f"{vfs_prefix}/{home_prefix}/lib-python/3",
147+
path_filter=lambda file=None, dir=None: dir
148+
and dir in ["idlelib", "ensurepip", "tkinter", "turtledemo"],
145149
)
146150

147151
if venv:
@@ -157,30 +161,27 @@ def bundle_python_resources(
157161
os.unlink(name)
158162

159163

160-
def write_folder_to_zipfile(zf, folder, prefix, data_only=False):
164+
def write_folder_to_zipfile(zf, folder, prefix, path_filter=lambda file=None, dir=None: False):
161165
"""
162166
Store a folder with Python modules. We do not store source code, instead,
163-
we for each py file we create a pyc entry rightaway. .py files are created
164-
as empty files, their hashed bitcode set to not check the file hash. This
165-
ensures that packages are still imported correctly without storing any
166-
source code in directly. Any other resources in the folder are stored
167-
as-is. If data_only is given, neither .py nor .pyc files are added to the
168-
archive.
167+
we for each py file we create a pyc entry rightaway. Any other resources in the
168+
folder are stored as-is. If data_only is given, neither .py nor .pyc files are
169+
added to the archive.
169170
"""
170171
folder = folder.rstrip("/\\")
171172
for root, dirs, files in os.walk(folder):
173+
dirs[:] = filter(lambda d: not path_filter(dir=d) and d != "__pycache__", dirs)
172174
for dir in dirs:
173175
fullname = os.path.join(root, dir)
174176
arcname = os.path.join(prefix, fullname[len(folder) + 1 :])
175177
zf.writestr(zipfile.ZipInfo(f"{arcname}/"), b"")
176178
for file in files:
179+
if path_filter(file=file):
180+
continue
177181
fullname = os.path.join(root, file)
178182
arcname = os.path.join(prefix, fullname[len(folder) + 1 :])
179183
if file.endswith(".py"):
180-
if data_only:
181-
continue
182-
zf.writestr(arcname, b"")
183-
pycname = _frozen_importlib_external.cache_from_source(fullname)
184+
arcname = os.path.splitext(arcname)[0] + ".pyc"
184185
with io.open_code(fullname) as sourcefile:
185186
code = sourcefile.read()
186187
try:
@@ -191,10 +192,7 @@ def write_folder_to_zipfile(zf, folder, prefix, data_only=False):
191192
data = _frozen_importlib_external._code_to_hash_pyc(
192193
bytecode, b"0" * 8, checked=False
193194
)
194-
arcname = os.path.join(prefix, pycname[len(folder) + 1 :])
195195
zf.writestr(arcname, data)
196-
elif file.endswith(".pyc"):
197-
pass
198196
else:
199197
zf.write(fullname, arcname)
200198

0 commit comments

Comments
 (0)