xref: /aosp_15_r20/external/bazelbuild-rules_python/examples/wheel/private/directory_writer.py (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1#!/usr/bin/env python3
2# Copyright 2023 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""The action executable of the `@rules_python//examples/wheel/private:wheel_utils.bzl%directory_writer` rule."""
17
18import argparse
19import json
20from pathlib import Path
21from typing import Tuple
22
23
24def _file_input(value) -> Tuple[Path, str]:
25    path, content = value.split("=", maxsplit=1)
26    return (Path(path), json.loads(content))
27
28
29def parse_args() -> argparse.Namespace:
30    parser = argparse.ArgumentParser()
31
32    parser.add_argument(
33        "--output", type=Path, required=True, help="The output directory to create."
34    )
35    parser.add_argument(
36        "--file",
37        dest="files",
38        type=_file_input,
39        action="append",
40        help="Files to create within the `output` directory.",
41    )
42
43    return parser.parse_args()
44
45
46def main() -> None:
47    args = parse_args()
48
49    args.output.mkdir(parents=True, exist_ok=True)
50
51    for path, content in args.files:
52        new_file = args.output / path
53        new_file.parent.mkdir(parents=True, exist_ok=True)
54        new_file.write_text(content)
55
56
57if __name__ == "__main__":
58    main()
59