Fix kvm support in Ubuntu
[tools/build.git] / createdebdeps
1 #!/usr/bin/perl -w
2
3 BEGIN {
4   unshift @INC, ($::ENV{"BUILD_DIR"} || "/usr/lib/build");
5 }
6
7 use strict;
8 use Digest::MD5;
9 use File::Path;
10 use Getopt::Long;
11 use Build ':deb';
12 use Build::Deb;
13 use Build::Debrepo;
14
15 Getopt::Long::Configure("no_ignore_case");
16
17 #
18 # supported urls
19 #
20 # distribution:   <baseurl>/<dist>/[components]
21 # flat repo:      <baseurl>/.
22
23 my $cachedir = "/var/cache/build";
24 my $archpath;
25
26 GetOptions('cachedir=s'  => \$cachedir, 'archpath=s' => \$archpath) or exit(1);
27
28 if (!$archpath) {
29   $archpath = `uname -p` || 'unknown';
30   chomp $archpath;
31 }
32 my $basearch = $archpath;
33 $basearch =~ s/:.*//;
34 $basearch = Build::Deb::basearch($basearch);
35 my $pkgnum = 0;
36
37 for my $url (@ARGV) {
38   die("$url: not an remote debian repo\n") unless $url =~ /^(:?ftps?|https?):\/\/([^\/]*)\/?/;
39   my $repoid = Digest::MD5::md5_hex($url);
40   my $dir = "$cachedir/$repoid";
41
42   my @components;
43   my $baseurl = $url;
44
45   if ($url =~ /^(.*\/)\.(\/.*)?$/) {
46     # flat repo
47     $baseurl = $1;
48     @components = ('.');
49     $url = defined($2) ? "$1$2" : $1;
50     $url .= '/' unless $url =~ /\/$/;
51   } else {
52     if ($url =~ /([^\/]+)$/) {
53       @components = split(/[,+]/, $1);
54       $url =~ s/([^\/]+)$//;
55     }
56     push @components, 'main' unless @components;
57     $url .= '/' unless $url =~ /\/$/;
58     $baseurl = $url;
59     $url =~ s/([^\/]+\/)$/dists\/$1/;
60     $baseurl =~ s/([^\/]+\/)$//;
61   }
62
63   File::Path::mkpath($dir);
64   for my $component (@components) {
65     unlink("$dir/Packages.gz");
66     if ($component eq '.') {
67       system($INC[0]."/download", $dir, "${url}Packages.gz");
68       die("Packages.gz missing\n") unless -s "$dir/Packages.gz";
69     } else {
70       system($INC[0]."/download", $dir, "$url$component/binary-$basearch/Packages.gz");
71       die("Packages.gz missing for basearch $basearch, component $component\n") unless -s "$dir/Packages.gz";
72     }
73     Build::Debrepo::parse("$dir/Packages.gz", sub {
74       $pkgnum++;
75       $_[0]->{'id'} = "$pkgnum/0/0";
76       Build::writedeps(\*STDOUT, $_[0], $baseurl);
77     }, 'addselfprovides' => 1);
78   }
79 }