* Updated hydra_eval_jobs for the new evaluator.

This commit is contained in:
Eelco Dolstra 2010-05-18 09:57:37 +00:00
parent 0c20748931
commit b59abaeb22

View file

@ -5,11 +5,9 @@
#include "store-api.hh" #include "store-api.hh"
#include "eval.hh" #include "eval.hh"
#include "parser.hh" #include "parser.hh"
#include "nixexpr-ast.hh"
#include "util.hh" #include "util.hh"
#include "xml-writer.hh" #include "xml-writer.hh"
#include "get-drvs.hh" #include "get-drvs.hh"
#include "aterm.hh"
using namespace nix; using namespace nix;
@ -23,57 +21,60 @@ void printHelp()
static Path gcRootsDir; static Path gcRootsDir;
Expr evalAttr(EvalState & state, Expr e) typedef std::map<Symbol, std::pair<unsigned int, Value> > ArgsUsed;
{ typedef std::map<Symbol, list<Value> > AutoArgs;
return e ? evalExpr(state, e) : e;
}
static void findJobs(EvalState & state, XMLWriter & doc, static void findJobs(EvalState & state, XMLWriter & doc,
const ATermMap & argsUsed, const ATermMap & argsLeft, const ArgsUsed & argsUsed, const AutoArgs & argsLeft,
Expr e, const string & attrPath); Value & v, const string & attrPath);
static void tryJobAlts(EvalState & state, XMLWriter & doc, static void tryJobAlts(EvalState & state, XMLWriter & doc,
const ATermMap & argsUsed, const ATermMap & argsLeft, const ArgsUsed & argsUsed, const AutoArgs & argsLeft,
const string & attrPath, Expr fun, const string & attrPath, Value & fun,
ATermList formals, const ATermMap & actualArgs) Formals::Formals_::iterator cur,
Formals::Formals_::iterator last,
const Bindings & actualArgs)
{ {
if (formals == ATempty) { if (cur == last) {
findJobs(state, doc, argsUsed, argsLeft, Value v, arg;
makeCall(fun, makeAttrs(actualArgs)), attrPath); state.mkAttrs(arg);
*arg.attrs = actualArgs;
mkApp(v, fun, arg);
findJobs(state, doc, argsUsed, argsLeft, v, attrPath);
return; return;
} }
Expr name; ATerm def2; ATermList values; AutoArgs::const_iterator a = argsLeft.find(cur->name);
if (!matchFormal(ATgetFirst(formals), name, def2)) abort();
if ((values = (ATermList) argsLeft.get(name))) { if (a == argsLeft.end())
int n = 0;
for (ATermIterator i(ATreverse(values)); i; ++i, ++n) {
ATermMap actualArgs2(actualArgs);
ATermMap argsUsed2(argsUsed);
ATermMap argsLeft2(argsLeft);
actualArgs2.set(name, makeAttrRHS(*i, makeNoPos()));
argsUsed2.set(name, (ATerm) ATmakeList2(*i, (ATerm) ATmakeInt(n)));
argsLeft2.remove(name);
tryJobAlts(state, doc, argsUsed2, argsLeft2, attrPath, fun, ATgetNext(formals), actualArgs2);
}
}
else
throw TypeError(format("job `%1%' requires an argument named `%2%'") throw TypeError(format("job `%1%' requires an argument named `%2%'")
% attrPath % aterm2String(name)); % attrPath % cur->name);
Formals::Formals_::iterator next = cur; ++next;
int n = 0;
foreach (list<Value>::const_iterator, i, a->second) {
Bindings actualArgs2(actualArgs); // !!! inefficient
ArgsUsed argsUsed2(argsUsed);
AutoArgs argsLeft2(argsLeft);
actualArgs2[cur->name].value = *i;
argsUsed2[cur->name] = std::pair<unsigned int, Value>(n, *i);
argsLeft2.erase(cur->name);
tryJobAlts(state, doc, argsUsed2, argsLeft2, attrPath, fun, next, last, actualArgs2);
++n;
}
} }
static void showArgsUsed(XMLWriter & doc, const ATermMap & argsUsed) static void showArgsUsed(XMLWriter & doc, const ArgsUsed & argsUsed)
{ {
foreach (ATermMap::const_iterator, i, argsUsed) { foreach (ArgsUsed::const_iterator, i, argsUsed) {
XMLAttrs xmlAttrs2; XMLAttrs xmlAttrs2;
xmlAttrs2["name"] = aterm2String(i->key); xmlAttrs2["name"] = i->first;
xmlAttrs2["value"] = showValue(ATelementAt((ATermList) i->value, 0)); xmlAttrs2["value"] = (format("%1%") % i->second.second).str();
xmlAttrs2["altnr"] = int2String(ATgetInt((ATermInt) ATelementAt((ATermList) i->value, 1))); xmlAttrs2["altnr"] = int2String(i->second.first);
doc.writeEmptyElement("arg", xmlAttrs2); doc.writeEmptyElement("arg", xmlAttrs2);
} }
} }
@ -100,26 +101,18 @@ static int queryMetaFieldInt(MetaInfo & meta, const string & name, int def)
static void findJobsWrapped(EvalState & state, XMLWriter & doc, static void findJobsWrapped(EvalState & state, XMLWriter & doc,
const ATermMap & argsUsed, const ATermMap & argsLeft, const ArgsUsed & argsUsed, const AutoArgs & argsLeft,
Expr e, const string & attrPath) Value & v, const string & attrPath)
{ {
debug(format("at path `%1%'") % attrPath); debug(format("at path `%1%'") % attrPath);
e = evalExpr(state, e); state.forceValue(v);
ATermList as, formals; if (v.type == tAttrs) {
ATermBool ellipsis;
ATerm pat, body, pos;
string s;
PathSet context;
if (matchAttrs(e, as)) {
ATermMap attrs;
queryAllAttrs(e, attrs);
DrvInfo drv; DrvInfo drv;
if (getDerivation(state, e, drv)) { if (getDerivation(state, v, drv)) {
XMLAttrs xmlAttrs; XMLAttrs xmlAttrs;
Path drvPath; Path drvPath;
@ -159,27 +152,30 @@ static void findJobsWrapped(EvalState & state, XMLWriter & doc,
} }
else { else {
foreach (ATermMap::const_iterator, i, attrs) foreach (Bindings::iterator, i, *v.attrs)
findJobs(state, doc, argsUsed, argsLeft, i->value, findJobs(state, doc, argsUsed, argsLeft, i->second.value,
(attrPath.empty() ? "" : attrPath + ".") + aterm2String(i->key)); (attrPath.empty() ? "" : attrPath + ".") + (string) i->first);
} }
} }
else if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis)) { else if (v.type == tLambda && v.lambda.fun->matchAttrs) {
tryJobAlts(state, doc, argsUsed, argsLeft, attrPath, e, formals, ATermMap()); tryJobAlts(state, doc, argsUsed, argsLeft, attrPath, v,
v.lambda.fun->formals->formals.begin(),
v.lambda.fun->formals->formals.end(),
Bindings());
} }
else else
throw TypeError(format("unknown value: %1%") % showValue(e)); throw TypeError(format("unsupported value: %1%") % v);
} }
static void findJobs(EvalState & state, XMLWriter & doc, static void findJobs(EvalState & state, XMLWriter & doc,
const ATermMap & argsUsed, const ATermMap & argsLeft, const ArgsUsed & argsUsed, const AutoArgs & argsLeft,
Expr e, const string & attrPath) Value & v, const string & attrPath)
{ {
try { try {
findJobsWrapped(state, doc, argsUsed, argsLeft, e, attrPath); findJobsWrapped(state, doc, argsUsed, argsLeft, v, attrPath);
} catch (EvalError & e) { } catch (EvalError & e) {
XMLAttrs xmlAttrs; XMLAttrs xmlAttrs;
xmlAttrs["location"] = attrPath; xmlAttrs["location"] = attrPath;
@ -194,7 +190,7 @@ void run(Strings args)
{ {
EvalState state; EvalState state;
Path releaseExpr; Path releaseExpr;
ATermMap autoArgs; AutoArgs autoArgs;
for (Strings::iterator i = args.begin(); i != args.end(); ) { for (Strings::iterator i = args.begin(); i != args.end(); ) {
string arg = *i++; string arg = *i++;
@ -207,12 +203,12 @@ void run(Strings args)
string name = *i++; string name = *i++;
if (i == args.end()) throw UsageError("missing argument"); if (i == args.end()) throw UsageError("missing argument");
string value = *i++; string value = *i++;
Expr e = arg == "--arg" Value v;
? evalExpr(state, parseExprFromString(state, value, absPath("."))) if (arg == "--arg")
: makeStr(value); state.eval(parseExprFromString(state, value, absPath(".")), v);
autoArgs.set(toATerm(name), (ATerm) ATinsert(autoArgs.get(toATerm(name)) else
? (ATermList) autoArgs.get(toATerm(name)) mkString(v, value);
: ATempty, e)); autoArgs[state.symbols.create(name)].push_back(v);
} }
else if (arg == "--gc-roots-dir") { else if (arg == "--gc-roots-dir") {
if (i == args.end()) throw UsageError("missing argument"); if (i == args.end()) throw UsageError("missing argument");
@ -230,11 +226,13 @@ void run(Strings args)
store = openStore(); store = openStore();
Expr e = parseExprFromFile(state, releaseExpr); Expr * e = parseExprFromFile(state, releaseExpr);
Value v;
state.mkThunk_(v, e);
XMLWriter doc(true, std::cout); XMLWriter doc(true, std::cout);
XMLOpenElement root(doc, "jobs"); XMLOpenElement root(doc, "jobs");
findJobs(state, doc, ATermMap(), autoArgs, e, ""); findJobs(state, doc, ArgsUsed(), autoArgs, v, "");
} }