File: sort_deps

package info (click to toggle)
debian-cd 3.2.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,848 kB
  • sloc: sh: 6,129; perl: 4,129; makefile: 413
file content (1264 lines) | stat: -rwxr-xr-x 34,839 bytes parent folder | download | duplicates (2)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
#!/usr/bin/perl -w
#
# Copyright 1999 Raphaƫl Hertzog <hertzog@debian.org>
# Copyright 2006-2016 Steve McIntyre <93sam@debian.org>
# See the README file for the license
#
# This script takes 1 argument on input :
# - a filename listing all the packages to include
#
# and it sorts those packages such that dependencies are met in order
#
# Used to be called list2cds, renamed as it now just sorts
# dependencies. Later code in make_disc_trees.pl actually splits on a
# per-disc basis now.

use strict;
use Data::Dumper;
use Dpkg::Version;

my $listfile = shift;

my $nonfree = read_env('NONFREE', 0);
my $extranonfree = read_env('EXTRANONFREE', 0);
my $force_firmware = read_env('FORCE_FIRMWARE', 0);
my @nonfree_components = split /\ /, read_env('NONFREE_COMPONENTS', 'non-free');
my $local = read_env('LOCAL', 0);
my $complete = read_env('COMPLETE', 0);
my $add_rec = ! read_env('NORECOMMENDS', 1);
my $add_sug = ! read_env('NOSUGGESTS', 1);
my $verbose = read_env('VERBOSE', 0);
my $max_pkg_size = read_env('MAX_PKG_SIZE', 9999999999999);
my $codename = $ENV{'CODENAME'};
my $backports_list = read_env('BACKPORTS', "");
my $backports = 1;
if ($backports_list =~ /^$/) {
    $backports = 0;
}

my $apt = "$ENV{'BASEDIR'}/tools/apt-selection";
my $adir = "$ENV{'APTTMP'}/$ENV{'CODENAME'}-$ENV{'ARCH'}";
my $arch = "$ENV{'ARCH'}";
my $dir = "$ENV{'TDIR'}/$ENV{'CODENAME'}";

my $force_unstable_tasks = read_env('FORCE_SID_TASKSEL', 0);
my $tasks_packages = read_env('TASKS_PACKAGES',
                              "$ENV{'MIRROR'}/dists/sid/main/binary-$ENV{'ARCH'}/Packages.gz");
my @output = ();

$| = 1; # Autoflush for debugging

open(LOG, ">$dir/sort_deps.$arch.log")
    || die "Can't write in $dir/sort_deps.$arch.log !\n";

sub read_env {
    my $env_var = shift;
    my $default = shift;

    if (exists($ENV{$env_var})) {
        return $ENV{$env_var};
    }
    # else
    return $default;
}

sub msg {
    my $level = shift;
    if ($verbose >= $level) {
	print @_;
    }
    print LOG @_;
}

my %included;
my %excluded;
my %excluded_firmware;
my %packages;
my %backport_packages;

