|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# -------------------------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 6 | +# -------------------------------------------------------------------------------------------- |
| 7 | + |
| 8 | +# This script is used to execute pylint within a tox environment. Depending on which package is being executed against, |
| 9 | +# a failure may be suppressed. |
| 10 | + |
| 11 | +import subprocess |
| 12 | +import argparse |
| 13 | +import os |
| 14 | +import logging |
| 15 | +import sys |
| 16 | + |
| 17 | +from ci_tools.environment_exclusions import is_check_enabled |
| 18 | +from ci_tools.parsing import ParsedSetup |
| 19 | +from ci_tools.variables import in_ci |
| 20 | + |
| 21 | +logging.getLogger().setLevel(logging.INFO) |
| 22 | + |
| 23 | +root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..")) |
| 24 | + |
| 25 | +if __name__ == "__main__": |
| 26 | + parser = argparse.ArgumentParser( |
| 27 | + description="Run black against target folder. Uses the black config in eng/black-pyproject.toml." |
| 28 | + ) |
| 29 | + |
| 30 | + parser.add_argument( |
| 31 | + "-t", |
| 32 | + "--target", |
| 33 | + dest="target_package", |
| 34 | + help="The target package directory on disk.", |
| 35 | + required=True, |
| 36 | + ) |
| 37 | + |
| 38 | + args = parser.parse_args() |
| 39 | + |
| 40 | + pkg_dir = os.path.abspath(args.target_package) |
| 41 | + pkg_details = ParsedSetup.from_path(pkg_dir) |
| 42 | + configFileLocation = os.path.join(root_dir, "eng/black-pyproject.toml") |
| 43 | + |
| 44 | + if in_ci(): |
| 45 | + if not is_check_enabled(args.target_package, "black"): |
| 46 | + logging.info( |
| 47 | + f"Package {pkg_details.name} opts-out of black check." |
| 48 | + ) |
| 49 | + exit(0) |
| 50 | + |
| 51 | + try: |
| 52 | + run_result = subprocess.run( |
| 53 | + [ |
| 54 | + sys.executable, |
| 55 | + "-m", |
| 56 | + "black", |
| 57 | + f"--config={configFileLocation}", |
| 58 | + args.target_package, |
| 59 | + ], |
| 60 | + stdout=subprocess.PIPE, |
| 61 | + stderr=subprocess.PIPE |
| 62 | + ) |
| 63 | + |
| 64 | + if run_result.stderr and "reformatted" in run_result.stderr.decode("utf-8"): |
| 65 | + if in_ci(): |
| 66 | + logging.info(f"The package {pkg_details.name} needs reformat. Run the `black` tox env locally to reformat.") |
| 67 | + exit(1) |
| 68 | + else: |
| 69 | + logging.info(f"The package {pkg_details.name} was reformatted.") |
| 70 | + |
| 71 | + except subprocess.CalledProcessError as e: |
| 72 | + logging.error( |
| 73 | + f"Unable to invoke black for {pkg_details.name}. Ran into exception {e}." |
| 74 | + ) |
| 75 | + |
0 commit comments