autoupdate.pl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. #! /usr/bin/perl -w
  2. # $Id: autoupdate.pl,v 1.36 2014/09/07 07:35:06 markus Exp $
  3. # Copyright (c) 2007,2008,2009
  4. # Markus Hennecke <markus-hennecke@markus-hennecke.de>
  5. #
  6. # Permission to use, copy, modify, and/or distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. use strict;
  18. use warnings;
  19. use Cwd;
  20. use File::Temp qw/tempfile tempdir/;
  21. use List::Util qw/reduce/;
  22. use OpenBSD::PackageName;
  23. use Getopt::Long;
  24. use FindBin;
  25. use File::Spec;
  26. use POSIX qw/uname :sys_wait_h/;
  27. # Silence the warning that is issued because List::Util won't register
  28. # $a and $b in a correct way for us
  29. $a = $a;
  30. $b = $b;
  31. # Defaults to 1, can be set via the --verbose switch
  32. my $verbose = 1;
  33. # If set logs of the build are written under this directory
  34. my $logdir = undef;
  35. # If set it will show the usage information and exit afterwards
  36. my $show_help = 0;
  37. # If set to more than one it will try to build ports in parallel
  38. my $num_jobs = 1;
  39. # If set to 1 the make clean part of the ports build will use sudo
  40. my $sudo_make_clean = 0;
  41. my $sudo = $ENV{SUDO};
  42. $sudo = `make -f /etc/mk.conf -V SUDO` if (not defined $sudo);
  43. $sudo = '' if (not defined $sudo);
  44. chomp $sudo;
  45. # If defined via command line the script will use that file as input for
  46. # out of date ports. If the filename is '-' it will use stdin.
  47. my $out_of_date = undef;
  48. # The default location of the ports tree and the path where we should look
  49. # for ports. TODO: Read these variables from /etc/mk.conf and fall back
  50. # to the default only if we do not find it either there or in the environment.
  51. my $portsdir = $ENV{PORTSDIR} || '/usr/ports';
  52. my $portsdir_path = $ENV{PORTSDIR_PATH} || "$portsdir:$portsdir/mystuff";
  53. # Number of jobs currently active
  54. my $jobs = 0;
  55. # Number of concurrent jobs in a build
  56. my $make_jobs = 1;
  57. # This hash will hold the PIDs of the forked build processes
  58. my %forked_builds = ();
  59. # Regex used to match a flavor
  60. my $regex_flavor = '((,[a-z][a-z0-9_]*)*)';
  61. # Regex used to match a subpackage
  62. my $regex_subpkg = '(,-[a-z][a-z0-9_+-]*)?';
  63. # List of pseudo flavors we apply if the port supports them
  64. my %pseudo_flavors = ();
  65. # Global abort flag, only set from the reaper func
  66. my $abort = 0;
  67. # Ports that failed to build
  68. my @aborted = ();
  69. # Hash of reaped child processes
  70. my %reaped_pids = ();
  71. # This function will remove the finished forked build from the list of
  72. # currently build ports.
  73. sub REAPER {
  74. while ((my $wpid = waitpid(-1, &WNOHANG)) > 0) {
  75. my $status = $?;
  76. $abort = 1 if ($status != 0);
  77. $reaped_pids{$wpid} = $status;
  78. }
  79. $SIG{CHLD} = \&REAPER;
  80. }
  81. # Remove the status codes/PIDs from the reaped_pids hash periodically
  82. sub reap {
  83. my @pids = keys %reaped_pids;
  84. while (scalar @pids) {
  85. my $wpid = shift @pids;
  86. if (exists $forked_builds{$wpid}) {
  87. my $portname = $forked_builds{$wpid};
  88. $jobs--;
  89. my $status = $reaped_pids{$wpid};
  90. if ($status == 0) {
  91. print STDOUT 'Finished build of ';
  92. }
  93. else {
  94. print STDOUT 'Build aborted of ';
  95. push @aborted, ($portname);
  96. }
  97. delete $forked_builds{$wpid};
  98. print STDOUT $portname . "\n";
  99. }
  100. else {
  101. print STDOUT 'No forked process recorded for '
  102. . $wpid . "\n";
  103. }
  104. delete $reaped_pids{$wpid};
  105. }
  106. }
  107. # Reads a config file and parses the options set in the config file.
  108. # A list of valid options must be passed to the function.
  109. sub read_rc_file {
  110. my $valid_vars = shift;
  111. my ($fh, @lines, %config);
  112. open($fh, '<', "$ENV{HOME}/.autoupdaterc")
  113. or return \%config;
  114. while (<$fh>) {
  115. chomp;
  116. s/\t/ /g;
  117. push @lines, ($_);
  118. }
  119. close($fh);
  120. my $temp_line = '';
  121. my $append = 0;
  122. my $real_lineno = 0;
  123. my $lineno = 0;
  124. foreach (@lines) {
  125. $lineno++;
  126. # Check if we have a multiline statement
  127. if (m/\\$/) {
  128. $append++;
  129. s/\\$//g;
  130. $temp_line .= $_;
  131. next;
  132. }
  133. elsif ($append != 0) {
  134. $_ = $temp_line . $_;
  135. $real_lineno = $lineno - $append;
  136. $append = 0;
  137. $temp_line = '';
  138. }
  139. # Remove comments and unnecessary whitespace
  140. s/\#.*$//g;
  141. s/^[[:space:]]//g;
  142. s/ *= */=/g;
  143. s/ +/ /g;
  144. next if (m/^$/);
  145. # Check if the line has a = character, if not issue an error
  146. if (! m/=/) {
  147. print STDERR 'Not a valid config in line '
  148. . $real_lineno . "\n";
  149. return undef;
  150. }
  151. # Split the line at the first '='
  152. my $split_index = index($_, '=');
  153. my $var = substr($_, 0, $split_index);
  154. my $val = substr($_, $split_index + 1);
  155. # Check if the varible is valid
  156. if (defined $valid_vars && defined $valid_vars->{$var}) {
  157. $config{$var} = $val;
  158. }
  159. else {
  160. print STDERR 'Warning: Unknown variable "' . $var
  161. . '" in config file' . "\n";
  162. }
  163. }
  164. return \%config;
  165. }
  166. # Fill a lookup hash from config
  167. sub setup_lookup_hash {
  168. my $config_line = shift;
  169. my $hash = shift;
  170. my $info = shift;
  171. if (defined $config_line) {
  172. $config_line =~ tr/,/ /;
  173. foreach (split / /, $config_line) {
  174. print STDOUT "Adding $_ to $info\n"
  175. unless ($verbose < 2);
  176. $hash->{$_} = 1;
  177. }
  178. }
  179. }
  180. # Sets up the log directory
  181. sub setup_logging {
  182. my $config = shift;
  183. if (defined $config->{logging} && $config->{logging} == 1) {
  184. my $tmpdir = $ENV{TMPDIR} ? $ENV{TMPDIR} : '/tmp';
  185. $logdir = tempdir("$tmpdir/autoupdate.XXXXXXXXXX");
  186. my $logfile = "$logdir/autoupdate.pl.log";
  187. open TEE, "| tee $logfile"
  188. or die "Unable to open log file \"$logfile\"\n";
  189. open STDOUT, ">&TEE"
  190. or die "Unable to redirect STDOUT to log file.\n";
  191. open STDERR, ">&TEE"
  192. or die "Unable to redirect STDERR to log file.\n";
  193. print STDOUT 'Logging builds in "' . $logdir . '"' . "\n";
  194. print STDOUT 'Using "' . $logfile . '" as mainlog' . "\n";
  195. }
  196. }
  197. # Returns the ports version or undef if no such version could be deduced
  198. sub get_ports_version {
  199. my $port = shift;
  200. chdir "$port" || return undef;
  201. my $cmd = 'make show=FULLPKGNAME';
  202. my $pkgname = '';
  203. open(my $in, "$cmd 2>&1 |")
  204. or die "Unable to get version for \"$port\"\n";
  205. while (<$in>) {
  206. chomp;
  207. $pkgname .= $_;
  208. }
  209. close($in);
  210. my ($stem, $version, @flavors)
  211. = OpenBSD::PackageName::splitname($pkgname);
  212. return $version;
  213. }
  214. # Returns an integer according to the version numbers supplied.
  215. # -1 if the first argument is the higher version number
  216. # 0 if both version numbers are equal
  217. # +1 if the second argument is the higher version number
  218. sub get_higher_version {
  219. my ($ver1, $ver2) = @_;
  220. if (! defined($ver1)) {
  221. return 0 if (! defined($ver2));
  222. return 1;
  223. }
  224. elsif (! defined($ver2)) {
  225. return -1;
  226. }
  227. my @vers1 = split /[\.pv]/,$ver1;
  228. my @vers2 = split /[\.pv]/,$ver2;
  229. my $max_len = scalar @vers1;
  230. $max_len = scalar @vers2 if (scalar @vers2 > scalar @vers1);
  231. my $index;
  232. for ($index = 0; $index < $max_len; $index++) {
  233. return -1 if ($index >= scalar @vers2);
  234. return 1 if ($index >= scalar @vers1);
  235. my $vers1 = $vers1[$index];
  236. my $vers2 = $vers2[$index];
  237. my $cmp;
  238. if (($vers1 =~ /[a-z]/) || ($vers2 =~ /[a-z]/)) {
  239. $cmp = ($vers2 cmp $vers1);
  240. }
  241. else {
  242. $cmp = ($vers2 <=> $vers1);
  243. }
  244. return $cmp if ($cmp);
  245. }
  246. return 0;
  247. }
  248. # Creates an array with the name of all packages that need updates
  249. sub read_update_package_list {
  250. my $input = shift;
  251. my ($in, @package_list, $out);
  252. # Save the output from out-of-date in a log file
  253. if (defined $logdir && not defined $input) {
  254. open($out, '>', "$logdir/out-of-date");
  255. print STDERR "Warning: Unable to open log for out-of-date\n"
  256. if (not defined $out);
  257. }
  258. if (not defined $input) {
  259. my $ood_path = 'infrastructure/';
  260. my @uname = POSIX::uname();
  261. if ($uname[2] >= 5.1) {
  262. $ood_path .= 'bin';
  263. }
  264. else {
  265. $ood_path .= 'build';
  266. }
  267. my $cmd = "env PORTSDIR=\"$portsdir\" "
  268. . "\"$portsdir/$ood_path/out-of-date\" ";
  269. unless (open($in, $cmd . '2>/dev/null |')) {
  270. print STDERR "Unable to execute $cmd\n";
  271. exit 1;
  272. }
  273. }
  274. elsif ($input eq '-') {
  275. unless (open($in, '<&=STDIN')) {
  276. print STDERR "Unable to open stdin\n";
  277. exit 1;
  278. }
  279. }
  280. else {
  281. unless (open($in, '<', $input)) {
  282. print STDERR "Unable to open file: " . $input
  283. . " for reading\n";
  284. exit 1;
  285. }
  286. }
  287. while (<$in>) {
  288. print $out $_ if (defined $out);
  289. chomp;
  290. my $pkg = $_;
  291. $pkg =~ s/ +#.*$//g;
  292. if ($_ =~ m/#\s+->/) {
  293. # Ignore ports that show up every time in out-of-date
  294. print STDERR 'Ignoring ' . $pkg . "\n";
  295. next;
  296. }
  297. push @package_list, ($pkg);
  298. }
  299. close($in);
  300. close($out) if (defined $out);
  301. return \@package_list;
  302. }
  303. # Creates an array with a hash for each port that needs an update
  304. sub create_package_information {
  305. my $pkg_list = shift;
  306. # Create an array of hashes that has the following data:
  307. # category => name of the ports category
  308. # port => name of the port (the directory in the ports tree)
  309. # subdir => version of the port (for ports with more than one
  310. # version, e.q. python)
  311. # flavor => list of flavors as an array reference
  312. # subpackage => name of the subpackage
  313. my @pkg_information = ();
  314. foreach my $pkg (@$pkg_list) {
  315. # Get all the variables from the port identification string
  316. my ($category, $port, $subdir, $flavor, $subpackage);
  317. # Each group in the regex has the following meaning:
  318. # $1 : category main which may consist of the alphabet
  319. # $2 : port name, which must start with at least one alpha
  320. # char and may have a number appended.
  321. # Some examples may be gtk+2, libIDL etc.
  322. # $3 : optional subdirs for the port, $4 will hold the last
  323. # $5 : an optional subpackage name, this will start with a
  324. # comma followed by an hyphen and have an alphanumeric
  325. # identifier
  326. # $6 : a list with optional flavors. Each flavor starts with
  327. # a comma and may be an alphanumeric identifier
  328. if ($pkg =~ m/^
  329. ([a-z][a-z0-9]*)\/
  330. ([A-Za-z][\.A-Za-z0-9-+_]*)
  331. ((\/[a-z0-9._-]*)*)?
  332. $regex_subpkg
  333. $regex_flavor/x) {
  334. $category = $1;
  335. $port = $2;
  336. $subdir = $3;
  337. $subpackage = $5;
  338. $flavor = $6;
  339. $subdir = '' if (not defined $subdir);
  340. $subdir =~ s/(^\/|\/$)//g;
  341. $subpackage = '' if (not defined $subpackage);
  342. $subpackage =~ s/^,//g;
  343. $flavor = '' if (not defined $flavor);
  344. $flavor =~ s/,/ /g;
  345. $flavor =~ s/^ //g;
  346. my %p = (
  347. category => $category,
  348. port => $port,
  349. subdir => $subdir,
  350. subpkg => $subpackage,
  351. flavor => $flavor,
  352. pkg => $pkg,
  353. );
  354. $p{dependencies} = create_dependencies_list(\%p);
  355. $p{jobs} = set_parallel_build(\%p);
  356. $p{deppkgs}
  357. = create_dependencies_hash($p{dependencies});
  358. $p{pseudo_flavors} = create_pseudo_flavors_list(\%p);
  359. add_pseudo_flavors(\%p);
  360. push @pkg_information, (\%p);
  361. }
  362. else {
  363. next if ((not defined $pkg) || ($pkg eq ''));
  364. print STDERR "Unknown port name: $pkg\n";
  365. exit 2;
  366. }
  367. print STDOUT "Category:\t$category\n"
  368. . "Port:\t\t$port\n"
  369. . "Subdir:\t\t$subdir\n"
  370. . "Subpackage:\t$subpackage\n"
  371. . "Flavor:\t\t$flavor\n\n" unless ($verbose < 5);
  372. }
  373. return \@pkg_information;
  374. }
  375. # Adds pseudo flavors to the flavors list if they appear both in the
  376. # pseudo_flavors array from the ports info hash and the %pseudo_flavors
  377. # global hash set via command line.
  378. sub add_pseudo_flavors {
  379. my $info = shift;
  380. my $flavors = $info->{flavor};
  381. foreach my $pseudo_flavor (@{$info->{pseudo_flavors}}) {
  382. if (exists $pseudo_flavors{$pseudo_flavor}) {
  383. $flavors .= ' ';
  384. $flavors .= $pseudo_flavor;
  385. }
  386. }
  387. $info->{flavor} = $flavors;
  388. }
  389. # Creates an array with pseudo flavors. We can check this array against the
  390. # %pseudo_flavors hash to add flavors on demand.
  391. sub create_pseudo_flavors_list {
  392. my $info = shift;
  393. my $cur_dir = getcwd;
  394. my $cmd = 'make show=PSEUDO_FLAVORS';
  395. # chdir into the ports directory
  396. my ($port, $port_dir) = find_newer_ports_dir($info);
  397. chdir $port_dir or die "Unable to change to \"$port_dir\"";
  398. open (my $in, "$cmd |")
  399. or die "Unable to get pseudo flavors for \"$port\"";
  400. my $output;
  401. while (<$in>) {
  402. chomp;
  403. $output .= $_ . ' ';
  404. }
  405. chop $output;
  406. close($in);
  407. if ($? != 0) {
  408. die "Unable to get pseudo flavors for \"$port\"";
  409. }
  410. my @pseudo_flavors = split / /,$output;
  411. return \@pseudo_flavors;
  412. }
  413. # Returns the reference of the package information belonging to a package
  414. # name from the package info array. The sub will die if there is no such
  415. # package listed.
  416. # A package listed more than one time, e.g. several subpackages, will
  417. # always return the first subpackage. It is taken care of the update of the
  418. # other subpackages via the ports Makefile, so we should only need this first
  419. # one.
  420. sub get_pkg_info {
  421. my ($info_array, $pkg_name) = @_;
  422. foreach my $info (@$info_array) {
  423. my $info_pkg_name = $info->{pkg};
  424. $info_pkg_name =~ s/$regex_subpkg$regex_flavor//g;
  425. return $info if ($info_pkg_name eq $pkg_name);
  426. }
  427. die "ERROR: Internal error, package information not consistent.\n";
  428. }
  429. # Tries to find the newest port from directories listed in $PORTSDIR_PATH
  430. # by comparing the output from make show=FULLPKGNAME.
  431. sub find_newer_ports_dir {
  432. my $info = shift;
  433. my $port = $info->{category} . '/' . $info->{port};
  434. $port .= '/' . $info->{subdir} if ($info->{subdir} ne '');
  435. return ($port, $info->{portdir})
  436. if ((defined $info->{portdir}) && ($info->{portdir} ne ''));
  437. my @port_locations = split /:/,$portsdir_path;
  438. # Take a shortcut here if we got only one location
  439. if (scalar @port_locations == 1) {
  440. $info->{portdir} = "$portsdir_path/$port";
  441. return ($port, $info->{portdir});
  442. }
  443. my @versions = ();
  444. foreach my $dir (@port_locations) {
  445. my $port_dir = "$dir/$port";
  446. my $version = get_ports_version($port_dir);
  447. push @versions, $version;
  448. }
  449. my $highest
  450. = reduce { get_higher_version($a, $b) <= 0 ? $a : $b } @versions;
  451. my $idx = 0;
  452. $idx = grep
  453. { defined($versions[$_])
  454. && ($versions[$_] eq $highest) ? $_ : -1 } 0..$#versions
  455. if (scalar @versions > 1);
  456. $info->{portdir} = "$port_locations[$idx - 1]/$port";
  457. return ($port, $info->{portdir});
  458. }
  459. # (Re)builds a package and updates it.
  460. # The following targets for make are given: repackage, update, clean
  461. # All output from the build process is logged and printed if the package did
  462. # not build.
  463. sub build_pkg {
  464. my $info = shift;
  465. my $cur_dir = getcwd();
  466. my ($port, $port_dir) = find_newer_ports_dir($info);
  467. my $pid = fork();
  468. if (! defined $pid) {
  469. die "Cannot fork to build $port\n";
  470. }
  471. elsif ($pid) {
  472. $forked_builds{$pid} = $port;
  473. $jobs++;
  474. return;
  475. }
  476. # The child must use the default sig handler for SIGCHLD
  477. $SIG{CHLD} = 'DEFAULT';
  478. # Give the parent time to update the forked_builds hash
  479. sleep 1;
  480. # Create the command that will build the package
  481. my $cmd = 'env ';
  482. $cmd .= 'MAKE_JOBS=' . $info->{jobs} . ' ';
  483. $cmd .= "FLAVOR=\"$info->{flavor}\" " if ($info->{flavor} ne '');
  484. $cmd .= "SUBPACKAGE=$info->{subpkg} " if ($info->{subpkg} ne '');
  485. $cmd .= 'make repackage';
  486. # chdir into the ports directory
  487. chdir $port_dir or die "Unable to change to \"$port_dir\"\n";
  488. print STDOUT "Building $info->{pkg}\n";
  489. my (@log, $logfile, $logfilename);
  490. open(my $in, "$cmd 2>&1 |")
  491. or die "Unable to make \"$port\"\n";
  492. if (defined $logdir) {
  493. my $tmp = $info->{port};
  494. $tmp .= "_$info->{subdir}" if ($info->{subdir} ne '');
  495. $tmp =~ s/\//_/g;
  496. $logfilename = "$logdir/$tmp.log";
  497. open($logfile, '>>', $logfilename)
  498. or print STDERR 'Warning: Unable to create log file'
  499. . " for $info->{pkg}\n";
  500. }
  501. while (<$in>) {
  502. exit 1 if (/^Detected loop/);
  503. print STDOUT $_ unless (($verbose < 2) || ($num_jobs > 1));
  504. print $logfile $_ if (defined $logfile);
  505. push @log, ($_);
  506. }
  507. close($in);
  508. my $build_result = $?;
  509. my $update_result = 1;
  510. if ($build_result == 0) {
  511. $cmd = 'env FORCE_UPDATE=Yes CLEANDEPENDS=No ';
  512. $cmd .= "FLAVOR=\"$info->{flavor}\" "
  513. if ($info->{flavor} ne '');
  514. $cmd .= "SUBPACKAGE=$info->{subpkg} "
  515. if ($info->{subpkg} ne '');
  516. $cmd .= $sudo if ($sudo_make_clean);
  517. $cmd .= ' make update clean';
  518. open($in, "$cmd 2>&1 |")
  519. or die "Unable to update \"$port\"\n";
  520. while (<$in>) {
  521. print STDOUT $_
  522. unless (($verbose < 2) || ($num_jobs > 1));
  523. print $logfile $_ if (defined $logfile);
  524. push @log, ($_);
  525. }
  526. close($in);
  527. $update_result = $?;
  528. }
  529. close($logfile) if (defined $logfile);
  530. if ($build_result != 0 || $update_result != 0) {
  531. # Something went wrong. We list the output of the make process
  532. # and kill the update process
  533. if (!defined $logdir) {
  534. foreach my $line (@log) {
  535. print STDOUT $line;
  536. }
  537. }
  538. else {
  539. print STDERR "Failed: See log in '$logfilename'\n";
  540. }
  541. print STDERR "Aborting build process...\n";
  542. exit 3;
  543. }
  544. chdir $cur_dir;
  545. exit 0;
  546. }
  547. sub set_parallel_build {
  548. my $info = shift;
  549. my $cur_dir = getcwd;
  550. my $cmd = 'make show=PARALLEL_BUILD';
  551. my ($port, $port_dir) = find_newer_ports_dir($info);
  552. chdir $port_dir or die "Unable to change to '$port_dir'\n";
  553. my $parallel = 1;
  554. open(my $in, "$cmd |")
  555. or die "Unable to determine parallel build info for \"$port\"\n";
  556. while (<$in>) {
  557. chomp;
  558. $parallel &= ($_ eq 'Yes');
  559. }
  560. close($in);
  561. chdir $cur_dir;
  562. $info->{jobs} = ($parallel) ? $make_jobs : 1;
  563. return $info->{jobs};
  564. }
  565. # Creates the dependencies list for a given port from the port information
  566. # hash
  567. sub create_dependencies_list {
  568. my $info = shift;
  569. my $cur_dir = getcwd;
  570. my @dep_list = ();
  571. # Create the command that will give us the dependencies list
  572. my $cmd = 'env ';
  573. $cmd .= "FLAVOR=\"$info->{flavor}\" " if ($info->{flavor} ne '');
  574. $cmd .= "SUBPACKAGE=$info->{subpkg} " if ($info->{subpkg} ne '');
  575. $cmd .= 'make build-dir-depends';
  576. # chdir into the ports directory
  577. my ($port, $port_dir) = find_newer_ports_dir($info);
  578. chdir $port_dir or die "Unable to change to \"$port_dir\"\n";
  579. open(my $in, "$cmd |")
  580. or die "Unable to get dependencies for \"$port\"\n";
  581. while (<$in>) {
  582. chomp;
  583. push @dep_list, ($_);
  584. }
  585. close($in);
  586. if ($? != 0) {
  587. die "Unable to gather information for " . $port;
  588. }
  589. # Add the port itself to the list
  590. $port = "$info->{category}/$info->{port}";
  591. $port .= "/$info->{subdir}" if ($info->{subdir} ne '');
  592. $port .= ",$info->{subpkg}" if ($info->{subpkg} ne '');
  593. # Add the flavors in raw form
  594. if ($info->{flavor} ne '') {
  595. $port .= ',';
  596. my $flavs = $info->{flavor};
  597. $flavs =~ s/ /,/g;
  598. $port .= $flavs;
  599. }
  600. #push @dep_list, ("$port $port");
  601. chdir $cur_dir;
  602. return \@dep_list;
  603. }
  604. # Creates a single hash with all dependant packages from the array created
  605. # via create_dependencies_list
  606. sub create_dependencies_hash {
  607. my $dep_list = shift;
  608. my %dep_hash;
  609. foreach my $pkg (@$dep_list) {
  610. my $key = $pkg;
  611. $key =~ s/^.* //g;
  612. $key =~ s/$regex_subpkg$regex_flavor//g;
  613. $dep_hash{$key} = 1;
  614. }
  615. return \%dep_hash;
  616. }
  617. # Returns true if a package with a given name is in the package list array
  618. # The function will ignore flavors and subpackages.
  619. sub is_pkg_in_list {
  620. my ($pkg_name, $pkgs) = @_;
  621. foreach my $p (@$pkgs) {
  622. $p =~ s/$regex_subpkg$regex_flavor//g;
  623. return 1 if ($p eq $pkg_name);
  624. }
  625. return 0;
  626. }
  627. # Returns 1 if one of the dependencies from the arguments pkg info is currently
  628. # build by a child
  629. sub can_pkg_be_build {
  630. my $pkg = shift;
  631. my $pkgs = $pkg->{deppkgs};
  632. my @pkg_list = keys %$pkgs;
  633. foreach my $pid (keys %forked_builds) {
  634. my $build_pkg = $forked_builds{$pid};
  635. return 1 if (is_pkg_in_list($build_pkg, \@pkg_list));
  636. }
  637. return 0;
  638. }
  639. # Prints out the usage information on STDERR
  640. sub usage {
  641. print STDERR "USAGE: autoupdate.pl [options]\n";
  642. print STDERR "OPTIONS:\n"
  643. . " -v|--verbose\t\tSet verbosity level (1..5)\n"
  644. . " -f|--outofdate\t\tUse file as output from the out-of-date script\n"
  645. . " -j|--jobs\t\tTry to build packages in parallel\n"
  646. . " \t\t\tIf the filename is '-' the input will be\n"
  647. . " \t\t\tread from stdin\n"
  648. . " -h|--help\t\tShow this help\n";
  649. exit (1);
  650. }
  651. ##############################################################################
  652. $SIG{CHLD} = 'DEFAULT';
  653. # Read the command line params
  654. my $result = GetOptions("v|verbose=i" => \$verbose,
  655. "f|outofdate=s" => \$out_of_date,
  656. "j|jobs=i" => \$num_jobs,
  657. "h|help" => \$show_help);
  658. usage() if ($show_help != 0 || ! $result || $num_jobs < 1);
  659. $out_of_date = File::Spec->rel2abs($out_of_date) if (defined($out_of_date));
  660. my $basedir = $FindBin::Bin;
  661. chdir($basedir);
  662. # Fill the hash with the allowed variables
  663. my %valid_vars = ( 'logging', 1,
  664. 'make_jobs', 1,
  665. 'sudo_make_clean', 1,
  666. 'pseudo_flavors', 1 );
  667. # Read the config and set everything up
  668. my $config = read_rc_file(\%valid_vars);
  669. setup_logging($config);
  670. $make_jobs = $config->{make_jobs} if $config->{make_jobs};
  671. setup_lookup_hash($config->{pseudo_flavors}, \%pseudo_flavors,
  672. 'pseudo flavors');
  673. $sudo_make_clean = $config->{sudo_make_clean}
  674. if (defined $config->{sudo_make_clean});
  675. print STDOUT "Reading package list scheduled for update...\n";
  676. my $package_list = read_update_package_list($out_of_date);
  677. print STDOUT "Got " . @$package_list . " packages for update\n";
  678. print STDOUT "Gathering package information...\n";
  679. my $port_info = create_package_information($package_list);
  680. print STDOUT "Merging dependencies...\n";
  681. my @dep_list = ();
  682. my @package_dep_list = ();
  683. # Get the dep list of each port
  684. foreach my $info (@$port_info) {
  685. my $deps = $info->{dependencies};
  686. push @dep_list, @$deps
  687. }
  688. print STDOUT "Create dependencies...\n";
  689. my ($fh, $file, $pipe);
  690. ($fh, $file) = tempfile(UNLINK => 1);
  691. open($pipe, "| tsort -r >$file") or die "ERROR: Unable to spawn tsort\n";
  692. foreach my $dep_entry (@dep_list) {
  693. # Zap the flavors and the subpackages for our dep list
  694. $dep_entry =~ s/$regex_subpkg$regex_flavor//g;
  695. print $dep_entry . "\n" unless ($verbose < 5);
  696. print $pipe $dep_entry . "\n";
  697. }
  698. close $pipe;
  699. die "ERROR: Internal error" if ($? != 0);
  700. # Read the result
  701. while (<$fh>) {
  702. my $pkg = $_;
  703. chomp $pkg;
  704. push @package_dep_list, ($pkg);
  705. }
  706. close $fh;
  707. print STDOUT "Got " . @package_dep_list . " packages as dependencies\n"
  708. unless ($verbose < 1);
  709. print STDOUT "Removing unneeded packages...\n";
  710. # Remove all packages from @package_dep_list that are not in the @package_list
  711. # array, so that only those packages may be build that are in need.
  712. my @pkg_list = ();
  713. foreach my $pkg_name (@package_dep_list) {
  714. if (is_pkg_in_list($pkg_name, $package_list) == 1) {
  715. push @pkg_list, ($pkg_name);
  716. }
  717. }
  718. if ($verbose > 1) {
  719. print STDOUT "Got " . @pkg_list . " packages for building:\n";
  720. foreach my $pkg_name (@pkg_list) {
  721. print STDOUT $pkg_name . "\n";
  722. }
  723. }
  724. $SIG{CHLD} = \&REAPER;
  725. # Build all the packages. Packages that are still unknown to us are ignored.
  726. foreach my $pkg_name (@pkg_list) {
  727. reap();
  728. # Get the package information from the info array
  729. my $info = get_pkg_info($port_info, $pkg_name);
  730. if (not defined $info) {
  731. print STDERR "Unable to retrieve package information for "
  732. . $pkg_name . "\n";
  733. next;
  734. }
  735. my $wait_for_dep_notice = 0;
  736. # XXX Here we must check if the package depends on a currently build
  737. # package too.
  738. while (($jobs >= $num_jobs || can_pkg_be_build($info)) && !$abort) {
  739. if ($wait_for_dep_notice == 0 && !can_pkg_be_build($info)) {
  740. print STDOUT "Waiting for dependant package to finish "
  741. . "building (" . $pkg_name . ")\n"
  742. unless ($verbose < 2);
  743. $wait_for_dep_notice++;
  744. }
  745. reap();
  746. sleep 1;
  747. }
  748. last if ($abort != 0);
  749. build_pkg($info);
  750. }
  751. # Wait for all childs to finish
  752. while (scalar (keys %forked_builds)) {
  753. sleep 1;
  754. reap();
  755. }
  756. $SIG{CHLD} = 'DEFAULT';
  757. # Close the pipe to our log file if we were logging
  758. if (defined $config->{logging} && $config->{logging} != 0) {
  759. close STDOUT;
  760. close STDERR;
  761. close TEE;
  762. }
  763. die 'Abort requested by child' if ($abort != 0);
  764. print STDOUT "Done.\n";
  765. exit 0;