1066656b00d81833a3ee7e11b7ef091b9c8e1f63
[cascardo/debsrc.git] / debsort.pl
1 #!/usr/bin/env perl
2 #
3 #   Copyright 2014 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU General Public License for more details.
14 #
15 #   You should have received a copy of the GNU General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 use strict;
20 use warnings;
21
22 open(PACKAGES, "<Packages");
23 open(SOURCES, "<Sources");
24
25 my @packages = ();
26 my %depends = ();
27 my %priority = ();
28 my %essential = ();
29
30 sub add_depends {
31         my ($package, $depends) = @_;
32         my $deps = [];
33         my @vdeps = split(",", $depends);
34         for my $i (@vdeps) {
35                 $i =~ qr,([0-9a-z-+.]+),;
36                 push @$deps, $1;
37         }
38         $depends{$package} = $deps;
39 }
40
41 my $package;
42 while (<PACKAGES>) {
43         if (/^Package: ([0-9a-z-+.]+)/) {
44                 $package = $1;
45                 push @packages, $package;
46         }
47         if (/^Depends: (.*)/) {
48                 add_depends($package, $1);
49         }
50         if (/^Priority: (.*)/) {
51                 $priority{$package} = $1;
52         }
53         if (/^Essential: yes/) {
54                 $essential{$package} = "yes";
55         }
56 }
57
58 close(PACKAGES);
59 close(SOURCES);
60
61 my @pp = ();
62
63 my @visit = ();
64
65 for my $i (keys %essential) {
66         push @visit, $i;
67 }
68 push @visit, "build-essential";
69
70 while (@visit) {
71         my $n = pop @visit;
72         next if grep /^$n$/, @pp;
73         push @pp, $n;
74         my $l = $depends{$n};
75         for my $d (@$l) {
76                 if (!grep /^$d$/, @pp && !grep /^$d$/, @visit) {
77                         push @visit, $d;
78                 }
79         }
80 }
81
82 for my $i (@pp) {
83         print "$i\n";
84 }