|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import re |
| 17 | +import subprocess |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +from synthtool import shell |
| 21 | +from synthtool.log import logger |
| 22 | + |
| 23 | +_TOOLS_DIRECTORY = "/synthtool" |
| 24 | +_EXCLUDED_DIRS = [r"node_modules", r"^\."] |
| 25 | +_TYPELESS_EXPRESSION = "s/export {};$/\\n/" |
| 26 | +_NPM_CONFIG_CACHE = "/var/tmp/.npm" |
| 27 | + |
| 28 | + |
| 29 | +def walk_through_owlbot_dirs(dir: Path, search_for_changed_files: bool) -> list[str]: |
| 30 | + """ |
| 31 | + Walks through all sample directories |
| 32 | + Returns: |
| 33 | + A list of directories |
| 34 | + """ |
| 35 | + owlbot_dirs: list[str] = [] |
| 36 | + packages_to_exclude = _EXCLUDED_DIRS |
| 37 | + if search_for_changed_files: |
| 38 | + try: |
| 39 | + # Need to run this step first in the post processor since we only clone |
| 40 | + # the branch the PR is on in the Docker container |
| 41 | + output = subprocess.run( |
| 42 | + ["git", "fetch", "origin", "main:main", "--deepen=200"], check=False |
| 43 | + ) |
| 44 | + output.check_returncode() |
| 45 | + except subprocess.CalledProcessError as e: |
| 46 | + if e.returncode == 128: |
| 47 | + logger.info(f"Error: ${e.output}; skipping fetching main") |
| 48 | + else: |
| 49 | + raise e |
| 50 | + for path_object in dir.glob("**/package.json"): |
| 51 | + object_dir = str(Path(path_object).parents[0]) |
| 52 | + if ( |
| 53 | + path_object.is_file() |
| 54 | + and object_dir != str(dir) |
| 55 | + and not re.search( |
| 56 | + "(?:% s)" % "|".join(packages_to_exclude), str(Path(path_object)) |
| 57 | + ) |
| 58 | + ): |
| 59 | + if search_for_changed_files: |
| 60 | + if ( |
| 61 | + subprocess.run( |
| 62 | + ["git", "diff", "--quiet", "main...", object_dir], check=False |
| 63 | + ).returncode |
| 64 | + == 1 |
| 65 | + ): |
| 66 | + owlbot_dirs.append(object_dir) |
| 67 | + else: |
| 68 | + owlbot_dirs.append(object_dir) |
| 69 | + for path_object in dir.glob("owl-bot-staging/*"): |
| 70 | + owlbot_dirs.append( |
| 71 | + f"{Path(path_object).parents[1]}/packages/{Path(path_object).name}" |
| 72 | + ) |
| 73 | + return owlbot_dirs |
| 74 | + |
| 75 | + |
| 76 | +def typeless_samples_hermetic( |
| 77 | + output_path: str, targets: str, hide_output: bool = False |
| 78 | +) -> None: |
| 79 | + """ |
| 80 | + Converts TypeScript samples in the current Node.js library |
| 81 | + to JavaScript samples. Run this step before fix() and friends. |
| 82 | + Assumes that typeless-sample-bot is already installed in a well |
| 83 | + known location on disk (node_modules/.bin). |
| 84 | + This is currently an optional, opt-in part of an individual repo's |
| 85 | + OwlBot.py, and must be called from there before calling owlbot_main. |
| 86 | + """ |
| 87 | + logger.debug("Run typeless sample bot") |
| 88 | + shell.run( |
| 89 | + [ |
| 90 | + f"{_TOOLS_DIRECTORY}/node_modules/.bin/typeless-sample-bot", |
| 91 | + "--outputpath", |
| 92 | + output_path, |
| 93 | + "--targets", |
| 94 | + targets, |
| 95 | + "--recursive", |
| 96 | + ], |
| 97 | + check=False, |
| 98 | + hide_output=hide_output, |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def trim(targets: str, hide_output: bool = False) -> None: |
| 103 | + """ |
| 104 | + Fixes the formatting of generated JS files |
| 105 | + """ |
| 106 | + logger.debug("Trim generated files") |
| 107 | + for file in os.listdir(f"{targets}"): |
| 108 | + if file.endswith(".js"): |
| 109 | + logger.debug(f"Updating {targets}/{file}") |
| 110 | + shell.run( |
| 111 | + ["sed", "-i", "-e", _TYPELESS_EXPRESSION, f"{targets}/{file}"], |
| 112 | + check=False, |
| 113 | + hide_output=hide_output, |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +def fix_hermetic(targets=".", hide_output=False): |
| 118 | + """ |
| 119 | + Fixes the formatting in the current Node.js library. It assumes that gts |
| 120 | + is already installed in a well known location on disk (node_modules/.bin). |
| 121 | + """ |
| 122 | + logger.debug("Copy eslint config") |
| 123 | + shell.run( |
| 124 | + ["cp", "-r", f"{_TOOLS_DIRECTORY}/node_modules", "."], |
| 125 | + check=True, |
| 126 | + hide_output=hide_output, |
| 127 | + ) |
| 128 | + logger.debug("Running fix...") |
| 129 | + shell.run( |
| 130 | + [f"{_TOOLS_DIRECTORY}/node_modules/.bin/gts", "fix", f"{targets}"], |
| 131 | + check=False, |
| 132 | + hide_output=hide_output, |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +# Avoid "Your cache folder contains root-owned files" error |
| 137 | +os.environ["npm_config_cache"] = _NPM_CONFIG_CACHE |
| 138 | + |
| 139 | +# Retrieve list of directories |
| 140 | +dirs: list[str] = walk_through_owlbot_dirs(Path.cwd(), search_for_changed_files=True) |
| 141 | +for d in dirs: |
| 142 | + logger.debug(f"Directory: {d}") |
| 143 | + # Run typeless bot to convert from TS -> JS |
| 144 | + typeless_samples_hermetic(output_path=d, targets=d) |
| 145 | + # Remove extra characters |
| 146 | + trim(targets=d) |
| 147 | + # Fix formatting |
| 148 | + fix_hermetic(targets=d) |
0 commit comments