1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# RPM information extension for PHP
Retrieve RPM information from PHP code using librpm.
This extension can be considered as stable, and be used on production environement.
----
# Sources
* Official git repository: https://git.remirepo.net/cgit/tools/php-rpminfo.git/
* Mirror on github for contributors: https://github.com/remicollet/rpminfo
# Build
You need the rpm development files (rpm-devel) version >= 4.13.
From the sources tree
$ phpize
$ ./configure --enable-rpminfo
$ make
$ make test
From https://pecl.php.net/ using pecl command
$ pecl install rpminfo
From https://packagist.org/ using PHP Installer for Extensions
$ pie install remi/rpminfo
----
# Usage
## rpmvercmp
int rpmvercmp(string evr1, string evr2);
Allow to compare 2 EVR (epoch:version-release) strings. The return value is < 0 if evr1 < evr2, > 0 if evr1 > evr2, 0 if equal.
$ php -a
php > var_dump(rpmvercmp("1.0", "1.1"));
int(-1)
php > var_dump(rpmvercmp("1:1.0", "1.1"));
int(1)
## rpminfo
rpminfo(string path [, bool full [, string &error]]): array;
Retrieve information from a rpm file, reading its metadata.
If given `error` will be used to store error message instead of raising a warning.
The return value is a hash table, or false if it fails.
$ php -a
php > print_r(rpminfo("tests/bidon.rpm"));
Array
(
[Name] => bidon
[Version] => 1
[Release] => 1.fc25.remi
[Arch] => x86_64
)
php > print_r(rpminfo("tests/bidon.rpm", true));
Array
(
[Headeri18ntable] => C
[Sigsize] => 2304
[Sigmd5] => 644819c3566819b1e10a5c97943de094
[Sha1header] => 0a86742fe53973ac9ab4611187a83ffb44f1de5a
[Sha256header] => 9aab7242a80212ad1fe4fdd3b250c0c4f176c0b3fb1355c0d62ff094fc3f7da0
[Name] => bidon
[Version] => 1
[Release] => 1.fc25.remi
[Summary] => Bidon
[Description] => A dummy package
...
[IsSource] =>
)
php > var_dump(rpminfo("missing.rpm"));
Warning: rpminfo(): Can't open 'missing.rpm': No such file or directory in php shell code on line 1
bool(false)
php > var_dump(rpminfo("missing.rpm", false, $error));
bool(false)
php > echo $error;
Can't open 'missing.rpm': No such file or directory
## rpmdbinfo
rpmdbinfo(string path [, bool full ]): array;
Retrieve information from rpm database about an installed package.
The return value is an array of hash tables, or false if it fails.
$ php -a
php > print_r(rpmdbinfo("php"));
Array
(
[0] => Array
(
[Name] => php
[Version] => 7.3.5
[Release] => 1.fc31.remi
[Summary] => PHP scripting language for creating dynamic web sites
[Arch] => x86_64
)
)
Retrieve information from rpm database about installed packages using glob or regex.
The return value is an array of hash tables, or false if it fails.
$ php -a
php > print_r(rpmdbsearch("php-pecl-r*", RPMTAG_NAME, RPMMIRE_GLOB));
Array
(
[0] => Array
(
[Name] => php-pecl-radius
[Version] => 1.4.0
[Release] => 0.10.b1.fc31
[Summary] => Radius client library
[Arch] => x86_64
)
[1] => Array
(
[Name] => php-pecl-redis5
[Version] => 5.2.0
[Release] => 1.fc31.remi.7.3
[Summary] => Extension for communicating with the Redis key-value store
[Arch] => x86_64
)
[2] => Array
(
[Name] => php-pecl-rpminfo
[Version] => 0.2.3
[Release] => 1.fc31.remi.7.3
[Summary] => RPM information
[Arch] => x86_64
)
)
$ php -a
php > print_r(rpmdbsearch("^php-pecl-r", RPMTAG_NAME, RPMMIRE_REGEX));
Array
(
[0] => Array
(
[Name] => php-pecl-radius
[Version] => 1.4.0
[Release] => 0.10.b1.fc31
[Summary] => Radius client library
[Arch] => x86_64
)
[1] => Array
(
[Name] => php-pecl-redis5
[Version] => 5.2.0
[Release] => 1.fc31.remi.7.3
[Summary] => Extension for communicating with the Redis key-value store
[Arch] => x86_64
)
[2] => Array
(
[Name] => php-pecl-rpminfo
[Version] => 0.2.3
[Release] => 1.fc31.remi.7.3
[Summary] => RPM information
[Arch] => x86_64
)
)
$ php -a
php > print_r(rpmdbsearch(PHP_BINARY, RPMTAG_INSTFILENAMES));
Array
(
[0] => Array
(
[Name] => php-cli
[Version] => 7.3.15
[Release] => 1.fc31.remi
[Summary] => Command-line interface for PHP
[Arch] => x86_64
)
)
## rpmexpand
rpmexpand($text): string
Retrieve expanded value of a RPM macro
$ php -a
php > var_dump(rpmexpand("%{?fedora:Fedora %{fedora}}%{?rhel:Enterprise Linux %{rhel}}"));
string(9) "Fedora 41"
## rpmexpandnumeric
rpmexpandnumeric($text): int
Retrieve numerical value of a RPM macro
$ php -a
php > var_dump(rpmexpandnumeric("%__isa_bits"));
int(64)
## rpmdefine
rpmdefine($text): bool
Define or change a RPM macro value.
For example, can be used to set the Database path and backend
$ mock -r almalinux-8-x86_64 init
...
$ mock -r fedora-41-x86_64 init
...
$ php -a
php > // use an old database (bdb) from an EL-8 chroot
php > rpmdefine("_dbpath /var/lib/mock/almalinux-8-x86_64/root/var/lib/rpm");
php > rpmdefine("_db_backend bdb_ro");
php > var_dump(rpmdbinfo("almalinux-release")[0]["Summary"]);
string(22) "AlmaLinux release file"
php > // use a new database (sqlite) from a Fedora-41 chroot
php > rpmdefine("_dbpath /var/lib/mock/fedora-41-x86_64/root/usr/lib/sysimage/rpm");
php > rpmdefine("_db_backend sqlite");
php > var_dump(rpmdbinfo("fedora-release")[0]["Summary"]);
string(20) "Fedora release files"
----
# LICENSE
Author: Remi Collet
This extension is licensed under [The PHP License, version 3.01](http://www.php.net/license/3_01.txt)
-----
# History
Created as a PoC, for fun, see history on
https://blog.remirepo.net/post/2018/01/26/About-repomanage-performance-regression
|