utilities/installmgr.cpp File Reference

#include <swmgr.h>
#include <installmgr.h>
#include <ftptrans.h>
#include <filemgr.h>
#include <iostream>
#include <map>
#include <swmodule.h>
#include <stdio.h>
#include <swlog.h>
Include dependency graph for installmgr.cpp:

Go to the source code of this file.

Classes

class  MyInstallMgr
class  MyStatusReporter

Functions

void createBasicConfig (bool enableRemote, bool addCrossWire)
void finish (int status)
void init ()
void initConfig ()
void listModules (SWMgr *otherMgr=0, bool onlyNewAndUpdates=false)
void listRemoteSources ()
void localDirInstallModule (const char *dir, const char *modName)
void localDirListModules (const char *dir)
int main (int argc, char **argv)
void refreshRemoteSource (const char *sourceName)
void remoteInstallModule (const char *sourceName, const char *modName)
void remoteListModules (const char *sourceName, bool onlyNewAndUpdated=false)
void syncConfig ()
void uninstallModule (const char *modName)
void usage (const char *progName=0, const char *error=0)

Variables

SWBuf baseDir
SWBuf confPath
InstallMgrinstallMgr = 0
SWMgr * mgr = 0
StatusReporterstatusReporter = 0

Function Documentation

void createBasicConfig ( bool  enableRemote,
bool  addCrossWire 
)

Definition at line 145 of file installmgr.cpp.

00145                                                              {
00146 
00147     FileMgr::createParent(confPath.c_str());
00148     remove(confPath.c_str());
00149 
00150     InstallSource is("FTP");
00151     is.caption = "CrossWire";
00152     is.source = "ftp.crosswire.org";
00153     is.directory = "/pub/sword/raw";
00154 
00155     SWConfig config(confPath.c_str());
00156     config["General"]["PassiveFTP"] = "true";
00157     if (enableRemote) {
00158         config["Sources"]["FTPSource"] = is.getConfEnt();
00159     }
00160     config.Save();
00161 }

void finish ( int  status  ) 

Definition at line 130 of file installmgr.cpp.

00130                         {
00131     delete statusReporter;
00132     delete installMgr;
00133     delete mgr;
00134 
00135     installMgr = 0;
00136     mgr        = 0;
00137 
00138     if (status < 1) {
00139         cout << "\n";
00140         exit(status);
00141     }
00142 }

void init (  ) 

Definition at line 112 of file installmgr.cpp.

00112             {
00113     if (!mgr) {
00114         mgr = new SWMgr();
00115 
00116         if (!mgr->config)
00117             usage(0, "ERROR: SWORD configuration not found.  Please configure SWORD before using this program.");
00118 
00119         SWBuf baseDir = mgr->getHomeDir();
00120         if (baseDir.length() < 1) baseDir = ".";
00121         baseDir += "/.sword/InstallMgr";
00122         confPath = baseDir + "/InstallMgr.conf";
00123         statusReporter = new MyStatusReporter();
00124         installMgr = new MyInstallMgr(baseDir, statusReporter);
00125     }
00126 }

void initConfig (  ) 

Definition at line 164 of file installmgr.cpp.

00164                   {
00165     init();
00166 
00167     bool enable = installMgr->isUserDisclaimerConfirmed();
00168 
00169     createBasicConfig(enable, true);
00170 
00171     cout << "\n\nInitialized basic config file at [" << confPath << "]\n";
00172     cout << "with remote source features " << ((enable) ? "ENABLED" : "DISABLED") << "\n";
00173 }

void listModules ( SWMgr *  otherMgr = 0,
bool  onlyNewAndUpdates = false 
)

Definition at line 237 of file installmgr.cpp.

00237                                                                       {
00238     init();
00239     SWModule *module;
00240     if (!otherMgr) otherMgr = mgr;
00241     std::map<SWModule *, int> mods = InstallMgr::getModuleStatus(*mgr, *otherMgr);
00242     for (std::map<SWModule *, int>::iterator it = mods.begin(); it != mods.end(); it++) {
00243         module = it->first;
00244         SWBuf version = module->getConfigEntry("Version");
00245         SWBuf status = " ";
00246         if (it->second & InstallMgr::MODSTAT_NEW) status = "*";
00247         if (it->second & InstallMgr::MODSTAT_OLDER) status = "-";
00248         if (it->second & InstallMgr::MODSTAT_UPDATED) status = "+";
00249 
00250         if (!onlyNewAndUpdates || status == "*" || status == "+") {
00251             cout << status << "[" << module->getName() << "]  \t(" << version << ")  \t- " << module->getDescription() << "\n";
00252         }
00253     }
00254 }

void listRemoteSources (  ) 

Definition at line 211 of file installmgr.cpp.

00211                          {
00212     init();
00213     cout << "Remote Sources:\n\n";
00214     for (InstallSourceMap::iterator it = installMgr->sources.begin(); it != installMgr->sources.end(); it++) {
00215         cout << "[" << it->second->caption << "]\n";
00216         cout << "\tType     : " << it->second->type << "\n";
00217         cout << "\tSource   : " << it->second->source << "\n";
00218         cout << "\tDirectory: " << it->second->directory << "\n";
00219     }
00220 }

