|
75 | 75 | import shlex # Added for POSIX shell escaping |
76 | 76 |
|
77 | 77 | # input validation helper |
78 | | -def safe_name(value, context, allow_path=False): |
79 | | - """ |
80 | | - Validates that the input string does not contain characters dangerous |
81 | | - for filesystem paths or shell command injection. |
82 | | - """ |
83 | | - if not value: |
84 | | - raise ValueError(f"{context} cannot be empty") |
85 | | - # blocks control characters and shell metacharacters |
86 | | - # allow path separators and drive colons for full paths when needed |
87 | | - if allow_path: |
88 | | - pattern = r'[\x00-\x1F\x7F*?"<>|;&`$\'()]' |
89 | | - else: |
90 | | - # blocks path traversal (/, \, :) in addition to shell metacharacters |
91 | | - pattern = r'[\x00-\x1F\x7F\\/:*?"<>|;&`$\'()]' |
92 | | - if re.search(pattern, value): |
93 | | - raise ValueError(f"Unsafe {context}: '{value}' contains illegal characters.") |
94 | | - return value |
95 | | - |
96 | | -MKCONCORE_VER = "22-09-18" |
97 | | - |
98 | | -SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
99 | | - |
100 | | -def _resolve_concore_path(): |
101 | | - script_concore = os.path.join(SCRIPT_DIR, "concore.py") |
102 | | - if os.path.exists(script_concore): |
103 | | - return SCRIPT_DIR |
104 | | - cwd_concore = os.path.join(os.getcwd(), "concore.py") |
105 | | - if os.path.exists(cwd_concore): |
106 | | - return os.getcwd() |
107 | | - return SCRIPT_DIR |
108 | | - |
109 | | -GRAPHML_FILE = sys.argv[1] |
110 | | -TRIMMED_LOGS = True |
111 | | -CONCOREPATH = _resolve_concore_path() |
112 | | -CPPWIN = "g++" #Windows C++ 6/22/21 |
113 | | -CPPEXE = "g++" #Ubuntu/macOS C++ 6/22/21 |
114 | | -VWIN = "iverilog" #Windows verilog 6/25/21 |
115 | | -VEXE = "iverilog" #Ubuntu/macOS verilog 6/25/21 |
116 | | -PYTHONEXE = "python3" #Ubuntu/macOS python3 |
117 | | -PYTHONWIN = "python" #Windows python3 |
118 | | -MATLABEXE = "matlab" #Ubuntu/macOS matlab |
119 | | -MATLABWIN = "matlab" #Windows matlab |
120 | | -OCTAVEEXE = "octave" #Ubuntu/macOS octave |
121 | | -OCTAVEWIN = "octave" #Windows octave |
| 78 | +def safe_name(value, context, allow_path=False): |
| 79 | + """ |
| 80 | + Validates that the input string does not contain characters dangerous |
| 81 | + for filesystem paths or shell command injection. |
| 82 | + """ |
| 83 | + if not value: |
| 84 | + raise ValueError(f"{context} cannot be empty") |
| 85 | + # blocks control characters and shell metacharacters |
| 86 | + # allow path separators and drive colons for full paths when needed |
| 87 | + if allow_path: |
| 88 | + pattern = r'[\x00-\x1F\x7F*?"<>|;&`$\'()]' |
| 89 | + else: |
| 90 | + # blocks path traversal (/, \, :) in addition to shell metacharacters |
| 91 | + pattern = r'[\x00-\x1F\x7F\\/:*?"<>|;&`$\'()]' |
| 92 | + if re.search(pattern, value): |
| 93 | + raise ValueError(f"Unsafe {context}: '{value}' contains illegal characters.") |
| 94 | + return value |
| 95 | + |
| 96 | +MKCONCORE_VER = "22-09-18" |
| 97 | + |
| 98 | +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 99 | + |
| 100 | +def _load_tool_config(filepath): |
| 101 | + tools = {} |
| 102 | + with open(filepath, "r") as f: |
| 103 | + for line in f: |
| 104 | + line = line.strip() |
| 105 | + if not line or line.startswith("#") or "=" not in line: |
| 106 | + continue |
| 107 | + k, v = line.split("=", 1) |
| 108 | + k, v = k.strip(), v.strip() |
| 109 | + if v: |
| 110 | + tools[k] = v |
| 111 | + return tools |
| 112 | + |
| 113 | +def _resolve_concore_path(): |
| 114 | + script_concore = os.path.join(SCRIPT_DIR, "concore.py") |
| 115 | + if os.path.exists(script_concore): |
| 116 | + return SCRIPT_DIR |
| 117 | + cwd_concore = os.path.join(os.getcwd(), "concore.py") |
| 118 | + if os.path.exists(cwd_concore): |
| 119 | + return os.getcwd() |
| 120 | + return SCRIPT_DIR |
| 121 | + |
| 122 | +GRAPHML_FILE = sys.argv[1] |
| 123 | +TRIMMED_LOGS = True |
| 124 | +CONCOREPATH = _resolve_concore_path() |
| 125 | +CPPWIN = os.environ.get("CONCORE_CPPWIN", "g++") #Windows C++ 6/22/21 |
| 126 | +CPPEXE = os.environ.get("CONCORE_CPPEXE", "g++") #Ubuntu/macOS C++ 6/22/21 |
| 127 | +VWIN = os.environ.get("CONCORE_VWIN", "iverilog") #Windows verilog 6/25/21 |
| 128 | +VEXE = os.environ.get("CONCORE_VEXE", "iverilog") #Ubuntu/macOS verilog 6/25/21 |
| 129 | +PYTHONEXE = os.environ.get("CONCORE_PYTHONEXE", "python3") #Ubuntu/macOS python3 |
| 130 | +PYTHONWIN = os.environ.get("CONCORE_PYTHONWIN", "python") #Windows python3 |
| 131 | +MATLABEXE = os.environ.get("CONCORE_MATLABEXE", "matlab") #Ubuntu/macOS matlab |
| 132 | +MATLABWIN = os.environ.get("CONCORE_MATLABWIN", "matlab") #Windows matlab |
| 133 | +OCTAVEEXE = os.environ.get("CONCORE_OCTAVEEXE", "octave") #Ubuntu/macOS octave |
| 134 | +OCTAVEWIN = os.environ.get("CONCORE_OCTAVEWIN", "octave") #Windows octave |
122 | 135 | M_IS_OCTAVE = False #treat .m as octave |
123 | 136 | MCRPATH = "~/MATLAB/R2021a" #path to local Ubunta Matlab Compiler Runtime |
124 | 137 | DOCKEREXE = "sudo docker"#assume simple docker install |
@@ -147,13 +160,26 @@ def _resolve_concore_path(): |
147 | 160 | with open(CONCOREPATH+"/concore.repo", "r") as f: |
148 | 161 | DOCKEREPO = f.readline().strip() #docker id for repo |
149 | 162 |
|
| 163 | +if os.path.exists(CONCOREPATH+"/concore.tools"): |
| 164 | + _tools = _load_tool_config(CONCOREPATH+"/concore.tools") |
| 165 | + CPPWIN = _tools.get("CPPWIN", CPPWIN) |
| 166 | + CPPEXE = _tools.get("CPPEXE", CPPEXE) |
| 167 | + VWIN = _tools.get("VWIN", VWIN) |
| 168 | + VEXE = _tools.get("VEXE", VEXE) |
| 169 | + PYTHONEXE = _tools.get("PYTHONEXE", PYTHONEXE) |
| 170 | + PYTHONWIN = _tools.get("PYTHONWIN", PYTHONWIN) |
| 171 | + MATLABEXE = _tools.get("MATLABEXE", MATLABEXE) |
| 172 | + MATLABWIN = _tools.get("MATLABWIN", MATLABWIN) |
| 173 | + OCTAVEEXE = _tools.get("OCTAVEEXE", OCTAVEEXE) |
| 174 | + OCTAVEWIN = _tools.get("OCTAVEWIN", OCTAVEWIN) |
| 175 | + |
150 | 176 |
|
151 | 177 | prefixedgenode = "" |
152 | 178 | sourcedir = sys.argv[2] |
153 | 179 | outdir = sys.argv[3] |
154 | 180 |
|
155 | | -# Validate outdir argument (allow full paths) |
156 | | -safe_name(outdir, "Output directory argument", allow_path=True) |
| 181 | +# Validate outdir argument (allow full paths) |
| 182 | +safe_name(outdir, "Output directory argument", allow_path=True) |
157 | 183 |
|
158 | 184 | if not os.path.isdir(sourcedir): |
159 | 185 | logging.error(f"{sourcedir} does not exist") |
@@ -1227,4 +1253,4 @@ def cleanup_script_files(): |
1227 | 1253 | os.chmod(outdir+"/clear",stat.S_IRWXU) |
1228 | 1254 | os.chmod(outdir+"/maxtime",stat.S_IRWXU) |
1229 | 1255 | os.chmod(outdir+"/params",stat.S_IRWXU) |
1230 | | - os.chmod(outdir+"/unlock",stat.S_IRWXU) |
| 1256 | + os.chmod(outdir+"/unlock",stat.S_IRWXU) |
0 commit comments