lfs-latest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #! /usr/bin/php
  2. <?php
  3. $dirs = array();
  4. $vers = array();
  5. date_default_timezone_set( "GMT" );
  6. $date = date( "Y-m-d H:i:s" );
  7. // Special cases
  8. $exceptions = array();
  9. //$exceptions[ 'gmp' ] = "UPDIR=/.*(gmp-\d[\d\.-]*\d).*/:DOWNDIR=";
  10. $regex = array();
  11. //$regex[ 'bzip2' ] = "/^.*current version is ([\d\.]+).*$/";
  12. $regex[ 'check' ] = "/^.*Check (\d[\d\.]+\d).*$/";
  13. $regex[ 'intltool' ] = "/^.*Latest version is (\d[\d\.]+\d).*$/";
  14. $regex[ 'less' ] = "/^.*current released version is less-(\d+).*$/";
  15. $regex[ 'mpfr' ] = "/^mpfr-([\d\.]+)\.tar.*$/";
  16. $regex[ 'systemd' ] = "/^.*v([\d]+)$/";
  17. //$regex[ 'sysvinit' ] = "/^.*sysvinit-([\d\.]+)dsf\.tar.*$/";
  18. $regex[ 'tzdata' ] = "/^.*tzdata([\d]+[a-z]).*$/";
  19. $regex[ 'xz' ] = "/^.*xz-([\d\.]*\d).*$/";
  20. $regex[ 'zlib' ] = "/^.*zlib ([\d\.]*\d).*$/";
  21. function find_max( $lines, $regex_match, $regex_replace )
  22. {
  23. $a = array();
  24. if ( ! is_array( $lines ) ) return -1;
  25. foreach ( $lines as $line )
  26. {
  27. if ( ! preg_match( $regex_match, $line ) ) continue;
  28. // Isolate the version and put in an array
  29. $slice = preg_replace( $regex_replace, "$1", $line );
  30. if ( $slice == $line ) continue;
  31. array_push( $a, $slice );
  32. }
  33. // SORT_NATURAL requires php-5.4.0 or later
  34. rsort( $a, SORT_NATURAL ); // Max version is at the top
  35. return ( isset( $a[0] ) ) ? $a[0] : -2;
  36. }
  37. function find_even_max( $lines, $regex_match, $regex_replace )
  38. {
  39. $a = array();
  40. foreach ( $lines as $line )
  41. {
  42. if ( ! preg_match( $regex_match, $line ) ) continue;
  43. // Isolate the version and put in an array
  44. $slice = preg_replace( $regex_replace, "$1", $line );
  45. if ( "x$slice" == "x$line" ) continue;
  46. // Skip odd numbered minor versions and minors > 80
  47. list( $major, $minor, $rest ) = explode( ".", $slice . ".0" );
  48. if ( $minor % 2 == 1 ) continue;
  49. if ( $minor > 80 ) continue;
  50. array_push( $a, $slice );
  51. }
  52. rsort( $a, SORT_NATURAL ); // Max version is at the top
  53. return ( isset( $a[0] ) ) ? $a[0] : -2;
  54. }
  55. function http_get_file( $url )
  56. {
  57. if ( ! preg_match( "/sourceforge/", $url ) )
  58. {
  59. exec( "curl --location --silent --max-time 30 $url", $dir );
  60. $s = implode( "\n", $dir );
  61. $dir = strip_tags( $s );
  62. return explode( "\n", $dir );
  63. }
  64. else
  65. {
  66. exec( "links -dump $url 2>/dev/null", $lines );
  67. return $lines;
  68. }
  69. }
  70. function max_parent( $dirpath, $prefix )
  71. {
  72. // First, remove a directory
  73. $dirpath = rtrim ( $dirpath, "/" ); // Trim any trailing slash
  74. $position = strrpos( $dirpath, "/" );
  75. $dirpath = substr ( $dirpath, 0, $position );
  76. $lines = http_get_file( $dirpath );
  77. $regex_match = "#${prefix}[\d\.]+/#";
  78. $regex_replace = "#^.*(${prefix}[\d\.]+)/.*$#";
  79. $max = find_max( $lines, $regex_match, $regex_replace );
  80. return "$dirpath/$max";
  81. }
  82. function get_packages( $package, $dirpath )
  83. {
  84. global $exceptions;
  85. global $regex;
  86. //if ( $package != "zstd" ) return 0; // Debug
  87. if ( $package == "bc" ) $dirpath = "https://github.com/gavinhoward/bc/releases";
  88. if ( $package == "check" ) $dirpath = "https://github.com/libcheck/check/releases";
  89. if ( $package == "e2fsprogs" ) $dirpath = "http://sourceforge.net/projects/e2fsprogs/files/e2fsprogs";
  90. if ( $package == "expat" ) $dirpath = "http://sourceforge.net/projects/expat/files";
  91. if ( $package == "elfutils" ) $dirpath = "https://sourceware.org/ftp/elfutils";
  92. if ( $package == "expect" ) $dirpath = "http://sourceforge.net/projects/expect/files";
  93. if ( $package == "file" ) $dirpath = "https://github.com/file/file/releases";
  94. if ( $package == "flex" ) $dirpath = "https://github.com/westes/flex/releases";
  95. if ( $package == "gcc" ) $dirpath = max_parent( $dirpath, "gcc-" );
  96. if ( $package == "intltool" ) $dirpath = "https://launchpad.net/intltool/trunk";
  97. if ( $package == "meson" ) $dirpath = "https://github.com/mesonbuild/meson/releases";
  98. if ( $package == "mpc" ) $dirpath = "https://ftp.gnu.org/gnu/mpc";
  99. if ( $package == "mpfr" ) $dirpath = "http://mpfr.loria.fr/mpfr-current";
  100. if ( $package == "ninja" ) $dirpath = "https://github.com/ninja-build/ninja/releases";
  101. //if ( $package == "procps-ng" ) $dirpath = "http://sourceforge.net/projects/procps-ng/files";
  102. if ( $package == "procps-ng" ) $dirpath = "https://gitlab.com/procps-ng/procps/-/tags";
  103. //if ( $package == "psmisc" ) $dirpath = "http://sourceforge.net/projects/$package/files";
  104. if ( $package == "psmisc" ) $dirpath = "https://gitlab.com/psmisc/psmisc/-/tags";
  105. if ( $package == "shadow" ) $dirpath = "https://github.com/shadow-maint/shadow/releases";
  106. if ( $package == "systemd" ) $dirpath = "https://github.com/systemd/systemd/releases";
  107. if ( $package == "tcl" ) $dirpath = "http://sourceforge.net/projects/tcl/files";
  108. if ( $package == "util-linux" ) $dirpath = max_parent( $dirpath, "v." );
  109. if ( $package == "vim" ) $dirpath = "https://github.com/vim/vim/releases";
  110. if ( $package == "zstd" ) $dirpath = "https://github.com/facebook/zstd/releases";
  111. //if ( $package == "vim" ) $dirpath = "ftp://ftp.vim.org/pub/vim/unix";
  112. // Check for ftp
  113. if ( preg_match( "/^ftp/", $dirpath ) )
  114. {
  115. $dirpath = substr( $dirpath, 6 ); // Remove ftp://
  116. $dirpath = rtrim ( $dirpath, "/" ); // Trim any trailing slash
  117. $position = strpos( $dirpath, "/" ); // Divide at first slash
  118. $server = substr( $dirpath, 0, $position );
  119. $path = substr( $dirpath, $position );
  120. $conn = ftp_connect( $server );
  121. ftp_login( $conn, "anonymous", "" );
  122. // See if we need special handling
  123. if ( isset( $exceptions[ $package ] ) )
  124. {
  125. $specials = explode( ":", $exceptions[ $package ] );
  126. foreach ( $specials as $i )
  127. {
  128. list( $op, $regexp ) = explode( "=", $i );
  129. switch ($op)
  130. {
  131. case "UPDIR":
  132. // Remove last dir from $path
  133. $position = strrpos( $path, "/" );
  134. $path = substr( $path, 0, $position );
  135. // Get dir listing
  136. $lines = ftp_rawlist ($conn, $path);
  137. $max = find_max( $lines, $regexp, $regexp );
  138. break;
  139. case "DOWNDIR":
  140. // Append found directory
  141. $path .= "/$max";
  142. break;
  143. default:
  144. echo "Error in specials array for $package\n";
  145. return -5;
  146. break;
  147. }
  148. }
  149. }
  150. $lines = ftp_rawlist ($conn, $path);
  151. ftp_close( $conn );
  152. }
  153. else // http
  154. {
  155. // Customize http directories as needed
  156. if ( $package == "tzdata" )
  157. {
  158. // Remove two directories
  159. $dirpath = rtrim ( $dirpath, "/" ); // Trim any trailing slash
  160. $position = strrpos( $dirpath, "/" );
  161. $dirpath = substr ( $dirpath, 0, $position );
  162. $position = strrpos( $dirpath, "/" );
  163. $dirpath = substr ( $dirpath, 0, $position );
  164. }
  165. //if ( $package == "bzip2" )
  166. //{
  167. // // Remove one directory
  168. // $dirpath = rtrim ( $dirpath, "/" ); // Trim any trailing slash
  169. // $position = strrpos( $dirpath, "/" );
  170. // $dirpath = substr ( $dirpath, 0, $position );
  171. //}
  172. $lines = http_get_file( $dirpath );
  173. if ( ! is_array( $lines ) ) return -6;
  174. } // End fetch
  175. //print_r($lines);
  176. if ( isset( $regex[ $package ] ) )
  177. {
  178. // Custom search for latest package name
  179. foreach ( $lines as $l )
  180. {
  181. $ver = preg_replace( $regex[ $package ], "$1", $l );
  182. if ( $ver == $l ) continue;
  183. return $ver; // Return first match of regex
  184. }
  185. return -7; // This is an error
  186. }
  187. if ( $package == "perl" ) // Custom for perl
  188. {
  189. $tmp = array();
  190. foreach ( $lines as $l )
  191. {
  192. if ( preg_match( "/sperl/", $l ) ) continue; // Don't want this
  193. $ver = preg_replace( "/^.*perl-([\d\.]+\d)\.tar.*$/", "$1", $l );
  194. if ( $ver == $l ) continue;
  195. list( $s1, $s2, $rest ) = explode( ".", $ver );
  196. if ( $s2 % 2 == 1 ) continue; // Remove odd minor versions
  197. array_push( $tmp, $l );
  198. }
  199. $lines = $tmp;
  200. }
  201. if ( $package == "attr" ||
  202. $package == "acl" )
  203. {
  204. return find_max( $lines, "/$package/", "/^.*$package-([\d\.-]*\d).tar.*$/" );
  205. }
  206. if ( $package == "e2fsprogs" )
  207. return find_max( $lines, "/v\d/", "/^.*v(\d[\d\.]+\d).*$/" );
  208. if ( $package == "expect" )
  209. return find_max( $lines, "/expect/", "/^.*expect(\d[\d\.]+\d).tar.*$/" );
  210. if ( $package == "elfutils" )
  211. return find_max( $lines, "/^\d/", "/^(\d[\d\.]+\d)\/.*$/" );
  212. if ( $package == "XML-Parser" )
  213. {
  214. $max = find_max( $lines, "/$package/", "/^.*$package-([\d\._]*\d).tar.*$/" );
  215. # 2.44_01 is a developer release
  216. if ( $max == "2.44_01" ) { return "2.44"; }
  217. return $max;
  218. }
  219. if ( $package == "tcl" )
  220. return find_max( $lines, "/tcl/", "/^.*tcl(\d[\d\.]*\d)-src.*$/" );
  221. if ( $package == "ninja" )
  222. return find_max( $lines, "/v\d/", "/^.*v(\d[\d\.]*\d).*$/" );
  223. if ( $package == "gmp" )
  224. return find_max( $lines, "/$package/", "/^.*$package-([\d\._]*\d[a-z]?).tar.*$/" );
  225. if ( $package == "dbus" )
  226. return find_even_max( $lines, "/$package/", "/^.*$package-([\d\.]+).tar.*$/" );
  227. if ( $package == "file" )
  228. {
  229. $max = find_max( $lines, "/FILE5/", "/^.*FILE(5_\d+)*$/" );
  230. return str_replace( "_", ".", $max );
  231. }
  232. if ( $package == "procps-ng" )
  233. return find_max( $lines, "/release/", "/^.* ([\d\.]+).*$/" );
  234. if ( $package == "psmisc" )
  235. return find_max( $lines, "/^v/", "/^v([\d\.]+).*$/" );
  236. if ( $package == "grub" )
  237. return find_max( $lines, "/grub/", "/^.*grub-(\d\..*).tar.xz.*$/" );
  238. if ( $package == "openssl" )
  239. return find_max( $lines, "/openssl/", "/^.*openssl-([\d\.p]*\d.?).tar.*$/" );
  240. if ( $package == "vim" )
  241. return find_max( $lines, "/v\d\./", "/^.*v([\d\.]+).*$/" );
  242. if ( $package == "zstd" )
  243. return find_max( $lines, "/Zstandard v/", "/^.*v([\d\.]+).*$/" );
  244. // Most packages are in the form $package-n.n.n
  245. // Occasionally there are dashes (e.g. 201-1)
  246. return find_max( $lines, "/$package/", "/^.*$package-([\d\.-]*\d)\.tar.*$/" );
  247. }
  248. function get_current()
  249. {
  250. global $dirs;
  251. global $vers;
  252. // Fetech from svn and get wget-list
  253. $current = array();
  254. $lfssvn = "svn://svn.linuxfromscratch.org/LFS/trunk";
  255. $tmpdir = exec( "mktemp -d /tmp/lfscheck.XXXXXX" );
  256. $cdir = getcwd();
  257. chdir( $tmpdir );
  258. exec ( "svn --quiet export $lfssvn LFS" );
  259. chdir( $cdir );
  260. $PAGE = "$tmpdir/LFS/BOOK/chapter03/chapter03.xml";
  261. $STYLESHEET = "$tmpdir/LFS/BOOK/stylesheets/wget-list.xsl";
  262. exec( "xsltproc --xinclude --nonet $STYLESHEET $PAGE", $current );
  263. exec( "rm -rf $tmpdir" );
  264. foreach ( $current as $line )
  265. {
  266. $file = basename( $line ) . "\n";
  267. if ( preg_match( "/patch$/", $file ) ) { continue; } // Skip patches
  268. $file = preg_replace( "/bz2/", '', $file ); // The 2 confusses the regex
  269. $file = rtrim( $file );
  270. $pkg_pattern = "/(\D*).*/";
  271. //$pattern = "/\D*(\d.*\d)\D*/";
  272. $pattern = "/\D*(\d.*\d)\D*/";
  273. if ( preg_match( "/e2fsprogs/", $file ) )
  274. {
  275. $pattern = "/e2\D*(\d.*\d)\D*/";
  276. $pkg_pattern = "/(e2\D*).*/";
  277. }
  278. else if ( preg_match( "/tzdata/", $file ) )
  279. {
  280. $pattern = "/\D*(\d.*[a-z])\.tar\D*/";
  281. }
  282. else if ( preg_match( "/openssl/", $file ) )
  283. {
  284. $pattern = "/\D*(\d.*\d.*).tar.*$/";
  285. }
  286. else if ( preg_match( "/gmp/", $file ) )
  287. {
  288. $pattern = "/\D*(\d.*[a-z]*)\.tar\D*/";
  289. }
  290. else if ( preg_match( "/systemd-man-pages/", $file ) ) continue;
  291. else if ( preg_match( "/python/" , $file ) ) continue;
  292. $version = preg_replace( $pattern, "$1", $file ); // Isolate version
  293. $version = preg_replace( "/^\d-/", "", $version ); // Remove leading #-
  294. // Touch up package names
  295. $pkg_name = preg_replace( $pkg_pattern, "$1", $file );
  296. $pkg_name = trim( $pkg_name, "-" );
  297. if ( preg_match( "/bzip|iproute/", $pkg_name ) ) { $pkg_name .= "2"; }
  298. if ( preg_match( "/^m$/" , $pkg_name ) ) { $pkg_name .= "4"; }
  299. if ( preg_match( "/shadow/" , $pkg_name ) ) { $pkg_name = "shadow"; }
  300. $dirs[ $pkg_name ] = dirname( $line );
  301. $vers[ $pkg_name ] = $version;
  302. }
  303. }
  304. function mail_to_lfs()
  305. {
  306. global $date;
  307. global $vers;
  308. global $dirs;
  309. //$to = "bruce.dubbs@gmail.com";
  310. $to = "lfs-book@lists.linuxfromscratch.org";
  311. $from = "bdubbs@linuxfromscratch.org";
  312. $subject = "LFS Package Currency Check - $date GMT";
  313. $headers = "From: bdubbs@anduin.linuxfromscratch.org";
  314. $message = "Package LFS Upstream Flag\n\n";
  315. foreach ( $dirs as $pkg => $dir )
  316. {
  317. //if ( $pkg != "gmp" ) continue; //debug
  318. $v = get_packages( $pkg, $dir );
  319. $flag = ( $vers[ $pkg ] != $v ) ? "*" : "";
  320. // Pad for output
  321. $pad = " ";
  322. $p = substr( $pkg . $pad, 0, 15 );
  323. $l = substr( $vers[ $pkg ] . $pad, 0, 10 );
  324. $c = substr( $v . $pad, 0, 10 );
  325. $message .= "$p $l $c $flag\n";
  326. }
  327. exec ( "echo '$message' | mailx -r $from -s '$subject' $to" );
  328. //echo $message;
  329. }
  330. function html()
  331. {
  332. global $date;
  333. global $vers;
  334. global $dirs;
  335. echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
  336. 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  337. <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  338. <head>
  339. <title>LFS Package Currency Check - $date</title>
  340. <style type='text/css'>
  341. h1, h2 {
  342. text-align : center;
  343. }
  344. table {
  345. border-width : 1px;
  346. border-spacing : 0px;
  347. border-style : outset;
  348. border-color : gray;
  349. border-collapse : separate;
  350. background-color: white;
  351. margin : 0px auto;
  352. }
  353. table th {
  354. border-width : 1px;
  355. padding : 2px;
  356. border-style : inset;
  357. border-color : gray;
  358. background-color: white;
  359. }
  360. table td {
  361. border-width : 1px;
  362. padding : 2px;
  363. border-style : inset;
  364. border-color : gray;
  365. background-color: white;
  366. }
  367. </style>
  368. </head>
  369. <body>
  370. <h1>LFS Package Currency Check</h1>
  371. <h2>As of $date GMT</h1>
  372. <table>
  373. <tr><th>LFS Package</th> <th>LFS Version</th> <th>Latest</th> <th>Flag</th></tr>\n";
  374. // Get the latest version of each package
  375. foreach ( $dirs as $pkg => $dir )
  376. {
  377. $v = get_packages( $pkg, $dir );
  378. $flag = ( $vers[ $pkg ] != $v ) ? "*" : "";
  379. echo "<tr><td>$pkg</td> <td>${vers[ $pkg ]}</td> <td>$v</td> <td>$flag</td></tr>\n";
  380. }
  381. echo "</table>
  382. </body>
  383. </html>\n";
  384. }
  385. get_current(); // Get what is in the book
  386. mail_to_lfs();
  387. //html(); // Write html output
  388. ?>