summaryrefslogtreecommitdiffstats
path: root/checkpkgist.php
blob: dabfcb4882e90e84398e1b6c934a04106e27159e (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
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
#!/usr/bin/env php
<?php
/**
 * CheckPkgist is a tool to check RPM update needed
 * using information from https://packagist.org/
 *
 * PHP version 5
 *
 * Copyright (C) 2014  Remi Collet
 * https://github.com/remicollet/remirepo/tree/master/tools/checkpkgist
 *
 * CheckPkgist is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FedoraClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * See <http://www.gnu.org/licenses/>
 *
 * @category  CheckPkgist
 * @package   FedoraClient
 *
 * @author    Remi Collet <remi@fedoraproject.org>
 * @copyright 2014 Remi Collet
 * @license   http://www.gnu.org/licenses/lgpl-2.1.txt LGPL License 2.1 or (at your option) any later version
 * @link      https://github.com/remicollet/remirepo/tree/master/tools/checkpkgist
 */

require_once 'Cache/Lite.php';

if (!ini_get('date.timezone')) {
     ini_set('date.timezone', 'Europe/Paris');
}

define ('VERSION', '1.0.0-dev');

class PkgClient {
	const URL = 'https://packagist.org/';
	protected $cache;

	function __construct () {
		$dir = "/tmp/pkgist-".posix_getlogin()."/";
		@mkdir($dir);
		$this->cache = new Cache_Lite(
			array(
				'memoryCaching'             => true,
				'cacheDir'                  => $dir,
				'automaticSerialization'    => true
			)
		);
	}

	function getPackage($name) {
		$url = self::URL.'packages/'.$name.'.json';
		$rep = $this->cache->get(__METHOD__, $url);
		if (!$rep) {
			$rep = @file_get_contents($url);
			$this->cache->save($rep, __METHOD__, $url);
		}
		return ($rep ? json_decode($rep, true) : false);
	}
}

function run($name, $rpm) {
	global $quiet, $verb, $client;

	if (in_array($name, ['znerol/php-stringprep', 'psr/http-message-implementation'])) {
		return;
	}
	list($owner, $library) = explode('/', $name, 2);
	if (in_array($owner, ['horde', 'kolab'])) {
		return;
	}

	if (!$quiet) {
		printf(" %-40s\r", $rpm);
	}

	$rpmver = exec("rpm -q --qf '%{VERSION}' $rpm", $out, $ret);
	if ($ret) {
		if ($quiet) {
			return;
		}
		$rpmver = "n/a";
	}
	$pkgs = $client->getPackage($name);
	if ($pkgs) {
		$maxver = "";
		$maxdat = false;
		$display = false;
		foreach ($pkgs['package']['versions'] as $pkver => $pkg) {
			if (preg_match('/^v[\.0-9]*$/', $pkver)) {
				$pkver = substr($pkver, 1);
			}
			if (strpos($pkver, 'dev') !== false) {
				continue;
			}
			$date = new DateTime($pkg['time']);
			if (version_compare($pkver, $maxver, 'gt')) {
				$maxver = $pkver;
				$maxdat = $date;
			}
			if (version_compare($pkver, $rpmver, 'gt')) {
				$diff = $date->diff(new DateTime("now"));
				if ($diff->days <2) {
					$note = "(Just released)";
				} else if ($diff->days <20) {
					$note = $diff->format("(%a days)");
				} else {
					$note = "";
				}

				//print_r($pkg);
				printf(" %-40s %15s %15s %15s %s\n", $rpm, $rpmver, $pkver, $date->format("Y-m-d"), $note);
				if ($pkg['source']['type']=='git' && $verb) {
					printf("\tURL:  %s\n\tHash: %s\n",
						($pkg['source']['url']?:'unkown'),
						($pkg['source']['reference']?:'unkown'));
				}
				$display = true;
				if (!$verb) {
					break;
				}
			}
			else if (version_compare($pkver, $rpmver, 'eq') && $verb && !$display) {
				printf(" %-40s %15s %15s %15s\n", $rpm, $rpmver, $pkver, $date->format("Y-m-d"));
				$display = true;
				if (!$verb) {
					break;
				}
			}
		}
		if ($verb && !$display) {
			printf(" %-40s %15s %15s %15s\n", $rpm, $rpmver, ($maxver ?: 'unkown'), ($maxdat ? $date->format("Y-m-d") : ''));
		}
	} else {
		printf(" %-40s %15s %15s\n", $rpm, $rpmver, 'Not found !');
	}
}

printf("\nCheckPkgist version %s by Remi Collet.\n\n", VERSION);

if (in_array('-h', $_SERVER['argv']) || in_array('--help', $_SERVER['argv'])) {
	echo <<<END
usage checkpkg [ options ]  [ name ... ]

    -h
    --help     Display help (this page)

    -v
    --verbose  Display all packages, with upsteam information

    -q
    --quiet    Don't display not installed packages
               or packages with latest version installed

    -s
    --sort     Sort output by package name
END;
	die("\n\n");
}

$sort   = (in_array('-s', $_SERVER['argv']) || in_array('--sort', $_SERVER['argv']));
$verb   = (in_array('-v', $_SERVER['argv']) || in_array('--verbose', $_SERVER['argv']));
$quiet  = (in_array('-q', $_SERVER['argv']) || in_array('--quiet', $_SERVER['argv']));
$client = new PkgClient();

$pkgs = file_get_contents(__DIR__."/rpmphp.json");
if (!$pkgs) {
	die("Missing configuration file rpmphp\n");
}
$pkgs = json_decode($pkgs, true, 5);
if (!$pkgs) {
	die("Bad configuration file rpmphp\n");
}
$pkg2 = file_get_contents(__DIR__."/checkpkgist.json");
if (!$pkg2) {
	die("Missing configuration file checkpkgist\n");
}
$pkg2 = json_decode($pkg2, true, 5);
if (!$pkg2) {
	die("Bad configuration file checkpkgist\n");
}

$change = false;
foreach ($pkg2 as $rpm => $pkg) {
	if (isset($pkgs[$rpm])) {
		unset($pkg2[$rpm]);
		$change = true;
	} else {
		$pkgs[$rpm] = $pkg2[$rpm];
	}
}
if ($change) {
	if (file_put_contents(__DIR__."/checkpkgist.json", json_encode($pkg2, JSON_PRETTY_PRINT))) {
		printf("Configuration file changes saved\n");
	}
}

if ($sort) {
	natcasesort($pkgs);
}
printf(" %-40s %15s %15s %15s\n", "Name", "Version", "Upstream", "Date");

$tmp = array();
for ($i=1 ; $i<$_SERVER['argc'] ; $i++) {
	if (is_dir($rpm = $_SERVER['argv'][$i])) {
		$rpm = basename(realpath($_SERVER['argv'][$i]));
	}
	if (isset($pkgs[$rpm])) {
		$tmp[$rpm] = $pkgs[$rpm];
	}
}
if (count($tmp)) {
	$verb = true;
	$pkgs = $tmp;
}
foreach ($pkgs as $rpm => $name) {
	run($name, $rpm);
}
echo str_repeat(' ', 50)."\n";