summaryrefslogtreecommitdiffstats
path: root/cleanoldrpm
blob: 98bba48abb3ae8831144ee6dbc61055b114c8526 (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
#!/usr/bin/php
<?php

if (!extension_loaded("rpminfo")) die("rpminfo extension is missing\n");

$mode = 0;
$keep = 0;
$dest = false;
$multi = [
	'php'				=> 3, 	// php/8.0
	'redis'				=> 3,	// redis/6.2
	'redis-debuginfo' 	=> 3,	// redis/6.2
	'redis-debugsource' => 3,	// redis/6.2
	'redis-devel' 		=> 3,	// redis/6.2
	'redis-doc' 		=> 3,	// redis/6.2
	'redis-trib' 		=> 3,	// redis/6.2
	'redis'				=> 3,	// redis/6.2
	'composer' 			=> 1,	// composer/2
	'phan' 				=> 1,	// phan/3
	'roundcubemail'		=> 4,	// roundcubemail/1.x.
	'remi-release'		=> 3,	// remi-release/8.5
];
if (isset($_SERVER['argv'][1])) {
	$keep = (int)$_SERVER['argv'][1];
}
if (!$keep) {
	printf("\nusage %s number [ trash_path ]\n\n", $_SERVER['argv'][0]);
	die("Number to keep is missing\n");
}
if (isset($_SERVER['argv'][2])) {
	$dest = $_SERVER['argv'][2];
	if (!is_dir($dest)) {
		die("$dest is not a directory\n");
	}
}

$rpms = [];
foreach (glob("*.rpm") as $name) {
	$info = rpminfo($name);
	$i = $info['Name'];
	if (isset($multi[$i])) {
		$i .= "/" . substr($info['Version'], 0, $multi[$i]);
	}
	$ver = $info['Version'] . '-' . $info['Release'];
	if (isset($info['Epoch'])) {
		$ver = $info['Epoch'] . ':' . $ver;
	}
	$rpms[$i][$ver] = $name;
}

foreach($rpms as $n => $rpm) {
	if (count($rpm) > $keep) {
		uksort($rpm, "rpmvercmp");
		$i = count($rpm) - $keep;
		while ($i--) {
			$name = array_shift($rpm);
			echo "\t$name\n";
			if ($dest) rename($name, "$dest/$name");
		}
	}
}