2222
2323import argparse
2424import sh
25-
25+ from appdirs import user_data_dir
2626
2727from pythonforandroid .recipe import (Recipe , PythonRecipe , CythonRecipe ,
2828 CompiledComponentsPythonRecipe ,
@@ -171,8 +171,6 @@ def split_argument_list(l):
171171class ToolchainCL (object ):
172172
173173 def __init__ (self ):
174- self ._ctx = None
175-
176174 parser = argparse .ArgumentParser (
177175 description = "Tool for managing the Android / Python toolchain" ,
178176 usage = """toolchain <command> [<args>]
@@ -206,18 +204,23 @@ def __init__(self):
206204 '--debug' , dest = 'debug' , action = 'store_true' ,
207205 help = 'Display debug output and all build info' )
208206 parser .add_argument (
209- '--sdk_dir' , dest = 'sdk_dir' , default = '' ,
207+ '--sdk-dir' , '-- sdk_dir' , dest = 'sdk_dir' , default = '' ,
210208 help = 'The filepath where the Android SDK is installed' )
211209 parser .add_argument (
212- '--ndk_dir' , dest = 'ndk_dir' , default = '' ,
210+ '--ndk-dir' , '-- ndk_dir' , dest = 'ndk_dir' , default = '' ,
213211 help = 'The filepath where the Android NDK is installed' )
214212 parser .add_argument (
215- '--android_api' , dest = 'android_api' , default = 0 , type = int ,
213+ '--android-api' , '-- android_api' , dest = 'android_api' , default = 0 , type = int ,
216214 help = 'The Android API level to build against.' )
217215 parser .add_argument (
218- '--ndk_version' , dest = 'ndk_version' , default = '' ,
216+ '--ndk-version' , '-- ndk_version' , dest = 'ndk_version' , default = '' ,
219217 help = ('The version of the Android NDK. This is optional, '
220218 'we try to work it out automatically from the ndk_dir.' ))
219+ parser .add_argument (
220+ '--storage-dir' , dest = 'storage_dir' ,
221+ default = self .default_storage_dir ,
222+ help = ('Primary storage directory for downloads and builds '
223+ '(default: {})' .format (self .default_storage_dir )))
221224
222225 # AND: This option doesn't really fit in the other categories, the
223226 # arg structure needs a rethink
@@ -228,7 +231,7 @@ def __init__(self):
228231
229232 # Options for specifying the Distribution
230233 parser .add_argument (
231- '--dist_name' ,
234+ '--dist-name' , '-- dist_name' ,
232235 help = 'The name of the distribution to use or create' ,
233236 default = '' )
234237 parser .add_argument (
@@ -291,6 +294,10 @@ def __init__(self):
291294
292295 if args .debug :
293296 logger .setLevel (logging .DEBUG )
297+
298+ self .ctx = Context ()
299+ self .storage_dir = args .storage_dir
300+ self .ctx .setup_dirs (self .storage_dir )
294301 self .sdk_dir = args .sdk_dir
295302 self .ndk_dir = args .ndk_dir
296303 self .android_api = args .android_api
@@ -322,6 +329,13 @@ def __init__(self):
322329
323330 getattr (self , args .command )(unknown )
324331
332+ @property
333+ def default_storage_dir (self ):
334+ udd = user_data_dir ('python-for-android' )
335+ if ' ' in udd :
336+ udd = '~/.python-for-android'
337+ return udd
338+
325339 def _read_configuration (self ):
326340 # search for a .p4a configuration file in the current directory
327341 if not exists (".p4a" ):
@@ -335,12 +349,6 @@ def _read_configuration(self):
335349 for arg in line :
336350 sys .argv .append (arg )
337351
338- @property
339- def ctx (self ):
340- if self ._ctx is None :
341- self ._ctx = Context ()
342- return self ._ctx
343-
344352 def recipes (self , args ):
345353 parser = argparse .ArgumentParser (
346354 description = "List all the available recipes" )
@@ -409,7 +417,7 @@ def clean_dists(self, args):
409417 parser = argparse .ArgumentParser (
410418 description = "Delete any distributions that have been built." )
411419 args = parser .parse_args (args )
412- ctx = Context ()
420+ ctx = self . ctx
413421 if exists (ctx .dist_dir ):
414422 shutil .rmtree (ctx .dist_dir )
415423
@@ -424,7 +432,7 @@ def clean_builds(self, args):
424432 parser = argparse .ArgumentParser (
425433 description = "Delete all build files (but not download caches)" )
426434 args = parser .parse_args (args )
427- ctx = Context ()
435+ ctx = self . ctx
428436 # if exists(ctx.dist_dir):
429437 # shutil.rmtree(ctx.dist_dir)
430438 if exists (ctx .build_dir ):
@@ -462,7 +470,7 @@ def clean_download_cache(self, args):
462470 parser = argparse .ArgumentParser (
463471 description = "Delete all download caches" )
464472 args = parser .parse_args (args )
465- ctx = Context ()
473+ ctx = self . ctx
466474 if exists (ctx .packages_path ):
467475 shutil .rmtree (ctx .packages_path )
468476
@@ -627,7 +635,7 @@ def print_context_info(self, args):
627635 python-for-android will internally use for package building, along
628636 with information about where the Android SDK and NDK will be called
629637 from.'''
630- ctx = Context ()
638+ ctx = self . ctx
631639 for attribute in ('root_dir' , 'build_dir' , 'dist_dir' , 'libs_dir' ,
632640 'ccache' , 'cython' , 'sdk_dir' , 'ndk_dir' ,
633641 'ndk_platform' , 'ndk_ver' , 'android_api' ):
@@ -647,7 +655,7 @@ def dists(self, args):
647655 def distributions (self , args ):
648656 '''Lists all distributions currently available (i.e. that have already
649657 been built).'''
650- ctx = Context ()
658+ ctx = self . ctx
651659 dists = Distribution .get_distributions (ctx )
652660
653661 if dists :
0 commit comments