/
options_parsing.py
61 lines (48 loc) · 2.17 KB
/
options_parsing.py
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
# coding=utf-8
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)
from pants.build_graph.build_configuration import BuildConfiguration
from pants.engine.fs import FileContent, PathGlobs
from pants.engine.rules import RootRule, rule
from pants.engine.selectors import Get, Select
from pants.init.options_initializer import BuildConfigInitializer, OptionsInitializer
from pants.option.options_bootstrapper import OptionsBootstrapper
from pants.util.objects import datatype
class OptionsParseRequest(datatype(['args', 'env'])):
"""Represents a request for Options computation."""
@classmethod
def create(cls, args, env):
assert isinstance(args, (list, tuple))
return cls(
tuple(args),
tuple(env.items() if isinstance(env, dict) else env)
)
class Options(datatype(['options', 'build_config'])):
"""Represents the result of an Options computation."""
@rule(OptionsBootstrapper, [Select(OptionsParseRequest)])
def reify_options_bootstrapper(parse_request):
options_bootstrapper = OptionsBootstrapper(
env=dict(parse_request.env),
args=parse_request.args
)
# TODO: Once we have the ability to get FileContent for arbitrary
# paths outside of the buildroot, we can invert this to use
# OptionsBootstrapper.produce_and_set_bootstrap_options() which
# will yield lists of file paths for use as subject values and permit
# us to avoid the direct file I/O that this rule currently requires.
options_bootstrapper.construct_and_set_bootstrap_options()
yield options_bootstrapper
# TODO: Accommodate file_option, dir_option, etc.
@rule(Options, [Select(OptionsBootstrapper), Select(BuildConfiguration)])
def parse_options(options_bootstrapper, build_config):
options = OptionsInitializer.create(options_bootstrapper, build_config)
options.freeze()
return Options(options, build_config)
def create_options_parsing_rules():
return [
reify_options_bootstrapper,
parse_options,
RootRule(OptionsParseRequest),
]