Move `run_command` method out of buildEnv class.

This commit is contained in:
Matthieu Gautier 2018-05-28 11:18:13 +02:00
parent 816e06a512
commit 7e0b403ccc
8 changed files with 67 additions and 90 deletions

View File

@ -34,33 +34,6 @@ class PlatformNeutralEnv:
sys.exit("ERROR: meson command not fount") sys.exit("ERROR: meson command not fount")
self.mesontest_command = "{} test".format(self.meson_command) self.mesontest_command = "{} test".format(self.meson_command)
def run_command(self, command, cwd, context, env=None, input=None):
os.makedirs(cwd, exist_ok=True)
if env is None:
env = Defaultdict(str, os.environ)
log = None
try:
if not self.options.verbose:
log = open(context.log_file, 'w')
print("run command '{}'".format(command), file=log)
print("current directory is '{}'".format(cwd), file=log)
print("env is :", file=log)
for k, v in env.items():
print(" {} : {!r}".format(k, v), file=log)
kwargs = dict()
if input:
kwargs['stdin'] = subprocess.PIPE
process = subprocess.Popen(command, shell=True, cwd=cwd, env=env, stdout=log or sys.stdout, stderr=subprocess.STDOUT, **kwargs)
if input:
process.communicate(input.encode())
retcode = process.wait()
if retcode:
raise subprocess.CalledProcessError(retcode, command)
finally:
if log:
log.close()
def detect_platform(self): def detect_platform(self):
_platform = platform.system() _platform = platform.system()
self.distname = _platform self.distname = _platform
@ -278,41 +251,6 @@ class BuildEnv:
env['PATH'] = ':'.join(bin_dirs + [env['PATH']]) env['PATH'] = ':'.join(bin_dirs + [env['PATH']])
return env return env
def run_command(self, command, cwd, context, env=None, input=None, cross_env_only=False):
os.makedirs(cwd, exist_ok=True)
cross_compile_env = True
cross_compile_compiler = True
cross_compile_path = True
if context.force_native_build:
cross_compile_env = False
cross_compile_compiler = False
cross_compile_path = False
if cross_env_only:
cross_compile_compiler = False
env = self._set_env(env, cross_compile_env, cross_compile_compiler, cross_compile_path)
log = None
try:
if not self.options.verbose:
log = open(context.log_file, 'w')
print("run command '{}'".format(command), file=log)
print("current directory is '{}'".format(cwd), file=log)
print("env is :", file=log)
for k, v in env.items():
print(" {} : {!r}".format(k, v), file=log)
kwargs = dict()
if input:
kwargs['stdin'] = subprocess.PIPE
process = subprocess.Popen(command, shell=True, cwd=cwd, env=env, stdout=log or sys.stdout, stderr=subprocess.STDOUT, **kwargs)
if input:
process.communicate(input.encode())
retcode = process.wait()
if retcode:
raise subprocess.CalledProcessError(retcode, command)
finally:
if log:
log.close()
def install_packages(self): def install_packages(self):
autoskip_file = pj(self.build_dir, ".install_packages_ok") autoskip_file = pj(self.build_dir, ".install_packages_ok")
if self.distname in ('fedora', 'redhat', 'centos'): if self.distname in ('fedora', 'redhat', 'centos'):

View File

