lfs-latest.php 13 KB

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