void localDirInstallModule ( const char *  dir,
const char *  modName 
)

Definition at line 300 of file installmgr.cpp.

00300                                                                  {
00301     init();
00302     SWMgr lmgr(dir);
00303     SWModule *module;
00304     ModMap::iterator it = lmgr.Modules.find(modName);
00305     if (it == lmgr.Modules.end()) {
00306         fprintf(stderr, "Module [%s] not available at path [%s]\n", modName, dir);
00307         finish(-4);
00308     }
00309     module = it->second;
00310     int error = installMgr->installModule(mgr, dir, module->getName());
00311     if (error) {
00312         cout << "\nError installing module: [" << module->getName() << "] (write permissions?)\n";
00313     } else cout << "\nInstalled module: [" << module->getName() << "]\n";
00314 }

void localDirListModules ( const char *  dir  ) 

Definition at line 269 of file installmgr.cpp.

00269                                           {
00270     cout << "Available Modules:\n\n";
00271     SWMgr mgr(dir);
00272     listModules(&mgr);
00273 }

int main ( int  argc,
char **  argv 
)

Definition at line 342 of file installmgr.cpp.

00342                                 {
00343 
00344     if (argc < 2) usage(*argv);
00345 
00346     for (int i = 1; i < argc; i++) {
00347         if (!strcmp(argv[i], "-d")) {
00348             SWLog::getSystemLog()->setLogLevel(SWLog::LOG_DEBUG);
00349         }
00350         else if (!strcmp(argv[i], "-init")) {
00351             initConfig();
00352         }
00353         else if (!strcmp(argv[i], "-l")) {  // list installed modules
00354             cout << "Installed Modules:\n\n";
00355             listModules();
00356         }
00357         else if (!strcmp(argv[i], "-ll")) { // list from local directory
00358             if (i+1 < argc) localDirListModules(argv[++i]);
00359             else usage(*argv, "-ll requires <path>");
00360         }
00361         else if (!strcmp(argv[i], "-li")) { // install from local directory
00362             if (i+2 < argc) {
00363                 const char *path = argv[++i];
00364                 const char *modName = argv[++i];
00365                 localDirInstallModule(path, modName);
00366             }
00367             else usage(*argv, "-li requires <path> <modName>");
00368         }
00369         else if (!strcmp(argv[i], "-u")) {  // uninstall module
00370             if (i+1 < argc) uninstallModule(argv[++i]);
00371             else usage(*argv, "-u requires <modName>");
00372         }
00373         else if (!strcmp(argv[i], "-s")) {  // list sources
00374             listRemoteSources();
00375         }
00376         else if (!strcmp(argv[i], "-sc")) { // sync config with master
00377             syncConfig();
00378         }
00379         else if (!strcmp(argv[i], "-r")) {  // refresh remote source
00380             if (i+1 < argc) refreshRemoteSource(argv[++i]);
00381             else usage(*argv, "-r requires <remoteSrcName>");
00382         }
00383         else if (!strcmp(argv[i], "-rl")) { // list remote modules
00384             if (i+1 < argc) remoteListModules(argv[++i]);
00385             else usage(*argv, "-rl requires <remoteSrcName>");
00386         }
00387         else if (!strcmp(argv[i], "-rd")) { // list differences between remote source and installed modules
00388             if (i+1 < argc) remoteListModules(argv[++i], true);
00389             else usage(*argv, "-rd requires <remoteSrcName>");
00390         }
00391         else if (!strcmp(argv[i], "-ri")) { // install from remote directory
00392             if (i+2 < argc) {
00393                 const char *source = argv[++i];
00394                 const char *modName = argv[++i];
00395                 remoteInstallModule(source, modName);
00396             }
00397             else usage(*argv, "-ri requires <remoteSrcName> <modName>");
00398         }
00399         else usage(*argv, (((SWBuf)"Unknown argument: ")+ argv[i]).c_str());
00400     }
00401 
00402     finish(0);
00403 
00404     return 0;
00405 }

void refreshRemoteSource ( const char *  sourceName  ) 

Definition at line 223 of file installmgr.cpp.

00223                                                  {
00224     init();
00225     InstallSourceMap::iterator source = installMgr->sources.find(sourceName);
00226     if (source == installMgr->sources.end()) {
00227         fprintf(stderr, "Couldn't find remote source [%s]\n", sourceName);
00228         finish(-3);
00229     }
00230 
00231     if (!installMgr->refreshRemoteSource(source->second))
00232         cout << "\nRemote Source Refreshed\n";
00233     else    cerr << "\nError Refreshing Remote Source\n";
00234 }

void remoteInstallModule ( const char *  sourceName,
const char *  modName 
)

Definition at line 276 of file installmgr.cpp.

