Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion product/gradle-plugin/src/main/kotlin/PythonTasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ internal class TaskBuilder(
File(fte.file.parent, fte.name + "c").exists()
) {
! python.extractPackages.any {
fte.path.replace("/", ".").startsWith(it + ".")
it == "*" || fte.path.replace("/", ".").startsWith(it + ".")
}
} else false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apply plugin: 'com.android.application'
apply plugin: 'com.chaquo.python'

android {
namespace "com.chaquo.python.test"
compileSdk 31
defaultConfig {
applicationId "com.chaquo.python.test"
minSdk 24
targetSdk 31
versionCode 1
versionName "0.0.1"
python {
extractPackages "pkg1", "*"
}
ndk {
abiFilters "x86"
}
}
}
15 changes: 14 additions & 1 deletion product/gradle-plugin/src/test/integration/test_gradle_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def checkZip(self, zip_filename, files, *, pyc=False, extract_packages=[],
with self.subTest(f=f):
filename, attrs = f if isinstance(f, tuple) else (f, {})
if pyc and filename.endswith(".py"):
if any(filename.startswith(ep.replace(".", "/") + "/")
if any(ep == "*" or filename.startswith(ep.replace(".", "/") + "/")
for ep in extract_packages):
expected_files.append(filename)
filename += "c"
Expand Down Expand Up @@ -676,6 +676,19 @@ def test_variant_merge(self):
variants={"red-debug": dict(extract_packages=["common"]),
"blue-debug": dict(extract_packages=["common", "blue"])})

def test_wildcard(self):
PY_FILES = [
"pkg1/__init__.py",
"pkg2/__init__.py",
"pkg3/__init__.py",
"pkg3/mod1.py",
"pkg3/sub_pkg/__init__.py",
"pkg3/sub_pkg/mod2.py",
]
self.RunGradle("base", "ExtractPackages/wildcard",
app=PY_FILES,
extract_packages=["pkg1", "*"])


class Pyc(GradleTestCase):
FAILED = "Failed to compile to .pyc format: "
Expand Down
17 changes: 15 additions & 2 deletions product/runtime/docs/sphinx/android.rst
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,21 @@ can declare them like this::
}
}

Each extracted file will slightly slow down your app's startup, so this setting should be
used on the deepest possible package.
The files are extracted when the package is imported for the first time. Each extracted
file will slightly slow down your app's startup, so this setting should be used on the
deepest possible package.

For development purposes, it is sometimes useful to extract all packages. This is
possible using a wildcard::

chaquopy {
defaultConfig {
extractPackages("*")
}
}

If the wildcard is used, all packages are imported before your app's code starts
running.

.. _android-data:

Expand Down
1 change: 1 addition & 0 deletions product/runtime/docs/sphinx/changes/1424.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the wildcard `*` to :ref:`extractPackages` to extract all packages at startup.
11 changes: 7 additions & 4 deletions product/runtime/src/main/python/java/android/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ def hook(path):
.format(entry, type(finder).__name__))

# Extract necessary files from the root directory. This includes .pth files,
# which will be read by addsitedir below.
finder.extract_dir("", recursive=False)
# which will be read by addsitedir below. If requested, also extract all Python
# packages.
finder.extract_dir("", recursive="*" in build_json["extract_packages"])

# Extract necessary files from top-level directories which aren't Python
# packages or dist-info directories.
Expand Down Expand Up @@ -531,8 +532,10 @@ def find_lib(self, filename):

def extract_dir(self, zip_dir, recursive=True):
dotted_dir = zip_dir.replace("/", ".")
extract_package = any((dotted_dir == ep) or dotted_dir.startswith(ep + ".")
for ep in self.extract_packages)
extract_package = any(
ep in (dotted_dir, "*") or dotted_dir.startswith(ep + ".")
for ep in self.extract_packages
)

for filename in self.listdir(zip_dir):
zip_path = join(zip_dir, filename)
Expand Down
Loading