autoupdate.pl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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-indexed=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 pkg_outdated in a log file
  253. if (defined $logdir && not defined $input) {
  254. open($out, '>', "$logdir/pkg_outdated");
  255. print STDERR "Warning: Unable to open log for pkg_outdated\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/pkg_outdated\" ";
  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 pkg_outdated
  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 if $version;
  448. }
  449. my $highest
  450. = reduce { get_higher_version($a, $b) <= 0 ? $a : $b } @versions;
  451. my $idx = 1;
  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 build';
  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 = '';
  512. $cmd .= $sudo if ($sudo_make_clean);
  513. $cmd .= ' env FORCE_UPDATE=Yes CLEANDEPENDS=No ';
  514. $cmd .= "FLAVOR=\"$info->{flavor}\" "
  515. if ($info->{flavor} ne '');
  516. $cmd .= "SUBPACKAGE=$info->{subpkg} "
  517. if ($info->{subpkg} ne '');
  518. $cmd .= ' make repackage update clean';
  519. open($in, "$cmd 2>&1 |")
  520. or die "Unable to update \"$port\"\n";
  521. while (<$in>) {
  522. print STDOUT $_
  523. unless (($verbose < 2) || ($num_jobs > 1));
  524. print $logfile $_ if (defined $logfile);
  525. push @log, ($_);
  526. }
  527. close($in);
  528. $update_result = $?;
  529. }
  530. close($logfile) if (defined $logfile);
  531. if ($build_result != 0 || $update_result != 0) {
  532. # Something went wrong. We list the output of the make process
  533. # and kill the update process
  534. if (!defined $logdir) {
  535. foreach my $line (@log) {
  536. print STDOUT $line;
  537. }
  538. }
  539. else {
  540. print STDERR "Failed: See log in '$logfilename'\n";
  541. }
  542. print STDERR "Aborting build process...\n";
  543. exit 3;
  544. }
  545. chdir $cur_dir;
  546. exit 0;
  547. }
  548. sub set_parallel_build {
  549. my $info = shift;
  550. my $cur_dir = getcwd;
  551. my $cmd = 'make show=PARALLEL_BUILD';
  552. my ($port, $port_dir) = find_newer_ports_dir($info);
  553. chdir $port_dir or die "Unable to change to '$port_dir'\n";
  554. my $parallel = 1;
  555. open(my $in, "$cmd |")
  556. or die "Unable to determine parallel build info for \"$port\"\n";
  557. while (<$in>) {
  558. chomp;
  559. $parallel &= ($_ eq 'Yes');
  560. }
  561. close($in);
  562. chdir $cur_dir;
  563. $info->{jobs} = ($parallel) ? $make_jobs : 1;
  564. return $info->{jobs};
  565. }
  566. # Creates the dependencies list for a given port from the port information
  567. # hash
  568. sub create_dependencies_list {
  569. my $info = shift;
  570. my $cur_dir = getcwd;
  571. my @dep_list = ();
  572. # Create the command that will give us the dependencies list
  573. my $cmd = 'env ';
  574. $cmd .= "FLAVOR=\"$info->{flavor}\" " if ($info->{flavor} ne '');
  575. $cmd .= "SUBPACKAGE=$info->{subpkg} " if ($info->{subpkg} ne '');
  576. $cmd .= 'make build-dir-depends';
  577. # chdir into the ports directory
  578. my ($port, $port_dir) = find_newer_ports_dir($info);
  579. chdir $port_dir or die "Unable to change to \"$port_dir\"\n";
  580. open(my $in, "$cmd |")
  581. or die "Unable to get dependencies for \"$port\"\n";
  582. while (<$in>) {
  583. chomp;
  584. push @dep_list, ($_);
  585. }
  586. close($in);
  587. if ($? != 0) {
  588. die "Unable to gather information for $port";
  589. }
  590. # Add the port itself to the list
  591. $port = "$info->{category}/$info->{port}";
  592. $port .= "/$info->{subdir}" if ($info->{subdir} ne '');
  593. $port .= ",$info->{subpkg}" if ($info->{subpkg} ne '');
  594. # Add the flavors in raw form
  595. if ($info->{flavor} ne '') {
  596. $port .= ',';
  597. my $flavs = $info->{flavor};
  598. $flavs =~ s/ /,/g;
  599. $port .= $flavs;
  600. }
  601. #push @dep_list, ("$port $port");
  602. chdir $cur_dir;
  603. return \@dep_list;
  604. }
  605. # Creates a single hash with all dependant packages from the array created
  606. # via create_dependencies_list
  607. sub create_dependencies_hash {
  608. my $dep_list = shift;
  609. my %dep_hash;
  610. foreach my $pkg (@$dep_list) {
  611. my $key = $pkg;
  612. $key =~ s/^.* //g;
  613. $key =~ s/$regex_subpkg$regex_flavor//g;
  614. $dep_hash{$key} = 1;
  615. }
  616. return \%dep_hash;
  617. }
  618. # Returns true if a package with a given name is in the package list array
  619. # The function will ignore flavors and subpackages.
  620. sub is_pkg_in_list {
  621. my ($pkg_name, $pkgs) = @_;
  622. foreach my $p (@$pkgs) {
  623. $p =~ s/$regex_subpkg$regex_flavor//g;
  624. return 1 if ($p eq $pkg_name);
  625. }
  626. return 0;
  627. }
  628. # Returns 1 if one of the dependencies from the arguments pkg info is currently
  629. # build by a child
  630. sub can_pkg_be_build {
  631. my $pkg = shift;
  632. my $pkgs = $pkg->{deppkgs};
  633. my @pkg_list = keys %$pkgs;
  634. foreach my $pid (keys %forked_builds) {
  635. my $build_pkg = $forked_builds{$pid};
  636. return 1 if (is_pkg_in_list($build_pkg, \@pkg_list));
  637. }
  638. return 0;
  639. }
  640. # Prints out the usage information on STDERR
  641. sub usage {
  642. print STDERR "USAGE: autoupdate.pl [options]\n";
  643. print STDERR "OPTIONS:\n"
  644. . " -v|--verbose\t\tSet verbosity level (1..5)\n"
  645. . " -f|--outdated\t\tUse file as output from the "
  646. . "pkg_outdated script\n"
  647. . " -j|--jobs\t\tTry to build packages in parallel\n"
  648. . " \t\t\tIf the filename is '-' the input will be\n"
  649. . " \t\t\tread from stdin\n"
  650. . " -h|--help\t\tShow this help\n";
  651. exit (1);
  652. }
  653. ##############################################################################
  654. $SIG{CHLD} = 'DEFAULT';
  655. # Read the command line params
  656. my $result = GetOptions("v|verbose=i" => \$verbose,
  657. "f|outdated=s" => \$out_of_date,
  658. "j|jobs=i" => \$num_jobs,
  659. "h|help" => \$show_help);
  660. usage() if ($show_help != 0 || ! $result || $num_jobs < 1);
  661. $out_of_date = File::Spec->rel2abs($out_of_date) if (defined($out_of_date));
  662. my $basedir = $FindBin::Bin;
  663. chdir($basedir);
  664. # Fill the hash with the allowed variables
  665. my %valid_vars = ( 'logging', 1,
  666. 'make_jobs', 1,
  667. 'sudo_make_clean', 1,
  668. 'pseudo_flavors', 1 );
  669. # Read the config and set everything up
  670. my $config = read_rc_file(\%valid_vars);
  671. setup_logging($config);
  672. $make_jobs = $config->{make_jobs} if $config->{make_jobs};
  673. setup_lookup_hash($config->{pseudo_flavors}, \%pseudo_flavors,
  674. 'pseudo flavors');
  675. $sudo_make_clean = $config->{sudo_make_clean}
  676. if (defined $config->{sudo_make_clean});
  677. print STDOUT "Reading package list scheduled for update...\n";
  678. my $package_list = read_update_package_list($out_of_date);
  679. print STDOUT "Got " . @$package_list . " packages for update\n";
  680. print STDOUT "Gathering package information...\n";
  681. my $port_info = create_package_information($package_list);
  682. print STDOUT "Merging dependencies...\n";
  683. my @dep_list = ();
  684. my @package_dep_list = ();
  685. # Get the dep list of each port
  686. foreach my $info (@$port_info) {
  687. my $deps = $info->{dependencies};
  688. push @dep_list, @$deps
  689. }
  690. print STDOUT "Create dependencies...\n";
  691. my ($fh, $file, $pipe);
  692. ($fh, $file) = tempfile(UNLINK => 1);
  693. open($pipe, "| tsort -r >$file") or die "ERROR: Unable to spawn tsort\n";
  694. foreach my $dep_entry (@dep_list) {
  695. # Zap the flavors and the subpackages for our dep list
  696. $dep_entry =~ s/$regex_subpkg$regex_flavor//g;
  697. print "$dep_entry\n" unless ($verbose < 5);
  698. print $pipe "$dep_entry\n";
  699. }
  700. close $pipe;
  701. die "ERROR: Internal error" if ($? != 0);
  702. # Read the result
  703. while (<$fh>) {
  704. my $pkg = $_;
  705. chomp $pkg;
  706. push @package_dep_list, ($pkg);
  707. }
  708. close $fh;
  709. print STDOUT "Got " . @package_dep_list . " packages as dependencies\n"
  710. unless ($verbose < 1);
  711. print STDOUT "Removing unneeded packages...\n";
  712. # Remove all packages from @package_dep_list that are not in the @package_list
  713. # array, so that only those packages may be build that are in need.
  714. my @pkg_list = ();
  715. foreach my $pkg_name (@package_dep_list) {
  716. if (is_pkg_in_list($pkg_name, $package_list) == 1) {
  717. push @pkg_list, ($pkg_name);
  718. }
  719. }
  720. if ($verbose > 1) {
  721. print STDOUT "Got " . @pkg_list . " packages for building:\n";
  722. foreach my $pkg_name (@pkg_list) {
  723. print STDOUT $pkg_name . "\n";
  724. }
  725. }
  726. $SIG{CHLD} = \&REAPER;
  727. # Build all the packages. Packages that are still unknown to us are ignored.
  728. foreach my $pkg_name (@pkg_list) {
  729. reap();
  730. # Get the package information from the info array
  731. my $info = get_pkg_info($port_info, $pkg_name);
  732. if (not defined $info) {
  733. print STDERR "Unable to retrieve package information for "
  734. . $pkg_name . "\n";
  735. next;
  736. }
  737. my $wait_for_dep_notice = 0;
  738. # XXX Here we must check if the package depends on a currently build
  739. # package too.
  740. while (($jobs >= $num_jobs || can_pkg_be_build($info)) && !$abort) {
  741. if ($wait_for_dep_notice == 0 && !can_pkg_be_build($info)) {
  742. print STDOUT "Waiting for dependant package to finish "
  743. . "building ($pkg_name)\n"
  744. unless ($verbose < 2);
  745. $wait_for_dep_notice++;
  746. }
  747. reap();
  748. sleep 1;
  749. }
  750. last if ($abort != 0);
  751. build_pkg($info);
  752. }
  753. # Wait for all childs to finish
  754. while (scalar (keys %forked_builds)) {
  755. sleep 1;
  756. reap();
  757. }
  758. $SIG{CHLD} = 'DEFAULT';
  759. # Close the pipe to our log file if we were logging
  760. if (defined $config->{logging} && $config->{logging} != 0) {
  761. close STDOUT;
  762. close STDERR;
  763. close TEE;
  764. }
  765. die 'Abort requested by child' if ($abort != 0);
  766. print STDOUT "Done.\n";
  767. exit 0;