From 95c97750137e4a9af144ac8531aed392501d7095 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 29 Mar 2020 00:02:48 +0000 Subject: project.cmake: factor out common utils --- project/ci/appveyor/cmake.py | 17 +++-------------- project/ci/travis/cmake.py | 17 +++-------------- project/cmake/build.py | 38 +++++++------------------------------- project/utils.py | 4 ++++ 4 files changed, 17 insertions(+), 59 deletions(-) (limited to 'project') diff --git a/project/ci/appveyor/cmake.py b/project/ci/appveyor/cmake.py index e1ebce0..66e89f0 100644 --- a/project/ci/appveyor/cmake.py +++ b/project/ci/appveyor/cmake.py @@ -1,11 +1,9 @@ -#!/usr/bin/env python3 - # Copyright (c) 2019 Egor Tensin # This file is part of the "cmake-common" project. # For details, see https://github.com/egor-tensin/cmake-common. # Distributed under the MIT License. -'''Build a CMake project on AppVeyor. +R'''Build a CMake project on AppVeyor. This is similar to build.py, but auto-fills some parameters for build.py from the AppVeyor-defined environment variables. @@ -20,6 +18,7 @@ import os import sys from project.cmake.build import build +import project.utils class Image(Enum): @@ -108,12 +107,6 @@ def _get_configuration(): return _env('CONFIGURATION') -def _setup_logging(): - logging.basicConfig( - format='%(asctime)s | %(levelname)s | %(message)s', - level=logging.INFO) - - def _parse_args(argv=None): if argv is None: argv = sys.argv[1:] @@ -152,12 +145,8 @@ def build_appveyor(argv=None): def main(argv=None): - _setup_logging() - try: + with project.utils.setup_logging(): build_appveyor(argv) - except Exception as e: - logging.exception(e) - raise if __name__ == '__main__': diff --git a/project/ci/travis/cmake.py b/project/ci/travis/cmake.py index 7a1f707..cb96941 100644 --- a/project/ci/travis/cmake.py +++ b/project/ci/travis/cmake.py @@ -1,11 +1,9 @@ -#!/usr/bin/env python3 - # Copyright (c) 2019 Egor Tensin # This file is part of the "cmake-common" project. # For details, see https://github.com/egor-tensin/cmake-common. # Distributed under the MIT License. -'''Build a CMake project on Travis. +R'''Build a CMake project on Travis. This is similar to build.py, but auto-fills some parameters for build.py from the Travis-defined environment variables. @@ -20,6 +18,7 @@ import os.path import sys from project.cmake.build import build +import project.utils def _env(name): @@ -45,12 +44,6 @@ def _get_configuration(): return _env('configuration') -def _setup_logging(): - logging.basicConfig( - format='%(asctime)s | %(levelname)s | %(message)s', - level=logging.INFO) - - def _parse_args(argv=None): if argv is None: argv = sys.argv[1:] @@ -87,12 +80,8 @@ def build_travis(argv=None): def main(argv=None): - _setup_logging() - try: + with project.utils.setup_logging(): build_travis(argv) - except Exception as e: - logging.exception(e) - raise if __name__ == '__main__': diff --git a/project/cmake/build.py b/project/cmake/build.py index 89ac6da..2400db2 100644 --- a/project/cmake/build.py +++ b/project/cmake/build.py @@ -36,23 +36,13 @@ cmake/toolchains in this repository for examples). import argparse from contextlib import contextmanager import logging -from enum import Enum import os import os.path -import subprocess import sys import tempfile from project.configuration import Configuration - - -def _run_executable(cmd_line): - logging.info('Running executable: %s', cmd_line) - return subprocess.run(cmd_line, check=True) - - -def _run_cmake(cmake_args): - _run_executable(['cmake'] + cmake_args) +import project.utils @contextmanager @@ -92,7 +82,7 @@ class GenerationPhase: return result def run(self): - _run_cmake(self._cmake_args()) + project.utils.run_cmake(self._cmake_args()) class BuildPhase: @@ -113,11 +103,7 @@ class BuildPhase: return result def run(self): - _run_cmake(self._cmake_args()) - - -def _parse_dir(s): - return os.path.abspath(os.path.normpath(s)) + project.utils.run_cmake(self._cmake_args()) def _parse_args(argv=None): @@ -130,16 +116,16 @@ def _parse_args(argv=None): formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('--build', metavar='DIR', dest='build_dir', - type=_parse_dir, + type=project.utils.normalize_path, help='build directory (temporary directory unless specified)') parser.add_argument('--install', metavar='DIR', dest='install_dir', - type=_parse_dir, + type=project.utils.normalize_path, help='install directory') parser.add_argument('--configuration', metavar='CONFIG', type=Configuration.parse, default=Configuration.DEBUG, help=f'build configuration ({"/".join(map(str, Configuration))})') parser.add_argument('src_dir', metavar='DIR', - type=_parse_dir, + type=project.utils.normalize_path, help='source directory') parser.add_argument('cmake_args', nargs='*', metavar='CMAKE_ARG', help='additional CMake arguments, to be passed verbatim') @@ -147,12 +133,6 @@ def _parse_args(argv=None): return args -def _setup_logging(): - logging.basicConfig( - format='%(asctime)s | %(levelname)s | %(message)s', - level=logging.INFO) - - def build(argv=None): args = _parse_args(argv) with _create_build_dir(args) as build_dir: @@ -163,12 +143,8 @@ def build(argv=None): def main(argv=None): - _setup_logging() - try: + with project.utils.setup_logging(): build(argv) - except Exception as e: - logging.exception(e) - raise if __name__ == '__main__': diff --git a/project/utils.py b/project/utils.py index a766f90..9d986b0 100644 --- a/project/utils.py +++ b/project/utils.py @@ -42,6 +42,10 @@ def run(cmd_line): return subprocess.run(cmd_line, check=True) +def run_cmake(cmake_args): + return run(['cmake'] + cmake_args) + + def on_windows(): return platform.system() == 'Windows' -- cgit v1.2.3