|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +from logging import Logger |
| 16 | +from pathlib import Path |
| 17 | +import re |
| 18 | +import subprocess |
| 19 | + |
15 | 20 | import synthtool as s
|
16 | 21 | import synthtool.gcp as gcp
|
| 22 | +from synthtool.log import logger |
| 23 | + |
| 24 | +logger: Logger = logger |
| 25 | + |
| 26 | +_EXCLUDED_DIRS = [r"^\."] |
| 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 error: |
| 46 | + if error.returncode == 128: |
| 47 | + logger.info(f"Error: ${error.output}; skipping fetching main") |
| 48 | + else: |
| 49 | + raise error |
| 50 | + for path_object in dir.glob("**/requirements.txt"): |
| 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 | + |
17 | 75 |
|
18 | 76 | templated_files = gcp.CommonTemplates().py_library()
|
19 | 77 |
|
20 | 78 | # Copy the standard noxfile from templated_files
|
21 | 79 | s.move(templated_files / "noxfile.py")
|
22 | 80 |
|
23 |
| -# Update BLACK_PATHS in order to run black on all files |
24 |
| -s.replace( |
25 |
| - "noxfile.py", |
26 |
| - r"""LINT_PATHS = \["docs", "google", "tests", "noxfile.py", "setup.py"\]""", |
27 |
| - r"""LINT_PATHS = ["."]""", |
28 |
| -) |
29 |
| - |
30 |
| -# TODO: Remove once https://github.com/googleapis/synthtool/pull/1811 is merged. |
31 |
| -s.replace( |
32 |
| - "noxfile.py", |
33 |
| - r"""BLACK_VERSION = "black==22.3.0"\nISORT_VERSION = "isort==5.10.1""", |
34 |
| - r"""BLACK_VERSION = "black[jupyter]==23.3.0"\nISORT_VERSION = "isort==5.11.0""", |
35 |
| -) |
36 |
| - |
37 |
| -# ---------------------------------------------------------------------------- |
38 |
| -# Run blacken session |
39 |
| -# ---------------------------------------------------------------------------- |
40 |
| - |
41 |
| -s.shell.run(["nox", "-s", "blacken"], hide_output=False) |
| 81 | +dirs: list[str] = walk_through_owlbot_dirs(Path.cwd(), search_for_changed_files=True) |
| 82 | +if dirs: |
| 83 | + lint_paths = ", ".join(f'"{d}"' for d in dirs) |
| 84 | + # Update LINT_PATHS in order to run black on changed files |
| 85 | + s.replace( |
| 86 | + "noxfile.py", |
| 87 | + r"""LINT_PATHS = \["docs", "google", "tests", "noxfile.py", "setup.py"\]""", |
| 88 | + f"""LINT_PATHS = [{lint_paths}]""", |
| 89 | + ) |
| 90 | + |
| 91 | + # TODO: Remove once https://github.com/googleapis/synthtool/pull/1811 is merged. |
| 92 | + s.replace( |
| 93 | + "noxfile.py", |
| 94 | + r"""BLACK_VERSION = "black==22.3.0"\nISORT_VERSION = "isort==5.10.1""", |
| 95 | + r"""BLACK_VERSION = "black[jupyter]==23.3.0"\nISORT_VERSION = "isort==5.11.0""", |
| 96 | + ) |
| 97 | + |
| 98 | + # ---------------------------------------------------------------------------- |
| 99 | + # Run blacken session |
| 100 | + # ---------------------------------------------------------------------------- |
| 101 | + |
| 102 | + s.shell.run(["nox", "-s", "blacken"], hide_output=False) |
0 commit comments