@ -2,7 +2,7 @@ import subprocess
import os import os
import shutil import shutil
from kiwixbuild.utils import pj, Context, SkipCommand, extract_archive, Defaultdict, StopBuild 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.versions import main_project_versions, base_deps_versions
from kiwixbuild._global import neutralEnv from kiwixbuild._global import neutralEnv
@ -162,13 +162,13 @@ class GitClone(Source):
raise SkipCommand() raise SkipCommand()
command = "git clone --depth=1 --branch {} {} {}".format( command = "git clone --depth=1 --branch {} {} {}".format(
self.git_ref, self.git_remote, self.source_dir) self.git_ref, self.git_remote, self.source_dir)
neutralEnv('run_command')(command, neutralEnv('source_dir'), context) run_command(command, neutralEnv('source_dir'), context)
def _git_update(self, context): def _git_update(self, context):
command = "git fetch origin {}".format( command = "git fetch origin {}".format(
self.git_ref) self.git_ref)
neutralEnv('run_command')(command, self.git_path, context) run_command(command, self.git_path, context)
neutralEnv('run_command')("git checkout "+self.git_ref, self.git_path, context) run_command("git checkout "+self.git_ref, self.git_path, context)
def prepare(self): def prepare(self):
self.command('gitclone', self._git_clone) self.command('gitclone', self._git_clone)
@ -190,11 +190,11 @@ class SvnClone(Source):
if os.path.exists(self.svn_path): if os.path.exists(self.svn_path):
raise SkipCommand() raise SkipCommand()
command = "svn checkout {} {}".format(self.svn_remote, self.svn_dir) command = "svn checkout {} {}".format(self.svn_remote, self.svn_dir)
neutralEnv('run_command')(command, neutralEnv('source_dir'), context) run_command(command, neutralEnv('source_dir'), context)
def _svn_update(self, context): def _svn_update(self, context):
context.try_skip(self.svn_path) context.try_skip(self.svn_path)
neutralEnv('run_command')("svn update", self.svn_path, context) run_command("svn update", self.svn_path, context)
def prepare(self): def prepare(self):
self.command('svncheckout', self._svn_checkout) self.command('svncheckout', self._svn_checkout)
@ -294,7 +294,7 @@ class MakeBuilder(Builder):
v = v.format(buildEnv=self.buildEnv, env=env) v = v.format(buildEnv=self.buildEnv, env=env)
self.configure_env[k[8:]] = v self.configure_env[k[8:]] = v
env.update(self.configure_env) env.update(self.configure_env)
self.buildEnv.run_command(command, self.build_path, context, env=env) run_command(command, self.build_path, context, buildEnv=self.buildEnv, env=env)
def _compile(self, context): def _compile(self, context):
context.try_skip(self.build_path) context.try_skip(self.build_path)
@ -302,7 +302,7 @@ class MakeBuilder(Builder):
make_target=self.make_target, make_target=self.make_target,
make_option=self.make_option make_option=self.make_option
) )
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)
def _install(self, context): def _install(self, context):
context.try_skip(self.build_path) context.try_skip(self.build_path)
@ -310,12 +310,12 @@ class MakeBuilder(Builder):
make_install_target=self.make_install_target, make_install_target=self.make_install_target,
make_option=self.make_option make_option=self.make_option
) )
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)
def _make_dist(self, context): def _make_dist(self, context):
context.try_skip(self.build_path) context.try_skip(self.build_path)
command = "make dist" command = "make dist"
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)
class CMakeBuilder(MakeBuilder): class CMakeBuilder(MakeBuilder):
@ -347,7 +347,7 @@ class CMakeBuilder(MakeBuilder):
v = v.format(buildEnv=self.buildEnv, env=env) v = v.format(buildEnv=self.buildEnv, env=env)
self.configure_env[k[8:]] = v self.configure_env[k[8:]] = v
env.update(self.configure_env) env.update(self.configure_env)
self.buildEnv.run_command(command, self.build_path, context, env=env, cross_env_only=True) run_command(command, self.build_path, context, env=env, buildEnv=self.buildEnv, cross_env_only=True)
class MesonBuilder(Builder): class MesonBuilder(Builder):
@ -382,11 +382,11 @@ class MesonBuilder(Builder):
buildEnv=self.buildEnv, buildEnv=self.buildEnv,
cross_option=cross_option cross_option=cross_option
) )
self.buildEnv.run_command(command, self.source_path, context, cross_env_only=True) run_command(command, self.source_path, context, buildEnv=self.buildEnv, cross_env_only=True)
def _compile(self, context): def _compile(self, context):
command = "{} -v".format(neutralEnv('ninja_command')) command = "{} -v".format(neutralEnv('ninja_command'))
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)
def _test(self, context): def _test(self, context):
if ( self.buildEnv.platform_info.build == 'android' if ( self.buildEnv.platform_info.build == 'android'
@ -395,15 +395,15 @@ class MesonBuilder(Builder):
): ):
raise SkipCommand() raise SkipCommand()
command = "{} --verbose {}".format(neutralEnv('mesontest_command'), self.test_option) command = "{} --verbose {}".format(neutralEnv('mesontest_command'), self.test_option)
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)
def _install(self, context): def _install(self, context):
command = "{} -v install".format(neutralEnv('ninja_command')) command = "{} -v install".format(neutralEnv('ninja_command'))
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)
def _make_dist(self, context): def _make_dist(self, context):
command = "{} -v dist".format(neutralEnv('ninja_command')) command = "{} -v dist".format(neutralEnv('ninja_command'))
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)
class GradleBuilder(Builder): class GradleBuilder(Builder):
@ -428,4 +428,4 @@ class GradleBuilder(Builder):
command = command.format( command = command.format(
gradle_target=self.gradle_target, gradle_target=self.gradle_target,
gradle_option=self.gradle_option) gradle_option=self.gradle_option)
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)

View File

@ -3,7 +3,7 @@ from .base import (
ReleaseDownload, ReleaseDownload,
CMakeBuilder) CMakeBuilder)
from kiwixbuild.utils import Remotefile, pj from kiwixbuild.utils import Remotefile, pj, run_command
class CTPP2(Dependency): class CTPP2(Dependency):
name = "ctpp2" name = "ctpp2"
@ -51,4 +51,4 @@ class CTPP2C(CTPP2):
ctpp2c=pj(self.build_path, 'ctpp2c'), ctpp2c=pj(self.build_path, 'ctpp2c'),
install_dir=pj(self.buildEnv.install_dir, 'bin') install_dir=pj(self.buildEnv.install_dir, 'bin')
) )
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)

View File

@ -4,7 +4,7 @@ from .base import (
MakeBuilder MakeBuilder
) )
from kiwixbuild.utils import Remotefile from kiwixbuild.utils import Remotefile, run_command
class Gumbo(Dependency): class Gumbo(Dependency):
@ -18,6 +18,6 @@ class Gumbo(Dependency):
def _post_prepare_script(self, context): def _post_prepare_script(self, context):
context.try_skip(self.extract_path) context.try_skip(self.extract_path)
command = "./autogen.sh" command = "./autogen.sh"
self.buildEnv.run_command(command, self.extract_path, context) run_command(command, self.extract_path, context)
Builder = MakeBuilder Builder = MakeBuilder

