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