<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># ------------------------------------------------------------------------------
# NAME
#   Fcm::ReposBranch
#
# DESCRIPTION
#   This class contains methods for gathering information for a repository
#   branch. It currently supports Subversion repository and local user
#   directory.
#
# COPYRIGHT
#   (C) Crown copyright Met Office. All rights reserved.
#   For further details please refer to the file COPYRIGHT.txt
#   which you should have received as part of this distribution.
# ------------------------------------------------------------------------------

use warnings;
use strict;

package Fcm::ReposBranch;
use base qw{Fcm::Base};

use Fcm::CfgLine;
use Fcm::Keyword;
use Fcm::Util      qw{expand_tilde is_url run_command w_report};
use File::Basename qw{dirname};
use File::Find     qw{find};
use File::Spec;

# List of scalar property methods for this class
my @scalar_properties = (
  'package',  # package name of which this repository belongs
  'repos',    # repository branch root URL/path
  'revision', # the revision of this branch
  'tag',      # "tag" name of this branch of the repository
  'type',     # repository type
);

# List of hash property methods for this class
my @hash_properties = (
  'dirs',    # list of non-recursive directories in this branch
  'expdirs', # list of recursive directories in this branch
);

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $obj = Fcm::ReposBranch-&gt;new (%args);
#
# DESCRIPTION
#   This method constructs a new instance of the Fcm::ReposBranch class. See
#   @scalar_properties above for allowed list of properties in the constructor.
#   (KEYS should be in uppercase.)
# ------------------------------------------------------------------------------

