|
2 | 2 | import os
|
3 | 3 | import shutil
|
4 | 4 | from pathlib import Path
|
| 5 | +from typing import List |
5 | 6 |
|
6 | 7 | import gnupg
|
7 | 8 |
|
@@ -42,6 +43,25 @@ def packager_prefix():
|
42 | 43 | return "RPM"
|
43 | 44 |
|
44 | 45 |
|
| 46 | +class Scriptlet: |
| 47 | + @staticmethod |
| 48 | + def _extract_shebang(data: str): |
| 49 | + first_line = data.splitlines()[0] |
| 50 | + |
| 51 | + if first_line[:2] != "#!": |
| 52 | + return None |
| 53 | + |
| 54 | + return first_line[2:] |
| 55 | + |
| 56 | + def __init__(self, type: str, source_file: str): |
| 57 | + self.type = type |
| 58 | + |
| 59 | + with open(source_file) as f: |
| 60 | + self.content = f.read() |
| 61 | + |
| 62 | + self.shebang = Scriptlet._extract_shebang(self.content) |
| 63 | + |
| 64 | + |
45 | 65 | class RpmPackager(AbstractPackager):
|
46 | 66 | """
|
47 | 67 | This class is inspired by CPack's DEB generator code.
|
@@ -96,10 +116,22 @@ def generate_spec_file(self):
|
96 | 116 | if fixed_version != version:
|
97 | 117 | logger.warning(f"version number {version} incompatible, changed to: {fixed_version}")
|
98 | 118 |
|
| 119 | + scriptlets: List[Scriptlet] = [] |
| 120 | + |
| 121 | + for scriptlet_type in ["pretrans", "pre", "post", "preun", "postun", "posttrans"]: |
| 122 | + scriptlet_path = os.environ.get(f"LDNP_RPM_SCRIPTLET_{scriptlet_type}") |
| 123 | + |
| 124 | + if scriptlet_path: |
| 125 | + logger.info(f"Found scriptlet of type {scriptlet_type} at {scriptlet_path}") |
| 126 | + scriptlets.append(Scriptlet(scriptlet_type, scriptlet_path)) |
| 127 | + |
99 | 128 | # sorting is technically not needed but makes reading and debugging easier
|
100 | 129 | # note: fixed_version is packager-specific, so we pass it separately
|
101 | 130 | rendered = jinja_env.get_template("rpm/spec").render(
|
102 |
| - files=list(sorted(files)), meta_info=self.meta_info, fixed_version=fixed_version |
| 131 | + files=list(sorted(files)), |
| 132 | + meta_info=self.meta_info, |
| 133 | + fixed_version=fixed_version, |
| 134 | + scriptlets=scriptlets, |
103 | 135 | )
|
104 | 136 |
|
105 | 137 | with open(self.context.work_dir / "package.spec", "w") as f:
|
|
0 commit comments