Skip to content

Commit c663c46

Browse files
robertpfeifferbrvier
authored andcommitted
Adding more assets (kivy#2132)
* command line option to add to the assets/ directory * allow custom destination paths * code review suggestions * fix bugs sleepliy introduced in commit "code review suggestions" * flake8 compliance
1 parent cfc7a15 commit c663c46

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ def get_bootstrap_name():
8787
join(curdir, 'templates')))
8888

8989

90-
def try_unlink(fn):
91-
if exists(fn):
92-
os.unlink(fn)
93-
94-
9590
def ensure_dir(path):
9691
if not exists(path):
9792
makedirs(path)
@@ -239,8 +234,7 @@ def make_package(args):
239234
assets_dir = "src/main/assets"
240235

241236
# Delete the old assets.
242-
try_unlink(join(assets_dir, 'public.mp3'))
243-
try_unlink(join(assets_dir, 'private.mp3'))
237+
shutil.rmtree(assets_dir, ignore_errors=True)
244238
ensure_dir(assets_dir)
245239

246240
# Add extra environment variable file into tar-able directory:
@@ -304,6 +298,15 @@ def make_package(args):
304298
tar_dirs.append(python_bundle_dir)
305299
if get_bootstrap_name() == "webview":
306300
tar_dirs.append('webview_includes')
301+
302+
for asset in args.assets:
303+
asset_src, asset_dest = asset.split(":")
304+
if isfile(realpath(asset_src)):
305+
ensure_dir(dirname(join(assets_dir, asset_dest)))
306+
shutil.copy(realpath(asset_src), join(assets_dir, asset_dest))
307+
else:
308+
shutil.copytree(realpath(asset_src), join(assets_dir, asset_dest))
309+
307310
if args.private or args.launcher:
308311
make_tar(
309312
join(assets_dir, 'private.mp3'), tar_dirs, args.ignore_path,
@@ -602,6 +605,10 @@ def parse_args_and_make_package(args=None):
602605
help='Custom key=value to add in application metadata')
603606
ap.add_argument('--uses-library', dest='android_used_libs', action='append', default=[],
604607
help='Used shared libraries included using <uses-library> tag in AndroidManifest.xml')
608+
ap.add_argument('--asset', dest='assets',
609+
action="append", default=[],
610+
metavar="/path/to/source:dest",
611+
help='Put this in the assets folder at assets/dest')
605612
ap.add_argument('--icon', dest='icon',
606613
help=('A png file to use as the icon for '
607614
'the application.'))

pythonforandroid/toolchain.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ def add_parser(subparsers, *args, **kwargs):
491491
# However, it is also needed before the distribution is finally
492492
# assembled for locating the setup.py / other build systems, which
493493
# is why we also add it here:
494+
parser_packaging.add_argument(
495+
'--add-asset', dest='assets',
496+
action="append", default=[],
497+
help='Put this in the assets folder in the apk.')
494498
parser_packaging.add_argument(
495499
'--private', dest='private',
496500
help='the directory with the app source code files' +
@@ -949,6 +953,14 @@ def _fix_args(args):
949953
fix_args = ('--dir', '--private', '--add-jar', '--add-source',
950954
'--whitelist', '--blacklist', '--presplash', '--icon')
951955
unknown_args = args.unknown_args
956+
957+
for asset in args.assets:
958+
if ":" in asset:
959+
asset_src, asset_dest = asset.split(":")
960+
else:
961+
asset_src = asset_dest = asset
962+
# take abspath now, because build.py will be run in bootstrap dir
963+
unknown_args += ["--asset", os.path.abspath(asset_src)+":"+asset_dest]
952964
for i, arg in enumerate(unknown_args):
953965
argx = arg.split('=')
954966
if argx[0] in fix_args:

0 commit comments

Comments
 (0)