|
4 | 4 | import subprocess
|
5 | 5 | import sys
|
6 | 6 |
|
7 |
| -def check_pyproject_toml(): |
| 7 | + |
| 8 | +def check_pyproject_toml(stand_alone): |
8 | 9 | # Check if 'pyproject.toml' exists in the project root.
|
9 |
| - if not os.path.isfile('pyproject.toml'): |
| 10 | + if not os.path.isfile("pyproject.toml"): |
| 11 | + if stand_alone: |
| 12 | + print("pyproject.toml check passed.") |
| 13 | + return True |
| 14 | + |
10 | 15 | print("Error: pyproject.toml not found.")
|
11 | 16 | return False
|
12 | 17 | else:
|
| 18 | + if stand_alone: |
| 19 | + print("Error: pyproject.toml found in standalone python module.") |
| 20 | + return False |
| 21 | + |
13 | 22 | print("pyproject.toml check passed.")
|
14 | 23 | return True
|
15 |
| - |
16 |
| -def check_poetry_lock(): |
| 24 | + |
| 25 | + |
| 26 | +def check_poetry_lock(stand_alone): |
17 | 27 | # Check if 'poetry.lock' exists in the project root.
|
18 |
| - if not os.path.isfile('poetry.lock'): |
| 28 | + if not os.path.isfile("poetry.lock"): |
| 29 | + if stand_alone: |
| 30 | + print("poetry.lock check passed.") |
| 31 | + return True |
| 32 | + |
19 | 33 | print("Error: poetry.lock not found.")
|
20 | 34 | return False
|
21 | 35 | else:
|
| 36 | + if stand_alone: |
| 37 | + print("Error: poetry.lock found in stand alone module.") |
| 38 | + return False |
| 39 | + |
22 | 40 | print("poetry.lock check passed.")
|
23 | 41 | return True
|
24 | 42 |
|
| 43 | + |
| 44 | +def check_lint_with_ruff(): |
| 45 | + # Check Python code linting issues using 'ruff'. |
| 46 | + result = subprocess.run(["ruff", "check", "."], capture_output=True) |
| 47 | + if result.returncode != 0: |
| 48 | + print("Code linting issues found.") |
| 49 | + print(result.stdout.decode()) |
| 50 | + return False |
| 51 | + else: |
| 52 | + print("Code linting check passed.") |
| 53 | + return True |
| 54 | + |
| 55 | + |
25 | 56 | def check_code_format_with_ruff():
|
26 | 57 | # Check Python code formatting and linting issues using 'ruff'.
|
27 |
| - result = subprocess.run(['ruff', 'check', '.'], capture_output=True) |
| 58 | + result = subprocess.run(["ruff", "format", "--check", "."], capture_output=True) |
28 | 59 | if result.returncode != 0:
|
29 |
| - print("Code formatting and linting issues found.") |
| 60 | + print("Code formatting issues found.") |
30 | 61 | print(result.stdout.decode())
|
31 | 62 | return False
|
32 | 63 | else:
|
33 |
| - print("Code formatting and linting check passed.") |
| 64 | + print("Code formatting check passed.") |
34 | 65 | return True
|
35 | 66 |
|
36 |
| -def main(): |
| 67 | + |
| 68 | +def zero_third_party_packages_found(output): |
| 69 | + lines = output.split("\n") # Split the multiline string into individual lines |
| 70 | + |
| 71 | + if len(lines) < 2: |
| 72 | + return False # The second line doesn't exist |
| 73 | + else: |
| 74 | + return lines[1].startswith("Found '0' third-party package imports") |
| 75 | + |
| 76 | + |
| 77 | +def check_no_third_party_imports(): |
| 78 | + # Check No third party imports have been used |
| 79 | + result = subprocess.run(["third-party-imports", "."], capture_output=True) |
| 80 | + output = result.stdout.decode() |
| 81 | + |
| 82 | + if result.returncode != 0 or not zero_third_party_packages_found(output): |
| 83 | + print("Checking third party imports failed.") |
| 84 | + print(output) |
| 85 | + return False |
| 86 | + else: |
| 87 | + print("Checking third party imports passed.") |
| 88 | + return True |
| 89 | + |
| 90 | + |
| 91 | +def main(stand_alone): |
| 92 | + if stand_alone: |
| 93 | + print( |
| 94 | + "Checking Standalone Python files (No third party imports or poetry project)" |
| 95 | + ) |
37 | 96 | checks_passed = True
|
38 | 97 | # Perform checks
|
39 |
| - checks_passed &= check_pyproject_toml() |
40 |
| - checks_passed &= check_poetry_lock() |
| 98 | + |
| 99 | + # These are true on python programs that require third party libraries, false otherwise |
| 100 | + checks_passed &= check_pyproject_toml(stand_alone) |
| 101 | + checks_passed &= check_poetry_lock(stand_alone) |
| 102 | + |
| 103 | + # Always done |
| 104 | + checks_passed &= check_lint_with_ruff() |
41 | 105 | checks_passed &= check_code_format_with_ruff()
|
42 | 106 |
|
| 107 | + # Only done if the code should be able to run without third part libraries |
| 108 | + if stand_alone: |
| 109 | + checks_passed &= check_no_third_party_imports() |
| 110 | + |
43 | 111 | if not checks_passed:
|
44 | 112 | sys.exit(1)
|
45 | 113 |
|
| 114 | + |
46 | 115 | if __name__ == "__main__":
|
47 |
| - main() |
| 116 | + print(f"Current Working Directory: {os.getcwd()}") |
| 117 | + main("--stand-alone" in sys.argv[1:]) |
0 commit comments