]>
git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* #####################################################################
5 cover around the internal solver to be able to run it like an external
7 ##################################################################### */
9 // Include Files /*{{{*/
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/cmndline.h>
14 #include <apt-pkg/init.h>
15 #include <apt-pkg/cachefile.h>
16 #include <apt-pkg/cacheset.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/edsp.h>
19 #include <apt-pkg/algorithms.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/pkgsystem.h>
22 #include <apt-pkg/upgrade.h>
23 #include <apt-pkg/configuration.h>
24 #include <apt-pkg/depcache.h>
25 #include <apt-pkg/pkgcache.h>
26 #include <apt-pkg/cacheiterators.h>
38 // ShowHelp - Show a help screen /*{{{*/
39 // ---------------------------------------------------------------------
41 static bool ShowHelp(CommandLine
&) {
42 ioprintf(std::cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
43 COMMON_ARCH
,__DATE__
,__TIME__
);
46 _("Usage: apt-internal-solver\n"
48 "apt-internal-solver is an interface to use the current internal\n"
49 "like an external resolver for the APT family for debugging or alike\n"
52 " -h This help text.\n"
53 " -q Loggable output - no progress indicator\n"
54 " -c=? Read this configuration file\n"
55 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
59 int main(int argc
,const char *argv
[]) /*{{{*/
61 CommandLine::Args Args
[] = {
62 {'h',"help","help",0},
63 {'v',"version","version",0},
64 {'q',"quiet","quiet",CommandLine::IntLevel
},
65 {'q',"silent","quiet",CommandLine::IntLevel
},
66 {'c',"config-file",0,CommandLine::ConfigFile
},
67 {'o',"option",0,CommandLine::ArbItem
},
70 CommandLine
CmdL(Args
,_config
);
71 if (pkgInitConfig(*_config
) == false ||
72 CmdL
.Parse(argc
,argv
) == false) {
77 // See if the help should be shown
78 if (_config
->FindB("help") == true ||
79 _config
->FindB("version") == true) {
84 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
86 if (pkgInitSystem(*_config
,_system
) == false) {
87 std::cerr
<< "System could not be initialized!" << std::endl
;
90 pkgCacheFile CacheFile
;
91 CacheFile
.Open(NULL
, false);
92 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
93 FILE* output
= stdout
;
94 if (pkgset
.empty() == true)
95 EDSP::WriteScenario(CacheFile
, output
);
97 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgset
);
99 _error
->DumpErrors(std::cerr
);
103 // Deal with stdout not being a tty
104 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
105 _config
->Set("quiet","1");
107 if (_config
->FindI("quiet", 0) < 1)
108 _config
->Set("Debug::EDSP::WriteSolution", true);
110 _config
->Set("APT::Solver", "internal");
111 _config
->Set("edsp::scenario", "stdin");
112 int input
= STDIN_FILENO
;
113 FILE* output
= stdout
;
114 SetNonBlock(input
, false);
116 EDSP::WriteProgress(0, "Start up solver…", output
);
118 if (pkgInitSystem(*_config
,_system
) == false) {
119 std::cerr
<< "System could not be initialized!" << std::endl
;
123 EDSP::WriteProgress(1, "Read request…", output
);
125 if (WaitFd(input
, false, 5) == false)
126 std::cerr
<< "WAIT timed out in the resolver" << std::endl
;
128 std::list
<std::string
> install
, remove
;
129 bool upgrade
, distUpgrade
, autoRemove
;
130 if (EDSP::ReadRequest(input
, install
, remove
, upgrade
, distUpgrade
, autoRemove
) == false) {
131 std::cerr
<< "Parsing the request failed!" << std::endl
;
135 EDSP::WriteProgress(5, "Read scenario…", output
);
137 pkgCacheFile CacheFile
;
138 CacheFile
.Open(NULL
, false);
140 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
142 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false) {
143 std::cerr
<< "Failed to apply request to depcache!" << std::endl
;
147 pkgProblemResolver
Fix(CacheFile
);
148 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
149 i
!= remove
.end(); ++i
) {
150 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
156 for (std::list
<std::string
>::const_iterator i
= install
.begin();
157 i
!= install
.end(); ++i
) {
158 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
163 for (std::list
<std::string
>::const_iterator i
= install
.begin();
164 i
!= install
.end(); ++i
)
165 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
167 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
169 if (upgrade
== true) {
170 if (pkgAllUpgrade(CacheFile
) == false) {
171 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occurred", output
);
174 } else if (distUpgrade
== true) {
175 if (pkgDistUpgrade(CacheFile
) == false) {
176 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occurred", output
);
179 } else if (Fix
.Resolve() == false) {
180 EDSP::WriteError("ERR_UNSOLVABLE", "An error occurred", output
);
184 EDSP::WriteProgress(95, "Write solution…", output
);
186 if (EDSP::WriteSolution(CacheFile
, output
) == false) {
187 std::cerr
<< "Failed to output the solution!" << std::endl
;
191 EDSP::WriteProgress(100, "Done", output
);
193 bool const Errors
= _error
->PendingError();
194 if (_config
->FindI("quiet",0) > 0)
195 _error
->DumpErrors(std::cerr
);
197 _error
->DumpErrors(std::cerr
, GlobalError::DEBUG
);
198 return Errors
== true ? 100 : 0;