aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Submit/Action/CVS.pm
blob: e9f1f4f7acda39f802b6db79af9212423bdbb70d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# $Id: CVS.pm 224115 2007-07-02 09:17:15Z pixel $
package Youri::Submit::Action::CVS;

=head1 NAME

Youri::Submit::Action::CVS - CVS versionning

=head1 DESCRIPTION

This action plugin ensures CVS versionning of package sources.

=cut

use warnings;
use strict;
use Carp;
use Cwd;
use File::Temp qw/tempdir/;
use base qw/Youri::Submit::Action/;

sub _init {
    my $self   = shift;
    my %options = (
        exclude => '\.(tar(\.(gz|bz2))?|zip)$',
        perms   => 644,
        @_
    );

    $self->{_exclude} = $options{exclude};
    $self->{_perms}   = $options{perms};
}

sub run {
    my ($self, $package, $repository, $target, $define) = @_;
    croak "Not a class method" unless ref $self;

    return unless $package->is_source();

    my $name    = $package->get_name();
    my $version = $package->get_version();
    my $release = $package->get_release();

    my $root = $repository->get_version_root();
    my $path = $repository->get_version_path($package, $target, $define);

    # remember original directory
    my $original_dir = cwd();

    # get a safe temporary directory
    my $dir = tempdir( CLEANUP => 1 );
    chdir $dir;

    # first checkout base directory only
    system("cvs -Q -d $root co -l $path");

    # try to checkout package directory
    my $dest = $path . '/' . $name;
    system("cvs -Q -d $root co $dest");

    # create directory if previous import failed
    unless (-d $dest) {
        print "adding directory $dest\n" if $self->{_verbose};
        system("install -d -m " . ($self->{_perms} + 111) . " $dest");
        system("cvs -Q -d $root add $dest");
    }

    chdir $dest;

    # remove all files
    unlink grep { -f } glob '*';

    # extract all rpm files locally
    $package->extract();

    # remove excluded files
    if ($self->{_exclude}) {
        unlink grep { -f && /$self->{_exclude}/ } glob '*'; 
    }

    # uncompress all compressed files
    system("bunzip2 *.bz2 2>/dev/null");
    system("gunzip *.gz 2>/dev/null");

    my (@to_remove, @to_add, @to_add_binary);
    foreach my $line (`cvs -nq update`) {
        if ($line =~ /^\? (\S+)/) {
            if (-B $1) {
                push(@to_add_binary, $1);
            } else {
                push(@to_add, $1);
            }
        }
        if ($line =~ /^U (\S+)/) {
            push(@to_remove, $1);
        }
    }
    if (@to_remove) {
        my $to_remove = join(' ', @to_remove);
        print "removing file(s) $to_remove\n" if $self->{_verbose};
        system("cvs -Q remove $to_remove");
    }
    if (@to_add) {
        my $to_add = join(' ', @to_add);
        print "adding text file(s) $to_add\n" if $self->{_verbose};
        system("cvs -Q add $to_add");
    }
    if (@to_add_binary) {
        my $to_add_binary = join(' ', @to_add_binary);
        print "adding binary file(s) $to_add_binary\n" if $self->{_verbose};
        system("cvs -Q add -kb $to_add_binary");
    }

    print "committing current directory\n" if $self->{_verbose};
    system("cvs -Q commit -m $version-$release") unless $self->{_test};

    # tag new release
    my $tag = "r$version-$release";
    $tag =~ s/\./_/g;
    print "tagging current directory as $tag\n" if $self->{_verbose};
    system("cvs -Q tag $tag") unless $self->{_test};

    # get back to original directory
    chdir $original_dir;

}

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1;