summaryrefslogtreecommitdiffstats
path: root/examples/repomanage.php
blob: 8428118caa6c2282ff5b3ba6b5cee2bbae5254d7 (plain)
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
#!/usr/bin/php
<?php

extension_loaded("rpminfo") or die("rpminfo extension missing");

for ($i=1 ; $i<$_SERVER['argc'] ; $i++) {
	switch($_SERVER['argv'][$i]) {
		case '-o':
		case '--old':
			if (isset($old)) {
				die("--old option incompatible with --new\n");
			}
			$old = true;
			break;
		case '-n':
		case '--new':
			if (isset($new)) {
				die("--new option incompatible with --old\n");
			}
		case '-k':
		case '--keep':
			if (!isset($_SERVER['argv'][$i+1])) {
				die("Missing argument for --keep option\n");
			}
			$keep = (int)$_SERVER['argv'][$i+1];
			$i++;
			break;
		default:
			$rpms = $_SERVER['argv'][$i];
	}
}
if (!isset($old)) {
	die("Missing --old or --new option\n");
}
if (!isset($keep)) {
	$keep = 1;
}
if (!isset($rpms)) {
	die("Missing rpm directory argument\n");
}
if (is_dir($rpms)) {
} else {
	die("$rpms is not a directory\n");
}

$tree = [];
$handle = popen('rpm -qp --qf "%{NAME} %{EPOCHNUM} %{VERSION} %{RELEASE} %{ARCH} %{NEVR}\n" ' . $rpms . "/*.rpm", "r");
while ($line = fgets($handle)) {
	$tab = explode(' ', trim($line));
	if (count($tab) == 6) {
		$tree[$tab[4]][$tab[0]][] = [
			'name' => $tab[0],
			'path' => $tab[5],
			'evr'  => "${tab[1]}:${tab[2]}-${tab[3]}",
		];
	} else {
		echo "Ignore $line\n";
	}
}
foreach($tree as $arch => $subtree) {
	foreach ($subtree as $name => $versions) {
		if (count($versions) > $keep) {
			if ($old) {
				usort($versions, function($a, $b) { return -rpmvercmp($a['evr'], $b['evr']);} );
			} else {
				usort($versions, function($a, $b) { return rpmvercmp($a['evr'], $b['evr']);} );
			}
			for ($i = $keep ; $i < count($versions) ; $i++) {
				echo $versions[$i]['path'] . "\n";
			}
		}
	}
}