]>
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>
30 // ShowHelp - Show a help screen /*{{{*/
31 // ---------------------------------------------------------------------
33 bool ShowHelp(CommandLine
&CmdL
) {
34 ioprintf(std::cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
35 COMMON_ARCH
,__DATE__
,__TIME__
);
38 _("Usage: apt-internal-solver\n"
40 "apt-internal-solver is an interface to use the current internal\n"
41 "like an external resolver for the APT family for debugging or alike\n"
44 " -h This help text.\n"
45 " -q Loggable output - no progress indicator\n"
46 " -c=? Read this configuration file\n"
47 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
51 int main(int argc
,const char *argv
[]) /*{{{*/
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
},
58 {'c',"config-file",0,CommandLine::ConfigFile
},
59 {'o',"option",0,CommandLine::ArbItem
},
62 CommandLine
CmdL(Args
,_config
);
63 if (pkgInitConfig(*_config
) == false ||
64 CmdL
.Parse(argc
,argv
) == false) {
69 // See if the help should be shown
70 if (_config
->FindB("help") == true ||
71 _config
->FindB("version") == true) {
76 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
78 if (pkgInitSystem(*_config
,_system
) == false) {
79 std::cerr
<< "System could not be initialized!" << std::endl
;
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
);
89 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgset
);
91 _error
->DumpErrors(std::cerr
);
95 // Deal with stdout not being a tty
96 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
97 _config
->Set("quiet","1");
99 if (_config
->FindI("quiet", 0) < 1)
100 _config
->Set("Debug::EDSP::WriteSolution", true);
102 _config
->Set("APT::Solver", "internal");
103 _config
->Set("edsp::scenario", "stdin");
104 int input
= STDIN_FILENO
;
105 FILE* output
= stdout
;
106 SetNonBlock(input
, false);
108 EDSP::WriteProgress(0, "Start up solver…", output
);
110 if (pkgInitSystem(*_config
,_system
) == false) {
111 std::cerr
<< "System could not be initialized!" << std::endl
;
115 EDSP::WriteProgress(1, "Read request…", output
);
117 if (WaitFd(input
, false, 5) == false)
118 std::cerr
<< "WAIT timed out in the resolver" << std::endl
;
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
;
127 EDSP::WriteProgress(5, "Read scenario…", output
);
129 pkgCacheFile CacheFile
;
130 CacheFile
.Open(NULL
, false);
132 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
134 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false) {
135 std::cerr
<< "Failed to apply request to depcache!" << std::endl
;
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
);
148 for (std::list
<std::string
>::const_iterator i
= install
.begin();
149 i
!= install
.end(); ++i
) {
150 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
155 for (std::list
<std::string
>::const_iterator i
= install
.begin();
156 i
!= install
.end(); ++i
)
157 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
159 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
161 if (upgrade
== true) {
162 if (pkgAllUpgrade(CacheFile
) == false) {
163 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occured", output
);
166 } else if (distUpgrade
== true) {
167 if (pkgDistUpgrade(CacheFile
) == false) {
168 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occured", output
);
171 } else if (Fix
.Resolve() == false) {
172 EDSP::WriteError("ERR_UNSOLVABLE", "An error occured", output
);
176 EDSP::WriteProgress(95, "Write solution…", output
);
178 if (EDSP::WriteSolution(CacheFile
, output
) == false) {
179 std::cerr
<< "Failed to output the solution!" << std::endl
;
183 EDSP::WriteProgress(100, "Done", output
);
185 bool const Errors
= _error
->PendingError();
186 if (_config
->FindI("quiet",0) > 0)
187 _error
->DumpErrors(std::cerr
);
189 _error
->DumpErrors(std::cerr
, GlobalError::DEBUG
);
190 return Errors
== true ? 100 : 0;