-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_runner.py
155 lines (139 loc) · 5.88 KB
/
actions_runner.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# **************************************************************************** #
# #
# ::: :::::::: #
# actions_runner.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: otodd <otodd@student.42london.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/12/03 11:56:46 by otodd #+# #+# #
# Updated: 2024/12/06 14:43:34 by otodd ### ########.fr #
# #
# **************************************************************************** #
import os
import logging
from copy import deepcopy
from yaml import safe_load
from subprocess import Popen, PIPE
from uuid import uuid4
from pathlib import Path
from git import Repo
from git.util import RemoteProgress
COLORS = {
"DEBUG": "\033[94m",
"INFO": "\033[92m",
"WARNING": "\033[93m",
"ERROR": "\033[91m",
"CRITICAL": "\033[95m",
"RESET": "\033[0m",
}
LOG_FORMAT = "[%(levelname)s] :: [%(asctime)s] :: %(message)s"
WORKFLOW_DIR = Path(".github/workflows/")
TMP_DIR = Path("/tmp/actions_runner/")
REPO = Repo(os.getcwd())
ENV = os.environ
class ColoredFormatter(logging.Formatter):
def format(self, record: logging.LogRecord):
color = COLORS.get(record.levelname, COLORS["RESET"])
r = deepcopy(record)
r.levelname = f"{color}{r.levelname}{COLORS['RESET']}"
return super().format(r)
class Progress(RemoteProgress):
def update(self, *args):
logger.debug(self._cur_line)
logger = logging.getLogger("actions_runner")
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setFormatter(ColoredFormatter(LOG_FORMAT, datefmt="%Y-%m-%d %H:%M:%S"))
console_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
_id = REPO.remotes[0].name + '_' + REPO.branches[0].name + '_' + uuid4().__str__()
file_handler = logging.FileHandler(Path(_id + ".log"))
file_handler.setFormatter(
logging.Formatter(LOG_FORMAT, datefmt="%Y-%m-%d %H:%M:%S")
)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
logger.debug("Fetching repo url")
repo_url = REPO.remotes[0].url
repo_dir = Path(TMP_DIR.__str__() + '/' + os.getcwd().split("/")[0] + uuid4().__str__())
logger.debug("Creating repo dir")
os.mkdir(repo_dir)
logger.debug("Checking tmp folder")
if not os.path.exists(TMP_DIR):
os.mkdir(TMP_DIR)
logger.debug("Checking workflows folder")
if not os.path.exists(WORKFLOW_DIR):
logger.critical("Workflows folder not found!!! Exiting")
exit(1)
logger.debug(f"Cloning [{repo_url}] into {repo_dir}")
Repo.clone_from(
multi_options=["--recursive"], to_path=repo_dir, url=repo_url, progress=Progress()
)
for workflow in os.listdir(WORKFLOW_DIR):
logger.debug(f"Parsing workflow file {workflow}")
with open(Path(WORKFLOW_DIR.__str__() + '/' + workflow)) as f:
action = safe_load(f)
for job in action["jobs"]:
env = action["jobs"][job].get("env", {})
env_i = [item for item in env.keys()]
logger.debug(f"Running steps for {action.get("name")} on job {job}")
length = len(action["jobs"][job].get("steps"))
for index, step in enumerate(action["jobs"][job].get("steps")):
if not step.get("run"):
continue
logger.info(
f"{10 * '='}| Running step ({index} / {length}): {step.get("name")} |{10 * '='}"
)
out = PIPE
cmd_prep: str = step.get("run")
for i in env_i:
cmd_prep = cmd_prep.replace(f"${i}", env.get(i))
proc = Popen(
cmd_prep,
stdout=out,
stderr=out,
cwd=(
Path(repo_dir.__str__() + '/' + step.get("working-directory"))
if step.get("working-directory")
else Path(repo_dir)
),
shell=True,
env=ENV
)
logger.debug(f"{5 * '='}| [ Step stdout start ] |{5 * '='}\n")
while True:
line = proc.stdout.readline()
if not line:
break
try:
line = line.decode("utf-8")
except UnicodeDecodeError:
line = line.decode("unicode_escape")
print(line.strip())
file_handler.stream.write(line)
ret = proc.wait(500)
if ret != 0:
logger.info(f"{5 * '='} [ Step stdout finish ] {5 * '='}")
logger.error(f"{4 * '-'}| Step {index} failed! |{4 * '-'}")
logger.error(f"CMD [{step.get("run")}]")
logger.error(f"{5 * '='} [ Step stderr start ] {5 * '='}\n")
while True:
line = proc.stderr.readline()
if not line:
break
try:
line = line.decode("utf-8")
except UnicodeDecodeError:
line = line.decode("unicode_escape")
print(line.strip())
file_handler.stream.write(line)
logger.error(f" {5 * '='} [ Step stderr finish ] {5 * '='}")
if step.get("continue-on-error", False) != True:
exit()
logger.info("Exiting")
logger.info(f"{5 * '='} [ Step stdout finish ] {5 * '='}")
logger.info(f"{10 * '='}| Finished step |{10 * '='}\n")
logger.warning(f"Cleaning tmp {repo_dir}")
os.system(f"rm -rf {repo_dir}")
logger.removeHandler(file_handler)
logger.info("Exiting")