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
|
#!/usr/bin/env python3
import sys
import os
import subprocess
import re
from collections import defaultdict
variables = {}
targets = {}
dependencies = defaultdict(list)
def substitute_vars(line):
for var, val in variables.items():
line = line.replace(f"${var}", val)
return line
def run_command(cmd):
try:
subprocess.run(cmd, shell=True, check=True)
except subprocess.CalledProcessError as e:
print(f"[HemeshP] Command failed: {cmd}")
sys.exit(1)
def parse_line(line, inside_target=None):
line = line.strip()
if not line or line.startswith("#"):
return None
line = substitute_vars(line)
if line.startswith("say "):
print(line[4:].strip('" '))
elif line.startswith("run "):
cmd = line[4:].strip('" ')
run_command(cmd)
elif line.startswith("set "):
match = re.match(r"set (\w+) *= *[\"'](.+)[\"']", line)
if match:
variables[match.group(1)] = match.group(2)
else:
return line # return any leftover line for further processing
def parse_target_header(line):
# Example: target build depends clean {
match = re.match(r"target (\w+)(?: depends ([\w ]+))? *{", line)
if not match:
return None, []
name = match.group(1)
deps = match.group(2).split() if match.group(2) else []
return name, deps
def interpret_file(file_path):
with open(file_path) as f:
lines = f.readlines()
current_target = None
collecting_block = False
block_lines = []
for line in lines:
stripped = line.strip()
if stripped.startswith("target "):
current_target, deps = parse_target_header(stripped)
dependencies[current_target].extend(deps)
block_lines = []
collecting_block = True
elif stripped == "}":
if current_target:
targets[current_target] = block_lines[:]
current_target = None
block_lines = []
collecting_block = False
elif collecting_block:
block_lines.append(line)
else:
parse_line(line)
def execute_target(name, visited=set()):
if name in visited:
return
visited.add(name)
for dep in dependencies.get(name, []):
execute_target(dep, visited)
if name in targets:
print(f"[HemeshP] Running target: {name}")
for line in targets[name]:
parse_line(line)
def main():
if len(sys.argv) < 3:
print("Usage: hemeshp <script.hp> <target>")
sys.exit(1)
script = sys.argv[1]
target = sys.argv[2]
interpret_file(script)
execute_target(target)
if __name__ == "__main__":
main()
|