lfs-latest.php 15 KB

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