msg(0, "Running sort_deps to sort packages for $arch:\n");
msg(1, "======================================================================
Here are the settings you've chosen for making the list:
Architecture: $arch
List of prefered packages: $listfile
Output file: $dir/packages.$arch
");
msg(1, "Complete selected packages with all the rest: ");
msg(1, yesno($complete)."\n");
msg(1, "Include non-free packages: ");
msg(1, yesno($nonfree)."\n");
msg(1, "Non-free components:\n");
foreach my $cmp (@nonfree_components) {
    msg(1, "  - $cmp\n");
}
msg(1, "Force inclusion of firmware packages: ");
msg(1, yesno($force_firmware)."\n");
msg(1, "Include Recommends: ");
msg(1, yesno($add_rec)."\n");
msg(1, "Include Suggests: ");
msg(1, yesno($add_sug)."\n");
msg(1, "Maximum allowed package size: $max_pkg_size bytes\n");
msg(1, "======================================================================
");

# Look for firmware packages that we've been told to exclude
if ($force_firmware) {
    msg(1, "Reading tasks/exclude-firmware...\n");
    my $cpp_command = "cpp -undef -P -Wall -nostdinc -I$dir/tasks -DARCH$arch";
    my $excludelist = "$dir/tasks/exclude-firmware";
    open (EXLIST, "$cpp_command $excludelist |") or
	die "Can't parse exclude list: $!\n";
    while (defined (my $exc = <EXLIST>)) {
	chomp $exc;
	$excluded_firmware{$exc} = 1;
	msg(1, " - Ignore $exc\n");
    }
    close EXLIST;
}

# Get the information on all packages
my $oldrs = $/;
$/ = '';
msg(1, "Parsing packages...\n");
open(AVAIL, "$apt cache dumpavail |") || die "Can't fork : $!\n";
my ($p, $re);
my $num_pkgs = 0;
while (defined($_=<AVAIL>)) {
    next if not m/^Package: (\S+)\s*$/m;
    if (!$force_unstable_tasks || $1 !~ /^task-/) {
        parse_package(0, $_);
	$num_pkgs++;
    }
}
msg(1, "Got $num_pkgs packages\n");
close AVAIL or die "apt-cache failed : $@ ($!)\n";
if ($backports) {
    $num_pkgs = 0;
    msg(1, "Parsing packages from backports...\n");
    open(AVAIL, "USE_BP=1 $apt cache dumpavail |") || die "Can't fork : $!\n";
    my ($p, $re);
    while (defined($_=<AVAIL>)) {
	next if not m/^Package: (\S+)\s*$/m;
	if (!$force_unstable_tasks || $1 !~ /^task-/) {
	    parse_package(1, $_);
	    $num_pkgs++;
	}
    }
    msg(1, "Got $num_pkgs packages\n");
    close AVAIL or die "apt-cache (backports) failed : $@ ($!)\n";
}

# Read in the extra (new/unstable) tasks packages
if ($force_unstable_tasks) {
    my $num = 0;
    if ($tasks_packages =~ /\.gz$/) {
        open(AVAIL, "zcat $tasks_packages |") || die "Can't zcat $tasks_packages : $!\n";
    } else {
        open(AVAIL, "< $tasks_packages") || die "Can't open $tasks_packages for reading: $!\n";
    }
    while (defined($_=<AVAIL>)) {
        next if not m/^Package: (\S+)\s*$/m;
        if ($1 =~ /^task-/) {
            parse_package(0, $_);
            $num++;
        }
    }
    close AVAIL or die "reading unstable tasks failed : $@ ($!)\n";
    msg(0, "  Read $num tasks packages from $tasks_packages\n");
}

$/ = $oldrs;

# Get the list of excluded packages
%excluded = %included;
my $count_excl = 0;

# Now exclude packages because of the non-free rules
if (not $nonfree) {
    foreach my $cmp (@nonfree_components) {
	msg(1, "Checking for $cmp packages to exclude\n");
	foreach (grep { $packages{$_}{"Section"} =~ /$cmp\//}
		 (keys %packages)) {
	    if ($force_firmware and $packages{$_}{"IsFirmware"}) {
		if ($excluded_firmware{$_}) {
		    msg(1, " - force_firmware: dropping $cmp package $_ due to listing in tasks/exclude_firmware\n");
		    $excluded{$_} = 'nonfree-exclude-firmware';
		    $count_excl++;
		} else {
		    msg(1, " - force_firmware: keeping $cmp package $_\n");
		}
	    } else {
		msg(1, " - excluding $cmp package $_\n");
		$excluded{$_} = 'nonfree';
		$count_excl++;
	    }
	}
    }
}

msg(1, "Statistics:
Number of packages: @{ [scalar(keys %packages)] }
Number of excluded: $count_excl of @{ [scalar(keys %excluded)] }
======================================================================

");

open(STATS, "> $dir/stats.excluded.$arch") 
    || die "Can't write in stats.excluded.$arch: $!\n";
foreach (keys %excluded) {
    print STATS "$_ => $excluded{$_}\n";
}
close (STATS);

# Browse the list of packages to include
my ($output_size, $size) = (0, 0);
my %cds;

# Generate a dependency tree for each package
msg(0, "  Generating dependency tree with apt-cache depends...\n");
my (@list) = grep (!/\/$codename-backports$/, keys %packages);
while (@list) {
    my (@pkg) = splice(@list,0,200);
    $ENV{'LC_ALL'} = 'C'; # Required since apt is now translated
    open (APT, "$apt cache depends @pkg |") || die "Can't fork : $!\n";
    my (@res) = (<APT>);
    close APT or die "'apt-cache depends failed: $! \n";
    # Getting rid of conflicts/replaces/provides
    my $i = 0;
    my $nb_lines = scalar @res;
    push @res, ""; # Avoid warnings ...
    while ($i < $nb_lines) {
	if ($res[$i] !~ m/^(\S+)\s*$/) {
	    msg(0, "UNEXPECTED: Line `$res[$i]' while parsing " .
		"end of deptree from '$p'\n");
	    die "sort_deps failed! :-(\n";
	}
	$p = lc $1;
	$i++;
	msg(2, "  Dependency tree of `$p' ...\n");
	read_depends (\$i, \@res, $p);
    }
    
}
# Now redo with backports packages
if ($backports) {
    @list = grep (/\/$codename-backports$/, keys %packages);
    while (@list) {
	my (@pkg) = splice(@list,0,200);
	$ENV{'LC_ALL'} = 'C'; # Required since apt is now translated
	open (APT, "USE_BP=1 $apt cache depends @pkg |") || die "Can't fork : $!\n";
	my (@res) = (<APT>);
	close APT or die "'apt-cache depends failed: $! \n";
	# Getting rid of conflicts/replaces/provides
	my $i = 0;
	my $nb_lines = scalar @res;
	push @res, ""; # Avoid warnings ...
	while ($i < $nb_lines) {
	    if ($res[$i] !~ m/^(\S+)\s*$/) {
		msg(0, "UNEXPECTED: Line `$res[$i]' while parsing " .
		    "end of deptree from '$p'\n");
		die "sort_deps failed! :-(\n";
	    }
	    $p = lc $1;
	    $i++;
	    msg(2, "  Dependency tree of `$p/$codename-backports' ...\n");
	    read_depends (\$i, \@res, "$p/$codename-backports");
	}
    }
    # Now check and maybe up our dependencies in the backports
    # packages. If any of them depend on package versions not already
    # in the base set, change them to be package/$codename-backports
    # instead. Only need to check direct dependencies here; any
    # indirects going through base packages shouldn't have any
    # backports dependencies, of course. Can only do this once we've
    # read *all* the packages in.
    @list = grep (/\/$codename-backports$/, keys %packages);
    foreach my $pkg (@list) {
	msg(1, "Fixing up deps for $pkg\n");
	$packages{$pkg}{"Depends"} = fix_backport_depends($packages{$pkg}{"Depends"});
	$packages{$pkg}{"Recommends"} = fix_backport_depends($packages{$pkg}{"Recommends"});
	$packages{$pkg}{"Suggests"} = fix_backport_depends($packages{$pkg}{"Suggests"});
    }
}

msg(0, "  Adding standard, required, important and base packages first\n");
# Automatically include packages listed in the status file
open(STATUS, "< $adir/status") || die "Can't open status file $adir/status: $!\n";
while (defined($_ = <STATUS>)) {
    next if not m/^Package: (\S+)/;
    $p = $1;
    if (not exists $packages{$p}) {
	msg(1, "WARNING: Package `$p' is listed in the status file "
	    . "but doesn't exist ! (ignored) \n",
	    "    TIP: Try to generate the status file with " .
	    "make (correct)status (after a make distclean)...\n");
	next;
    }
    next if $excluded{$p};
    if ($p =~ /\/$codename-backports$/) {
	add_package($p, 1);
    } else {
	add_package($p, 0);
    }
}
close STATUS;
msg(0, "  S/R/I/B packages take $output_size bytes\n");

# Now start to look for packages wanted by the user ...
msg(0, "  Adding the rest of the requested packages\n");
open (LIST, "< $listfile") || die "Can't open $listfile : $!\n";
while (defined($_=<LIST>)) {
    chomp;
    msg(1, "Looking at list, line \"$_\"\n");
    next if m/^\s*$/;
    if (not exists $packages{$_}) { 
	msg(1, "WARNING: '$_' does not appear to be available ... " . 
	    "(ignored)\n");
	next;
    }
    next if $excluded{$_};
    if ($included{$_}) {
	msg(3, "$_ has already been included.\n");
	next;
    }
    # This is because udebs tend to have bad dependencies but work
    # nevertheless ... this may be removed once the udebs have a
    # better depencency system
    if ($packages{$_}{"IsUdeb"}) {
	add_to_output($_);
    } else {
	if ($_ =~ /\/$codename-backports$/) {
	    add_package($_, 1);
	} else {
	    add_package($_, 0);
	}
    }
}
close LIST;

msg(0, "  Now up to $output_size bytes\n");
# All requested packages have been included
# But we'll continue to add if $complete was requested
if ($complete) {
    msg(0, "  COMPLETE=1; add all remaining packages\n");
    # Try to sort them by section even if packages from
    # other sections will get in through dependencies
    # With some luck, most of them will already be here
    foreach my $p (sort { ($packages{lc $a}{"Section"} cmp $packages{lc $b}{"Section"})
                       || (lc $a cmp lc $b) }
             grep { not ($included{$_} or $excluded{$_}) } keys %packages) {
	# At this point, we should *not* be adding any more udebs,
	# as they're no use to anybody.
	if ($packages{lc $p}{"IsUdeb"}) {
	    msg(2, "  Ignoring udeb $p ...\n");
	} else {
	    if ($p =~ /\/$codename-backports$/i) {
		add_package (lc $p, 1);
	    } else {
		add_package (lc $p, 0);
	    }
	}
    }
}

# Now select the non-free packages for an extra CD
if ($extranonfree and (! $nonfree))
{
	my ($p, @toinclude);
	
	msg(0, "  Adding non-free* packages now\n");

	# Finally accept non-free packages that were excluded earlier ...
	foreach my $reason ("nonfree", "nonfree-exclude-firmware") {
	    foreach $p (grep { $excluded{$_} eq $reason } (keys %excluded))
	    {
		$excluded{$p} = 0;
		push @toinclude, $p;
	    }
	}
	
	# Include non-free packages
	foreach $p (@toinclude)
	{
	    if ($p =~ /\/$codename-backports$/i) {
		add_package (lc $p, 1);
	    } else {
		add_package (lc $p, 0);
	    }
	}

	# If a contrib package was listed in the list of packages to
	# include and if COMPLETE=0 there's a chance that the package
	# will not get included in any CD ... so I'm checking the complete
	# list again
	open (LIST, "< $listfile") || die "Can't open $listfile : $!\n";
	while (defined($_=<LIST>)) {
		chomp;
		next if m/^\s*$/;
		next if $included{$_};
		next if $included{lc $_};
		next if $excluded{$_};
		next if $excluded{lc $_};
		if (not exists $packages{$_} && not exists $packages{lc $_}) { 
		  msg(1, "WARNING: '$_' does not appear to be available ... " . 
	          	 "(ignored)\n");
		  next;
		}
		if ($packages{lc $p}{"IsUdeb"}) {
			msg(2, "  Ignoring udeb $p ...\n");
		} else {
		    if ($_ =~ /\/$codename-backports$/i) {
			add_package (lc $_, 1);
		    } else {
			add_package (lc $_, 0);
		    }
		}
	}
	close LIST;

	# Try to include other packages that could not be included
	# before (because they depends on excluded non-free packages)
	if ($complete)
	{
	    foreach $p (sort { ($packages{$a}{"Section"} 
				cmp $packages{$b}{"Section"}) || ($a cmp $b) }
			grep { not ($included{$_} or $excluded{$_}) } 
			keys %packages) 
	    {
			if ($packages{lc $p}{"IsUdeb"}) {
				msg(2, "  Ignoring udeb $p ...\n");
			} else {
			    if ($p =~ /\/$codename-backports$/i) {
				add_package (lc $p, 1);
			    } else {
				add_package (lc $p, 0);
			    }
			}
		}
	}

}

# Remove old files
foreach (glob("$dir/*.packages*")) {
	unlink $_;
}

# Now write the list down
my $count = 0;
open(CDLIST, "> $dir/packages.$arch") 
    || die "Can't write in $dir/$_.packages.$arch: $!\n";
open(FWLIST, ">> $dir/firmware-packages")
    || die "Can't write in $dir/firmware-packages: $!\n";
foreach (@output) {
    my $component = $packages{$_}{"Component"};
    my $size = $packages{$_}{"Size"};
    my $bu = $packages{$_}{"Built-Using"};
    print CDLIST "$arch:$component:$_:$size:$bu\n";
    if ($packages{$_}{"IsFirmware"}) {
        print FWLIST "$_\n";
    }
    $count++;
}
close CDLIST;
close FWLIST;
msg(0, "Done: processed/sorted $count packages, total size $output_size bytes.\n");

close LOG;

## END OF MAIN
## BEGINNING OF SUBS

sub parse_package {
	my $p;
	my $use_bp = shift;
	m/^Package: (\S+)\s*$/m and $p = $1;
	if ($use_bp) {
	    $p = "$p/$codename-backports";
	}
	$included{$p} = 0;
	$packages{$p}{"Package"} = $p;
	foreach $re (qw(Version Priority Section Filename Size MD5sum)) {
		(m/^$re: (\S+)\s*$/m and $packages{$p}{$re} = $1)
		|| msg(1, "Header field '$re' missing for package '$p'.\n");
	}
	$packages{$p}{"Depends"} = [];
	$packages{$p}{"Suggests"} = [];
	$packages{$p}{"Recommends"} = [];
	$packages{$p}{"Built-Using"} = "";	
	if (m/^Built-Using: (.*)$/m) {
	    my $built = $1;
	    $built =~ s/ \(= \S*\)//g;
	    $built =~ s/,//g;
	    $built =~ s/ /,/g;
	    $packages{$p}{"Built-Using"} = $built;
	}
	$packages{$p}{"IsUdeb"} = ($packages{$p}{"Filename"} =~ /.udeb$/) ? 1 : 0;
	$packages{$p}{"IsFirmware"} = ($packages{$p}{"Filename"} =~ /(firmware|microcode)/) ? 1 : 0;
	if ($packages{$p}{"Section"} =~ /contrib\//) {
		$packages{$p}{"Component"} = "contrib";
	} elsif ($packages{$p}{"Section"} =~ /non-free\//) {
		$packages{$p}{"Component"} = "non-free";
	} elsif ($packages{$p}{"Section"} =~ /non-free-firmware\//) {
		$packages{$p}{"Component"} = "non-free-firmware";
	} elsif ($packages{$p}{"IsUdeb"}) {
		$packages{$p}{"Component"} = "main-installer";
	} else {
		$packages{$p}{"Component"} = "main";
	}
}

sub dump_depend {
    my $tmpin = shift;
    my %d;
    my $ret = "";

    if ("ARRAY" eq ref($tmpin)) {
        my @array = @{$tmpin};
        foreach (@array) {
            %d = %$_;
            $ret .= $d{"Package"};
            if ($d{"CmpOp"} ne "") {
                $ret .= " (" . $d{"CmpOp"} . " " . $d{"Version"} . ")";
            }
            $ret .= " ";
        }
    } elsif ("HASH" eq ref($tmpin)) {
        %d = %$tmpin;
        $ret .= $d{"Package"};
        if ($d{"CmpOp"} ne "") {
            $ret .= " (" . $d{"CmpOp"} . " " . $d{"Version"} . ")";
        }
    } else {
        die "dump_depend: $tmpin is neither an array nor a hash!\n";
    }

    return $ret;
}

sub dump_or_list {
	my $out_type = shift;
	my $elt = shift;
	my @or = @$elt;

	if (scalar @or == 1) {
		msg(1, "    $out_type: " . dump_depend($or[0]) . "\n");
	} else {
		msg(1, "    $out_type: OR (");
		foreach my $t (@or) {
			msg(1, dump_depend($t) . " ");
		}
		msg(1, ")\n");
	}
}

sub read_depends {
	my $i = shift;     # Ref
	my $lines = shift; # Ref
	my $pkg = shift;   # string
	my $types = "(?:Pre)?Depends|Suggests|Recommends|Replaces|Conflicts|Breaks|Enhances";
	my (@dep, @rec, @sug);
	my ($type, $or);

	while ($lines->[$$i] =~ m/^\s([\s\|])($types):/) {
		$type = $2; $or = $1;
		# Get rid of replaces, conflicts and any other fields we don't
		# care about...
		if (($type eq "Replaces") or
			($type eq "Conflicts") or
			($type eq "Breaks") or
			($type eq "Enhances")) {
			$$i++;
			while ($lines->[$$i] =~ m/^\s{4}/) {
				$$i++;
			}
			next;
		}

		my $out_type = $type;
		$out_type =~ s/^Pre//; # PreDepends are like Depends for me 

		# Check the kind of depends : or, virtual, normal
		if ($or eq '|') {
			my $elt = read_ordepends ($i, $lines);
			dump_or_list($out_type, \@$elt);
			push @{$packages{$pkg}{$out_type}}, $elt;
		} elsif ($lines->[$$i] =~ m/^\s\s$type: <([^>]+)>/) {
			my $elt = read_virtualdepends ($i, $lines);
			foreach my $t (@$elt) {
				msg(1, "    $out_type: " . dump_depend($t) . " <virt>\n");
			}
			push @{$packages{$pkg}{$out_type}}, $elt;
		} elsif ($lines->[$$i] =~ m/^\s\s$type: (\S+)( \((\S+) (\S+)\))*/) {
			my @or;
			my %elt;
			$elt{"Package"} = $1;
			if (defined $2) {
				$elt{"CmpOp"} = $3;
				$elt{"Version"} = $4;
			} else {
				$elt{"CmpOp"} = "";
				$elt{"Version"} = "";
			}
			push @or, \%elt;
			$$i++;

			# Special case for packages providing not
			# truly virtual packages
			if ($lines->[$$i] =~ m/^\s{4}/) {
				while ($lines->[$$i] =~ m/\s{4}(\S+)( \((\S+) (\S+)\))*/) {
					my %elt1;
					$elt1{"Package"} = $1;
					if (defined $2) {
						$elt1{"CmpOp"} = $3;
						$elt1{"Version"} = $4;
					} else {
						$elt1{"CmpOp"} = "";
						$elt1{"Version"} = "";
					}
					push @or, \%elt1;
					$$i++;
				}
			}
			dump_or_list($out_type, \@or);
			push @{$packages{$pkg}{$out_type}}, \@or;
		} else {
			msg(0, "ERROR: Unknown depends line : $lines->[$$i]\n");
			foreach ($$i - 3 .. $$i + 3) {
				msg(0, "      ", $lines->[$_]);
			}
		}
	}
}

# Big matrix of tests. Check to see if the available version of a
# package matches what we're requesting in a dependency relationship
sub check_versions {
	my $wanted = shift;
	my $op = shift;
	my $available = shift;

	# Trivial check - if we don't care about versioning, anything will
	# do!
	if ($op eq "") {
		return 1;
	}

	# Ask the dpkg perl code to compare the version strings
	my $comp = version_compare($available, $wanted);

	if ($op eq "<=") {
		if ($comp == -1 || $comp == 0) {
			return 1;
		}
	} elsif ($op eq ">=") {
		if ($comp == 0 || $comp == 1) {
			return 1;
		}
	} elsif ($op eq "<<") {
		if ($comp == -1) {
			return 1;
		}
	} elsif ($op eq ">>") {
		if ($comp == 1) {
			return 1;
		}
	} elsif ($op eq "=") {
		if ($comp == 0) {
			return 1;
		}
	# Not sure this ("!") actually exists!
	# Mentioned in apt sources, but not in debian policy
	# No harm done by checking for it, though...
	} elsif ($op eq "!") {
		if ($comp == -1 || $comp == 1) {
			return 1;
		}
	}
	# else
	return 0;
}

# Check if a specific dependency package is installed already
sub dep_pkg_included {
	my $p = shift;
	my $check_backports = shift;
	my $need_udeb = shift;
	my %d = %$p;
	my $pn = $d{"Package"};

	if ($included{$pn}) {
		if (check_versions($d{"Version"}, $d{"CmpOp"}, $packages{$pn}{"Version"})) {
			if ($packages{$pn}{"IsUdeb"} == $need_udeb) {
				msg(1, "      $pn is included already, acceptable version " . $packages{$pn}{"Version"} . "\n");
				return 1;
			} else {
				my $explanation = "it's a udeb instead of regular deb";
				$explanation = "it's a deb instead of an udeb" if $need_udeb;
				msg(1, "      $pn is included already, but $explanation\n");
			}
		} else {
			msg(1, "      $pn is included already, but invalid version " . $packages{$pn}{"Version"} . "\n");
		}
	}
	msg(1, "    $pn not included in a useful version, check_backports $check_backports\n");
	if ($check_backports) {
	    $pn = "$pn/$codename-backports";
	    msg(1, "    Checking $pn too:\n");
	    if ($included{$pn}) {
		if (check_versions($d{"Version"}, $d{"CmpOp"}, $packages{$pn}{"Version"})) {
		    if ($packages{$pn}{"IsUdeb"} == $need_udeb) {
			msg(1, "      $pn is included already, acceptable version " . $packages{$pn}{"Version"} . "\n");
			return 1;
		    } else {
			my $explanation = "it's a udeb instead of regular deb";
			$explanation = "it's a deb instead of an udeb" if $need_udeb;
			msg(1, "      $pn is included already, but $explanation\n");
		    }
		} else {
		    msg(1, "      $pn is included already, but invalid version " . $packages{$pn}{"Version"} . "\n");
		}
		msg(1, "    $pn not included in a useful version\n");
	    }
	}
	# else
	return 0;
}

# Check backports package dependencies; update them if they are also only in backports
sub fix_backport_depends {
    my $deplist = shift;
    my @new_dep_list;
    foreach my $thisdep (@{$deplist}) {
	if ("ARRAY" eq ref($thisdep)) {
	    # If it's an OR list
	    my @new_or_list;
	    foreach my $pkg (@{$thisdep}) {
		my %t = %$pkg;
		my $pkgname = lc $t{"Package"};
		# Does the package exist?
		if (exists $excluded{$pkgname} &&
		    check_versions($t{"Version"}, $t{"CmpOp"}, $packages{$pkgname}{"Version"})) {
		    # Looks fine already
		    push (@new_or_list, $pkg);
		    next;
		}
		# Doesn't exist, or version doesn't work. Try backports
		$pkgname = "$pkgname/$codename-backports";
		if (exists $excluded{$pkgname} &&
		    check_versions($t{"Version"}, $t{"CmpOp"}, $packages{$pkgname}{"Version"})) {
		    my %elt;
		    $elt{"Package"} = $pkgname;
		    $elt{"CmpOp"} = $t{"CmpOp"};
		    $elt{"Version"} = $t{"Version"};
		    push @new_or_list, \%elt;
		    msg(1, "  Upgrading dep to $pkgname\n");
		    next;
		}
	    }
	    push (@new_dep_list, \@new_or_list);
	} else {
	    # It's virtual or a normal package
	    my %t = %{$thisdep};
	    my $pkgname = lc $t{"Package"};
	    # Does the package exist?
	    if (exists $excluded{$pkgname} &&
		check_versions($t{"Version"}, $t{"CmpOp"}, $packages{$pkgname}{"Version"})) {
		# Looks fine already
		push (@new_dep_list, $thisdep);
		next;
	    }
	    # Doesn't exist, or version doesn't work. Try backports
	    $pkgname = "$pkgname/$codename-backports";
	    if (exists $excluded{$pkgname} &&
		check_versions($t{"Version"}, $t{"CmpOp"}, $packages{$pkgname}{"Version"})) {
		my %elt;
		$elt{"Package"} = $pkgname;
		$elt{"CmpOp"} = $t{"CmpOp"};
		$elt{"Version"} = $t{"Version"};
		push @new_dep_list, \%elt;
		msg(1, "  Upgrading dep to $pkgname\n");
		next;
	    }
	}
    }
    return \@new_dep_list;
}

# Check to see if a dependency is satisfied, either a direct
# dependency or any one of an OR array
sub dep_satisfied {
    my $p = shift;
    my $check_backports = shift;
    my $need_udeb = shift;
    
    if ("ARRAY" eq ref $p) {
	foreach (@{$p}) {
	    if (dep_pkg_included($_, $check_backports, $need_udeb)) {
		return 1;
	    }
	}
    } elsif ("HASH" eq ref $p) {
	return dep_pkg_included($p, $check_backports, $need_udeb);
    } else {
    }
    return 0;
}

sub read_ordepends {
	my $i = shift;
	my $lines = shift;
	my @or = ();
	my ($val, $dep, $last) = ('','',0);
	my ($op, $version);

	chomp $lines->[$$i];
	
	while ($lines->[$$i] 
	            =~ m/^\s([\s\|])((?:Pre)?Depends|Suggests|Recommends): (\S+)( \((\S+) (\S+)\))*/) {
		$val = $3;
		if (defined $4) {
			$op = $5;
			$version = $6;
		} else {
			$op = "";
			$version = "";
		}
		$last = 1 if $1 ne '|'; #Stop when no more '|'
		if ($val =~ m/^<.*>$/) {
			$dep = read_virtualdepends ($i, $lines);
			if (ref $dep) {
				push @or, @{$dep};
			} else {
				push @or, $dep;
			}
		} else {
			my %elt;
			$elt{"Package"} = $val;
			$elt{"CmpOp"} = $op;
			$elt{"Version"} = $version;
			push @or, \%elt;
			$$i++;
			# Hack for packages providing not a truly
			# virtual package
			while ($lines->[$$i] =~ m/^\s{4}(\S+)( \((\S+) (\S+)\))*/) {
				my %elt1;
				$elt1{"Package"} = $1;
				if (defined $2) {
					$elt1{"CmpOp"} = $3;
					$elt1{"Version"} = $4;
				} else {
					$elt1{"CmpOp"} = "";
					$elt1{"Version"} = "";
				}
				msg(1, "    " . dump_depend(\%elt1) . "\n");
				push @or, \%elt1;
				$$i++;
			}
		}
		last if $last;
	}
	return \@or;
}

sub read_virtualdepends {
	my $i = shift;
	my $lines = shift;
	my $virtual;
	my @or = ();

	#Check for the lines with <>
	if ($lines->[$$i] 
	    =~ m/^\s[\s\|]((?:Pre)?Depends|Recommends|Suggests): <([^>]+)>/) {
	    $virtual = $2;
	    $$i++
	}
	# Now look at the alternatives on the following lines
	while ($lines->[$$i] =~ m/^\s{4}(\S+)( \((\S+) (\S+)\))*/) {
		my %elt;
		$elt{"Package"} = $1;
		if (defined $2) {
			$elt{"CmpOp"} = $3;
			$elt{"Version"} = $4;
		} else {
			$elt{"CmpOp"} = "";
			$elt{"Version"} = "";
		}
		push @or, \%elt;
		$$i++;
	}
	if (@or) {
		return \@or;
	} else {
		my %elt;
		$elt{"Package"} = $virtual;
		$elt{"CmpOp"} = "";
		$elt{"Version"} = "";
		push @or, \%elt;
		return \@or;
	}
}

sub add_package {
	my $p = shift;
	my $check_backports = shift;
	my ($ok, $reasons);
	
	msg(2, "+ Trying to add $p...\n");
	if ($included{$p}) {
		msg(2, "  Already included ...\n");
		return;
	}
	
	# Get all dependencies (not yet included) of each package
	my (@dep) = (get_missing ($p, $check_backports));

	# Stop here if apt failed
	if (not scalar(@dep)) {
		msg(2, "Can't add $p ... dependency problem.\n");
		return;
	}

	if ($packages{$p}{"Size"} > $max_pkg_size) {
	        $size = $packages{$p}{"Size"};
		msg(2, "Can't add $p ... too big! ($size > $max_pkg_size)\n");
		$excluded{$p} = 'toobig';
		return;
	}
	
	msg(3, "  \@dep before checklist = " . dump_depend(\@dep) . "\n");
	
	# Check if all packages are allowed (fail if one cannot)
	($ok, $reasons) = check_list (\@dep, 1, $check_backports);
	if (not $ok) {
		msg(2, "Can't add $p ... one of the packages needed has " .
		       "been refused. Reasons: $reasons\n"); 
		return;
	}
	
	msg(3, "  \@dep after checklist = " . dump_depend(\@dep) . "\n");
	
	# All packages are ok, now list them out and add sizes
	foreach my $t (@dep) {
		my %t = %$t;
		my $pkgname = $t{"Package"};
		add_to_output($pkgname);
        }
}

sub accepted {
	my $p = shift;
	if (exists $excluded{$p}) {
	    return not $excluded{$p}
	}
	# Return false for a non-existent package ...
	return 0;
}

sub get_missing {
	my $p = shift;
	my $check_backports = shift;
	my @deps_list = ();
	my %t;
	my $dep_text;
	my $found_missing = 0;

	$t{"Package"} = $p;
	$t{"CmpOp"} = "";
	$t{"Version"} = "";

	if (add_missing (\@deps_list, $packages{$p}{"Depends"}, \%t, 0, $check_backports)) {
		$found_missing = 1;
	}

	if ($add_rec and add_missing (\@deps_list, $packages{$p}{"Recommends"}, \%t, 1, $check_backports)) {
		$found_missing = 1;
	}

	if ($add_sug and add_missing (\@deps_list, $packages{$p}{"Suggests"}, \%t, 1, $check_backports)) {
		$found_missing = 1;
	}

	if (not $found_missing) {
		return ();
	}

	# Explicitly move the package itself to the end of the list,
	# i.e. *after* all its dependencies
	remove_entry(\%t, \@deps_list);
	push @deps_list, \%t;

	return (@deps_list);
}

# Recursive function adding packages to our list
sub add_missing {
	my $list = shift;
	my $new = shift;
	my $pkgin = shift;
	my @backup = @{$list};
	my $ok = 1;
	my $soft_depend = shift;
	my $check_backports = shift;
	my (%pkgin);

	if (ref $pkgin eq "HASH") {
		%pkgin = %$pkgin;
	} else {
		die "add_missing passed a non-hash";
	}

	my $need_udeb = $packages{$pkgin{"Package"}}{"IsUdeb"};
	my $pkgname = $pkgin{"Package"};

	# Check all dependencies 
	foreach my $thisdep (@{$new}) {
		my $textout = "";
		my $type_rel = "simple";

		if ("ARRAY" ne ref($thisdep)) {
			die "add_missing: $thisdep should be an array!\n";
		}

		# Print out status
		if (scalar(@{$thisdep} > 1)) {
			$textout = "(OR ";
			$type_rel = "OR";
		}
		foreach my $orpkg (@{$thisdep}) {
			$textout .= dump_depend($orpkg) . " ";
		}
		if (scalar(@{$thisdep} > 1)) {
			$textout .= ")";
		}
		msg(3, "    $pkgname Dep: $textout soft_depend $soft_depend\n");

		# Bail out early if we can!
		if (dep_satisfied ($thisdep, $check_backports, $need_udeb)) {
			next;
		}

		# Still work to do...
		my $or_ok = 0;

		# First check all the OR packages up-front with no
		# recursion. If *any* one of them is already installed, it
		# will do.
		foreach my $pkg (@{$thisdep}) {
			my %t = %$pkg;
			my $pkgname = lc $t{"Package"};

			if (exists $packages{$pkgname} &&
			    ($packages{$pkgname}{"Size"} > $max_pkg_size)) {
			    msg(2, "    $pkgname is too big, mark it as excluded\n");
			    $excluded{$pkgname} = 'toobig';
			}

			# Already installed?
			if (dep_satisfied($pkg, $check_backports, $need_udeb)) {
				msg(3, "    $type_rel relationship already installed: " . dump_depend($pkg) . "\n");
				$or_ok = 1;
				last;
			}

			# Pulled in already somewhere above us in the
			# depth-first search? (yes, we have to cope with
			# circular dependencies here...)
			if (is_in_dep_list($pkg, $list, $soft_depend)) {
				msg(3, "    $type_rel relationship already satisfied by current list " . dump_depend($pkg) . "\n");
				$or_ok = 1;
				last;
			}
			# else
			msg(3, "      " . dump_depend($pkg) . " not already installed\n");
		}

		# If we don't have any of the OR packages, then start
		# again and try them in order. We always add the first
		# package in the OR to allow APT to figure out which is
		# the better one to install for any combination of
		# packages that have similar alternative dependencies, but
		# in different order. Having the first alternative
		# available should be good enough for all cases we care
		# about.
		if (not $or_ok) {
			msg(3, "    $type_rel relationship not already satisfied, looking at alternatives in order, check_backports $check_backports\n");

			foreach my $pkg (@{$thisdep}) {
				my %t = %$pkg;
				my $pkgname = $t{"Package"};
				if (not accepted($pkgname)) {
				    if ($check_backports && accepted("$pkgname/$codename-backports")) {
					$pkgname = "$pkgname/$codename-backports";
					$t{"Package"} = $pkgname;
				    } else {
					next;
				    }
				}

				# Stop after the first
				# package that is
				# added successfully
				# FIXME! NEED TO CHECK IF VERSION DEPS ARE SATISFIED, FALL BACK TO BPO VERSION
				push (@{$list}, $pkg);
				if (add_missing ($list, $packages{$pkgname}{"Depends"}, $pkg, $soft_depend, $check_backports)) {
				    $or_ok = 1;
				    if ($add_rec) {
					add_missing ($list, $packages{$pkgname}{"Recommends"}, $pkg, 1, $check_backports);
				    }
				    if ($add_sug) {
					add_missing ($list, $packages{$pkgname}{"Suggests"}, $pkg, 1, $check_backports);
				    }
				    remove_entry($pkg, $list);
				    push @{$list}, $pkg;
				    last;
				} else {
				    pop @{$list};
				}
			}
		}
		$ok &&= $or_ok;
		if (not $ok) {
			$pkgname = $pkgin{"Package"};
			if ($soft_depend) {
				msg(1, "  $pkgname failed, couldn't satisfy $type_rel dep (but it's a soft dep, so ignoring...)\n");
				$ok = 1;
			} else {
				msg(1, "  $pkgname failed, couldn't satisfy $type_rel dep\n");
			}
		}
	}
	# If a problem has come up, then restore the original list
	if (not $ok) {
		@{$list} = @backup;
	}
	if (not is_in_dep_list(\%pkgin, $list, $soft_depend)) {
		push @{$list}, \%pkgin;
	}
	return $ok;
}

# Check if $value is in @{$array}
sub is_in {
	my $value = shift;
	my $array = shift;
	foreach my $key (@{$array}) {
		return 1 if ($key eq $value);
	}
	return 0;		
}

# Check if a package dependency is already in a dependency list
sub is_in_dep_list {
	my $hash = shift;
	my $array = shift;
	my $soft_dep = shift // 0;  # When analyzing recommends
	my %t = %$hash;

	foreach my $key (@{$array}) {
		my %a = %$key;
		if ($a{"Package"} eq $t{"Package"}) {
			my $pn = $a{"Package"};
			if ($soft_dep) {
				# We don't have to ensure exact version when we analyze
				# Recommends/Suggests
				return 1;
			} elsif (check_versions($t{"Version"}, $t{"CmpOp"}, $packages{$pn}{"Version"})) {
				return 1;
			}
		}
	}
	return 0;		
}

# Remove an antry from @{$array}
sub remove_entry {
	my $tmp1 = shift;
	my $array = shift;
	my $entries = scalar(@{$array});
	my $i;
	my %t1 = %$tmp1;

	for ($i=0; $i < $entries; $i++) {
		my $tmp2 = @{$array}[$i];
		my %t2 = %$tmp2;
		if ($t1{"Package"} eq $t2{"Package"}) {
			splice(@{$array}, $i, 1);
			$i--;
			$entries--;
		}
	}
}

# Check a list of packages
sub check_list {
	my $ref = shift;
	my $fail = shift;
	my $check_backports = shift;
	my $ok = 1;
	my @to_remove = ();
	my $reasons = "";
	foreach my $thispkg (@{$ref}) {
		my %t = %$thispkg;
		my $pkgname = $t{"Package"};
		if (not exists $excluded{$pkgname}) {
			msg(1,"  $pkgname has been refused because it doesn't exist ...\n");
			$ok = 0;
			push @to_remove, $thispkg;
			$reasons = $reasons . " noexist";
			next;
		}
		if (not accepted($pkgname)) {
			my $text = $excluded{"$pkgname"};
			msg(1,"  $pkgname has been refused because of $text ...\n");
			$ok = 0;
			push @to_remove, $thispkg;
			$reasons = $reasons . " " . $excluded{$pkgname};
			next;
		}
		if ($check_backports &&
		    ($pkgname !~ /\/$codename-backports/) &&
		    (not accepted("$pkgname/$codename-backports"))) {
			my $text = $excluded{"$pkgname/$codename-backports"};
			msg(1,"  $pkgname/$codename-backports has been refused because of $text} ...\n");
			$ok = 0;
			push @to_remove, $thispkg;
			$reasons = $reasons . " " . $excluded{$pkgname};
			next;
		}
		if ($included{$pkgname}) {
			msg(1, "  $pkgname has already been included.\n");
			push @to_remove, $thispkg;
			$reasons = $reasons . " alreadyinc";
			next;
		}
		if ($check_backports && $included{"$pkgname/$codename-backports"}) {
			msg(1, "  $pkgname/$codename-backports has already been included.\n");
			push @to_remove, $thispkg;
			$reasons = $reasons . " alreadyinc";
			next;
		}
	}
	foreach my $removed (@to_remove) {
		my %t = %$removed;
		my $pkgname = $t{"Package"};
		msg(2, "  Removing $pkgname ... ($reasons )\n");
		@{$ref} = grep { $_ ne $removed } @{$ref};
	}
	return ($fail ? $ok : 1, $reasons);
}

# Add packages to the output list
sub add_to_output {
	my $pkgname = shift;
	my $size = $packages{$pkgname}{"Size"};

	$output_size += $size;
	$included{$pkgname} = 1;
	push(@output, $pkgname);
}

sub yesno {
	my $in = shift;
	return $in ? "yes" : "no";
}