00276                                                                       {
00277     init();
00278     InstallSourceMap::iterator source = installMgr->sources.find(sourceName);
00279     if (source == installMgr->sources.end()) {
00280         fprintf(stderr, "Couldn't find remote source [%s]\n", sourceName);
00281         finish(-3);
00282     }
00283     InstallSource *is = source->second;
00284     SWMgr *rmgr = is->getMgr();
00285     SWModule *module;
00286     ModMap::iterator it = rmgr->Modules.find(modName);
00287     if (it == rmgr->Modules.end()) {
00288         fprintf(stderr, "Remote source [%s] does not make available module [%s]\n", sourceName, modName);
00289         finish(-4);
00290     }
00291     module = it->second;
00292 
00293     int error = installMgr->installModule(mgr, 0, module->getName(), is);
00294     if (error) {
00295         cout << "\nError installing module: [" << module->getName() << "] (write permissions?)\n";
00296     } else cout << "\nInstalled module: [" << module->getName() << "]\n";
00297 }

void remoteListModules ( const char *  sourceName,
bool  onlyNewAndUpdated = false 
)

Definition at line 257 of file installmgr.cpp.

00257                                                                                {
00258     init();
00259     cout << "Available Modules:\n(be sure to refresh remote source (-r) first for most current list)\n\n";
00260     InstallSourceMap::iterator source = installMgr->sources.find(sourceName);
00261     if (source == installMgr->sources.end()) {
00262         fprintf(stderr, "Couldn't find remote source [%s]\n", sourceName);
00263         finish(-3);
00264     }
00265     listModules(source->second->getMgr(), onlyNewAndUpdated);
00266 }

void syncConfig (  ) 

Definition at line 176 of file installmgr.cpp.

00176                   {
00177     init();
00178 
00179     if (!installMgr->isUserDisclaimerConfirmed()) {  // assert disclaimer is accepted
00180         cout << "\n\nDisclaimer not accepted.  Aborting.";
00181         return;
00182     }
00183 
00184     // be sure we have at least some config file already out there
00185     if (!FileMgr::existsFile(confPath.c_str())) {
00186         createBasicConfig(true, false);
00187         finish(1); // cleanup and don't exit
00188         init();    // re-init with InstallMgr which uses our new config
00189     }
00190 
00191     if (!installMgr->refreshRemoteSourceConfiguration()) 
00192         cout << "\nSync'd config file with master remote source list.\n";
00193     else cout << "\nFailed to sync config file with master remote source list.\n";
00194 }

void uninstallModule ( const char *  modName  ) 

Definition at line 197 of file installmgr.cpp.

00197                                           {
00198     init();
00199     SWModule *module;
00200     ModMap::iterator it = mgr->Modules.find(modName);
00201     if (it == mgr->Modules.end()) {
00202         fprintf(stderr, "Couldn't find module [%s] to remove\n", modName);
00203         finish(-2);
00204     }
00205     module = it->second;
00206     installMgr->removeModule(mgr, module->getName());
00207     cout << "Removed module: [" << modName << "]\n";
00208 }

void usage ( const char *  progName = 0,
const char *  error = 0 
)

Definition at line 317 of file installmgr.cpp.

00317                                                     {
00318 
00319     if (error) fprintf(stderr, "\n%s: %s\n", (progName ? progName : "installmgr"), error);
00320 
00321     fprintf(stderr, "\nusage: %s <command> [command ...]\n"
00322         "\n  Commands (run in order they are passed):\n\n"
00323         "\t-init\t\t\t\tcreate a basic user config file.\n"
00324         "\t\t\t\t\t\tWARNING: overwrites existing.\n"
00325         "\t-sc\t\t\t\tsync config with known remote repo list\n"
00326         "\t\t\t\t\t\tNOTE: also creates if none exists\n"
00327         "\t-s\t\t\t\tlist remote sources\n"
00328         "\t-r  <remoteSrcName>\t\trefresh remote source\n"
00329         "\t-rl <remoteSrcName>\t\tlist available modules from remote source\n"
00330         "\t-rd <remoteSrcName>\t\tlist new/updated modules from remote source\n"
00331         "\t-ri <remoteSrcName> <modName>\tinstall module from remote source\n"
00332         "\t-l\t\t\t\tlist installed modules\n"
00333         "\t-u <modName>\t\t\tuninstall module\n"
00334         "\t-ll <path>\t\t\tlist available modules at local path\n"
00335         "\t-li <path> <modName>\t\tinstall module from local path\n"
00336         "\t-d\t\t\t\tturn debug output on\n"
00337         , (progName ? progName : "installmgr"));
00338     finish(-1);
00339 }


Variable Documentation

SWBuf baseDir

Definition at line 42 of file installmgr.cpp.

SWBuf confPath

Definition at line 43 of file installmgr.cpp.

Definition at line 40 of file installmgr.cpp.

SWMgr* mgr = 0

Definition at line 39 of file installmgr.cpp.

Definition at line 41 of file installmgr.cpp.


Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1