-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathvalidate_formatting.py
More file actions
75 lines (56 loc) · 2.56 KB
/
validate_formatting.py
File metadata and controls
75 lines (56 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import argparse
import os
import logging
import sys
import subprocess
logging.getLogger().setLevel(logging.INFO)
from ci_tools.functions import discover_targeted_packages
from ci_tools.environment_exclusions import is_check_enabled
root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", ".."))
sdk_dir = os.path.join(root_dir, "sdk")
def run_black(service_dir):
results = []
logging.info("Running black for {}".format(service_dir))
discovered_packages = discover_targeted_packages("azure*", os.path.join(root_dir, "sdk", service_dir))
for package in discovered_packages:
package_name = os.path.basename(package)
if is_check_enabled(package, "black", True):
out = subprocess.Popen([sys.executable, "-m", "black", "-l", "120", "sdk/{}/{}".format(service_dir, package_name)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd = root_dir
)
stdout,stderr = out.communicate()
if stderr:
results.append((package_name, stderr))
if stdout:
if "reformatted" in stdout.decode('utf-8'):
results.append((package_name, False))
else:
print(f"black succeeded against {package_name}")
return results
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run black to verify formatted code."
)
parser.add_argument(
"--service_directory", help="Directory of the package being tested"
)
parser.add_argument(
"--validate", help=("Flag that enables formatting validation.")
)
args = parser.parse_args()
if args.validate != "False":
results = run_black(args.service_directory)
if len(results) > 0:
for result in results:
error = "Code needs reformat." if result[1] == False else error
logging.error(f"Black run for {result[0]} ran into an issue: {error}")
raise ValueError("Found difference between formatted code and current commit. Please re-generate with the latest autorest.")
else:
print("Skipping formatting validation")