diff --git a/kiwixbuild/__init__.py b/kiwixbuild/__init__.py index 73bb7c2..ff70f55 100644 --- a/kiwixbuild/__init__.py +++ b/kiwixbuild/__init__.py @@ -6,7 +6,6 @@ import argparse from .dependencies import Dependency from .platforms import PlatformInfo from .builder import Builder -from .utils import setup_print_progress from . import _global def parse_args(): @@ -65,9 +64,9 @@ def parse_args(): def main(): options = parse_args() options.working_dir = os.path.abspath(options.working_dir) - setup_print_progress(options.show_progress) - neutralEnv = buildenv.PlatformNeutralEnv(options) + _global.set_options(options) + neutralEnv = buildenv.PlatformNeutralEnv() _global.set_neutralEnv(neutralEnv) - builder = Builder(options) + builder = Builder() builder.run() diff --git a/kiwixbuild/_global.py b/kiwixbuild/_global.py index 6e4dcad..42e5285 100644 --- a/kiwixbuild/_global.py +++ b/kiwixbuild/_global.py @@ -1,6 +1,7 @@ from collections import OrderedDict as _OrderedDict _neutralEnv = None +_options = None _target_steps = _OrderedDict() _plt_steps = _OrderedDict() @@ -11,6 +12,13 @@ def set_neutralEnv(env): def neutralEnv(what): return getattr(_neutralEnv, what) +def set_options(options): + global _options + _options = options + +def option(what): + return getattr(_options, what) + def add_target_step(key, what): _target_steps[key] = what diff --git a/kiwixbuild/buildenv.py b/kiwixbuild/buildenv.py index 672c906..816ca04 100644 --- a/kiwixbuild/buildenv.py +++ b/kiwixbuild/buildenv.py @@ -5,15 +5,15 @@ import platform from .toolchains import Toolchain from .utils import pj, download_remote, Defaultdict -from ._global import neutralEnv +from ._global import neutralEnv, option class PlatformNeutralEnv: - def __init__(self, options): - self.options = options - self.source_dir = pj(options.working_dir, "SOURCE") - self.archive_dir = pj(options.working_dir, "ARCHIVE") - self.toolchain_dir = pj(options.working_dir, "TOOLCHAINS") + def __init__(self): + self.working_dir = option('working_dir') + self.source_dir = pj(self.working_dir, "SOURCE") + self.archive_dir = pj(self.working_dir, "ARCHIVE") + self.toolchain_dir = pj(self.working_dir, "TOOLCHAINS") self.log_dir = pj(self.working_dir, 'LOGS') for d in (self.source_dir, self.archive_dir, @@ -47,7 +47,7 @@ class PlatformNeutralEnv: def download(self, what, where=None): where = where or self.archive_dir - download_remote(what, where, not self.options.no_cert_check) + download_remote(what, where) def _detect_ninja(self): for n in ['ninja', 'ninja-build']: @@ -71,15 +71,12 @@ class PlatformNeutralEnv: if retcode == 0: return n - def __getattr__(self, name): - return getattr(self.options, name) - class BuildEnv: def __init__(self, platformInfo): build_dir = "BUILD_{}".format(platformInfo.name) self.platformInfo = platformInfo - self.build_dir = pj(neutralEnv('working_dir'), build_dir) + self.build_dir = pj(option('working_dir'), build_dir) self.install_dir = pj(self.build_dir, "INSTALL") self.log_dir = pj(self.build_dir, 'LOGS') for d in (self.build_dir, @@ -87,7 +84,7 @@ class BuildEnv: self.log_dir): os.makedirs(d, exist_ok=True) - self.libprefix = neutralEnv('libprefix') or self._detect_libdir() + self.libprefix = option('libprefix') or self._detect_libdir() def clean_intermediate_directories(self): for subdir in os.listdir(self.build_dir): @@ -99,9 +96,6 @@ class BuildEnv: else: os.remove(subpath) - def __getattr__(self, name): - return _global.neutralEnv(name) - def _is_debianlike(self): return os.path.isfile('/etc/debian_version') diff --git a/kiwixbuild/builder.py b/kiwixbuild/builder.py index d56724c..587811a 100644 --- a/kiwixbuild/builder.py +++ b/kiwixbuild/builder.py @@ -7,33 +7,32 @@ from .platforms import PlatformInfo from .utils import remove_duplicates, StopBuild from .dependencies import Dependency from ._global import ( - neutralEnv, + neutralEnv, option, add_target_step, get_target_step, target_steps, add_plt_step, get_plt_step, plt_steps) from . import _global class Builder: - def __init__(self, options): - self.options = options - platformClass = PlatformInfo.all_platforms[options.target_platform] + def __init__(self): + platformClass = PlatformInfo.all_platforms[option('target_platform')] if neutralEnv('distname') not in platformClass.compatible_hosts: print(('ERROR: The target platform {} cannot be build on host {}.\n' 'Select another target platform, or change your host system.' - ).format(options.target_platform, self.distname)) + ).format(option('target_platform'), self.distname)) sys.exit(-1) self.platform = platform = platformClass() _targets = {} - targetDef = (options.target_platform, options.targets) + targetDef = (option('target_platform'), option('targets')) self.add_targets(targetDef, _targets) dependencies = self.order_dependencies(_targets, targetDef) dependencies = list(remove_duplicates(dependencies)) - if options.build_nodeps: + if option('build_nodeps'): add_target_step(targetDef, _targets[targetDef]) else: for dep in dependencies: - if self.options.build_deps_only and dep == targetDef: + if option('build_deps_only') and dep == targetDef: continue add_target_step(dep, _targets[dep]) @@ -79,7 +78,7 @@ class Builder: source.prepare() def prepare_sources(self): - if self.options.skip_source_prepare: + if option('skip_source_prepare'): print("SKIP") return @@ -113,7 +112,7 @@ class Builder: source = get_target_step(builderDef[1], 'source') env = PlatformInfo.all_running_platforms[builderDef[0]].buildEnv builder = get_target_step(builderDef)(depClass, source, env) - if self.options.make_dist and builderDef[1] == self.options.targets: + if option('make_dist') and builderDef[1] == option('targets'): print("make dist {}:".format(builder.name)) builder.make_dist() continue @@ -136,7 +135,7 @@ class Builder: self.build() # No error, clean intermediate file at end of build if needed. print("[CLEAN]") - if self.options.clean_at_end: + if option('clean_at_end'): self.platform.clean_intermediate_directories() else: print("SKIP") diff --git a/kiwixbuild/dependencies/base.py b/kiwixbuild/dependencies/base.py index 9d93795..60b16c8 100644 --- a/kiwixbuild/dependencies/base.py +++ b/kiwixbuild/dependencies/base.py @@ -4,7 +4,7 @@ import shutil from kiwixbuild.utils import pj, Context, SkipCommand, extract_archive, Defaultdict, StopBuild, run_command from kiwixbuild.versions import main_project_versions, base_deps_versions -from kiwixbuild._global import neutralEnv +from kiwixbuild._global import neutralEnv, option SCRIPT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) @@ -130,7 +130,7 @@ class GitClone(Source): @property def source_dir(self): - if neutralEnv('make_release'): + if option('make_release'): return "{}_release".format(self.git_dir) else: return self.git_dir @@ -141,7 +141,7 @@ class GitClone(Source): @property def git_ref(self): - if neutralEnv('make_release'): + if option('make_release'): return self.release_git_ref else: return self.base_git_ref diff --git a/kiwixbuild/dependencies/kiwix_android.py b/kiwixbuild/dependencies/kiwix_android.py index 6e00b33..fa19b09 100644 --- a/kiwixbuild/dependencies/kiwix_android.py +++ b/kiwixbuild/dependencies/kiwix_android.py @@ -6,6 +6,7 @@ from .base import ( GradleBuilder) from kiwixbuild.utils import pj +from kiwixbuild._global import option class KiwixAndroid(Dependency): name = "kiwix-android" @@ -18,7 +19,7 @@ class KiwixAndroid(Dependency): dependencies = ["Gradle", "kiwix-lib"] def build(self): - if self.buildEnv.options.targets == 'kiwix-android-custom': + if option('targets') == 'kiwix-android-custom': print("SKIP") else: super().build() diff --git a/kiwixbuild/dependencies/kiwix_custom_app.py b/kiwixbuild/dependencies/kiwix_custom_app.py index 43b90a9..9422301 100644 --- a/kiwixbuild/dependencies/kiwix_custom_app.py +++ b/kiwixbuild/dependencies/kiwix_custom_app.py @@ -7,14 +7,14 @@ from .base import ( GradleBuilder) from kiwixbuild.utils import Remotefile, pj, SkipCommand -from kiwixbuild._global import get_target_step +from kiwixbuild._global import option, get_target_step class KiwixCustomApp(Dependency): name = "kiwix-android-custom" def __init__(self, buildEnv): super().__init__(buildEnv) - self.custom_name = buildEnv.options.android_custom_app + self.custom_name = option('android_custom_app') class Source(GitClone): git_remote = "https://github.com/kiwix/kiwix-android-custom" @@ -51,7 +51,7 @@ class KiwixCustomApp(Dependency): def _get_zim_size(self): try: - zim_size = self.buildEnv.options.zim_file_size + zim_size = option('zim_file_size') except AttributeError: with open(pj(self.source_path, self.target.custom_name, 'info.json')) as f: app_info = json.load(f) @@ -64,7 +64,7 @@ class KiwixCustomApp(Dependency): self.command('compile', self._compile) def _download_zim(self, context): - zim_url = self.buildEnv.options.zim_file_url + zim_url = option('zim_file_url') if zim_url is None: raise SkipCommand() with open(pj(self.source_path, self.target.custom_name, 'info.json')) as f: diff --git a/kiwixbuild/platforms/base.py b/kiwixbuild/platforms/base.py index 2d1babe..dc21532 100644 --- a/kiwixbuild/platforms/base.py +++ b/kiwixbuild/platforms/base.py @@ -2,11 +2,12 @@ import os import subprocess +from kiwixbuild.dependencies import Dependency from kiwixbuild.toolchains import Toolchain from kiwixbuild.packages import PACKAGE_NAME_MAPPERS from kiwixbuild.utils import pj, remove_duplicates from kiwixbuild.buildenv import BuildEnv -from kiwixbuild._global import neutralEnv, add_plt_step, target_steps +from kiwixbuild._global import neutralEnv, option, add_plt_step, target_steps _SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) TEMPLATES_DIR = pj(os.path.dirname(_SCRIPT_DIR), 'templates') @@ -117,7 +118,7 @@ class PlatformInfo(metaclass=_MetaPlatform): packages_to_have = self.get_packages() packages_to_have = remove_duplicates(packages_to_have) - if not neutralEnv('force_install_packages') and os.path.exists(autoskip_file): + if not option('force_install_packages') and os.path.exists(autoskip_file): print("SKIP") return diff --git a/kiwixbuild/utils.py b/kiwixbuild/utils.py index 7eb0d29..b76dc5a 100644 --- a/kiwixbuild/utils.py +++ b/kiwixbuild/utils.py @@ -10,11 +10,10 @@ import ssl import subprocess from collections import namedtuple, defaultdict -from kiwixbuild._global import neutralEnv +from kiwixbuild._global import neutralEnv, option pj = os.path.join -g_print_progress = True REMOTE_PREFIX = 'http://download.kiwix.org/dev/' @@ -30,11 +29,6 @@ def xrun_find(name): return output[:-1].decode() -def setup_print_progress(print_progress): - global g_print_progress - g_print_progress = print_progress - - class Defaultdict(defaultdict): def __getattr__(self, name): return self[name] @@ -69,7 +63,7 @@ def get_sha256(path): def print_progress(progress): - if g_print_progress: + if option('show_progress'): text = "{}\033[{}D".format(progress, len(progress)) print(text, end="") @@ -92,7 +86,7 @@ def copy_tree(src, dst, post_copy_function=None): post_copy_function(dstfile) -def download_remote(what, where, check_certificate=True): +def download_remote(what, where): file_path = pj(where, what.name) file_url = what.url or (REMOTE_PREFIX + what.name) if os.path.exists(file_path): @@ -100,7 +94,7 @@ def download_remote(what, where, check_certificate=True): raise SkipCommand() os.remove(file_path) - if not check_certificate: + if option('no_cert_check'): context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE @@ -241,7 +235,7 @@ def run_command(command, cwd, context, buildEnv=None, env=None, input=None, cros env = buildEnv._set_env(env, cross_compile_env, cross_compile_compiler, cross_compile_path) log = None try: - if not neutralEnv('verbose'): + if not option('verbose'): log = open(context.log_file, 'w') print("run command '{}'".format(command), file=log) print("current directory is '{}'".format(cwd), file=log)