summaryrefslogtreecommitdiffstats
path: root/zarb-ml/mageia-dev/attachments/20110128/77feba8c/attachment.ksh
blob: 34ba18f8549d6bc7d666152588ca1b44c89c4d71 (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
#!/usr/bin/perl

use Getopt::Long;
use Pod::Usage;

my %options;

GetOptions(
    "l" => sub { $options{local} = 1 },
) or pod2usage(1);

my ($action, @args) = @ARGV;

my $mdvsoft = MDV::Soft->new(%options);

if ($mdvsoft->can($action)) {
    $mdvsoft->$action(@args);
} else {
    pod2usage(1);
}

exit(0);

package MDV::Soft;

use SVN::Client;
use File::Temp qw(tempdir);
use MDK::Common;

sub new {
    my ($class, %options) = @_;
    bless {
        svn => SVN::Client->new,
        branch => 'trunk',
        root => 'svn+ssh://svn.mandriva.com/svn/soft',
        opts => \%options,
    }, $class;
}

sub export {
    my ($self, $name, $o_destdir) = @_;
    unless ($name) {
        warn "Can't export without a module name";
        return;
    }
    my $module_url = $self->{opts}{local} ? $ENV{PWD} : $self->{root} . '/' . $name . '/' . $self->{branch};
    my $tmp_dir = tempdir(CLEANUP => 1);
    my $export_dir = $tmp_dir . '/' . $name;

    $self->{svn}->export($module_url, $export_dir, undef, undef)
      or die "Can't checkout $module_url";

    my $config = read_config($export_dir);
    my $distname = $config->{NAME} . '-' . $config->{VERSION};
    $distname .= '-' . $config->{RELEASE} if $config->{RELEASE} > 1;

    rename($export_dir, $tmp_dir . '/' . $distname);
    my $tarball = $distname . '.tar.bz2';
    $tarball = $o_destdir . '/' . $tarball if -d $o_destdir;
    system('tar', '-C', $tmp_dir, '-cjf', $tarball, $distname) == 0
      or die "Can't compress $distname";

    print "Exported $tarball\n";

    1;
}

sub read_config {
    my ($config_dir) = @_;
    my $config = {};
    my @mandatory_vars = qw(NAME VERSION);
    my @config_vars =  (@mandatory_vars, qw(RELEASE));
    my $config_name = 'Makefile';

    foreach my $line (cat_($config_dir . '/' . $config_name)) {
        foreach my $var (@config_vars) {
            $line =~ /^$var\s*:?=\s*([^#]+)/ and $config->{$var} = chomp_($1);
        }
    }

    foreach (@mandatory_vars) {
        $config->{$_} or die "Can't find config variable $_ in $config_name";
    }

    $config;
}

1;