From b72176095dcf1faca89e636fab9f7d6576511f6b Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Fri, 8 Nov 2013 22:10:48 +0000 Subject: [PATCH 1/9] Added BUILDOZER_P4A_DIR env var behaviour --- buildozer/targets/android.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/buildozer/targets/android.py b/buildozer/targets/android.py index a2105033e..db446ccc7 100644 --- a/buildozer/targets/android.py +++ b/buildozer/targets/android.py @@ -300,8 +300,15 @@ 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', - cwd=self.buildozer.platform_dir) + print ' FILE NOT EXISTS' + buildozer_p4a_dir = os.environ.get('BUILDOZER_P4A_DIR') + print '!!!', buildozer_p4a_dir + if buildozer_p4a_dir: + cmd('ln -s {} ./python-for-android'.format(buildozer_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) cmd('git pull origin master', cwd=pa_dir) From 1fcae9834f85b4e2c23ba096533791f0d31ca5b2 Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Fri, 8 Nov 2013 22:17:08 +0000 Subject: [PATCH 2/9] Made p4a distribution dir name == app.package.name --- buildozer/targets/android.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/buildozer/targets/android.py b/buildozer/targets/android.py index db446ccc7..8d965af6e 100644 --- a/buildozer/targets/android.py +++ b/buildozer/targets/android.py @@ -300,9 +300,7 @@ 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): - print ' FILE NOT EXISTS' buildozer_p4a_dir = os.environ.get('BUILDOZER_P4A_DIR') - print '!!!', buildozer_p4a_dir if buildozer_p4a_dir: cmd('ln -s {} ./python-for-android'.format(buildozer_p4a_dir), cwd = self.buildozer.platform_dir) @@ -357,7 +355,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: @@ -367,8 +367,9 @@ 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('git clean -dxf', 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')) @@ -388,7 +389,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() From 707b4935cd81eb7c99f67b09e6a70963685e16b9 Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Fri, 8 Nov 2013 22:58:03 +0000 Subject: [PATCH 3/9] Minor code cleanup (formatting) --- buildozer/targets/android.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/buildozer/targets/android.py b/buildozer/targets/android.py index 8d965af6e..869b89230 100644 --- a/buildozer/targets/android.py +++ b/buildozer/targets/android.py @@ -306,7 +306,7 @@ def install_platform(self): cwd = self.buildozer.platform_dir) else: cmd('git clone git://github.com/kivy/python-for-android', - cwd=self.buildozer.platform_dir) + cwd=self.buildozer.platform_dir) elif self.platform_update: cmd('git clean -dxf', cwd=pa_dir) cmd('git pull origin master', cwd=pa_dir) @@ -367,7 +367,6 @@ 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}" -d "{1}"'.format(modules_str, dist_name), cwd=self.pa_dir) self.buildozer.debug('Remove temporary build files') From 3afa85a7fb9329783b06e981b4c4cbf223b88438 Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Sat, 23 Nov 2013 00:33:58 +0000 Subject: [PATCH 4/9] Added env var querying for default and bool --- buildozer/__init__.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/buildozer/__init__.py b/buildozer/__init__.py index d1caacfa9..28acd9942 100644 --- a/buildozer/__init__.py +++ b/buildozer/__init__.py @@ -934,7 +934,11 @@ 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 + env_var = query_env(section, token) + if env_var is not None: + return env_var if not self.config.has_section(section): return default if not self.config.has_option(section, token): @@ -944,6 +948,9 @@ 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 + env_var = query_env_bool(section, token) + if env_var is not None: + return env_var if not self.config.has_section(section): return default if not self.config.has_option(section, token): @@ -1206,3 +1213,32 @@ def run_remote(): pass except BuildozerException as error: Buildozer().error('%s' % error) + + +def query_env(section, token): + '''Given config section and token names, returns the environment + variable SECTION_TOKEN, or None if the variable does not exist.''' + env_var_name = ''.join([section.upper(), '_', + token.upper().replace('.', '_')]) + env_var = os.environ.get(env_var_name) + return env_var + +def query_env_bool(section, token): + '''Given config section and token names, uses query_env to query an + appropriate environment variable. If the variable exists, coerces to a + bool before returning.''' + env_var = query_env(section, token) + if env_var is None: + return env_var + elif env_var.tolower() in ['1', 'yes', 'true', 'on']: + return True + elif env_var.tolower() in ['0', 'no', 'false', 'off']: + return False + else: + env_var_name = ''.join([section.upper(), '_', + token.upper().replace('.', '_')]) + raise ValueError('Failed to parse {} as a config boolean. Should be one' + 'of {}'.format(env_var_name + ['1', 'yes', 'true', 'on', + '0', 'no', 'false', 'off'])) + From 11e994de147a8029674b3bef62d511ad7cac67d9 Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Sat, 23 Nov 2013 00:58:59 +0000 Subject: [PATCH 5/9] Added env var checking to all config get methods. The new method replaces the manual env var parsing with simply setting the config's value to the env var value. This lets the normal parsing methods do the work later, to avoid duplication of effort. --- buildozer/__init__.py | 45 ++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/buildozer/__init__.py b/buildozer/__init__.py index 28acd9942..2fdb8f59c 100644 --- a/buildozer/__init__.py +++ b/buildozer/__init__.py @@ -915,6 +915,8 @@ def _get_config_list(self, section, token, default=None, with_values=False): # get a key as a list of string, seperated from the comma # if a section:token is defined, let's use the content as a list. + set_config_from_env(section, token, self.config) + l_section = '{}:{}'.format(section, token) if self.config.has_section(l_section): values = self.config.options(l_section) @@ -936,9 +938,7 @@ 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 - env_var = query_env(section, token) - if env_var is not None: - return env_var + set_config_from_env(section, token, self.config) if not self.config.has_section(section): return default if not self.config.has_option(section, token): @@ -948,9 +948,7 @@ 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 - env_var = query_env_bool(section, token) - if env_var is not None: - return env_var + set_config_from_env(section, token, self.config) if not self.config.has_section(section): return default if not self.config.has_option(section, token): @@ -1214,31 +1212,22 @@ def run_remote(): except BuildozerException as error: Buildozer().error('%s' % error) +def set_config_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. -def query_env(section, token): - '''Given config section and token names, returns the environment - variable SECTION_TOKEN, or None if the variable does not exist.''' + ''' env_var_name = ''.join([section.upper(), '_', token.upper().replace('.', '_')]) env_var = os.environ.get(env_var_name) - return env_var - -def query_env_bool(section, token): - '''Given config section and token names, uses query_env to query an - appropriate environment variable. If the variable exists, coerces to a - bool before returning.''' - env_var = query_env(section, token) if env_var is None: - return env_var - elif env_var.tolower() in ['1', 'yes', 'true', 'on']: - return True - elif env_var.tolower() in ['0', 'no', 'false', 'off']: return False - else: - env_var_name = ''.join([section.upper(), '_', - token.upper().replace('.', '_')]) - raise ValueError('Failed to parse {} as a config boolean. Should be one' - 'of {}'.format(env_var_name - ['1', 'yes', 'true', 'on', - '0', 'no', 'false', 'off'])) - + config.set(section, token, env_var) + return True From fdb9c52371923afd4ad36a3a23c9a758c2e8d96a Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Sat, 23 Nov 2013 01:51:51 +0000 Subject: [PATCH 6/9] Added env check during config build --- buildozer/__init__.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/buildozer/__init__.py b/buildozer/__init__.py index 2fdb8f59c..8861d9334 100644 --- a/buildozer/__init__.py +++ b/buildozer/__init__.py @@ -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')) @@ -915,7 +919,7 @@ def _get_config_list(self, section, token, default=None, with_values=False): # get a key as a list of string, seperated from the comma # if a section:token is defined, let's use the content as a list. - set_config_from_env(section, token, self.config) + set_config_token_from_env(section, token, self.config) l_section = '{}:{}'.format(section, token) if self.config.has_section(l_section): @@ -938,7 +942,7 @@ 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 - set_config_from_env(section, token, self.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): @@ -948,7 +952,7 @@ 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 - set_config_from_env(section, token, self.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): @@ -1212,7 +1216,17 @@ def run_remote(): except BuildozerException as error: Buildozer().error('%s' % error) -def set_config_from_env(section, token, config): +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. From a949095642fc4c337f1b3deae895c3f898040dde Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Sat, 23 Nov 2013 01:55:15 +0000 Subject: [PATCH 7/9] Added a few comments and formatting changes --- buildozer/__init__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/buildozer/__init__.py b/buildozer/__init__.py index 8861d9334..4e36e4f97 100644 --- a/buildozer/__init__.py +++ b/buildozer/__init__.py @@ -918,9 +918,10 @@ 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 - # if a section:token is defined, let's use the content as a list. + # 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): values = self.config.options(l_section) @@ -942,7 +943,10 @@ 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): @@ -952,7 +956,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): From aba7f78e6c6b92faabd9e999c7897278b649f642 Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Sat, 23 Nov 2013 10:13:19 +0000 Subject: [PATCH 8/9] Added app.p4a_dir token --- buildozer/default.spec | 3 +++ buildozer/targets/android.py | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/buildozer/default.spec b/buildozer/default.spec index e7dfe80d2..534741899 100644 --- a/buildozer/default.spec +++ b/buildozer/default.spec @@ -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 diff --git a/buildozer/targets/android.py b/buildozer/targets/android.py index 869b89230..334b4f53d 100644 --- a/buildozer/targets/android.py +++ b/buildozer/targets/android.py @@ -300,9 +300,9 @@ 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): - buildozer_p4a_dir = os.environ.get('BUILDOZER_P4A_DIR') - if buildozer_p4a_dir: - cmd('ln -s {} ./python-for-android'.format(buildozer_p4a_dir), + p4a_dir = self.buildozer.config.getdefault('app', 'p4a_dir') + if p4a_dir: + cmd('ln -s {} ./python-for-android'.format(p4a_dir), cwd = self.buildozer.platform_dir) else: cmd('git clone git://github.com/kivy/python-for-android', From 58d9d284fb9457f3f0cf2036a3b5e82bdf2122a1 Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Sat, 23 Nov 2013 10:35:58 +0000 Subject: [PATCH 9/9] Variable renaming for clarity --- buildozer/targets/android.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/buildozer/targets/android.py b/buildozer/targets/android.py index 334b4f53d..d3ed5721f 100644 --- a/buildozer/targets/android.py +++ b/buildozer/targets/android.py @@ -300,9 +300,9 @@ 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): - p4a_dir = self.buildozer.config.getdefault('app', 'p4a_dir') - if p4a_dir: - cmd('ln -s {} ./python-for-android'.format(p4a_dir), + 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',