-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkc_compile.py
61 lines (53 loc) · 2.39 KB
/
mkc_compile.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
from settings import settings,compiler
import sys
import os
import configparser
from subprocess import Popen
def get_project_file(file_name:str) -> str:
file_name = os.path.abspath(file_name)
if file_name.endswith(".mcppi"):
return file_name
file_base,_ = os.path.splitext(os.path.basename(file_name))
file_dir = os.path.dirname(file_name)
files = [file_ for file_ in os.listdir(file_dir) if file_.endswith(".mcppi")]
if len(files):
for file_ in files:
if os.path.splitext(os.path.basename(file_))[0] == file_base:
return os.path.join(file_dir,file_)
return os.path.join(file_dir,files[0])
else:
raise FileNotFoundError(".mcppi")
def main(argc,argv):
if (argc != 2):
print("python3 mkc_compile.py <fileName>")
else:
file_name = argv[1]
try:
if os.path.exists(file_name):
project_file = get_project_file(file_name)
cfg = configparser.ConfigParser()
cfg.read(project_file)
try:
cmdln0 = settings["cmdln0"].format(
compiler = compiler,
filename =([value for key,value in cfg["FILES"].items() if key.startswith("file")] or [None])[0] or os.path.basename(file_name),
device = cfg["DEVICE"]["Name"],
clock = cfg["DEVICE"]["clock"][:-6],
project = project_file,
)
def format_path_dictionary(format:str,dictionary:dict)->str:
return " ".join([format.format(path = value) for key,value in dictionary.items() if key.startswith("path")])
cmdlnSP = format_path_dictionary(settings["cmdlnSP"],cfg["SEARCH_PATH"])
cmdlnIP = format_path_dictionary(settings["cmdlnIP"],cfg["HEADER_PATH"])
cmdln = " ".join((cmdln0,cmdlnSP,cmdlnIP,settings["cmdlnLIBS"]))
Popen(cmdln)
except KeyError:
print("Key not found, invalid project file")
except:
print("Some exception occured")
else:
raise FileNotFoundError("argv[1]")
except FileNotFoundError as e:
print(f"File not found : {e.args[0] or ''}")
if __name__=="__main__":
main(len(sys.argv),[*sys.argv])