Utility class for writing build scripts
| 153 | |
| 154 | |
| 155 | class BuildScript: |
| 156 | """Utility class for writing build scripts""" |
| 157 | |
| 158 | def __init__(self, filepath, desc=None, verbose=False): |
| 159 | self._filepath = filepath |
| 160 | self._file = open(self._filepath, "w") |
| 161 | self._verbose = verbose |
| 162 | self.header(desc) |
| 163 | |
| 164 | def __enter__(self): |
| 165 | return self |
| 166 | |
| 167 | def __exit__(self, type, value, traceback): |
| 168 | self.close() |
| 169 | |
| 170 | def __del__(self): |
| 171 | self.close() |
| 172 | |
| 173 | def close(self): |
| 174 | if self._file is not None: |
| 175 | """Close the file""" |
| 176 | self._file.close() |
| 177 | self._file = None |
| 178 | st = os.stat(self._filepath) |
| 179 | os.chmod(self._filepath, st.st_mode | stat.S_IEXEC) |
| 180 | |
| 181 | def blankln(self): |
| 182 | self._file.write("\n") |
| 183 | |
| 184 | def commentln(self, cnt): |
| 185 | self._file.write("#" * cnt + "\n") |
| 186 | |
| 187 | def comment(self, msg=""): |
| 188 | if not isinstance(msg, str): |
| 189 | try: |
| 190 | for m in msg: |
| 191 | self._file.write(f"# {msg}\n") |
| 192 | return |
| 193 | except TypeError: |
| 194 | pass |
| 195 | self._file.write(f"# {msg}\n") |
| 196 | |
| 197 | def comment_verbose(self, msg=""): |
| 198 | if self._verbose: |
| 199 | self.comment(msg) |
| 200 | |
| 201 | def header(self, desc=None): |
| 202 | self._file.write("#!/usr/bin/env bash\n\n") |
| 203 | |
| 204 | if desc is not None: |
| 205 | self.comment() |
| 206 | self.comment(desc) |
| 207 | self.comment() |
| 208 | self.blankln() |
| 209 | |
| 210 | self.comment("Exit script immediately if any command fails") |
| 211 | self._file.write("set -e\n") |
| 212 | if self._verbose: |
no outgoing calls
no test coverage detected