View File

@ -4,8 +4,7 @@ from .base import (
MakeBuilder MakeBuilder
) )
from kiwixbuild.utils import Remotefile from kiwixbuild.utils import Remotefile, run_command
from kiwixbuild._global import neutralEnv
class Aria2(Dependency): class Aria2(Dependency):
name = "libaria2" name = "libaria2"
@ -21,7 +20,7 @@ class Aria2(Dependency):
def _post_prepare_script(self, context): def _post_prepare_script(self, context):
context.try_skip(self.extract_path) context.try_skip(self.extract_path)
command = "autoreconf -i" command = "autoreconf -i"
neutralEnv('run_command')(command, self.extract_path, context) run_command(command, self.extract_path, context)
class Builder(MakeBuilder): class Builder(MakeBuilder):
configure_option = "--enable-libaria2 --disable-ssl --disable-bittorent --disable-metalink --without-sqlite3 --without-libxml2 --without-libexpat" configure_option = "--enable-libaria2 --disable-ssl --disable-bittorent --disable-metalink --without-sqlite3 --without-libxml2 --without-libexpat"

View File

@ -2,7 +2,7 @@ import os
from .base_toolchain import Toolchain from .base_toolchain import Toolchain
from kiwixbuild.dependencies import ReleaseDownload, Builder from kiwixbuild.dependencies import ReleaseDownload, Builder
from kiwixbuild.utils import Remotefile, add_execution_right from kiwixbuild.utils import Remotefile, add_execution_right, run_command
pj = os.path.join pj = os.path.join
@ -91,7 +91,7 @@ class android_ndk(Toolchain):
api=self.target.api, api=self.target.api,
install_dir=self.install_path install_dir=self.install_path
) )
self.buildEnv.run_command(command, self.build_path, context) run_command(command, self.build_path, context, buildEnv=self.buildEnv)
def _fix_permission_right(self, context): def _fix_permission_right(self, context):
context.try_skip(self.build_path) context.try_skip(self.build_path)

View File

@ -3,7 +3,7 @@ import shutil
from .base_toolchain import Toolchain from .base_toolchain import Toolchain
from kiwixbuild.dependencies import ReleaseDownload, Builder from kiwixbuild.dependencies import ReleaseDownload, Builder
from kiwixbuild.utils import Remotefile from kiwixbuild.utils import Remotefile, run_command
pj = os.path.join pj = os.path.join
@ -38,7 +38,7 @@ class android_sdk(Toolchain):
# - 8 : Android SDK Build-tools, revision 24.0.1 # - 8 : Android SDK Build-tools, revision 24.0.1
# - 34 : SDK Platform Android 7.0, API 24, revision 2 # - 34 : SDK Platform Android 7.0, API 24, revision 2
# - 162 : Android Support Repository, revision 44 # - 162 : Android Support Repository, revision 44
self.buildEnv.run_command(command, self.install_path, context, input="y\n") run_command(command, self.install_path, context, input="y\n")
def _fix_licenses(self, context): def _fix_licenses(self, context):
context.try_skip(self.install_path) context.try_skip(self.install_path)

View File

@ -10,6 +10,8 @@ import ssl
import subprocess import subprocess
from collections import namedtuple, defaultdict from collections import namedtuple, defaultdict
from kiwixbuild._global import neutralEnv
pj = os.path.join pj = os.path.join
g_print_progress = True g_print_progress = True
@ -221,3 +223,41 @@ def extract_archive(archive_path, dest_dir, topdir=None, name=None):
if archive is not None: if archive is not None:
archive.close() archive.close()
def run_command(command, cwd, context, buildEnv=None, env=None, input=None, cross_env_only=False):
os.makedirs(cwd, exist_ok=True)
if env is None:
env = Defaultdict(str, os.environ)
if buildEnv is not None:
cross_compile_env = True
cross_compile_compiler = True
cross_compile_path = True
if context.force_native_build:
cross_compile_env = False
cross_compile_compiler = False
cross_compile_path = False
if cross_env_only:
cross_compile_compiler = False
env = buildEnv._set_env(env, cross_compile_env, cross_compile_compiler, cross_compile_path)
log = None
try:
if not neutralEnv('verbose'):
log = open(context.log_file, 'w')
print("run command '{}'".format(command), file=log)
print("current directory is '{}'".format(cwd), file=log)
print("env is :", file=log)
for k, v in env.items():
print(" {} : {!r}".format(k, v), file=log)
kwargs = dict()
if input:
kwargs['stdin'] = subprocess.PIPE
process = subprocess.Popen(command, shell=True, cwd=cwd, env=env, stdout=log or sys.stdout, stderr=subprocess.STDOUT, **kwargs)
if input:
process.communicate(input.encode())
retcode = process.wait()
if retcode:
raise subprocess.CalledProcessError(retcode, command)
finally:
if log:
log.close()