|
26 | 26 | import tempfile |
27 | 27 | import locale |
28 | 28 | import datetime |
| 29 | +import re |
| 30 | +import string |
29 | 31 |
|
30 | 32 | from . import generic |
31 | 33 |
|
@@ -154,22 +156,33 @@ def get_changelog_date(override_datetime=None): |
154 | 156 | return result + timezone |
155 | 157 | finally: |
156 | 158 | locale.setlocale(locale.LC_TIME, current_lc) |
157 | | - template_parsing_defs = { |
158 | | - ("changelog.in", "changelog"): { |
159 | | - "VERSION": "{}-{}".format(self.version, self.revision), |
160 | | - "DATETIME": get_changelog_date() |
161 | | - } |
162 | | - } |
| 159 | + |
| 160 | + class CustomTemplate(string.Template): # Inspired by http://stackoverflow.com/questions/12768107/string-substitutions-using-templates-in-python |
| 161 | + pattern = r""" |
| 162 | + {delim}(?: |
| 163 | + (?P<escaped>{delim}) | |
| 164 | + _(?P<named>{id}) | |
| 165 | + {{(?P<braced>{id})}} | |
| 166 | + (?P<invalid>{delim}((?!_)|(?!{{))) |
| 167 | + ) |
| 168 | + """.format(delim=re.escape("$ungoog"), id=string.Template.idpattern) |
| 169 | + |
| 170 | + template_parsing_defs = dict( |
| 171 | + changelog_version="{}-{}".format(self.version, self.revision), |
| 172 | + changelog_datetime=get_changelog_date(), |
| 173 | + build_output=str(self.build_output) |
| 174 | + ) |
163 | 175 | self.logger.info("Building Debian package...") |
164 | 176 | destination_dpkg_dir = self.sandbox_root / pathlib.Path("debian") |
165 | 177 | distutils.dir_util.copy_tree(str(self.PLATFORM_RESOURCES / pathlib.Path("dpkg_dir")), str(destination_dpkg_dir)) |
166 | | - for template_expr in template_parsing_defs: |
167 | | - old_name, new_name = template_expr |
168 | | - with (destination_dpkg_dir / pathlib.Path(old_name)).open() as old_file: |
169 | | - content = old_file.read().format(**template_parsing_defs[template_expr]) |
170 | | - with (destination_dpkg_dir / pathlib.Path(new_name)).open("w") as new_file: |
171 | | - new_file.write(content) |
172 | | - (destination_dpkg_dir / pathlib.Path(old_name)).unlink() |
| 178 | + for old_path in destination_dpkg_dir.glob("*.in"): |
| 179 | + new_path = destination_dpkg_dir / old_path.stem |
| 180 | + old_path.replace(new_path) |
| 181 | + with new_path.open("r+") as new_file: |
| 182 | + content = CustomTemplate(new_file.read()).substitute(**template_parsing_defs) |
| 183 | + new_file.seek(0) |
| 184 | + new_file.write(content) |
| 185 | + new_file.truncate() |
173 | 186 | result = subprocess.run(["dpkg-buildpackage", "-b", "-uc"], cwd=str(self.sandbox_root)) |
174 | 187 | if not result.returncode == 0: |
175 | 188 | raise Exception("dpkg-buildpackage returned non-zero exit code: {}".format(result.returncode)) |
0 commit comments