Skip to content

Commit fe2f8d7

Browse files
authored
chore: Update owlbot.py to only format changed files (#10310)
1 parent 0d87fbc commit fe2f8d7

File tree

1 file changed

+80
-19
lines changed

1 file changed

+80
-19
lines changed

owlbot.py

+80-19
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,91 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from logging import Logger
16+
from pathlib import Path
17+
import re
18+
import subprocess
19+
1520
import synthtool as s
1621
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+
1775

1876
templated_files = gcp.CommonTemplates().py_library()
1977

2078
# Copy the standard noxfile from templated_files
2179
s.move(templated_files / "noxfile.py")
2280

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

Comments
 (0)