]>
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 /*{{{*/
10 #include <apt-pkg/error.h>
11 #include <apt-pkg/cmndline.h>
12 #include <apt-pkg/init.h>
13 #include <apt-pkg/cachefile.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/edsp.h>
16 #include <apt-pkg/algorithms.h>
17 #include <apt-pkg/fileutl.h>
26 // ShowHelp - Show a help screen /*{{{*/
27 // ---------------------------------------------------------------------
29 bool ShowHelp(CommandLine
&CmdL
) {
30 ioprintf(std::cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,VERSION
,
31 COMMON_ARCH
,__DATE__
,__TIME__
);
34 _("Usage: apt-internal-resolver\n"
36 "apt-internal-resolver is an interface to use the current internal\n"
37 "like an external resolver for the APT family for debugging or alike\n"
40 " -h This help text.\n"
41 " -q Loggable output - no progress indicator\n"
42 " -c=? Read this configuration file\n"
43 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
44 "apt.conf(5) manual pages for more information and options.\n"
45 " This APT has Super Cow Powers.\n");
49 int main(int argc
,const char *argv
[]) /*{{{*/
51 CommandLine::Args Args
[] = {
52 {'h',"help","help",0},
53 {'v',"version","version",0},
54 {'q',"quiet","quiet",CommandLine::IntLevel
},
55 {'q',"silent","quiet",CommandLine::IntLevel
},
56 {'c',"config-file",0,CommandLine::ConfigFile
},
57 {'o',"option",0,CommandLine::ArbItem
},
60 CommandLine
CmdL(Args
,_config
);
61 if (pkgInitConfig(*_config
) == false ||
62 CmdL
.Parse(argc
,argv
) == false) {
67 // See if the help should be shown
68 if (_config
->FindB("help") == true ||
69 _config
->FindB("version") == true) {
74 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
76 if (pkgInitSystem(*_config
,_system
) == false) {
77 std::cerr
<< "System could not be initialized!" << std::endl
;
80 pkgCacheFile CacheFile
;
81 CacheFile
.Open(NULL
, false);
82 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
83 FILE* output
= stdout
;
84 if (pkgset
.empty() == true)
85 EDSP::WriteScenario(CacheFile
, output
);
87 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgset
);
89 _error
->DumpErrors(std::cerr
);
93 // Deal with stdout not being a tty
94 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
95 _config
->Set("quiet","1");
97 if (_config
->FindI("quiet", 0) < 1)
98 _config
->Set("Debug::EDSP::WriteSolution", true);
100 _config
->Set("APT::Solver::Name", "internal");
101 _config
->Set("edsp::scenario", "stdin");
102 int input
= STDIN_FILENO
;
103 FILE* output
= stdout
;
104 SetNonBlock(input
, false);
106 EDSP::WriteProgress(0, "Start up solver…", output
);
108 if (pkgInitSystem(*_config
,_system
) == false) {
109 std::cerr
<< "System could not be initialized!" << std::endl
;
113 EDSP::WriteProgress(1, "Read request…", output
);
115 if (WaitFd(input
, false, 5) == false)
116 std::cerr
<< "WAIT timed out in the resolver" << std::endl
;
118 std::list
<std::string
> install
, remove
;
119 bool upgrade
, distUpgrade
, autoRemove
;
120 if (EDSP::ReadRequest(input
, install
, remove
, upgrade
, distUpgrade
, autoRemove
) == false) {
121 std::cerr
<< "Parsing the request failed!" << std::endl
;
125 EDSP::WriteProgress(5, "Read scenario…", output
);
127 pkgCacheFile CacheFile
;
128 CacheFile
.Open(NULL
, false);
130 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
132 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false) {
133 std::cerr
<< "Failed to apply request to depcache!" << std::endl
;
137 pkgProblemResolver
Fix(CacheFile
);
138 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
139 i
!= remove
.end(); ++i
) {
140 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
146 for (std::list
<std::string
>::const_iterator i
= install
.begin();
147 i
!= install
.end(); ++i
) {
148 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
153 for (std::list
<std::string
>::const_iterator i
= install
.begin();
154 i
!= install
.end(); ++i
)
155 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
157 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
159 if (Fix
.Resolve() == false) {
160 EDSP::WriteError("An error occured", output
);
164 EDSP::WriteProgress(95, "Write solution…", output
);
166 if (EDSP::WriteSolution(CacheFile
, output
) == false) {
167 std::cerr
<< "Failed to output the solution!" << std::endl
;
171 EDSP::WriteProgress(100, "Done", output
);
173 bool const Errors
= _error
->PendingError();
174 if (_config
->FindI("quiet",0) > 0)
175 _error
->DumpErrors(std::cerr
);
177 _error
->DumpErrors(std::cerr
, GlobalError::DEBUG
);
178 return Errors
== true ? 100 : 0;