Parse Sources file.
[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
24 my @packages = ();
25 my %depends = ();
26 my %priority = ();
27 my %essential = ();
28
29 sub add_depends {
30         my ($package, $depends) = @_;
31         my $deps = [];
32         my @vdeps = split(",", $depends);
33         for my $i (@vdeps) {
34                 $i =~ qr,([0-9a-z-+.]+),;
35                 push @$deps, $1;
36         }
37         $depends{$package} = $deps;
38 }
39
40 my $package;
41 while (<PACKAGES>) {
42         if (/^Package: ([0-9a-z-+.]+)/) {
43                 $package = $1;
44                 push @packages, $package;
45         }
46         if (/^Depends: (.*)/) {
47                 add_depends($package, $1);
48         }
49         if (/^Priority: (.*)/) {
50                 $priority{$package} = $1;
51         }
52         if (/^Essential: yes/) {
53                 $essential{$package} = "yes";
54         }
55 }
56
57 close(PACKAGES);
58
59 open(SOURCES, "<Sources");
60
61 my @sources = ();
62 my %binaries = ();
63 my %csource = ();
64 my %bdeps = ();
65
66 sub add_binaries {
67         my ($package, $binaries) = @_;
68         my $bb = [];
69         my @vbb = split(", ", $binaries);
70         for my $i (@vbb) {
71                 $i =~ qr,([0-9a-z-+.]+),;
72                 push @$bb, $1;
73                 $csource{$i} = $package;
74         }
75         $binaries{$package} = $bb;
76 }
77
78 sub add_bdeps {
79         my ($package, $bdeps) = @_;
80         my $bd = [];
81         my @vbd = split(", ", $bdeps);
82         for my $i (@vbd) {
83                 $i =~ qr,([0-9a-z-+.]+),;
84                 push @$bd, $1;
85         }
86         $bdeps{$package} = $bd;
87 }
88
89
90 while (<SOURCES>) {
91         if (/^Package: ([0-9a-z-+.]+)/) {
92                 $package = $1;
93                 push @packages, $package;
94         }
95         if (/^Binary: (.*)/) {
96                 add_binaries($package, $1);
97         }
98         if (/^Build-Depends: (.*)/) {
99                 add_bdeps($package, $1);
100         }
101 }
102
103 close(SOURCES);
104
105 my @pp = ();
106
107 my @visit = ();
108
109 for my $i (keys %essential) {
110         push @visit, $i;
111 }
112 push @visit, "build-essential";
113
114 while (@visit) {
115         my $n = pop @visit;
116         next if grep /^$n$/, @pp;
117         push @pp, $n;
118         my $l = $depends{$n};
119         for my $d (@$l) {
120                 if (!grep /^$d$/, @pp && !grep /^$d$/, @visit) {
121                         push @visit, $d;
122                 }
123         }
124 }
125
126 for my $i (@pp) {
127         print "$i\n";
128 }