]>
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>
29 // ShowHelp - Show a help screen /*{{{*/
30 // ---------------------------------------------------------------------
32 bool ShowHelp(CommandLine
&CmdL
) {
33 ioprintf(std::cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
34 COMMON_ARCH
,__DATE__
,__TIME__
);
37 _("Usage: apt-internal-solver\n"
39 "apt-internal-solver is an interface to use the current internal\n"
40 "like an external resolver for the APT family for debugging or alike\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");
50 int main(int argc
,const char *argv
[]) /*{{{*/
52 CommandLine::Args Args
[] = {
53 {'h',"help","help",0},
54 {'v',"version","version",0},
55 {'q',"quiet","quiet",CommandLine::IntLevel
},
56 {'q',"silent","quiet",CommandLine::IntLevel
},
57 {'c',"config-file",0,CommandLine::ConfigFile
},
58 {'o',"option",0,CommandLine::ArbItem
},
61 CommandLine
CmdL(Args
,_config
);
62 if (pkgInitConfig(*_config
) == false ||
63 CmdL
.Parse(argc
,argv
) == false) {
68 // See if the help should be shown
69 if (_config
->FindB("help") == true ||
70 _config
->FindB("version") == true) {
75 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
77 if (pkgInitSystem(*_config
,_system
) == false) {
78 std::cerr
<< "System could not be initialized!" << std::endl
;
81 pkgCacheFile CacheFile
;
82 CacheFile
.Open(NULL
, false);
83 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
84 FILE* output
= stdout
;
85 if (pkgset
.empty() == true)
86 EDSP::WriteScenario(CacheFile
, output
);
88 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgset
);
90 _error
->DumpErrors(std::cerr
);
94 // Deal with stdout not being a tty
95 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
96 _config
->Set("quiet","1");
98 if (_config
->FindI("quiet", 0) < 1)
99 _config
->Set("Debug::EDSP::WriteSolution", true);
101 _config
->Set("APT::Solver", "internal");
102 _config
->Set("edsp::scenario", "stdin");
103 int input
= STDIN_FILENO
;
104 FILE* output
= stdout
;
105 SetNonBlock(input
, false);
107 EDSP::WriteProgress(0, "Start up solver…", output
);
109 if (pkgInitSystem(*_config
,_system
) == false) {
110 std::cerr
<< "System could not be initialized!" << std::endl
;
114 EDSP::WriteProgress(1, "Read request…", output
);
116 if (WaitFd(input
, false, 5) == false)
117 std::cerr
<< "WAIT timed out in the resolver" << std::endl
;
119 std::list
<std::string
> install
, remove
;
120 bool upgrade
, distUpgrade
, autoRemove
;
121 if (EDSP::ReadRequest(input
, install
, remove
, upgrade
, distUpgrade
, autoRemove
) == false) {
122 std::cerr
<< "Parsing the request failed!" << std::endl
;
126 EDSP::WriteProgress(5, "Read scenario…", output
);
128 pkgCacheFile CacheFile
;
129 CacheFile
.Open(NULL
, false);
131 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
133 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false) {
134 std::cerr
<< "Failed to apply request to depcache!" << std::endl
;
138 pkgProblemResolver
Fix(CacheFile
);
139 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
140 i
!= remove
.end(); ++i
) {
141 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
147 for (std::list
<std::string
>::const_iterator i
= install
.begin();
148 i
!= install
.end(); ++i
) {
149 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
154 for (std::list
<std::string
>::const_iterator i
= install
.begin();
155 i
!= install
.end(); ++i
)
156 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
158 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
160 if (upgrade
== true) {
161 if (pkgAllUpgrade(CacheFile
) == false) {
162 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occured", output
);
165 } else if (distUpgrade
== true) {
166 if (pkgDistUpgrade(CacheFile
) == false) {
167 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occured", output
);
170 } else if (Fix
.Resolve() == false) {
171 EDSP::WriteError("ERR_UNSOLVABLE", "An error occured", output
);
175 EDSP::WriteProgress(95, "Write solution…", output
);
177 if (EDSP::WriteSolution(CacheFile
, output
) == false) {
178 std::cerr
<< "Failed to output the solution!" << std::endl
;
182 EDSP::WriteProgress(100, "Done", output
);
184 bool const Errors
= _error
->PendingError();
185 if (_config
->FindI("quiet",0) > 0)
186 _error
->DumpErrors(std::cerr
);
188 _error
->DumpErrors(std::cerr
, GlobalError::DEBUG
);
189 return Errors
== true ? 100 : 0;