Skip to content

Commit

Permalink
Merge pull request #60 from inclement/p4a
Browse files Browse the repository at this point in the history
Add ability to choose python-for-android directory
  • Loading branch information
tito committed Feb 6, 2014
2 parents f04a56a + 58d9d28 commit c6c0acd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
46 changes: 46 additions & 0 deletions buildozer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def __init__(self, filename='buildozer.spec', target=None):
self.config.read(filename)
self.check_configuration_tokens()

# Check all section/tokens for env vars, and replace the
# config value if a suitable env var exists.
set_config_from_envs(self.config)

try:
self.log_level = int(self.config.getdefault(
'buildozer', 'log_level', '1'))
Expand Down Expand Up @@ -919,6 +923,9 @@ def _get_config_list(self, section, token, default=None, with_values=False):
# monkey-patch method for ConfigParser
# get a key as a list of string, seperated from the comma

# check if an env var exists that should replace the file config
set_config_token_from_env(section, token, self.config)

# if a section:token is defined, let's use the content as a list.
l_section = '{}:{}'.format(section, token)
if self.config.has_section(l_section):
Expand All @@ -939,7 +946,12 @@ def _get_config_list(self, section, token, default=None, with_values=False):

def _get_config_default(self, section, token, default=None):
# monkey-patch method for ConfigParser
# get an appropriate env var if it exists, else
# get a key in a section, or the default

# check if an env var exists that should replace the file config
set_config_token_from_env(section, token, self.config)

if not self.config.has_section(section):
return default
if not self.config.has_option(section, token):
Expand All @@ -949,6 +961,10 @@ def _get_config_default(self, section, token, default=None):
def _get_config_bool(self, section, token, default=False):
# monkey-patch method for ConfigParser
# get a key in a section, or the default

# check if an env var exists that should replace the file config
set_config_token_from_env(section, token, self.config)

if not self.config.has_section(section):
return default
if not self.config.has_option(section, token):
Expand Down Expand Up @@ -1211,3 +1227,33 @@ def run_remote():
pass
except BuildozerException as error:
Buildozer().error('%s' % error)

def set_config_from_envs(config):
'''Takes a ConfigParser, and checks every section/token for an
environment variable of the form SECTION_TOKEN, with any dots
replaced by underscores. If the variable exists, sets the config
variable to the env value.
'''
for section in config.sections():
for token in config.options(section):
set_config_token_from_env(section, token, config)

def set_config_token_from_env(section, token, config):
'''Given a config section and token, checks for an appropriate
environment variable. If the variable exists, sets the config entry to
its value.
The environment variable checked is of the form SECTION_TOKEN, all
upper case, with any dots replaced by underscores.
Returns True if the environment variable exists and was used, or
False otherwise.
'''
env_var_name = ''.join([section.upper(), '_',
token.upper().replace('.', '_')])
env_var = os.environ.get(env_var_name)
if env_var is None:
return False
config.set(section, token, env_var)
return True
3 changes: 3 additions & 0 deletions buildozer/default.spec
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ fullscreen = 1
# (str) Android SDK directory (if empty, it will be automatically downloaded.)
#android.sdk_path =

# (str) python-for-android git clone directory (if empty, it will be automatically cloned from github)
#android.p4a_dir =

# (str) Android entry point, default is ok for Kivy-based app
#android.entrypoint = org.renpy.android.PythonActivity

Expand Down
18 changes: 13 additions & 5 deletions buildozer/targets/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ def install_platform(self):
cmd = self.buildozer.cmd
self.pa_dir = pa_dir = join(self.buildozer.platform_dir, 'python-for-android')
if not self.buildozer.file_exists(pa_dir):
cmd('git clone git://github.com/kivy/python-for-android',
system_p4a_dir = self.buildozer.config.getdefault('app', 'p4a_dir')
if system_p4a_dir:
cmd('ln -s {} ./python-for-android'.format(system_p4a_dir),
cwd = self.buildozer.platform_dir)
else:
cmd('git clone git://github.com/kivy/python-for-android',
cwd=self.buildozer.platform_dir)
elif self.platform_update:
cmd('git clean -dxf', cwd=pa_dir)
Expand Down Expand Up @@ -352,7 +357,9 @@ def compile_platform(self):
need_compile = 0
if last_requirements != android_requirements:
need_compile = 1
if not self.buildozer.file_exists(self.pa_dir, 'dist', 'default', 'build.py'):

dist_name = self.buildozer.config.get('app', 'package.name')
if not self.buildozer.file_exists(self.pa_dir, 'dist', dist_name, 'build.py'):
need_compile = 1

if not need_compile:
Expand All @@ -362,8 +369,8 @@ def compile_platform(self):
modules_str = ' '.join(android_requirements)
cmd = self.buildozer.cmd
self.buildozer.debug('Clean and build python-for-android')
cmd('git clean -dxf', cwd=self.pa_dir)
cmd('./distribute.sh -m "{0}"'.format(modules_str), cwd=self.pa_dir)
cmd('./distribute.sh -m "{0}" -d "{1}"'.format(modules_str, dist_name),
cwd=self.pa_dir)
self.buildozer.debug('Remove temporary build files')
self.buildozer.rmdir(join(self.pa_dir, 'build'))
self.buildozer.rmdir(join(self.pa_dir, '.packages'))
Expand All @@ -383,7 +390,8 @@ def _get_package(self):
return package.lower()

def build_package(self):
dist_dir = join(self.pa_dir, 'dist', 'default')
dist_name = self.buildozer.config.get('app', 'package.name')
dist_dir = join(self.pa_dir, 'dist', dist_name)
config = self.buildozer.config
package = self._get_package()
version = self.buildozer.get_version()
Expand Down

0 comments on commit c6c0acd

Please sign in to comment.