Debian Codegraph @

examples/apt-cache

raw
1 #!/usr/bin/perl
2
3 #
4 # Example: demonstrate accessing the package cache
5 #
6 # Usage: apt-cache PACKAGE
7 #
8
9 use AptPkg::Config '$_config';
10 use AptPkg::System '$_system';
11 use AptPkg::Cache;
12
13 (my $self = $0) =~ s#.*/##;
14
15 # initialise the global config object with the default values and
16 # setup the $_system object
17 $_config->init;
18 $_system = $_config->system;
19
20 # suppress cache building messages
21 $_config->{quiet} = 2;
22
23 # set up the cache
24 my $cache = AptPkg::Cache->new;
25 my $policy = $cache->policy;
26
27 die "Usage: $self PACKAGE ...\n" unless @ARGV;
28
29 for my $pack (@ARGV)
30 {
31 my $p = $cache->{$pack};
32 unless ($p)
33 {
34 warn "$self: don't know anything about package `$pack'\n";
35 next;
36 }
37
38 print "Package: $pack\n";
39 for my $field (qw/Section SelectedState InstState CurrentState Flags/)
40 {
41 print "$field: $p->{$field}\n" if $p->{$field};
42 }
43
44 print "CurrentVer: $p->{CurrentVer}{VerStr}\n" if $p->{CurrentVer};
45 if (my $c = $policy->candidate($p))
46 {
47 print "CandidateVer: ", $c->{VerStr}, "\n";
48 }
49
50 if (my $available = $p->{VersionList})
51 {
52 print "VersionList:\n";
53 my $i = 0;
54 for my $v (@$available)
55 {
56 print " [$i] $v->{VerStr}\n";
57 for my $f (map $_->{File}, @{$v->{FileList}})
58 {
59 printf " * %s (prio %d)\n", $f->{FileName},
60 $policy->priority($f);
61 }
62
63 for my $field (qw/MultiArch Arch Priority/)
64 {
65 print " $field: $v->{$field}\n" if $v->{$field};
66 }
67
68 if (my $deps = $v->{DependsList})
69 {
70 my $type = '';
71 my $delim = '';
72
73 for my $d (@$deps)
74 {
75 if ($d->{DepType} ne $type)
76 {
77 print "\n" if $type;
78 $type = $d->{DepType};
79 print " $type: ";
80 }
81 else
82 {
83 print $delim;
84 }
85
86 $delim = ($d->{CompType} & AptPkg::Dep::Or) ? ' | ' : ', ';
87 print $d->{TargetPkg}{ShortName};
88 print " ($d->{CompTypeDeb} $d->{TargetVer})"
89 if $d->{TargetVer};
90 }
91
92 print "\n";
93 }
94
95 if (my $p = $v->{ProvidesList})
96 {
97 print ' Provides: ', (join ', ', map $_->{Name}, @$p),
98 "\n";
99 }
100
101 $i++;
102 }
103 }
104
105 if (my $revdeps = $p->{RevDependsList})
106 {
107 print "RevDependsList:";
108 my $parent = '';
109 my $type = '';
110
111 for my $r (@$revdeps)
112 {
113 my $new_parent = "$r->{ParentPkg}{ShortName} $r->{ParentVer}{VerStr}";
114 unless ($new_parent eq $parent)
115 {
116 $parent = $new_parent;
117 $type = '';
118 }
119
120 if ($r->{DepType} ne $type)
121 {
122 printf "\n %-30s", $type ? '' : $parent;
123 $type = $r->{DepType};
124 print " $type: ";
125 }
126 else
127 {
128 print ', ';
129 }
130
131 print $r->{TargetPkg}{ShortName};
132 print " ($r->{CompTypeDeb} $r->{TargetVer})" if $r->{TargetVer};
133 }
134
135 print "\n";
136 }
137
138 if (my $provides = $p->{ProvidesList})
139 {
140 print 'ProvidesList: ', (join ', ',
141 map "$_->{OwnerPkg}{ShortName} $_->{OwnerVer}{VerStr}", @$provides),
142 "\n";
143 }
144
145 print "\n";
146 }
147
148 1;