Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb273bc7

Browse files
committed
Addandroid.py test --site-packages and --cwd options
1 parentfc8c1e1 commitb273bc7

File tree

6 files changed

+76
-23
lines changed

6 files changed

+76
-23
lines changed

‎Android/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ repository's `Lib` directory will be picked up immediately. Changes in C files,
156156
and architecture-specific files such as sysconfigdata, will not take effect
157157
until you re-run`android.py make-host` or`build`.
158158

159+
The testbed app can also be used to test third-party packages. For more details,
160+
run`android.py test --help`, paying attention to the options`--site-packages`,
161+
`--cwd`,`-c` and`-m`.
162+
159163

160164
##Using in your own app
161165

‎Android/android.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
fromcontextlibimportasynccontextmanager
1515
fromdatetimeimportdatetime,timezone
1616
fromglobimportglob
17-
fromos.pathimportbasename,relpath
17+
fromos.pathimportabspath,basename,relpath
1818
frompathlibimportPath
1919
fromsubprocessimportCalledProcessError
2020
fromtempfileimportTemporaryDirectory
@@ -541,12 +541,17 @@ def log(line):
541541
args= [
542542
gradlew,"--console","plain",f"{task_prefix}DebugAndroidTest",
543543
]+ [
544+
# Build-time properties
545+
f"-Ppython.{name}={value}"
546+
forname,valuein [
547+
("sitePackages",context.site_packages), ("cwd",context.cwd)
548+
]ifvalue
549+
]+ [
550+
# Runtime properties
544551
f"-Pandroid.testInstrumentationRunnerArguments.python{name}={value}"
545552
forname,valuein [
546-
("Mode",mode),
547-
("Module",module),
548-
("Args",join_command(context.args)),
549-
]
553+
("Mode",mode), ("Module",module), ("Args",join_command(context.args))
554+
]ifvalue
550555
]
551556
log("> "+join_command(args))
552557

@@ -684,32 +689,32 @@ def signal_handler(*args):
684689

685690
defparse_args():
686691
parser=argparse.ArgumentParser()
687-
subcommands=parser.add_subparsers(dest="subcommand")
692+
subcommands=parser.add_subparsers(dest="subcommand",required=True)
688693

689694
# Subcommands
690-
build=subcommands.add_parser("build",help="Build everything")
691-
configure_build=subcommands.add_parser("configure-build",
692-
help="Run `configure` for the "
693-
"build Python")
694-
subcommands.add_parser("make-build",help="Run `make` for the build Python")
695-
configure_host=subcommands.add_parser("configure-host",
696-
help="Run `configure` for Android")
697-
make_host=subcommands.add_parser("make-host",
698-
help="Run `make` for Android")
699-
subcommands.add_parser(
700-
"clean",help="Delete all build and prefix directories")
695+
build=subcommands.add_parser(
696+
"build",help="Run configure-build, make-build, configure-host and "
697+
"make-host")
698+
configure_build=subcommands.add_parser(
699+
"configure-build",help="Run `configure` for the build Python")
701700
subcommands.add_parser(
702-
"build-testbed",help="Build the testbed app")
703-
test=subcommands.add_parser(
704-
"test",help="Run the test suite")
701+
"make-build",help="Run `make` for the build Python")
702+
configure_host=subcommands.add_parser(
703+
"configure-host",help="Run `configure` for Android")
704+
make_host=subcommands.add_parser(
705+
"make-host",help="Run `make` for Android")
706+
707+
subcommands.add_parser("clean",help="Delete all build directories")
708+
subcommands.add_parser("build-testbed",help="Build the testbed app")
709+
test=subcommands.add_parser("test",help="Run the testbed app")
705710
package=subcommands.add_parser("package",help="Make a release package")
706711
env=subcommands.add_parser("env",help="Print environment variables")
707712

708713
# Common arguments
709714
forsubcommandinbuild,configure_build,configure_host:
710715
subcommand.add_argument(
711716
"--clean",action="store_true",default=False,dest="clean",
712-
help="Delete the relevant buildand prefixdirectories first")
717+
help="Delete the relevant build directories first")
713718

