-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconductor.py
executable file
·131 lines (105 loc) · 4.03 KB
/
conductor.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
#!/usr/bin/env python3
# -----------------------------------------------------------------------------
# Conductor - orchestrate your scripts' execution
# Copyright (C) 2023 Jacob Kochems
# SPDX-License-Identifier: GPL-3.0-or-later
# -----------------------------------------------------------------------------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
import os
import sys
import re
import json
from collections import OrderedDict
# this matches anywhere in a filename
EXCLUSION_PATTERN = ["##", "README", "readme"]
# this matches only at the end of a filename
EXCLUSION_SUFFIX = [
"~", ".md", ".lst", ".archived",
".deprecated", ".disabled", ".todo"]
KEYPHRASE = '!depends_on:'
CACHE_SUFFIX = '-job.cache'
def get_files_recursively(directory):
paths = []
for root, directories, files in os.walk(directory):
for file in files:
path = os.path.join(root, file)
paths.append(path)
paths.sort()
return paths
def get_dependencies(file, directory):
with open(file, "r") as file:
content = file.read()
match = re.findall(fr"{KEYPHRASE}[ \t]*(\w+.+)\b", content)
return [os.path.join(directory, dep)
for dep in re.split(r"[ ,\t]+", match[0])] if match else None
def get_playbook(directory):
return OrderedDict(
[(file, get_dependencies(file, directory))
for file in get_files_recursively(directory)
if not any(p in file for p in EXCLUSION_PATTERN)
and not any(file.endswith(s) for s in EXCLUSION_SUFFIX)
and os.access(file, os.X_OK)])
def get_playlist(plays, playbook, playlist=[]):
candidates = []
for play in plays:
dependencies = playbook[play]
if dependencies:
candidates += get_playlist(dependencies, playbook, playlist)
candidates += [play]
playlist += [c for c in candidates if c not in playlist]
return playlist
def Msg(this):
def msg(msg_text, status=""):
sep = " - " if status else ""
print(f'{os.path.basename(this)}:',
f"{status}{sep}{msg_text}",
file=sys.stderr if status else sys.stdout)
return msg
def cache_jobs(playlist, filename):
with open(filename, 'w') as f:
json.dump(playlist, f)
def load_cache(filename):
with open(filename, 'r') as f:
return json.load(f)
def main(this) -> bool:
CACHE = f'{this}{CACHE_SUFFIX}'
MAX_WIDTH = 80
msg = Msg(this)
playbook = get_playbook(this+'.d')
if os.path.exists(CACHE):
playlist = load_cache(CACHE)
os.remove(CACHE)
else:
try:
playlist = get_playlist(playbook.keys(), playbook)
except KeyError as key:
msg(f"Could not resolve dependency {key}", "ERROR")
return False
try:
columns, _ = os.get_terminal_size()
except OSError:
columns = MAX_WIDTH
width = min(columns, MAX_WIDTH)
for play in playlist:
print(f"\n--- {os.path.basename(play)} ---".ljust(width, "-"))
if os.system(play) != 0:
msg("Catched non zero exit status", f"ERROR in {play}")
cache_jobs(playlist[playlist.index(play):], CACHE)
msg(f"Remaining jobs written to: {CACHE}", "INFO")
msg(f"Fix '{play}' and run again to resume", "INFO")
return False
return True
if __name__ == "__main__":
exit(0 if main(sys.argv[0]) else 1)