~ubuntu-branches/ubuntu/hardy/slack/hardy-proposed

« back to all changes in this revision

Viewing changes to src/slack-rolediff

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Pollock
  • Date: 2007-10-27 16:14:42 UTC
  • Revision ID: james.westby@ubuntu.com-20071027161442-z3wjuy3juutuxu7m
Tags: upstream-0.14.1
ImportĀ upstreamĀ versionĀ 0.14.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
# $Id: slack-rolediff,v 1.6 2006/09/27 07:50:07 alan Exp $
 
3
# vim:sw=2
 
4
# vim600:fdm=marker
 
5
# Copyright (C) 2004-2006 Alan Sundell <alan@sundell.net>
 
6
# All Rights Reserved.  This program comes with ABSOLUTELY NO WARRANTY.
 
7
# See the file COPYING for details.
 
8
#
 
9
# This script provides a preview of scripts or files about to be installed.
 
10
# Basically, it calls diff -- its smarts are in knowing where things are.
 
11
 
 
12
require 5.006;
 
13
use warnings FATAL => qw(all);
 
14
use strict;
 
15
use sigtrap qw(die untrapped normal-signals
 
16
               stack-trace any error-signals);
 
17
 
 
18
use File::Path;
 
19
use File::Find;
 
20
 
 
21
use constant LIB_DIR => '/usr/lib/slack';
 
22
use lib LIB_DIR;
 
23
use Slack;
 
24
 
 
25
my @diff = ('slack-diff',
 
26
              '-uN',
 
27
              );
 
28
 
 
29
# directories to compare
 
30
my %subdir = (
 
31
  files => 1,
 
32
  scripts => 1,
 
33
);
 
34
 
 
35
(my $PROG = $0) =~ s#.*/##;
 
36
 
 
37
sub diff ($$;@);
 
38
 
 
39
########################################
 
40
# Environment
 
41
# Helpful prefix to die messages
 
42
$SIG{__DIE__} = sub { die "FATAL[$PROG]: @_"; };
 
43
# Set a reasonable umask
 
44
umask 077;
 
45
# Get out of wherever (possibly NFS-mounted) we were
 
46
chdir("/")
 
47
  or die "Could not chdir /: $!";
 
48
# Autoflush on STDERR
 
49
select((select(STDERR), $|=1)[0]);
 
50
 
 
51
########################################
 
52
# Config and option parsing {{{
 
53
my $usage = Slack::default_usage("$PROG [options] <role> [<role>...]");
 
54
$usage .= <<EOF;
 
55
 
 
56
  --subdir DIR
 
57
      Check this subdir only.  Possible values for DIR are 'files' and
 
58
      'scripts'.
 
59
 
 
60
  --diff PROG
 
61
      Use this program to do diffs.  [@diff]
 
62
EOF
 
63
# Option defaults
 
64
my %opt = ();
 
65
Slack::get_options(
 
66
  opthash => \%opt,
 
67
  command_line_options => [
 
68
    'subdir=s',
 
69
    'diff=s',
 
70
  ],
 
71
  usage => $usage,
 
72
  required_options => [ qw(cache stage root) ],
 
73
);
 
74
 
 
75
# Arguments are required
 
76
die "No roles given!\n\n$usage" unless @ARGV;
 
77
 
 
78
# We only allow certain values for this option
 
79
if ($opt{subdir}) {
 
80
  unless ($opt{subdir} eq 'files' or $opt{subdir} eq 'scripts') {
 
81
    die "--subdir option must be 'files' or 'scripts'\n\n$usage";
 
82
  }
 
83
  # Only do this subdir
 
84
  %subdir = ( $opt{subdir} => 1 );
 
85
}
 
86
 
 
87
# Let people override our diff.  Split on spaces so they can pass args.
 
88
if ($opt{diff}) {
 
89
  @diff = split(/\s+/, $opt{diff});
 
90
}
 
91
 
 
92
# }}}
 
93
 
 
94
my $exit = 0;
 
95
# Do the diffs
 
96
for my $full_role (@ARGV) {
 
97
  # Split the full role (e.g. google.foogle.woogle) into components
 
98
  my @role = split(/\./, $full_role);
 
99
 
 
100
  if ($subdir{scripts}) {
 
101
    # Then we compare the cache vs the stage
 
102
    my $old = $opt{stage} . "/roles/" . $full_role . "/scripts";
 
103
    my $new = $opt{cache} . "/roles/" . $role[0] . "/scripts";
 
104
    # For scripts, we don't care so much about mode and owner (since those are
 
105
    # inherited in the CACHE from the SOURCE), so --noperms.
 
106
    $exit |= diff($old, $new, '--noperms');
 
107
  }
 
108
 
 
109
  if ($subdir{files}) {
 
110
    # Then we compare the stage vs the root
 
111
    my $old = $opt{root};
 
112
    my $new = $opt{stage} . "/roles/" . $full_role . "/files";
 
113
    # For files, we don't care about files that exist in $old but not $new
 
114
    $exit |= diff($old, $new, '--unidirectional-new-file');
 
115
  }
 
116
}
 
117
exit $exit;
 
118
 
 
119
sub diff ($$;@) {
 
120
  my ($old, $new, @options) = @_;
 
121
 
 
122
  my @command = (@diff, @options);
 
123
 
 
124
  # return if there's nothing to do
 
125
  return 0 if (not -d $old and not -d $new);
 
126
 
 
127
  ($opt{verbose} > 0) and print STDERR "$PROG: Previewing with '@command'\n";
 
128
 
 
129
  my $return = 0;
 
130
  my $callback = sub {
 
131
    my ($new_file) = @_;
 
132
    my $old_file = $new_file;
 
133
    ($old_file =~ s#^$new#$old#)
 
134
      or die "sub failed: $new|$new_file";
 
135
    if (system(@command, $old_file, $new_file) != 0) {
 
136
      $return |= Slack::get_system_exit(@command);
 
137
    }
 
138
  };
 
139
 
 
140
  # We have to use this function, rather than recursive mode for slack-diff,
 
141
  # because otherwise we'll print a bunch of bogus stuff about directories
 
142
  # that exist in $ROOT and therefore aren't being synced.
 
143
  Slack::find_files_to_install($new, $old, $callback);
 
144
 
 
145
  return $return;
 
146
}