summaryrefslogtreecommitdiffstats
path: root/examples/repomanage.php
blob: 7682c3ddf0334a0a1ec6352d0c186d942feb0e65 (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
75
#!/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 = [];
foreach(glob("$rpms/*.rpm") as $rpm) {
	$info = rpminfo($rpm);
	if (is_array($info)) {
		$info['path'] = $rpm;
		$info['evr'] = $info['Version'] . '-' . $info['Release'];
		if (isset($info['Epoch'])) {
			$info['evr'] = $info['Epoch'] . ":" . $info['evr'];
		}
		$tree[$info['Name']][$info['Arch']][] = $info;
	} else {
		echo "Skip $rpm\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";
			}
		}
	}
}