sub new {
  my $this  = shift;
  my %args  = @_;
  my $class = ref $this || $this;

  my $self = Fcm::Base-&gt;new (%args);

  for (@scalar_properties) {
    $self-&gt;{$_} = exists $args{uc ($_)} ? $args{uc ($_)} : undef;
  }

  $self-&gt;{$_} = {} for (@hash_properties);

  bless $self, $class;
  return $self;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $value = $obj-&gt;X;
#   $obj-&gt;X ($value);
#
# DESCRIPTION
#   Details of these properties are explained in @scalar_properties.
# ------------------------------------------------------------------------------

for my $name (@scalar_properties) {
  no strict 'refs';

  *$name = sub {
    my $self = shift;

    # Argument specified, set property to specified argument
    if (@_) {
      $self-&gt;{$name} = $_[0];
    }

    return $self-&gt;{$name};
  }
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   %hash = %{ $obj-&gt;X () };
#   $obj-&gt;X (\%hash);
#
#   $value = $obj-&gt;X ($index);
#   $obj-&gt;X ($index, $value);
#
# DESCRIPTION
#   Details of these properties are explained in @hash_properties.
#
#   If no argument is set, this method returns a hash containing a list of
#   objects. If an argument is set and it is a reference to a hash, the objects
#   are replaced by the the specified hash.
#
#   If a scalar argument is specified, this method returns a reference to an
#   object, if the indexed object exists or undef if the indexed object does
#   not exist. If a second argument is set, the $index element of the hash will
#   be set to the value of the argument.
# ------------------------------------------------------------------------------

for my $name (@hash_properties) {
  no strict 'refs';

  *$name = sub {
    my ($self, $arg1, $arg2) = @_;

    # Ensure property is defined as a reference to a hash
    $self-&gt;{$name} = {} if not defined ($self-&gt;{$name});

    # Argument 1 can be a reference to a hash or a scalar index
    my ($index, %hash);

    if (defined $arg1) {
      if (ref ($arg1) eq 'HASH') {
        %hash = %$arg1;

      } else {
        $index = $arg1;
      }
    }

    if (defined $index) {
      # A scalar index is defined, set and/or return the value of an element
      $self-&gt;{$name}{$index} = $arg2 if defined $arg2;

      return (
        exists $self-&gt;{$name}{$index} ? $self-&gt;{$name}{$index} : undef
      );

    } else {
      # A scalar index is not defined, set and/or return the hash
      $self-&gt;{$name} = \%hash if defined $arg1;
      return $self-&gt;{$name};
    }
  }
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $obj-&gt;expand_revision;
#
# DESCRIPTION
#   This method expands the revision keywords of the current branch to a
#   revision number. It returns true on success.
# ------------------------------------------------------------------------------

sub expand_revision {
  my $self = shift;

  my $rc = 1;
  if ($self-&gt;type eq 'svn') {
    # Expand revision keyword
    my $rev = (Fcm::Keyword::expand($self-&gt;repos(), $self-&gt;revision()))[1];

    # Get last changed revision of the specified revision
    my $info_ref = $self-&gt;_svn_info($self-&gt;repos(), $rev);
    if (!defined($info_ref-&gt;{'Revision'})) {
      my $url = $self-&gt;repos() . ($rev ? '@' . $rev : q{});
      w_report("ERROR: $url: not a valid URL\n");
      return 0;
    }
    my $lc_rev = $info_ref-&gt;{'Last Changed Rev'};
    $rev       = $info_ref-&gt;{'Revision'};

    # Print info if specified revision is not the last commit revision
    if (uc($self-&gt;revision()) ne 'HEAD' &amp;&amp; $lc_rev != $rev) {
      my $message = $self-&gt;repos . '@' . $rev . ': last changed at [' .
                    $lc_rev . '].';
      if ($self-&gt;setting ('EXT_REVMATCH') and uc ($self-&gt;revision) ne 'HEAD') {
        w_report "ERROR: specified and last changed revisions differ:\n",
                 '       ', $message, "\n";
        $rc = 0;

      } else {
        print 'INFO: ', $message, "\n";
      }
    }

    if ($self-&gt;verbose &gt; 1 and uc ($self-&gt;revision) ne 'HEAD') {
      # See if there is a later change of the branch at the HEAD
      my $head_lc_rev = $self-&gt;_svn_info($self-&gt;repos())-&gt;{'Last Changed Rev'};

      if (defined($head_lc_rev) &amp;&amp; $head_lc_rev != $lc_rev) {
        # Ensure that this is the same branch by checking its history
        my @lines = &amp;run_command (
          [qw/svn log -q --incremental -r/, $lc_rev, $self-&gt;repos . '@HEAD'],
          METHOD =&gt; 'qx', TIME =&gt; $self-&gt;verbose &gt; 2,
        );

        print 'INFO: ', $self-&gt;repos, '@', $rev,
              ': newest commit at [', $head_lc_rev, '].', "\n"
          if @lines;
      }
    }

    $self-&gt;revision ($rev) if $rev ne $self-&gt;revision;

  } elsif ($self-&gt;type eq 'user') {
    1; # Do nothing

  } else {
    w_report 'ERROR: ', $self-&gt;repos, ': repository type "', $self-&gt;type,
             '" not supported.';
    $rc = 0;
  }

  return $rc;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $obj-&gt;expand_path;
#
# DESCRIPTION
#   This method expands the relative path names of sub-directories to full
#   path names. It returns true on success.
# ------------------------------------------------------------------------------

sub expand_path {
  my $self = shift;

  my $rc = 1;
  if ($self-&gt;type eq 'svn') {
    # SVN repository
    # Do nothing unless there is a declared repository for this branch
    return unless $self-&gt;repos;

    # Remove trailing /
    my $repos = $self-&gt;repos;
    $self-&gt;repos ($repos) if $repos =~ s#/+$##;

    # Consider all declared (expandable) sub-directories
    for my $name (qw/dirs expdirs/) {
      for my $dir (keys %{ $self-&gt;$name }) {
        # Do nothing if declared sub-directory is quoted as a full URL
        next if &amp;is_url ($self-&gt;$name ($dir));

        # Expand sub-directory to full URL
        $self-&gt;$name ($dir, $self-&gt;repos . (
          $self-&gt;$name ($dir) ? ('/' . $self-&gt;$name ($dir)) : ''
        ));
      }
    }
    # Note: "catfile" cannot be used in the above statement because it has
    #       the tendency of removing a slash from double slashes.

  } elsif ($self-&gt;type eq 'user') {
    # Local user directories

    # Expand leading ~ for all declared (expandable) sub-directories
    for my $name (qw/dirs expdirs/) {
      for my $dir (keys %{ $self-&gt;$name }) {
        $self-&gt;$name ($dir, expand_tilde $self-&gt;$name ($dir));
      }
    }

    # A top directory for the source is declared
    if ($self-&gt;repos) {
      # Expand leading ~ for the top directory
      $self-&gt;repos (expand_tilde $self-&gt;repos);

      # Get the root directory of the file system
      my $rootdir = File::Spec-&gt;rootdir ();

      # Expand top directory to absolute path, if necessary
      $self-&gt;repos (File::Spec-&gt;rel2abs ($self-&gt;repos))
        if $self-&gt;repos !~ m/^$rootdir/;

      # Remove trailing /
      my $repos = $self-&gt;repos;
      $self-&gt;repos ($repos) if $repos =~ s#/+$##;

      # Consider all declared (expandable) sub-directories
      for my $name (qw/dirs expdirs/) {
        for my $dir (keys %{ $self-&gt;$name }) {
          # Do nothing if declared sub-directory is quoted as a full path
          next if $self-&gt;$name ($dir) =~ m#^$rootdir#;

          # Expand sub-directory to full path
          $self-&gt;$name (
            $dir, $self-&gt;$name ($dir)
                  ? File::Spec-&gt;catfile ($self-&gt;repos, $self-&gt;$name ($dir))
                  : $self-&gt;repos
          );
        }
      }
    }

  } else {
    w_report 'ERROR: ', $self-&gt;repos, ': repository type "', $self-&gt;type,
             '" not supported.';
    $rc = 0;
  }

  return $rc;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $rc = $obj-&gt;expand_all();
#
# DESCRIPTION
#   This method searches the expandable source directories recursively for
#   source directories containing regular files. The namespaces and the locators
#   of these sub-directories are then added to the source directory hash table.
#   Returns true on success.
# ------------------------------------------------------------------------------

sub expand_all {
  my ($self) = @_;
  my %finder_of = (
    user =&gt; sub {
      my ($root_locator) = @_;
      my %ns_of;
      my $wanted = sub {
        my $base_name = $_;
        my $path = $File::Find::name;
        if (-f $path &amp;&amp; -r $path &amp;&amp; !-l $path) {
          my $dir_path      = dirname($path);
          my $rel_dir_path  = File::Spec-&gt;abs2rel($dir_path, $root_locator);
          if (!exists($ns_of{$dir_path})) {
            $ns_of{$dir_path} = [File::Spec-&gt;splitdir($rel_dir_path)];
          }
        }
      };
      find($wanted, $root_locator);
      return \%ns_of;
    },
    svn  =&gt; sub {
      my ($root_locator) = @_;
      my $runner = sub {
        map {chomp($_); $_} run_command(
          ['svn', @_,  '-R', join('@', $root_locator, $self-&gt;revision())],
          METHOD =&gt; 'qx', TIME =&gt; $self-&gt;config()-&gt;verbose() &gt; 2,
        );
      };
      # FIXME: check for symlink switched off due to "svn pg" being very slow
      #my %symlink_in
      #  = map {($_ =~ qr{\A(.+)\s-\s(\*)\z}xms)} ($runner-&gt;(qw{pg svn:special}));
      #my @locators
      #  = grep {$_ !~ qr{/\z}xms &amp;&amp; !$symlink_in{$_}} ($runner-&gt;('ls'));
      my @locators = grep {$_ !~ qr{/\z}xms} ($runner-&gt;('ls'));
      my %ns_of;
      for my $locator (@locators) {
        my ($rel_dir_locator) = $locator =~ qr{\A(.*)/[^/]+\z}xms; # dirname
        $rel_dir_locator ||= q{};
        my $dir_locator = join(q{/}, $root_locator, $rel_dir_locator);
        if (!exists($ns_of{$dir_locator})) {
          $ns_of{$dir_locator} = [split(q{/}, $rel_dir_locator)];
        }
      }
      return \%ns_of;
    },
  );

  if (!defined($finder_of{$self-&gt;type()})) {
    w_report(sprintf(
        qq{ERROR: %s: resource type "%s" not supported},
        $self-&gt;repos(),
        $self-&gt;type(),
    ));
    return;
  }
  while (my ($root_ns, $root_locator) = each(%{$self-&gt;expdirs()})) {
    my @root_ns_list = split(qr{$Fcm::Config::DELIMITER}xms, $root_ns);
    my $ns_hash_ref = $finder_of{$self-&gt;type()}-&gt;($root_locator);
    while (my ($dir_path, $ns_list_ref) = each(%{$ns_hash_ref})) {
      if (!grep {$_ =~ qr{\A\.}xms || $_ =~ qr{~\z}xms} @{$ns_list_ref}) {
        my $ns = join($Fcm::Config::DELIMITER, @root_ns_list, @{$ns_list_ref});
        $self-&gt;dirs($ns, $dir_path);
      }
    }
  }
  return 1;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   $n = $obj-&gt;add_base_dirs ($base);
#
# DESCRIPTION
#   Add a list of source directories to the current branch based on the set
#   provided by $base, which must be a reference to a Fcm::ReposBranch
#   instance. It returns the total number of used sub-directories in the
#   current repositories.
# ------------------------------------------------------------------------------

sub add_base_dirs {
  my $self = shift;
  my $base = shift;

  my %base_dirs = %{ $base-&gt;dirs };

  for my $key (keys %base_dirs) {
    # Remove repository root from base directories
    if ($base_dirs{$key} eq $base-&gt;repos) {
      $base_dirs{$key} = '';

    } else {
      $base_dirs{$key} = substr $base_dirs{$key}, length ($base-&gt;repos) + 1;
    }

    # Append base directories to current repository root
    $self-&gt;dirs ($key, $base_dirs{$key});
  }

  # Expand relative path names of sub-directories
  $self-&gt;expand_path;

  return scalar keys %{ $self-&gt;dirs };
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   @cfglines = $obj-&gt;to_cfglines ();
#
# DESCRIPTION
#   This method returns a list of configuration lines for the current branch.
# ------------------------------------------------------------------------------

sub to_cfglines {
  my ($self) = @_;
  my @return = ();

  my $suffix = $self-&gt;package . $Fcm::Config::DELIMITER . $self-&gt;tag;
  push @return, Fcm::CfgLine-&gt;new (
    label =&gt; $self-&gt;cfglabel ('REPOS') . $Fcm::Config::DELIMITER . $suffix,
    value =&gt; $self-&gt;repos,
  ) if $self-&gt;repos;

  push @return, Fcm::CfgLine-&gt;new (
    label =&gt; $self-&gt;cfglabel ('REVISION') . $Fcm::Config::DELIMITER . $suffix,
    value =&gt; $self-&gt;revision,
  ) if $self-&gt;revision;

  for my $key (sort keys %{ $self-&gt;dirs }) {
    my $value = $self-&gt;dirs ($key);

    # Use relative path where possible
    if ($self-&gt;repos) {
      if ($value eq $self-&gt;repos) {
        $value = '';

      } elsif (index ($value, $self-&gt;repos) == 0) {
        $value = substr ($value, length ($self-&gt;repos) + 1);
      }
    }

    # Use top package name where possible
    my $dsuffix = $key . $Fcm::Config::DELIMITER . $self-&gt;tag;
    $dsuffix = $suffix if $value ne $self-&gt;dirs ($key) and $key eq join (
      $Fcm::Config::DELIMITER, $self-&gt;package, File::Spec-&gt;splitdir ($value)
    );

    push @return, Fcm::CfgLine-&gt;new (
      label =&gt; $self-&gt;cfglabel ('DIRS') . $Fcm::Config::DELIMITER . $dsuffix,
      value =&gt; $value,
    );
  }

  push @return, Fcm::CfgLine-&gt;new ();

  return @return;
}

# ------------------------------------------------------------------------------
# SYNOPSIS
#   my $hash_ref = $self-&gt;_svn_info($url[, $rev]);
#
# DESCRIPTION
#   Executes "svn info" and returns each field in a hash.
# ------------------------------------------------------------------------------
sub _svn_info {
  my ($self, $url, $rev) = @_;
  return {
    map {
      chomp();
      my ($key, $value) = split(qr{\s*:\s*}xms, $_, 2);
      $key ? ($key, $value) : ();
    } run_command(
      [qw{svn info}, ($rev ? ('-r', $rev, join('@', $url, $rev)) : $url)], 
      DEVNULL =&gt; 1, METHOD =&gt; 'qx', TIME =&gt; $self-&gt;verbose() &gt; 2,
    )
  };
}

# ------------------------------------------------------------------------------

1;

__END__
</pre></body></html>