This repository was archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.py
More file actions
115 lines (101 loc) · 2.95 KB
/
scripts.py
File metadata and controls
115 lines (101 loc) · 2.95 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
This script contains the functions that are used to build and clean the project.
"""
import argparse
import os
import shutil
import subprocess
import sys
def build():
"""
This function checks if the dist directory exists and deletes it.
Then, it runs the 'poetry build' command.
"""
try:
if os.path.exists("dist/"):
shutil.rmtree("dist/")
subprocess.run(["poetry", "build"], check=True)
except (FileNotFoundError, subprocess.CalledProcessError) as e:
print(f"An error occurred: {e}")
def run_command(command, capture_output=True, text=True, check=False):
"""
This function runs a command and returns the result.
:param command:
:param capture_output:
:param text:
:param check:
:return result:
"""
result = subprocess.run(
command,
capture_output=capture_output,
text=text,
check=check,
)
if result.returncode != 0:
print(f"{command[2]} exited with status {result.returncode}.")
if result.stderr:
print(f"{result.stderr}")
return result
return result
def run_and_handle_command(command, auto_fix):
"""
This function runs a command and handles the result.
:param command:
:param auto_fix:
:return result:
"""
if not auto_fix:
if "check" in command:
command.append("--fix")
else:
command.append("--check")
result = run_command(command)
if result and result.stdout:
print(result.stdout)
if result and result.returncode != 0:
return result
return None
def clean():
"""
This function cleans a Python project. It runs ruff and mypy.
"""
parser = argparse.ArgumentParser(description="Clean a Python project.")
parser.add_argument(
"path", help="The path of the project to clean.", default="AniLinkPy", nargs="?"
)
parser.add_argument(
"--no-fix", help="Do not automatically fix problems.", action="store_true"
)
args = parser.parse_args()
path = args.path
auto_fix = not args.no_fix
errors = []
print("Running ruff...")
error = run_and_handle_command(["poetry", "run", "ruff", "check", path], auto_fix)
if error:
errors.append(error)
error = run_and_handle_command(["poetry", "run", "ruff", "format", path], auto_fix)
if error:
errors.append(error)
print("\nRunning mypy...")
error = run_and_handle_command(
[
"poetry",
"run",
"mypy",
"--install-types",
"--non-interactive",
path,
],
auto_fix,
)
if error:
errors.append(error)
if errors:
print("\nErrors occurred during the cleaning process:")
for error in errors:
print(
f"Command '{' '.join(error.args)}' returned non-zero exit status {error.returncode}."
)
sys.exit(errors[0].returncode)