|
1 | 1 | # SPDX-License-Identifier: BSD-2-Clause |
2 | | - |
3 | | -import os |
4 | | -import sys |
5 | | -import inspect |
6 | | -import importlib |
7 | 2 | import argparse |
8 | | -import tomli |
9 | | -import jsonschema |
10 | | - |
11 | | -from . import ChipFlowError |
12 | | - |
13 | | - |
14 | | -def _get_cls_by_reference(reference, context): |
15 | | - module_ref, _, class_ref = reference.partition(":") |
16 | | - try: |
17 | | - module_obj = importlib.import_module(module_ref) |
18 | | - except ModuleNotFoundError as e: |
19 | | - raise ChipFlowError(f"Module `{module_ref}` referenced by {context} is not found") |
20 | | - try: |
21 | | - return getattr(module_obj, class_ref) |
22 | | - except AttributeError as e: |
23 | | - raise ChipFlowError(f"Module `{module_ref}` referenced by {context} does not define " |
24 | | - f"`{class_ref}`") from None |
25 | | - |
26 | | - |
27 | | -def _ensure_chipflow_root(): |
28 | | - if "CHIPFLOW_ROOT" not in os.environ: |
29 | | - os.environ["CHIPFLOW_ROOT"] = os.getcwd() |
30 | | - if os.environ["CHIPFLOW_ROOT"] not in sys.path: |
31 | | - sys.path.append(os.environ["CHIPFLOW_ROOT"]) |
32 | | - return os.environ["CHIPFLOW_ROOT"] |
| 3 | +import inspect |
| 4 | +import sys |
| 5 | +import traceback |
| 6 | +import logging |
33 | 7 |
|
| 8 | +from pprint import pformat |
34 | 9 |
|
35 | | -config_schema = { |
36 | | - "$schema": "https://json-schema.org/draft/2020-12/schema", |
37 | | - "$id": "https://chipflow.io/meta/chipflow.toml.schema.json", |
38 | | - "title": "chipflow.toml", |
39 | | - "type": "object", |
40 | | - "required": [ |
41 | | - "chipflow" |
42 | | - ], |
43 | | - "properties": { |
44 | | - "chipflow": { |
45 | | - "type": "object", |
46 | | - "required": [ |
47 | | - "steps", |
48 | | - "silicon" |
49 | | - ], |
50 | | - "additionalProperties": False, |
51 | | - "properties": { |
52 | | - "project_id": { |
53 | | - "type": "integer", |
54 | | - }, |
55 | | - "steps": { |
56 | | - "type": "object", |
57 | | - }, |
58 | | - "silicon": { |
59 | | - "type": "object", |
60 | | - "required": [ |
61 | | - "process", |
62 | | - "pad_ring", |
63 | | - "pads", |
64 | | - ], |
65 | | - "additionalProperties": False, |
66 | | - "properties": { |
67 | | - "process": { |
68 | | - "enum": ["sky130", "gf180", "customer1", "gf130bcd", "ihp_sg13g2"] |
69 | | - }, |
70 | | - "pad_ring": { |
71 | | - "enum": ["caravel", "cf20", "pga144"] |
72 | | - }, |
73 | | - "pads": { |
74 | | - "type": "object", |
75 | | - "additionalProperties": False, |
76 | | - "minProperties": 1, |
77 | | - "patternProperties": { |
78 | | - ".+": { |
79 | | - "type": "object", |
80 | | - "required": [ |
81 | | - "type", |
82 | | - "loc", |
83 | | - ], |
84 | | - "additionalProperties": False, |
85 | | - "properties": { |
86 | | - "type": { |
87 | | - "enum": ["io", "i", "o", "oe", "clk"] |
88 | | - }, |
89 | | - "loc": { |
90 | | - "type": "string", |
91 | | - "pattern": "^[NSWE]?[0-9]+$" |
92 | | - }, |
93 | | - } |
94 | | - } |
95 | | - } |
96 | | - }, |
97 | | - "power": { |
98 | | - "type": "object", |
99 | | - "additionalProperties": False, |
100 | | - "patternProperties": { |
101 | | - ".+": { |
102 | | - "type": "object", |
103 | | - "required": [ |
104 | | - "loc", |
105 | | - ], |
106 | | - "additionalProperties": False, |
107 | | - "properties": { |
108 | | - "loc": { |
109 | | - "type": "string", |
110 | | - "pattern": "^[NSWE]?[0-9]+$" |
111 | | - }, |
112 | | - } |
113 | | - } |
114 | | - } |
115 | | - }, |
116 | | - } |
117 | | - }, |
118 | | - }, |
119 | | - } |
120 | | - } |
121 | | -} |
| 10 | +from . import ( |
| 11 | + ChipFlowError, |
| 12 | + _get_cls_by_reference, |
| 13 | + _parse_config, |
| 14 | +) |
| 15 | +from .pin_lock import PinCommand |
122 | 16 |
|
123 | 17 |
|
124 | | -def _parse_config(): |
125 | | - chipflow_root = _ensure_chipflow_root() |
126 | | - config_file = f"{chipflow_root}/chipflow.toml" |
127 | | - return _parse_config_file(config_file) |
| 18 | +logging.basicConfig(stream=sys.stdout, level=logging.WARNING) |
128 | 19 |
|
129 | 20 |
|
130 | | -def _parse_config_file(config_file): |
131 | | - with open(config_file, "rb") as f: |
132 | | - config_dict = tomli.load(f) |
133 | | - |
134 | | - try: |
135 | | - jsonschema.validate(config_dict, config_schema) |
136 | | - return config_dict |
137 | | - except jsonschema.ValidationError as e: |
138 | | - raise ChipFlowError(f"Syntax error in `chipflow.toml` at `{'.'.join(e.path)}`: {e.message}") |
| 21 | +class UnexpectedError(ChipFlowError): |
| 22 | + pass |
139 | 23 |
|
140 | 24 |
|
141 | 25 | def run(argv=sys.argv[1:]): |
142 | 26 | config = _parse_config() |
143 | 27 |
|
144 | | - steps = {} |
| 28 | + commands = {} |
| 29 | + commands["pin"] = PinCommand(config) |
| 30 | + |
145 | 31 | for step_name, step_reference in config["chipflow"]["steps"].items(): |
146 | 32 | step_cls = _get_cls_by_reference(step_reference, context=f"step `{step_name}`") |
147 | 33 | try: |
148 | | - steps[step_name] = step_cls(config) |
| 34 | + commands[step_name] = step_cls(config) |
149 | 35 | except Exception: |
150 | 36 | raise ChipFlowError(f"Encountered error while initializing step `{step_name}` " |
151 | | - f"using `{step_reference}`") |
152 | | - |
153 | | - parser = argparse.ArgumentParser() |
154 | | - step_argument = parser.add_subparsers(dest="step", required=True) |
155 | | - for step_name, step_cls in steps.items(): |
156 | | - step_subparser = step_argument.add_parser(step_name, help=inspect.getdoc(step_cls)) |
| 37 | + f"using `{step_reference}`") |
| 38 | + |
| 39 | + parser = argparse.ArgumentParser( |
| 40 | + prog="chipflow", |
| 41 | + description="Command line tool for interacting with the ChipFlow platform") |
| 42 | + |
| 43 | + parser.add_argument( |
| 44 | + "--verbose", "-v", |
| 45 | + dest="log_level", |
| 46 | + action="append_const", |
| 47 | + const=10, |
| 48 | + help="increase verbosity of messages; can be supplied multiple times to increase verbosity" |
| 49 | + ) |
| 50 | + |
| 51 | + command_argument = parser.add_subparsers(dest="command", required=True) |
| 52 | + for command_name, command in commands.items(): |
| 53 | + command_subparser = command_argument.add_parser(command_name, help=inspect.getdoc(command)) |
157 | 54 | try: |
158 | | - step_cls.build_cli_parser(step_subparser) |
| 55 | + command.build_cli_parser(command_subparser) |
159 | 56 | except Exception: |
160 | 57 | raise ChipFlowError(f"Encountered error while building CLI argument parser for " |
161 | | - f"step `{step_name}`") |
| 58 | + f"step `{command_name}`") |
162 | 59 |
|
| 60 | + # each verbose flag increases versbosity (e.g. -v -v, -vv, --verbose --verbose) |
| 61 | + # cute trick using append_const and summing |
163 | 62 | args = parser.parse_args(argv) |
164 | | - try: |
165 | | - steps[args.step].run_cli(args) |
166 | | - except ChipFlowError: |
167 | | - raise |
168 | | - except Exception: |
169 | | - raise ChipFlowError(f"Encountered error while running CLI for step `{args.step}`") |
170 | | - |
| 63 | + if args.log_level: |
| 64 | + log_level = max(logging.DEBUG, logging.WARNING - sum(args.log_level)) |
| 65 | + logging.getLogger().setLevel(log_level) |
171 | 66 |
|
172 | | -if __name__ == '__main__': |
173 | | - run() |
| 67 | + try: |
| 68 | + try: |
| 69 | + commands[args.command].run_cli(args) |
| 70 | + except ChipFlowError: |
| 71 | + raise |
| 72 | + except Exception as e: |
| 73 | + # convert to ChipFlowError so all handling is same. |
| 74 | + raise UnexpectedError( |
| 75 | + f"Unexpected error, please report to ChipFlow:\n" |
| 76 | + f"args =\n{pformat(args)}\n" |
| 77 | + f"traceback =\n{''.join(traceback.format_exception(e))}" |
| 78 | + ) from e |
| 79 | + except ChipFlowError as e: |
| 80 | + cmd = args.command |
| 81 | + if hasattr(args, "action"): |
| 82 | + cmd += f" {args.action}" |
| 83 | + print(f"Error while executing `{cmd}`: {e}") |
0 commit comments