summaryrefslogtreecommitdiff
path: root/packages/vpp/build.py
blob: d1dd54b1a608e00aeb0be810ab08fc81d23478aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
#
# Copyright (C) 2023 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from argparse import ArgumentParser
from pathlib import Path
from subprocess import run
from sys import exit
from json import load as json_load


def check_args(args) -> bool:
    """Check command arguments

    Args:
        args (Namespace): Namespace with arguments

    Returns:
        bool: check result
    """
    sources_dir = Path(f'../{args.package}')
    if not sources_dir.exists():
        print(f'Sourced directory {sources_dir.as_posix()} does not exist')
        return False
    return True


# apply patches
def apply_patches(package_name: str) -> bool:
    """Apply patches to sources directory

    Args:
        package_name (str): package name (the same as sources directory)
    """
    patches_dir = Path(f'../patches/{package_name}')
    if patches_dir.exists():
        for patch_file in patches_dir.iterdir():
            patch_cmd: list[str] = [
                'git', '-c', 'user.email=support@vyos.io', '-c',
                'user.name=vyos', 'am',
                patch_file.as_posix()
            ]
            print(f'Applying patch: {patch_file.name}')
            if run(patch_cmd).returncode != 0:
                return False
    return True


def build_package(package_name: str) -> bool:
    """Build a package using commands from external file

    Args:
        package_name (str): package name

    Returns:
        bool: build status
    """
    build_config_path: str = f'../build_{package_name}.json'
    with open(build_config_path, 'r') as openfile:
        try:
            build_params = json_load(openfile)
        except Exception as err:
            print(f'Error parsing config file {build_config_path}: {err}')
            return False

    for cmd in build_params.get('build_commands', []):
        print(f'Building: {cmd}')
        if run(cmd).returncode != 0:
            return False

    return True


# build a package
if __name__ == '__main__':
    # prepare argument parser
    arg_parser = ArgumentParser()
    arg_parser.add_argument('--package',
                            required=True,
                            help='Package name to build')
    args = arg_parser.parse_args()

    if not check_args(args):
        exit(1)

    if not apply_patches(args.package):
        exit(1)

    if not build_package(args.package):
        exit(1)

    exit()