* Refactoring.

This commit is contained in:
Eelco Dolstra 2003-03-24 11:50:20 +00:00
parent 8d682ba551
commit 9d2f128252

View file

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <list> #include <list>
#include <map>
#include <cstdio> #include <cstdio>
#include <unistd.h> #include <unistd.h>
@ -225,22 +226,17 @@ void readPkgDescr(const string & pkgfile,
string getPkg(string hash); string getPkg(string hash);
typedef pair<string, string> EnvPair; typedef map<string, string> Environment;
typedef list<EnvPair> Environment;
void installPkg(string hash) void fetchDeps(string hash, Environment & env)
{ {
string pkgfile; string pkgfile;
string src;
string path;
string cmd;
string builder;
if (!queryDB(dbRefs, hash, pkgfile)) if (!queryDB(dbRefs, hash, pkgfile))
throw Error("unknown package " + hash); throw Error("unknown package " + hash);
cerr << "installing package " + hash + " from " + pkgfile + "\n"; cerr << "reading information about " + hash + " from " + pkgfile + "\n";
/* Verify that the file hasn't changed. !!! race */ /* Verify that the file hasn't changed. !!! race */
if (makeRef(pkgfile) != hash) if (makeRef(pkgfile) != hash)
@ -252,15 +248,13 @@ void installPkg(string hash)
/* Recursively fetch all the dependencies, filling in the /* Recursively fetch all the dependencies, filling in the
environment as we go along. */ environment as we go along. */
Environment env;
for (DepList::iterator it = pkgImports.begin(); for (DepList::iterator it = pkgImports.begin();
it != pkgImports.end(); it++) it != pkgImports.end(); it++)
{ {
cerr << "fetching package dependency " cerr << "fetching package dependency "
<< it->name << " <- " << it->ref << it->name << " <- " << it->ref
<< endl; << endl;
env.push_back(EnvPair(it->name, getPkg(it->ref))); env[it->name] = getPkg(it->ref);
} }
for (DepList::iterator it = fileImports.begin(); for (DepList::iterator it = fileImports.begin();
@ -278,15 +272,34 @@ void installPkg(string hash)
if (makeRef(file) != it->ref) if (makeRef(file) != it->ref)
throw Error("file " + file + " is stale"); throw Error("file " + file + " is stale");
if (it->name == "build") env[it->name] = file;
builder = file;
else
env.push_back(EnvPair(it->name, file));
} }
}
if (builder == "")
throw Error("no builder specified");
string getFromEnv(const Environment & env, const string & key)
{
Environment::const_iterator it = env.find(key);
if (it == env.end())
throw Error("key " + key + " not found in the environment");
return it->second;
}
void installPkg(string hash)
{
string pkgfile;
string src;
string path;
string cmd;
string builder;
Environment env;
/* Fetch dependencies. */
fetchDeps(hash, env);
builder = getFromEnv(env, "build");
/* Construct a path for the installed package. */ /* Construct a path for the installed package. */
path = pkgHome + "/" + hash; path = pkgHome + "/" + hash;
@ -361,62 +374,17 @@ string getPkg(string hash)
void runPkg(string hash) void runPkg(string hash)
{ {
string pkgfile;
string src; string src;
string path; string path;
string cmd; string cmd;
string runner; string runner;
if (!queryDB(dbRefs, hash, pkgfile))
throw Error("unknown package " + hash);
cerr << "running package " + hash + " from " + pkgfile + "\n";
/* Verify that the file hasn't changed. !!! race */
if (makeRef(pkgfile) != hash)
throw Error("file " + pkgfile + " is stale");
/* Read the package description file. */
DepList pkgImports, fileImports;
readPkgDescr(pkgfile, pkgImports, fileImports);
/* Recursively fetch all the dependencies, filling in the
environment as we go along. */
Environment env; Environment env;
for (DepList::iterator it = pkgImports.begin(); /* Fetch dependencies. */
it != pkgImports.end(); it++) fetchDeps(hash, env);
{
cerr << "fetching package dependency "
<< it->name << " <- " << it->ref
<< endl;
env.push_back(EnvPair(it->name, getPkg(it->ref)));
}
for (DepList::iterator it = fileImports.begin();
it != fileImports.end(); it++)
{
cerr << "fetching file dependency "
<< it->name << " = " << it->ref
<< endl;
string file;
if (!queryDB(dbRefs, it->ref, file))
throw Error("unknown file " + it->ref);
if (makeRef(file) != it->ref)
throw Error("file " + file + " is stale");
if (it->name == "run")
runner = file;
else
env.push_back(EnvPair(it->name, file));
}
if (runner == "")
throw Error("no runner specified");
runner = getFromEnv(env, "run");
/* Fork a child to build the package. */ /* Fork a child to build the package. */
pid_t pid; pid_t pid;
switch (pid = fork()) { switch (pid = fork()) {
@ -612,6 +580,9 @@ Subcommands:
Ensure that the package referenced by HASH is installed. Prints Ensure that the package referenced by HASH is installed. Prints
out the path of the package on stdout. out the path of the package on stdout.
listinst
Prints a list of installed packages.
run HASH run HASH
Run the descriptor referenced by HASH. Run the descriptor referenced by HASH.
"; ";