Extract.pm 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. # ------------------------------------------------------------------------------
  2. # NAME
  3. # Fcm::Extract
  4. #
  5. # DESCRIPTION
  6. # This is the top level class for the FCM extract system.
  7. #
  8. # COPYRIGHT
  9. # (C) Crown copyright Met Office. All rights reserved.
  10. # For further details please refer to the file COPYRIGHT.txt
  11. # which you should have received as part of this distribution.
  12. # ------------------------------------------------------------------------------
  13. package Fcm::Extract;
  14. @ISA = qw(Fcm::ConfigSystem);
  15. # Standard pragma
  16. use warnings;
  17. use strict;
  18. # Standard modules
  19. use File::Path;
  20. use File::Spec;
  21. # FCM component modules
  22. use Fcm::CfgFile;
  23. use Fcm::CfgLine;
  24. use Fcm::Config;
  25. use Fcm::ConfigSystem;
  26. use Fcm::Dest;
  27. use Fcm::ExtractFile;
  28. use Fcm::ExtractSrc;
  29. use Fcm::Keyword;
  30. use Fcm::ReposBranch;
  31. use Fcm::SrcDirLayer;
  32. use Fcm::Util;
  33. # List of scalar property methods for this class
  34. my @scalar_properties = (
  35. 'bdeclare', # list of build declarations
  36. 'branches', # list of repository branches
  37. 'conflict', # conflict mode
  38. 'rdest' , # remote destination information
  39. );
  40. # List of hash property methods for this class
  41. my @hash_properties = (
  42. 'srcdirs' , # list of source directory extract info
  43. 'files', # list of files processed key=pkgname, value=Fcm::ExtractFile
  44. );
  45. # ------------------------------------------------------------------------------
  46. # SYNOPSIS
  47. # $obj = Fcm::Extract->new;
  48. #
  49. # DESCRIPTION
  50. # This method constructs a new instance of the Fcm::Extract class.
  51. # ------------------------------------------------------------------------------
  52. sub new {
  53. my $this = shift;
  54. my %args = @_;
  55. my $class = ref $this || $this;
  56. my $self = Fcm::ConfigSystem->new (%args);
  57. $self->{$_} = undef for (@scalar_properties);
  58. $self->{$_} = {} for (@hash_properties);
  59. bless $self, $class;
  60. # List of sub-methods for parse_cfg
  61. push @{ $self->cfg_methods }, (qw/rdest bld conflict project/);
  62. # System type
  63. $self->type ('ext');
  64. return $self;
  65. }
  66. # ------------------------------------------------------------------------------
  67. # SYNOPSIS
  68. # $value = $obj->X;
  69. # $obj->X ($value);
  70. #
  71. # DESCRIPTION
  72. # Details of these properties are explained in @scalar_properties.
  73. # ------------------------------------------------------------------------------
  74. for my $name (@scalar_properties) {
  75. no strict 'refs';
  76. *$name = sub {
  77. my $self = shift;
  78. # Argument specified, set property to specified argument
  79. if (@_) {
  80. $self->{$name} = $_[0];
  81. }
  82. # Default value for property
  83. if (not defined $self->{$name}) {
  84. if ($name eq 'bdeclare' or $name eq 'branches') {
  85. # Reference to an array
  86. $self->{$name} = [];
  87. } elsif ($name eq 'rdest') {
  88. # New extract destination local/remote
  89. $self->{$name} = Fcm::Dest->new (DEST0 => $self->dest(), TYPE => 'ext');
  90. } elsif ($name eq 'conflict') {
  91. # Conflict mode, default to "merge"
  92. $self->{$name} = 'merge';
  93. }
  94. }
  95. return $self->{$name};
  96. }
  97. }
  98. # ------------------------------------------------------------------------------
  99. # SYNOPSIS
  100. # %hash = %{ $obj->X () };
  101. # $obj->X (\%hash);
  102. #
  103. # $value = $obj->X ($index);
  104. # $obj->X ($index, $value);
  105. #
  106. # DESCRIPTION
  107. # Details of these properties are explained in @hash_properties.
  108. #
  109. # If no argument is set, this method returns a hash containing a list of
  110. # objects. If an argument is set and it is a reference to a hash, the objects
  111. # are replaced by the the specified hash.
  112. #
  113. # If a scalar argument is specified, this method returns a reference to an
  114. # object, if the indexed object exists or undef if the indexed object does
  115. # not exist. If a second argument is set, the $index element of the hash will
  116. # be set to the value of the argument.
  117. # ------------------------------------------------------------------------------
  118. for my $name (@hash_properties) {
  119. no strict 'refs';
  120. *$name = sub {
  121. my ($self, $arg1, $arg2) = @_;
  122. # Ensure property is defined as a reference to a hash
  123. $self->{$name} = {} if not defined ($self->{$name});
  124. # Argument 1 can be a reference to a hash or a scalar index
  125. my ($index, %hash);
  126. if (defined $arg1) {
  127. if (ref ($arg1) eq 'HASH') {
  128. %hash = %$arg1;
  129. } else {
  130. $index = $arg1;
  131. }
  132. }
  133. if (defined $index) {
  134. # A scalar index is defined, set and/or return the value of an element
  135. $self->{$name}{$index} = $arg2 if defined $arg2;
  136. return (
  137. exists $self->{$name}{$index} ? $self->{$name}{$index} : undef
  138. );
  139. } else {
  140. # A scalar index is not defined, set and/or return the hash
  141. $self->{$name} = \%hash if defined $arg1;
  142. return $self->{$name};
  143. }
  144. }
  145. }
  146. # ------------------------------------------------------------------------------
  147. # SYNOPSIS
  148. # $rc = $self->check_lock_is_allowed ($lock);
  149. #
  150. # DESCRIPTION
  151. # This method returns true if it is OK for $lock to exist in the destination.
  152. # ------------------------------------------------------------------------------
  153. sub check_lock_is_allowed {
  154. my ($self, $lock) = @_;
  155. # Allow existence of build lock in inherited extract
  156. return ($lock eq $self->dest->bldlock and @{ $self->inherited });
  157. }
  158. # ------------------------------------------------------------------------------
  159. # SYNOPSIS
  160. # $rc = $self->invoke_extract ();
  161. #
  162. # DESCRIPTION
  163. # This method invokes the extract stage of the extract system. It returns
  164. # true on success.
  165. # ------------------------------------------------------------------------------
  166. sub invoke_extract {
  167. my $self = shift;
  168. my $rc = 1;
  169. my @methods = (
  170. 'expand_cfg', # expand URL, revision keywords, relative path, etc
  171. 'create_dir_stack', # analyse the branches to create an extract sequence
  172. 'extract_src', # use the sequence to extract source to destination
  173. 'write_cfg', # generate final configuration file
  174. 'write_cfg_bld', # generate build configuration file
  175. );
  176. for my $method (@methods) {
  177. $rc = $self->$method if $rc;
  178. }
  179. return $rc;
  180. }
  181. # ------------------------------------------------------------------------------
  182. # SYNOPSIS
  183. # $rc = $self->invoke_mirror ();
  184. #
  185. # DESCRIPTION
  186. # This method invokes the mirror stage of the extract system. It returns
  187. # true on success.
  188. # ------------------------------------------------------------------------------
  189. sub invoke_mirror {
  190. my $self = shift;
  191. return $self->rdest->mirror ([qw/bldcfg extcfg srcdir/]);
  192. }
  193. # ------------------------------------------------------------------------------
  194. # SYNOPSIS
  195. # $rc = $self->invoke_system ();
  196. #
  197. # DESCRIPTION
  198. # This method invokes the extract system. It returns true on success.
  199. # ------------------------------------------------------------------------------
  200. sub invoke_system {
  201. my $self = shift;
  202. my $rc = 1;
  203. $rc = $self->invoke_stage ('Extract', 'invoke_extract');
  204. $rc = $self->invoke_stage ('Mirror', 'invoke_mirror')
  205. if $rc and $self->rdest->rootdir;
  206. return $rc;
  207. }
  208. # ------------------------------------------------------------------------------
  209. # SYNOPSIS
  210. # $rc = $self->parse_cfg_rdest(\@cfg_lines);
  211. #
  212. # DESCRIPTION
  213. # This method parses the remote destination settings in the @cfg_lines.
  214. # ------------------------------------------------------------------------------
  215. sub parse_cfg_rdest {
  216. my ($self, $cfg_lines_ref) = @_;
  217. # RDEST declarations
  218. # ----------------------------------------------------------------------------
  219. for my $line (grep {$_->slabel_starts_with_cfg('RDEST')} @{$cfg_lines_ref}) {
  220. my ($d, $method) = map {lc($_)} $line->slabel_fields();
  221. $method ||= 'rootdir';
  222. if ($self->rdest()->can($method)) {
  223. $self->rdest()->$method(expand_tilde($line->value()));
  224. $line->parsed(1);
  225. }
  226. }
  227. # MIRROR declaration, deprecated = RDEST::MIRROR_CMD
  228. # ----------------------------------------------------------------------------
  229. for my $line (grep {$_->slabel_starts_with_cfg('MIRROR')} @{$cfg_lines_ref}) {
  230. $self->rdest()->mirror_cmd($line->value());
  231. $line->parsed(1);
  232. }
  233. return 1;
  234. }
  235. # ------------------------------------------------------------------------------
  236. # SYNOPSIS
  237. # $rc = $self->parse_cfg_bld (\@cfg_lines);
  238. #
  239. # DESCRIPTION
  240. # This method parses the build configurations in the @cfg_lines.
  241. # ------------------------------------------------------------------------------
  242. sub parse_cfg_bld {
  243. my ($self, $cfg_lines) = @_;
  244. # BLD declarations
  245. # ----------------------------------------------------------------------------
  246. for my $line (grep {$_->slabel_starts_with_cfg ('BDECLARE')} @$cfg_lines) {
  247. # Remove BLD from label
  248. my @words = $line->slabel_fields;
  249. # Check that a declaration follows BLD
  250. next if @words <= 1;
  251. push @{ $self->bdeclare }, Fcm::CfgLine->new (
  252. LABEL => join ($Fcm::Config::DELIMITER, @words),
  253. PREFIX => $self->cfglabel ('BDECLARE'),
  254. VALUE => $line->value,
  255. );
  256. $line->parsed (1);
  257. }
  258. return 1;
  259. }
  260. # ------------------------------------------------------------------------------
  261. # SYNOPSIS
  262. # $rc = $self->parse_cfg_conflict (\@cfg_lines);
  263. #
  264. # DESCRIPTION
  265. # This method parses the conflict settings in the @cfg_lines.
  266. # ------------------------------------------------------------------------------
  267. sub parse_cfg_conflict {
  268. my ($self, $cfg_lines) = @_;
  269. # Deprecated: Override mode setting
  270. # ----------------------------------------------------------------------------
  271. for my $line (grep {$_->slabel_starts_with_cfg ('OVERRIDE')} @$cfg_lines) {
  272. next if ($line->slabel_fields) > 1;
  273. $self->conflict ($line->bvalue ? 'override' : 'fail');
  274. $line->parsed (1);
  275. $line->warning($line->slabel . ' is deprecated. Use ' .
  276. $line->cfglabel('CONFLICT') . ' override|merge|fail.');
  277. }
  278. # Conflict mode setting
  279. # ----------------------------------------------------------------------------
  280. my @conflict_modes = qw/fail merge override/;
  281. my $conflict_modes_pattern = join ('|', @conflict_modes);
  282. for my $line (grep {$_->slabel_starts_with_cfg ('CONFLICT')} @$cfg_lines) {
  283. if ($line->value =~ /$conflict_modes_pattern/i) {
  284. $self->conflict (lc ($line->value));
  285. $line->parsed (1);
  286. } elsif ($line->value =~ /^[012]$/) {
  287. $self->conflict ($conflict_modes[$line->value]);
  288. $line->parsed (1);
  289. } else {
  290. $line->error ($line->value, ': invalid value');
  291. }
  292. }
  293. return 1;
  294. }
  295. # ------------------------------------------------------------------------------
  296. # SYNOPSIS
  297. # $rc = $self->parse_cfg_project (\@cfg_lines);
  298. #
  299. # DESCRIPTION
  300. # This method parses the project settings in the @cfg_lines.
  301. # ------------------------------------------------------------------------------
  302. sub parse_cfg_project {
  303. my ($self, $cfg_lines) = @_;
  304. # Flag to indicate that a declared branch revision must match with its changed
  305. # revision
  306. # ----------------------------------------------------------------------------
  307. for my $line (grep {$_->slabel_starts_with_cfg ('REVMATCH')} @$cfg_lines) {
  308. next if ($line->slabel_fields) > 1;
  309. $self->setting ([qw/EXT_REVMATCH/], $line->bvalue);
  310. $line->parsed (1);
  311. }
  312. # Repository, revision and source directories
  313. # ----------------------------------------------------------------------------
  314. for my $name (qw/repos revision dirs expdirs/) {
  315. my @lines = grep {
  316. $_->slabel_starts_with_cfg (uc ($name)) or
  317. $name eq 'revision' and $_->slabel_starts_with_cfg ('VERSION');
  318. } @$cfg_lines;
  319. for my $line (@lines) {
  320. my @names = $line->slabel_fields;
  321. shift @names;
  322. # Detemine package and tag
  323. my $tag = pop @names;
  324. my $pckroot = $names[0];
  325. my $pck = join ($Fcm::Config::DELIMITER, @names);
  326. # Check that $tag and $pckroot are defined
  327. next unless $tag and $pckroot;
  328. # Check if branch already exists.
  329. # If so, set $branch to point to existing branch
  330. my $branch = undef;
  331. for (@{ $self->branches }) {
  332. next unless $_->package eq $pckroot and $_->tag eq $tag;
  333. $branch = $_;
  334. last;
  335. }
  336. # Otherwise, create a new branch
  337. if (not $branch) {
  338. $branch = Fcm::ReposBranch->new (PACKAGE => $pckroot, TAG => $tag,);
  339. push @{ $self->branches }, $branch;
  340. }
  341. if ($name eq 'repos' or $name eq 'revision') {
  342. # Branch location or revision
  343. $branch->$name ($line->value);
  344. } else { # $name eq 'dirs' or $name eq 'expdirs'
  345. # Source directory or expandable source directory
  346. if ($pck eq $pckroot and $line->value !~ m#^/#) {
  347. # Sub-package name not set and source directory quoted as a relative
  348. # path, determine package name from path name
  349. $pck = join (
  350. $Fcm::Config::DELIMITER,
  351. ($pckroot, File::Spec->splitdir ($line->value)),
  352. );
  353. }
  354. # A "/" is equivalent to the top (empty) package
  355. my $value = ($line->value =~ m#^/+$#) ? '' : $line->value;
  356. $branch->$name ($pck, $value);
  357. }
  358. $line->parsed (1);
  359. }
  360. }
  361. return 1;
  362. }
  363. # ------------------------------------------------------------------------------
  364. # SYNOPSIS
  365. # $rc = $obj->expand_cfg ();
  366. #
  367. # DESCRIPTION
  368. # This method expands the settings of the extract configuration.
  369. # ------------------------------------------------------------------------------
  370. sub expand_cfg {
  371. my $self = shift;
  372. my $rc = 1;
  373. for my $use (@{ $self->inherit }) {
  374. $rc = $use->expand_cfg if $rc;
  375. }
  376. return $rc unless $rc;
  377. # Establish a set of source directories from the "base repository"
  378. my %base_branches = ();
  379. # Inherit "base" set of source directories from re-used extracts
  380. for my $use (@{ $self->inherit }) {
  381. my @branches = @{ $use->branches };
  382. for my $branch (@branches) {
  383. my $package = $branch->package;
  384. $base_branches{$package} = $branch unless exists $base_branches{$package};
  385. }
  386. }
  387. for my $branch (@{ $self->branches }) {
  388. # Expand URL keywords if necessary
  389. if ($branch->repos) {
  390. my $repos = Fcm::Util::tidy_url(Fcm::Keyword::expand($branch->repos()));
  391. $branch->repos ($repos) if $repos ne $branch->repos;
  392. }
  393. # Check that repository type and revision are set
  394. if ($branch->repos and &is_url ($branch->repos)) {
  395. $branch->type ('svn') unless $branch->type;
  396. $branch->revision ('head') unless $branch->revision;
  397. } else {
  398. $branch->type ('user') unless $branch->type;
  399. $branch->revision ('user') unless $branch->revision;
  400. }
  401. $rc = $branch->expand_revision if $rc; # Get revision number from keywords
  402. $rc = $branch->expand_path if $rc; # Expand relative path to full path
  403. $rc = $branch->expand_all if $rc; # Search sub-directories
  404. last unless $rc;
  405. my $package = $branch->package;
  406. if (exists $base_branches{$package}) {
  407. # A base branch for this package exists
  408. # If current branch has no source directory, use the set provided by the
  409. # base branch
  410. my %dirs = %{ $branch->dirs };
  411. $branch->add_base_dirs ($base_branches{$package}) unless keys %dirs;
  412. } else {
  413. # This package does not yet have a base branch, set this branch as base
  414. $base_branches{$package} = $branch;
  415. }
  416. }
  417. return $rc;
  418. }
  419. # ------------------------------------------------------------------------------
  420. # SYNOPSIS
  421. # $rc = $obj->create_dir_stack ();
  422. #
  423. # DESCRIPTION
  424. # This method creates a hash of source directories to be processed. If the
  425. # flag INHERITED is set to true, the source directories are assumed processed
  426. # and extracted.
  427. # ------------------------------------------------------------------------------
  428. sub create_dir_stack {
  429. my $self = shift;
  430. my %args = @_;
  431. # Inherit from USE ext cfg
  432. for my $use (@{ $self->inherit }) {
  433. $use->create_dir_stack () or return 0;
  434. my %use_srcdirs = %{ $use->srcdirs };
  435. while (my ($key, $value) = each %use_srcdirs) {
  436. $self->srcdirs ($key, $value);
  437. # Re-set destination to current destination
  438. my @path = split (/$Fcm::Config::DELIMITER/, $key);
  439. $self->srcdirs ($key)->{DEST} = File::Spec->catfile (
  440. $self->dest->srcdir, @path,
  441. );
  442. }
  443. }
  444. # Build stack from current ext cfg
  445. for my $branch (@{ $self->branches }) {
  446. my %branch_dirs = %{ $branch->dirs };
  447. for my $dir (keys %branch_dirs) {
  448. # Check whether source directory is already in the list
  449. if (not $self->srcdirs ($dir)) { # if not, create it
  450. $self->srcdirs ($dir, {
  451. DEST => File::Spec->catfile (
  452. $self->dest->srcdir, split (/$Fcm::Config::DELIMITER/, $dir)
  453. ),
  454. STACK => [],
  455. FILES => {},
  456. });
  457. }
  458. my $stack = $self->srcdirs ($dir)->{STACK}; # copy reference
  459. # Create a new layer in the input stack
  460. my $layer = Fcm::SrcDirLayer->new (
  461. NAME => $dir,
  462. PACKAGE => $branch->package,
  463. TAG => $branch->tag,
  464. LOCATION => $branch->dirs ($dir),
  465. REPOSROOT => $branch->repos,
  466. REVISION => $branch->revision,
  467. TYPE => $branch->type,
  468. EXTRACTED => @{ $self->inherited }
  469. ? $self->srcdirs ($dir)->{DEST} : undef,
  470. );
  471. # Check whether layer is already in the stack
  472. my $exist = grep {
  473. $_->location eq $layer->location and $_->revision eq $layer->revision;
  474. } @{ $stack };
  475. if (not $exist) {
  476. # If not already exist, put layer into stack
  477. # Note: user stack always comes last
  478. if (! $layer->user and exists $stack->[-1] and $stack->[-1]->user) {
  479. my $lastlayer = pop @{ $stack };
  480. push @{ $stack }, $layer;
  481. $layer = $lastlayer;
  482. }
  483. push @{ $stack }, $layer;
  484. } elsif ($layer->user) {
  485. # User layer already exists, overwrite it
  486. $stack->[-1] = $layer;
  487. }
  488. }
  489. }
  490. # Use the cache to sort the source directory layer hash
  491. return $self->compare_setting (METHOD_LIST => ['sort_dir_stack']);
  492. }
  493. # ------------------------------------------------------------------------------
  494. # SYNOPSIS
  495. # ($rc, \@new_lines) = $self->sort_dir_stack ($old_lines);
  496. #
  497. # DESCRIPTION
  498. # This method sorts thesource directories hash to be processed.
  499. # ------------------------------------------------------------------------------
  500. sub sort_dir_stack {
  501. my ($self, $old_lines) = @_;
  502. my $rc = 0;
  503. my %old = ();
  504. if ($old_lines) {
  505. for my $line (@$old_lines) {
  506. $old{$line->label} = $line->value;
  507. }
  508. }
  509. my %new;
  510. # Compare each layer to base layer, discard unnecessary layers
  511. DIR: for my $srcdir (keys %{ $self->srcdirs }) {
  512. my @stack = ();
  513. while (my $layer = shift @{ $self->srcdirs ($srcdir)->{STACK} }) {
  514. if ($layer->user) {
  515. # Local file system branch, check that the declared location exists
  516. if (-d $layer->location) {
  517. # Local file system branch always takes precedence
  518. push @stack, $layer;
  519. } else {
  520. w_report 'ERROR: ', $layer->location, ': declared source directory ',
  521. 'does not exists ';
  522. $rc = undef;
  523. last DIR;
  524. }
  525. } else {
  526. my $key = join ($Fcm::Config::DELIMITER, (
  527. $srcdir, $layer->location, $layer->revision
  528. ));
  529. unless ($layer->extracted and $layer->commit) {
  530. # See if commit revision information is cached
  531. if (keys %old and exists $old{$key}) {
  532. $layer->commit ($old{$key});
  533. } else {
  534. $layer->get_commit;
  535. $rc = 1;
  536. }
  537. # Check source directory for commit revision, if necessary
  538. if (not $layer->commit) {
  539. w_report 'Error: cannot determine the last changed revision of ',
  540. $layer->location;
  541. $rc = undef;
  542. last DIR;
  543. }
  544. # Set cache directory for layer
  545. my $tag_ver = $layer->tag . '__' . $layer->commit;
  546. $layer->cachedir (File::Spec->catfile (
  547. $self->dest->cachedir,
  548. split (/$Fcm::Config::DELIMITER/, $srcdir),
  549. $tag_ver,
  550. ));
  551. }
  552. # New line in cache config file
  553. $new{$key} = $layer->commit;
  554. # Push this layer in the stack:
  555. # 1. it has a different revision compared to the top layer
  556. # 2. it is the top layer (base line code)
  557. if (@stack > 0) {
  558. push @stack, $layer if $layer->commit != $stack[0]->commit;
  559. } else {
  560. push @stack, $layer;
  561. }
  562. }
  563. }
  564. $self->srcdirs ($srcdir)->{STACK} = \@stack;
  565. }
  566. # Write "commit cache" file
  567. my @new_lines;
  568. if (defined ($rc)) {
  569. for my $key (sort keys %new) {
  570. push @new_lines, Fcm::CfgLine->new (label => $key, value => $new{$key});
  571. }
  572. }
  573. return ($rc, \@new_lines);
  574. }
  575. # ------------------------------------------------------------------------------
  576. # SYNOPSIS
  577. # $rc = $self->extract_src ();
  578. #
  579. # DESCRIPTION
  580. # This internal method performs the extract of the source directories and
  581. # files if necessary.
  582. # ------------------------------------------------------------------------------
  583. sub extract_src {
  584. my $self = shift;
  585. my $rc = 1;
  586. # Ensure destinations exist and are directories
  587. for my $srcdir (values %{ $self->srcdirs }) {
  588. last if not $rc;
  589. if (-f $srcdir->{DEST}) {
  590. w_report $srcdir->{DEST},
  591. ': destination exists and is not a directory, abort.';
  592. $rc = 0;
  593. }
  594. }
  595. # Retrieve previous/record current extract configuration for each file.
  596. $rc = $self->compare_setting (
  597. CACHEBASE => $self->setting ('CACHE_FILE_SRC'),
  598. METHOD_LIST => ['compare_setting_srcfiles'],
  599. ) if $rc;
  600. return $rc;
  601. }
  602. # ------------------------------------------------------------------------------
  603. # SYNOPSIS
  604. # ($rc, \@new_lines) = $self->compare_setting_srcfiles ($old_lines);
  605. #
  606. # DESCRIPTION
  607. # For each file to be extracted, this method creates an instance of an
  608. # Fcm::ExtractFile object. It then compares its file's sources to determine
  609. # if they have changed. If so, it will allow the Fcm::ExtractFile to
  610. # "re-extract" the file to the destination. Otherwise, it will set
  611. # Fcm::ExtractFile->dest_status to a null string to denote an "unchanged"
  612. # dest_status.
  613. #
  614. # SEE ALSO
  615. # Fcm::ConfigSystem->compare_setting.
  616. # ------------------------------------------------------------------------------
  617. sub compare_setting_srcfiles {
  618. my ($self, $old_lines) = @_;
  619. my $rc = 1;
  620. # Retrieve previous extract configuration for each file
  621. # ----------------------------------------------------------------------------
  622. my %old = ();
  623. if ($old_lines) {
  624. for my $line (@$old_lines) {
  625. $old{$line->label} = $line->value;
  626. }
  627. }
  628. # Build up a sequence using a Fcm::ExtractFile object for each file
  629. # ----------------------------------------------------------------------------
  630. for my $srcdir (values %{ $self->srcdirs }) {
  631. my %pkgnames0; # (to be) list of package names in the base layer
  632. for my $i (0 .. @{ $srcdir->{STACK} } - 1) {
  633. my $layer = $srcdir->{STACK}->[$i];
  634. # Update the cache for each layer of the stack if necessary
  635. $layer->update_cache unless $layer->extracted or -d $layer->localdir;
  636. # Get list of files in the cache or local directory
  637. my %pkgnames;
  638. for my $file (($layer->get_files)) {
  639. my $pkgname = join (
  640. '/', split (/$Fcm::Config::DELIMITER/, $layer->name), $file
  641. );
  642. $pkgnames0{$pkgname} = 1 if $i == 0; # store package name in base layer
  643. $pkgnames{$pkgname} = 1; # store package name in the current layer
  644. if (not $self->files ($pkgname)) {
  645. $self->files ($pkgname, Fcm::ExtractFile->new (
  646. conflict => $self->conflict,
  647. dest => $self->dest->srcpath,
  648. pkgname => $pkgname,
  649. ));
  650. # Base is empty
  651. $self->files ($pkgname)->src->[0] = Fcm::ExtractSrc->new (
  652. id => $layer->tag,
  653. pkgname => $pkgname,
  654. ) if $i > 0;
  655. }
  656. my $cache = File::Spec->catfile ($layer->localdir, $file);
  657. push @{ $self->files ($pkgname)->src }, Fcm::ExtractSrc->new (
  658. cache => $cache,
  659. id => $layer->tag,
  660. pkgname => $pkgname,
  661. rev => ($layer->user ? (stat ($cache))[9] : $layer->commit),
  662. uri => join ('/', $layer->location, $file),
  663. );
  664. }
  665. # List of removed files in this layer (relative to base layer)
  666. if ($i > 0) {
  667. for my $pkgname (keys %pkgnames0) {
  668. push @{ $self->files ($pkgname)->src }, Fcm::ExtractSrc->new (
  669. id => $layer->tag,
  670. pkgname => $pkgname,
  671. ) if not exists $pkgnames{$pkgname}
  672. }
  673. }
  674. }
  675. }
  676. # Compare with old settings
  677. # ----------------------------------------------------------------------------
  678. my %new = ();
  679. for my $key (sort keys %{ $self->files }) {
  680. # Set up value for cache
  681. my @sources = ();
  682. for my $src (@{ $self->files ($key)->src }) {
  683. push @sources, (defined ($src->uri) ? ($src->uri . '@' . $src->rev) : '');
  684. }
  685. my $value = join ($Fcm::Config::DELIMITER, @sources);
  686. # Set Fcm::ExtractFile->dest_status to "unchanged" if value is unchanged
  687. $self->files ($key)->dest_status ('')
  688. if exists $old{$key} and $old{$key} eq $value;
  689. # Write current settings
  690. $new{$key} = $value;
  691. }
  692. # Delete those that exist in previous extract but not in current
  693. # ----------------------------------------------------------------------------
  694. for my $key (sort keys %old) {
  695. next if exists $new{$key};
  696. $self->files ($key, Fcm::ExtractFile->new (
  697. dest => $self->dest->srcpath,
  698. pkgname => $key,
  699. ));
  700. }
  701. # Extract each file, if necessary
  702. # ----------------------------------------------------------------------------
  703. for my $key (sort keys %{ $self->files }) {
  704. $rc = $self->files ($key)->run if defined ($rc);
  705. last if not defined ($rc);
  706. }
  707. # Report status
  708. # ----------------------------------------------------------------------------
  709. if (defined ($rc) and $self->verbose) {
  710. my %src_status_count = ();
  711. my %dest_status_count = ();
  712. for my $key (sort keys %{ $self->files }) {
  713. # Report changes in destination in verbose 1 or above
  714. my $dest_status = $self->files ($key)->dest_status;
  715. my $src_status = $self->files ($key)->src_status;
  716. next unless $self->verbose and $dest_status;
  717. if ($dest_status and $dest_status ne '?') {
  718. if (exists $dest_status_count{$dest_status}) {
  719. $dest_status_count{$dest_status}++;
  720. } else {
  721. $dest_status_count{$dest_status} = 1;
  722. }
  723. }
  724. if ($src_status and $src_status ne '?') {
  725. if (exists $src_status_count{$src_status}) {
  726. $src_status_count{$src_status}++;
  727. } else {
  728. $src_status_count{$src_status} = 1;
  729. }
  730. }
  731. # Destination status in column 1, source status in column 2
  732. if ($self->verbose > 1) {
  733. my @srcs = @{$self->files ($key)->src_actual};
  734. print ($dest_status ? $dest_status : ' ');
  735. print ($src_status ? $src_status : ' ');
  736. print ' ' x 5, $key;
  737. print ' (', join (', ', map {$_->id} @srcs), ')' if @srcs;
  738. print "\n";
  739. }
  740. }
  741. # Report number of files in each dest_status category
  742. if (%dest_status_count) {
  743. print 'Column 1: ' if $self->verbose > 1;
  744. print 'Destination status summary:', "\n";
  745. for my $key (sort keys %Fcm::ExtractFile::DEST_STATUS_CODE) {
  746. next unless $key;
  747. next if not exists ($dest_status_count{$key});
  748. print ' No of files ';
  749. print '[', $key, '] ' if $self->verbose > 1;
  750. print $Fcm::ExtractFile::DEST_STATUS_CODE{$key}, ': ',
  751. $dest_status_count{$key}, "\n";
  752. }
  753. }
  754. # Report number of files in each dest_status category
  755. if (%src_status_count) {
  756. print 'Column 2: ' if $self->verbose > 1;
  757. print 'Source status summary:', "\n";
  758. for my $key (sort keys %Fcm::ExtractFile::SRC_STATUS_CODE) {
  759. next unless $key;
  760. next if not exists ($src_status_count{$key});
  761. print ' No of files ';
  762. print '[', $key, '] ' if $self->verbose > 1;
  763. print $Fcm::ExtractFile::SRC_STATUS_CODE{$key}, ': ',
  764. $src_status_count{$key}, "\n";
  765. }
  766. }
  767. }
  768. # Record configuration of current extract for each file
  769. # ----------------------------------------------------------------------------
  770. my @new_lines;
  771. if (defined ($rc)) {
  772. for my $key (sort keys %new) {
  773. push @new_lines, Fcm::CfgLine->new (label => $key, value => $new{$key});
  774. }
  775. }
  776. return ($rc, \@new_lines);
  777. }
  778. # ------------------------------------------------------------------------------
  779. # SYNOPSIS
  780. # @array = $self->sort_bdeclare ();
  781. #
  782. # DESCRIPTION
  783. # This method returns sorted build declarations, filtering out repeated
  784. # entries, where possible.
  785. # ------------------------------------------------------------------------------
  786. sub sort_bdeclare {
  787. my $self = shift;
  788. # Get list of build configuration labels that can be declared multiple times
  789. my %cfg_keyword = map {
  790. ($self->cfglabel ($_), 1)
  791. } split (/$Fcm::Config::DELIMITER_LIST/, $self->setting ('CFG_KEYWORD'));
  792. my @bdeclares = ();
  793. for my $d (reverse @{ $self->bdeclare }) {
  794. # Reconstruct array from bottom up
  795. # * always add declarations that can be declared multiple times
  796. # * otherwise add only if it is declared below
  797. unshift @bdeclares, $d
  798. if exists $cfg_keyword{uc (($d->slabel_fields)[0])} or
  799. not grep {$_->slabel eq $d->slabel} @bdeclares;
  800. }
  801. return (sort {$a->slabel cmp $b->slabel} @bdeclares);
  802. }
  803. # ------------------------------------------------------------------------------
  804. # SYNOPSIS
  805. # @cfglines = $obj->to_cfglines ();
  806. #
  807. # DESCRIPTION
  808. # See description of Fcm::ConfigSystem->to_cfglines for further information.
  809. # ------------------------------------------------------------------------------
  810. sub to_cfglines {
  811. my ($self) = @_;
  812. return (
  813. Fcm::ConfigSystem::to_cfglines($self),
  814. $self->rdest->to_cfglines (),
  815. Fcm::CfgLine->new (),
  816. @{ $self->bdeclare } ? (
  817. Fcm::CfgLine::comment_block ('Build declarations'),
  818. map {
  819. Fcm::CfgLine->new (label => $_->label, value => $_->value)
  820. } ($self->sort_bdeclare),
  821. Fcm::CfgLine->new (),
  822. ) : (),
  823. Fcm::CfgLine::comment_block ('Project and branches'),
  824. (map {($_->to_cfglines ())} @{ $self->branches }),
  825. ($self->conflict ne 'merge') ? (
  826. Fcm::CfgLine->new (
  827. label => $self->cfglabel ('CONFLICT'), value => $self->conflict,
  828. ),
  829. Fcm::CfgLine->new (),
  830. ) : (),
  831. );
  832. }
  833. # ------------------------------------------------------------------------------
  834. # SYNOPSIS
  835. # @cfglines = $obj->to_cfglines_bld ();
  836. #
  837. # DESCRIPTION
  838. # Returns a list of configuration lines of the current extract suitable for
  839. # feeding into the build system.
  840. # ------------------------------------------------------------------------------
  841. sub to_cfglines_bld {
  842. my ($self) = @_;
  843. my $dest = $self->rdest->rootdir ? 'rdest' : 'dest';
  844. my $root = File::Spec->catfile ('$HERE', '..');
  845. my @inherits;
  846. my @no_inherits;
  847. if (@{ $self->inherit }) {
  848. # List of inherited builds
  849. for (@{ $self->inherit }) {
  850. push @inherits, Fcm::CfgLine->new (
  851. label => $self->cfglabel ('USE'), value => $_->$dest->rootdir
  852. );
  853. }
  854. # List of files that should not be inherited
  855. for my $key (sort keys %{ $self->files }) {
  856. next unless $self->files ($key)->dest_status eq 'd';
  857. my $label = join ('::', (
  858. $self->cfglabel ('INHERIT'),
  859. $self->cfglabel ('FILE'),
  860. split (m#/#, $self->files ($key)->pkgname),
  861. ));
  862. push @no_inherits, Fcm::CfgLine->new (label => $label, value => 'false');
  863. }
  864. }
  865. return (
  866. Fcm::CfgLine::comment_block ('File header'),
  867. (map
  868. {my ($lbl, $val) = @{$_}; Fcm::CfgLine->new(label => $lbl, value => $val)}
  869. (
  870. [$self->cfglabel('CFGFILE') . $Fcm::Config::DELIMITER . 'TYPE' , 'bld'],
  871. [$self->cfglabel('CFGFILE') . $Fcm::Config::DELIMITER . 'VERSION', '1.0'],
  872. [],
  873. )
  874. ),
  875. @{ $self->inherit } ? (
  876. @inherits,
  877. @no_inherits,
  878. Fcm::CfgLine->new (),
  879. ) : (),
  880. Fcm::CfgLine::comment_block ('Destination'),
  881. Fcm::CfgLine->new (label => $self->cfglabel ('DEST'), value => $root),
  882. Fcm::CfgLine->new (),
  883. @{ $self->bdeclare } ? (
  884. Fcm::CfgLine::comment_block ('Build declarations'),
  885. map {
  886. Fcm::CfgLine->new (label => $_->slabel, value => $_->value)
  887. } ($self->sort_bdeclare),
  888. Fcm::CfgLine->new (),
  889. ) : (),
  890. );
  891. }
  892. # ------------------------------------------------------------------------------
  893. # SYNOPSIS
  894. # $rc = $self->write_cfg ();
  895. #
  896. # DESCRIPTION
  897. # This method writes the configuration file at the end of the run. It calls
  898. # $self->write_cfg_system ($cfg) to write any system specific settings.
  899. # ------------------------------------------------------------------------------
  900. sub write_cfg {
  901. my $self = shift;
  902. my $cfg = Fcm::CfgFile->new (TYPE => $self->type);
  903. $cfg->lines ([$self->to_cfglines()]);
  904. $cfg->print_cfg ($self->dest->extcfg);
  905. return 1;
  906. }
  907. # ------------------------------------------------------------------------------
  908. # SYNOPSIS
  909. # $rc = $self->write_cfg_bld ();
  910. #
  911. # DESCRIPTION
  912. # This internal method writes the build configuration file.
  913. # ------------------------------------------------------------------------------
  914. sub write_cfg_bld {
  915. my $self = shift;
  916. my $cfg = Fcm::CfgFile->new (TYPE => 'bld');
  917. $cfg->lines ([$self->to_cfglines_bld()]);
  918. $cfg->print_cfg ($self->dest->bldcfg);
  919. return 1;
  920. }
  921. # ------------------------------------------------------------------------------
  922. 1;
  923. __END__