aboutsummaryrefslogtreecommitdiff
path: root/scripts/export_ebuild_tree.pl
blob: 3da8247de670904d27b409bc640a1735fc04df2f (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
#!/usr/bin/env perl

use strict;
use warnings;

use Getopt::Long qw(GetOptions);
use File::Temp qw(tempdir);
use File::Path qw(make_path);
use IPC::Open3 qw(open3);
use Symbol qw(gensym);

my ($repo, $tag, $output_dir, $suffix);
my $help;

GetOptions(
    'repo=s'       => \$repo,
    'tag=s'        => \$tag,
    'output-dir=s' => \$output_dir,
    'suffix=s'     => \$suffix,
    'help|h'       => \$help,
) or usage(1);

usage(0) if $help;
usage(1) unless defined $repo && defined $tag && defined $suffix;

$output_dir //= '.';

sub usage {
    my ($exit) = @_;

    print <<"EOF";
Usage: $0 --repo PATH --tag TAG --suffix SUFFIX [--output-dir DIR]

Options:
    --repo PATH       Git repository
    --tag TAG         Git tag
    --suffix SUFFIX   Snapshot filename suffix
    --output-dir DIR  Output directory (default: .)
    --help, -h        Show this help

Output:
    webrsync-SUFFIX.tar.bz2

EOF

    exit $exit;
}

sub run {
    my (@cmd) = @_;

    print '+ ', join(' ', @cmd), "\n";

    system(@cmd);

    die "command failed: $?\n" if $? == -1;
    die "command failed: signal " . ($? & 127) . "\n" if $? & 127;
    die "command failed: exit " . ($? >> 8) . "\n" if $? >> 8;
}

die "Repository does not exist: $repo\n"
    unless -d $repo;

make_path($output_dir)
    unless -d $output_dir;

my $tmp = tempdir(
    'webrsync-XXXXXX',
    TMPDIR  => 1,
    CLEANUP => 1,
);

my $tree = "$tmp/tree";
my $archive = "$tmp/tree.tar";

make_path($tree);

run(
    'git',
    '-C', $repo,
    'archive',
    '--format=tar',
    '--output', $archive,
    $tag,
);

run(
    'tar',
    '-xf', $archive,
    '-C', $tree,
);

my $timestamp = "$tree/metadata/timestamp.x";

make_path("$tree/metadata");

open my $fh, '>', $timestamp
    or die "$timestamp: $!\n";

print {$fh} time, "\n"
    or die "$timestamp: $!\n";

close $fh
    or die "$timestamp: $!\n";

my $output = "$output_dir/webrsync-$suffix.tar.bz2";

if (system qw{rsync -P --delete -a}, 'rsync://rsync.gentoo.org/gentoo-portage/metadata/glsa/', "$tree/metadata/glsa/") {
    die 'Unable to append security advisories (glsa)';
}

run(
    'tar',
    '-C', $tree,
    '--sort=name',
    '-cjf', $output,
    '.',
);

print "Created $output\n";