714719
host_commands= [build,configure_host,make_host,package]
715720
ifin_source_tree:
@@ -737,6 +742,13 @@ def parse_args():
737742
"--managed",metavar="NAME",help="Run on a Gradle-managed device. "
738743
"These are defined in `managedDevices` in testbed/app/build.gradle.kts.")
739744

745+
test.add_argument(
746+
"--site-packages",metavar="DIR",type=abspath,
747+
help="Directory to copy as the app's site-packages.")
748+
test.add_argument(
749+
"--cwd",metavar="DIR",type=abspath,
750+
help="Directory to copy as the app's working directory.")
751+
740752
mode_group=test.add_mutually_exclusive_group()
741753
mode_group.add_argument(
742754
"-c",dest="command",help="Execute the given Python code.")

‎Android/testbed/app/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,29 @@ androidComponents.onVariants { variant ->
205205

206206
into("site-packages") {
207207
from("$projectDir/src/main/python")
208+
209+
val sitePackages= findProperty("python.sitePackages")asString?
210+
if (!sitePackages.isNullOrEmpty()) {
211+
if (!file(sitePackages).exists()) {
212+
throwGradleException("$sitePackages does not exist")
213+
}
214+
from(sitePackages)
215+
}
208216
}
209217

210218
duplicatesStrategy=DuplicatesStrategy.EXCLUDE
211219
exclude("**/__pycache__")
212220
}
221+
222+
into("cwd") {
223+
val cwd= findProperty("python.cwd")asString?
224+
if (!cwd.isNullOrEmpty()) {
225+
if (!file(cwd).exists()) {
226+
throwGradleException("$cwd does not exist")
227+
}
228+
from(cwd)
229+
}
230+
}
213231
}
214232
}
215233

‎Android/testbed/app/src/main/java/org/python/testbed/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PythonTestRunner(val context: Context) {
2525
funrun(instrumentationArgs:Bundle)=run(
2626
instrumentationArgs.getString("pythonMode")!!,
2727
instrumentationArgs.getString("pythonModule")!!,
28-
instrumentationArgs.getString("pythonArgs")!!,
28+
instrumentationArgs.getString("pythonArgs")?:"",
2929
)
3030

3131
/** Run Python.
@@ -35,7 +35,7 @@ class PythonTestRunner(val context: Context) {
3535
* "-m" mode.
3636
* @param args Arguments to add to sys.argv. Will be parsed by `shlex.split`.
3737
* @return The Python exit status: zero on success, nonzero on failure.*/
38-
funrun(mode:String,module:String,args:String = "") :Int {
38+
funrun(mode:String,module:String,args:String) :Int {
3939
Os.setenv("PYTHON_MODE", mode,true)
4040
Os.setenv("PYTHON_MODULE", module,true)
4141
Os.setenv("PYTHON_ARGS", args,true)

‎Android/testbed/app/src/main/python/android_testbed_main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,19 @@
3030
module=os.environ["PYTHON_MODULE"]
3131
sys.argv[1:]=shlex.split(os.environ["PYTHON_ARGS"])
3232

33+
cwd=f"{sys.prefix}/cwd"
34+
ifnotos.path.exists(cwd):
35+
# Empty directories are lost in the asset packing/unpacking process.
36+
os.mkdir(cwd)
37+
os.chdir(cwd)
38+
3339
ifmode=="-c":
40+
# In -c mode, sys.path starts with an empty string, which means whatever the current
41+
# working directory is at the moment of each import.
42+
sys.path.insert(0,"")
3443
exec(module, {})
3544
elifmode=="-m":
45+
sys.path.insert(0,os.getcwd())
3646
runpy.run_module(module,run_name="__main__",alter_sys=True)
3747
else:
3848
raiseValueError(f"unknown mode:{mode}")

‎Doc/using/android.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ link to the relevant file.
6363
* Add code to your app to:source:`start Python in embedded mode
6464
<Android/testbed/app/src/main/c/main_activity.c>`. This will need to be C code
6565
called via JNI.
66+
67+
Building a Python package for Android
68+
-------------------------------------
69+
70+
Python packages can be built for Android as wheels and released on PyPI. The
71+
recommended tool for doing this is `cibuildwheel
72+
<https://cibuildwheel.pypa.io/en/stable/platforms/#android>`__, which automates
73+
all the details of setting up a cross-compilation environment, building the
74+
wheel, and testing it on an emulator.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp