rename the 'universe' to 'scenario' to reflect the naming in the draft
[ntk/apt.git] / apt-pkg / edsp / edspwriter.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 Set of methods to help writing and reading everything needed for EDSP
5 ##################################################################### */
6 /*}}}*/
7 // Include Files /*{{{*/
8 #include <apt-pkg/edspwriter.h>
9 #include <apt-pkg/error.h>
10 #include <apt-pkg/configuration.h>
11 #include <apt-pkg/version.h>
12 #include <apt-pkg/policy.h>
13
14 #include <apti18n.h>
15 #include <limits>
16
17 #include <stdio.h>
18 /*}}}*/
19
20 // edspWriter::WriteUniverse - to the given file descriptor /*{{{*/
21 bool edspWriter::WriteScenario(pkgDepCache &Cache, FILE* output)
22 {
23 // we could use pkgCache::DepType and ::Priority, but these would be lokalized stringsā€¦
24 const char * const PrioMap[] = {0, "important", "required", "standard",
25 "optional", "extra"};
26 const char * const DepMap[] = {"", "Depends", "PreDepends", "Suggests",
27 "Recommends" , "Conflicts", "Replaces",
28 "Obsoletes", "Breaks", "Enhances"};
29
30 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
31 {
32 for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
33 {
34 fprintf(output, "Package: %s\n", Pkg.Name());
35 fprintf(output, "Architecture: %s\n", Ver.Arch());
36 fprintf(output, "Version: %s\n", Ver.VerStr());
37 if (Pkg.CurrentVer() == Ver)
38 fprintf(output, "Installed: yes\n");
39 if (Pkg->SelectedState == pkgCache::State::Hold)
40 fprintf(output, "Hold: yes\n");
41 fprintf(output, "APT-ID: %u\n", Ver->ID);
42 fprintf(output, "Priority: %s\n", PrioMap[Ver->Priority]);
43 if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
44 fprintf(output, "Essential: yes\n");
45 fprintf(output, "Section: %s\n", Ver.Section());
46 if (Ver->MultiArch == pkgCache::Version::Allowed || Ver->MultiArch == pkgCache::Version::AllAllowed)
47 fprintf(output, "Multi-Arch: allowed\n");
48 else if (Ver->MultiArch == pkgCache::Version::Foreign || Ver->MultiArch == pkgCache::Version::AllForeign)
49 fprintf(output, "Multi-Arch: foreign\n");
50 else if (Ver->MultiArch == pkgCache::Version::Same)
51 fprintf(output, "Multi-Arch: same\n");
52 signed short Pin = std::numeric_limits<signed short>::min();
53 for (pkgCache::VerFileIterator File = Ver.FileList(); File.end() == false; ++File) {
54 signed short const p = Cache.GetPolicy().GetPriority(File.File());
55 if (Pin < p)
56 Pin = p;
57 }
58 fprintf(output, "APT-Pin: %d\n", Pin);
59 if (Cache.GetCandidateVer(Pkg) == Ver)
60 fprintf(output, "APT-Candidate: yes\n");
61 if ((Cache[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto)
62 fprintf(output, "APT-Automatic: yes\n");
63 std::string dependencies[pkgCache::Dep::Enhances + 1];
64 bool orGroup = false;
65 for (pkgCache::DepIterator Dep = Ver.DependsList(); Dep.end() == false; ++Dep)
66 {
67 // Ignore implicit dependencies for multiarch here
68 if (strcmp(Pkg.Arch(), Dep.TargetPkg().Arch()) != 0)
69 continue;
70 if (orGroup == false)
71 dependencies[Dep->Type].append(", ");
72 dependencies[Dep->Type].append(Dep.TargetPkg().Name());
73 if (Dep->Version != 0)
74 dependencies[Dep->Type].append(" (").append(pkgCache::CompTypeDeb(Dep->CompareOp)).append(" ").append(Dep.TargetVer()).append(")");
75 if ((Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
76 {
77 dependencies[Dep->Type].append(" | ");
78 orGroup = true;
79 }
80 else
81 orGroup = false;
82 }
83 for (int i = 1; i < pkgCache::Dep::Enhances + 1; ++i)
84 if (dependencies[i].empty() == false)
85 fprintf(output, "%s: %s\n", DepMap[i], dependencies[i].c_str()+2);
86 string provides;
87 for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv)
88 {
89 // Ignore implicit provides for multiarch here
90 if (strcmp(Pkg.Arch(), Prv.ParentPkg().Arch()) != 0 || strcmp(Pkg.Name(),Prv.Name()) == 0)
91 continue;
92 provides.append(", ").append(Prv.Name());
93 }
94 if (provides.empty() == false)
95 fprintf(output, "Provides: %s\n", provides.c_str()+2);
96
97
98 fprintf(output, "\n");
99 }
100 }
101 return true;
102 }
103 /*}}}*/
104 // edspWriter::WriteRequest - to the given file descriptor /*{{{*/
105 bool edspWriter::WriteRequest(pkgDepCache &Cache, FILE* output)
106 {
107 string del, inst, upgrade;
108 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
109 {
110 string* req;
111 if (Cache[Pkg].Delete() == true)
112 req = &del;
113 else if (Cache[Pkg].NewInstall() == true)
114 req = &inst;
115 else if (Cache[Pkg].Upgrade() == true)
116 req = &upgrade;
117 else
118 continue;
119 req->append(", ").append(Pkg.FullName());
120 }
121 if (del.empty() == false)
122 fprintf(output, "Remove: %s\n", del.c_str()+2);
123 if (inst.empty() == false)
124 fprintf(output, "Install: %s\n", inst.c_str()+2);
125 if (upgrade.empty() == false)
126 fprintf(output, "Upgrade: %s\n", upgrade.c_str()+2);
127
128 return true;
129 }
130 /*}}}*/
131 // edspWriter::WriteSolution - to the given file descriptor /*{{{*/
132 bool edspWriter::WriteSolution(pkgDepCache &Cache, FILE* output)
133 {
134 bool const Debug = _config->FindB("Debug::EDSPWriter::WriteSolution", false);
135 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
136 {
137 if (Cache[Pkg].Delete() == true)
138 fprintf(output, "Remove: %d\n", Cache.GetCandidateVer(Pkg)->ID);
139 else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
140 fprintf(output, "Install: %d\n", Cache.GetCandidateVer(Pkg)->ID);
141 else
142 continue;
143 if (Debug == true)
144 fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Cache.GetCandidateVer(Pkg).VerStr());
145 fprintf(output, "\n");
146 }
147
148 return true;
149 }
150 /*}}}*/