Skip to content

Commit 6beb5f1

Browse files
committed
Support RPM scriptlets
1 parent c1c4648 commit 6beb5f1

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

ldnp/rpm.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import shutil
44
from pathlib import Path
5+
from typing import List
56

67
import gnupg
78

@@ -42,6 +43,25 @@ def packager_prefix():
4243
return "RPM"
4344

4445

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+
4565
class RpmPackager(AbstractPackager):
4666
"""
4767
This class is inspired by CPack's DEB generator code.
@@ -96,10 +116,22 @@ def generate_spec_file(self):
96116
if fixed_version != version:
97117
logger.warning(f"version number {version} incompatible, changed to: {fixed_version}")
98118

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+
99128
# sorting is technically not needed but makes reading and debugging easier
100129
# note: fixed_version is packager-specific, so we pass it separately
101130
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,
103135
)
104136

105137
with open(self.context.work_dir / "package.spec", "w") as f:

ldnp/templates/rpm/spec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ cp -rfa %{_install_root}/* %{buildroot}
7979
{%- for file in files %}
8080
{{ file }}
8181
{%- endfor %}
82+
83+
{% for script_type in scriptlets %}
84+
%{{ scriptlet.type }} -p {{ scriptlet.shebang }}
85+
{{ scriptlet.content }}
86+
{% endfor %}

0 commit comments

Comments
 (0)