-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
84 lines (63 loc) · 1.85 KB
/
main.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
import os
import subprocess
import sys
import pathlib
from typing import List
def main():
#print("Running nake ver 0.0.1")
if len(sys.argv) < 2:
print("Not enough params")
sys.exit(1)
target = sys.argv[1]
#print(f"Target: {target}")
nakefile_path = pathlib.Path('./Nakefile')
if not nakefile_path.exists():
print("Nakefile does not exists")
sys.exit(1)
if not nakefile_path.is_file():
print("Nakefile is not file")
sys.exit(1)
with open(nakefile_path) as f:
lines = f.readlines()
rules = parse_nakefile(lines)
if target in rules:
rule = rules[target]
# print(f"To run: found rule with: {rule}")
commands = ""
for command in rule:
commands += command.strip() + "; "
call_system(commands)
else:
print(f"Didn't found '{target}' in Nakefile.")
def call_system(cmd: str) -> int:
print(f"> {cmd}")
cmd = f"/bin/bash -c '{cmd}'"
result = subprocess.run(cmd, shell=True)
if result.returncode != 0:
# print(result.stderr)
return result.returncode
return 0
def parse_nakefile(lines: List[str]) -> dict:
rules = {}
line_nr = 0
while line_nr < len(lines):
line = lines[line_nr]
if not line.endswith(":\n"):
line_nr += 1
continue
rule_name = line.rstrip(": \n")
# print(f"found rule: {rule_name}")
rule = []
line_nr += 1
while line_nr < len(lines):
line = lines[line_nr]
if len(line.strip()) == 0:
break
rule.append(line)
line_nr += 1
if len(rule) > 0:
# print(f"adding rule: {rule}")
rules[rule_name] = rule
return rules
if __name__ == '__main__':
main()