-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path__main__.py
More file actions
132 lines (111 loc) · 3.82 KB
/
__main__.py
File metadata and controls
132 lines (111 loc) · 3.82 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT
"""Build packages from the recipe in [DIR]."""
import argparse
import os
import sys
from importlib.util import find_spec, spec_from_file_location, module_from_spec
from typing import Dict, List, Optional
from toltec import parse_recipe
from toltec.builder import Builder
from toltec.recipe import Package
from toltec.repo import make_index
from toltec import util
def main() -> int:
"""Execute requested commands and return appropriate exit code."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"recipe_dir",
metavar="DIR",
nargs="?",
default=os.getcwd(),
help="""path to a directory containing the recipe to build
(default: current directory)""",
)
parser.add_argument(
"-w",
"--work-dir",
metavar="DIR",
default=os.path.join(os.getcwd(), "build"),
help="""path to a directory used for building the package
(default: [current directory]/build)""",
)
parser.add_argument(
"-d",
"--dist-dir",
metavar="DIR",
default=os.path.join(os.getcwd(), "dist"),
help="""path to a directory where built packages are stored
(default: [current directory]/dist)""",
)
parser.add_argument(
"-s",
"--source-dir",
metavar="DIR",
default=None,
help="""path to the source directory
(optional: when specified, a local build is performed instead of fetching
sources)""",
)
parser.add_argument(
"-a",
"--arch-name",
metavar="ARCHNAME",
action="append",
help="""only build for the given architecture (can
be repeated)""",
)
parser.add_argument(
"-p",
"--package-name",
metavar="PACKAGENAME",
action="append",
help="""list of packages to build (default: all packages from the
recipe, can be repeated)""",
)
parser.add_argument(
"-H",
"--hook",
metavar="PATH",
action="append",
help="""name or path to a Python module that registers listeners for
build hooks (can be repeated) - if a path is passed, it must start with
either a dot or a slash""",
)
util.argparse_add_verbose(parser)
util.argparse_add_warning(parser)
args = parser.parse_args()
util.setup_logging(args)
recipe_bundle = parse_recipe(args.recipe_dir)
with Builder(args.work_dir, args.dist_dir, args.source_dir) as builder:
if args.hook:
for ident in args.hook:
if ident and ident[0] in (".", "/"):
spec = spec_from_file_location("toltec.hooks.user", ident)
else:
spec = find_spec(ident)
if spec:
module = module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore
module.register(builder) # type: ignore
else:
raise RuntimeError(
f"Hook module '{ident}' couldn’t be loaded"
)
build_matrix: Optional[Dict[str, Optional[List[Package]]]] = None
if args.arch_name or args.package_name:
build_matrix = {}
for arch, recipes in recipe_bundle.items():
if args.package_name:
build_matrix[arch] = [
recipes.packages[pkg_name]
for pkg_name in args.package_name
]
else:
build_matrix[arch] = None
if not builder.make(recipe_bundle, build_matrix):
return 1
make_index(args.dist_dir)
return 0
if __name__ == "__main__":
sys.exit(main())