move defines for version to macros.h
[ntk/apt.git] / cmdline / apt-internal-solver.cc
CommitLineData
4128c846
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* #####################################################################
4
5 cover around the internal solver to be able to run it like an external
6
7 ##################################################################### */
8 /*}}}*/
9// Include Files /*{{{*/
a00a9b44
DK
10#include <config.h>
11
4128c846
DK
12#include <apt-pkg/error.h>
13#include <apt-pkg/cmndline.h>
14#include <apt-pkg/init.h>
15#include <apt-pkg/cachefile.h>
472ff00e 16#include <apt-pkg/cacheset.h>
4128c846
DK
17#include <apt-pkg/strutl.h>
18#include <apt-pkg/edsp.h>
19#include <apt-pkg/algorithms.h>
20#include <apt-pkg/fileutl.h>
472ff00e 21#include <apt-pkg/pkgsystem.h>
82e369c4 22#include <apt-pkg/upgrade.h>
4128c846 23
4128c846
DK
24#include <unistd.h>
25#include <cstdio>
a00a9b44
DK
26
27#include <apti18n.h>
4128c846
DK
28 /*}}}*/
29
30// ShowHelp - Show a help screen /*{{{*/
31// ---------------------------------------------------------------------
32/* */
65512241 33static bool ShowHelp(CommandLine &) {
9179f697 34 ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
4128c846
DK
35 COMMON_ARCH,__DATE__,__TIME__);
36
37 std::cout <<
3999d158 38 _("Usage: apt-internal-solver\n"
4128c846 39 "\n"
3999d158 40 "apt-internal-solver is an interface to use the current internal\n"
4128c846
DK
41 "like an external resolver for the APT family for debugging or alike\n"
42 "\n"
43 "Options:\n"
44 " -h This help text.\n"
45 " -q Loggable output - no progress indicator\n"
46 " -c=? Read this configuration file\n"
3999d158 47 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
4128c846
DK
48 return true;
49}
50 /*}}}*/
51int main(int argc,const char *argv[]) /*{{{*/
52{
53 CommandLine::Args Args[] = {
54 {'h',"help","help",0},
55 {'v',"version","version",0},
56 {'q',"quiet","quiet",CommandLine::IntLevel},
57 {'q',"silent","quiet",CommandLine::IntLevel},
904be352
DK
58 {'c',"config-file",0,CommandLine::ConfigFile},
59 {'o',"option",0,CommandLine::ArbItem},
4128c846
DK
60 {0,0,0,0}};
61
62 CommandLine CmdL(Args,_config);
63 if (pkgInitConfig(*_config) == false ||
64 CmdL.Parse(argc,argv) == false) {
65 _error->DumpErrors();
66 return 2;
67 }
68
69 // See if the help should be shown
70 if (_config->FindB("help") == true ||
71 _config->FindB("version") == true) {
72 ShowHelp(CmdL);
73 return 1;
74 }
75
904be352
DK
76 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
77 {
78 if (pkgInitSystem(*_config,_system) == false) {
79 std::cerr << "System could not be initialized!" << std::endl;
80 return 1;
81 }
82 pkgCacheFile CacheFile;
83 CacheFile.Open(NULL, false);
84 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
85 FILE* output = stdout;
86 if (pkgset.empty() == true)
87 EDSP::WriteScenario(CacheFile, output);
88 else
89 EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
90 fclose(output);
91 _error->DumpErrors(std::cerr);
92 return 0;
93 }
94
4128c846
DK
95 // Deal with stdout not being a tty
96 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
97 _config->Set("quiet","1");
98
99 if (_config->FindI("quiet", 0) < 1)
100 _config->Set("Debug::EDSP::WriteSolution", true);
101
98278a81 102 _config->Set("APT::Solver", "internal");
4128c846
DK
103 _config->Set("edsp::scenario", "stdin");
104 int input = STDIN_FILENO;
105 FILE* output = stdout;
106 SetNonBlock(input, false);
107
e876223c
DK
108 EDSP::WriteProgress(0, "Start up solver…", output);
109
4128c846
DK
110 if (pkgInitSystem(*_config,_system) == false) {
111 std::cerr << "System could not be initialized!" << std::endl;
112 return 1;
113 }
114
e876223c
DK
115 EDSP::WriteProgress(1, "Read request…", output);
116
4128c846
DK
117 if (WaitFd(input, false, 5) == false)
118 std::cerr << "WAIT timed out in the resolver" << std::endl;
119
120 std::list<std::string> install, remove;
121 bool upgrade, distUpgrade, autoRemove;
122 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false) {
123 std::cerr << "Parsing the request failed!" << std::endl;
124 return 2;
125 }
126
e876223c
DK
127 EDSP::WriteProgress(5, "Read scenario…", output);
128
4128c846
DK
129 pkgCacheFile CacheFile;
130 CacheFile.Open(NULL, false);
131
e876223c
DK
132 EDSP::WriteProgress(50, "Apply request on scenario…", output);
133
4128c846
DK
134 if (EDSP::ApplyRequest(install, remove, CacheFile) == false) {
135 std::cerr << "Failed to apply request to depcache!" << std::endl;
136 return 3;
137 }
d9933172
DK
138
139 pkgProblemResolver Fix(CacheFile);
140 for (std::list<std::string>::const_iterator i = remove.begin();
141 i != remove.end(); ++i) {
142 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
143 Fix.Clear(P);
144 Fix.Protect(P);
145 Fix.Remove(P);
146 }
147
148 for (std::list<std::string>::const_iterator i = install.begin();
149 i != install.end(); ++i) {
150 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
151 Fix.Clear(P);
152 Fix.Protect(P);
153 }
154
4128c846
DK
155 for (std::list<std::string>::const_iterator i = install.begin();
156 i != install.end(); ++i)
157 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
158
e876223c 159 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
d9933172 160
80699703
DK
161 if (upgrade == true) {
162 if (pkgAllUpgrade(CacheFile) == false) {
1e3f4083 163 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occurred", output);
80699703
DK
164 return 0;
165 }
166 } else if (distUpgrade == true) {
167 if (pkgDistUpgrade(CacheFile) == false) {
1e3f4083 168 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occurred", output);
80699703
DK
169 return 0;
170 }
171 } else if (Fix.Resolve() == false) {
1e3f4083 172 EDSP::WriteError("ERR_UNSOLVABLE", "An error occurred", output);
4128c846
DK
173 return 0;
174 }
175
e876223c
DK
176 EDSP::WriteProgress(95, "Write solution…", output);
177
4128c846
DK
178 if (EDSP::WriteSolution(CacheFile, output) == false) {
179 std::cerr << "Failed to output the solution!" << std::endl;
180 return 4;
181 }
182
e876223c
DK
183 EDSP::WriteProgress(100, "Done", output);
184
4128c846
DK
185 bool const Errors = _error->PendingError();
186 if (_config->FindI("quiet",0) > 0)
904be352 187 _error->DumpErrors(std::cerr);
4128c846 188 else
904be352 189 _error->DumpErrors(std::cerr, GlobalError::DEBUG);
4128c846
DK
190 return Errors == true ? 100 : 0;
191}
192 /*}}}*/