2009-03-05 14:59:43 +00:00
|
|
|
#include <map>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "eval.hh"
|
|
|
|
#include "parser.hh"
|
2009-03-05 15:41:43 +00:00
|
|
|
#include "nixexpr-ast.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
#include "xml-writer.hh"
|
2009-03-06 15:16:29 +00:00
|
|
|
#include "get-drvs.hh"
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
|
|
|
void printHelp()
|
|
|
|
{
|
|
|
|
std::cout << "Syntax: eval-jobs <expr>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 15:41:43 +00:00
|
|
|
Expr evalAttr(EvalState & state, Expr e)
|
|
|
|
{
|
|
|
|
return e ? evalExpr(state, e) : e;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void findJobs(EvalState & state, XMLWriter & doc,
|
2009-03-06 16:55:19 +00:00
|
|
|
const ATermMap & argsUsed, const ATermMap & argsLeft,
|
|
|
|
Expr e, const string & attrPath);
|
|
|
|
|
|
|
|
|
|
|
|
static void tryJobAlts(EvalState & state, XMLWriter & doc,
|
|
|
|
const ATermMap & argsUsed, const ATermMap & argsLeft,
|
|
|
|
const string & attrPath, Expr fun,
|
|
|
|
ATermList formals, const ATermMap & actualArgs)
|
|
|
|
{
|
|
|
|
if (formals == ATempty) {
|
|
|
|
findJobs(state, doc, argsUsed, argsLeft,
|
|
|
|
makeCall(fun, makeAttrs(actualArgs)), attrPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr name, def; ATerm def2; ATermList values;
|
|
|
|
if (!matchFormal(ATgetFirst(formals), name, def2)) abort();
|
|
|
|
|
|
|
|
if ((values = (ATermList) argsLeft.get(name))) {
|
|
|
|
|
|
|
|
for (ATermIterator i(ATreverse(values)); i; ++i) {
|
|
|
|
ATermMap actualArgs2(actualArgs);
|
|
|
|
ATermMap argsUsed2(argsUsed);
|
|
|
|
ATermMap argsLeft2(argsLeft);
|
|
|
|
actualArgs2.set(name, makeAttrRHS(*i, makeNoPos()));
|
|
|
|
argsUsed2.set(name, *i);
|
|
|
|
argsLeft2.remove(name);
|
|
|
|
tryJobAlts(state, doc, argsUsed2, argsLeft2, attrPath, fun, ATgetNext(formals), actualArgs2);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (!matchDefaultValue(def2, def))
|
|
|
|
throw TypeError(format("cannot auto-call a function that has an argument without a default value (`%1%')")
|
|
|
|
% aterm2String(name));
|
|
|
|
else
|
|
|
|
tryJobAlts(state, doc, argsUsed, argsLeft, attrPath, fun, ATgetNext(formals), actualArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void findJobs(EvalState & state, XMLWriter & doc,
|
|
|
|
const ATermMap & argsUsed, const ATermMap & argsLeft,
|
2009-03-05 15:41:43 +00:00
|
|
|
Expr e, const string & attrPath)
|
|
|
|
{
|
2009-03-06 16:55:19 +00:00
|
|
|
debug(format("at path `%1%'") % attrPath);
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
|
|
e = evalExpr(state, e);
|
|
|
|
|
2009-03-06 15:18:59 +00:00
|
|
|
ATermList as, es, formals;
|
|
|
|
ATermBool ellipsis;
|
2009-03-05 15:41:43 +00:00
|
|
|
ATerm pat, body, pos;
|
|
|
|
string s;
|
|
|
|
PathSet context;
|
|
|
|
|
|
|
|
if (matchAttrs(e, as)) {
|
|
|
|
ATermMap attrs;
|
|
|
|
queryAllAttrs(e, attrs);
|
|
|
|
|
2009-03-06 15:16:29 +00:00
|
|
|
DrvInfo drv;
|
|
|
|
|
|
|
|
if (getDerivation(state, e, drv)) {
|
2009-03-05 15:41:43 +00:00
|
|
|
XMLAttrs xmlAttrs;
|
|
|
|
Path outPath, drvPath;
|
|
|
|
|
|
|
|
xmlAttrs["name"] = attrPath;
|
2009-03-06 15:16:29 +00:00
|
|
|
xmlAttrs["system"] = drv.system;
|
|
|
|
xmlAttrs["drvPath"] = drv.queryDrvPath(state);
|
|
|
|
xmlAttrs["outPath"] = drv.queryOutPath(state);
|
|
|
|
xmlAttrs["description"] = drv.queryMetaInfo(state, "description");
|
|
|
|
xmlAttrs["longDescription"] = drv.queryMetaInfo(state, "longDescription");
|
|
|
|
xmlAttrs["license"] = drv.queryMetaInfo(state, "license");
|
|
|
|
xmlAttrs["homepage"] = drv.queryMetaInfo(state, "homepage");
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
|
|
XMLOpenElement _(doc, "job", xmlAttrs);
|
2009-03-06 16:55:19 +00:00
|
|
|
|
|
|
|
foreach (ATermMap::const_iterator, i, argsUsed) {
|
|
|
|
XMLAttrs xmlAttrs2;
|
|
|
|
xmlAttrs2["name"] = aterm2String(i->key);
|
|
|
|
xmlAttrs2["value"] = showValue(i->value);
|
|
|
|
doc.writeEmptyElement("arg", xmlAttrs2);
|
|
|
|
}
|
2009-03-05 15:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
foreach (ATermMap::const_iterator, i, attrs)
|
2009-03-06 16:55:19 +00:00
|
|
|
findJobs(state, doc, argsUsed, argsLeft, i->value,
|
2009-03-05 15:41:43 +00:00
|
|
|
(attrPath.empty() ? "" : attrPath + ".") + aterm2String(i->key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 15:18:59 +00:00
|
|
|
else if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis)) {
|
2009-03-06 16:55:19 +00:00
|
|
|
tryJobAlts(state, doc, argsUsed, argsLeft, attrPath, e, formals, ATermMap());
|
2009-03-05 15:41:43 +00:00
|
|
|
}
|
2009-03-06 16:55:19 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
printMsg(lvlError, format("unknown value: %1%") % showValue(e));
|
2009-03-05 15:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 14:59:43 +00:00
|
|
|
void run(Strings args)
|
|
|
|
{
|
|
|
|
EvalState state;
|
|
|
|
Path releaseExpr;
|
2009-03-06 16:55:19 +00:00
|
|
|
ATermMap autoArgs;
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
|
|
for (Strings::iterator i = args.begin(); i != args.end(); ) {
|
|
|
|
string arg = *i++;
|
2009-03-06 16:55:19 +00:00
|
|
|
if (arg == "--arg") {
|
|
|
|
/* This is like --arg in nix-instantiate, except that it
|
|
|
|
supports multiple versions for the same argument.
|
|
|
|
That is, autoArgs is a mapping from variable names to
|
|
|
|
*lists* of values. */
|
|
|
|
if (i == args.end()) throw UsageError("missing argument");
|
|
|
|
string name = *i++;
|
|
|
|
if (i == args.end()) throw UsageError("missing argument");
|
|
|
|
string value = *i++;
|
|
|
|
Expr e = parseExprFromString(state, value, absPath("."));
|
|
|
|
autoArgs.set(toATerm(name), (ATerm) ATinsert(autoArgs.get(toATerm(name))
|
|
|
|
? (ATermList) autoArgs.get(toATerm(name))
|
|
|
|
: ATempty, e));
|
|
|
|
}
|
|
|
|
else if (arg[0] == '-')
|
2009-03-05 14:59:43 +00:00
|
|
|
throw UsageError(format("unknown flag `%1%'") % arg);
|
|
|
|
else
|
|
|
|
releaseExpr = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
store = openStore();
|
|
|
|
|
2009-03-05 15:41:43 +00:00
|
|
|
Expr e = parseExprFromFile(state, releaseExpr);
|
|
|
|
|
|
|
|
XMLWriter doc(true, std::cout);
|
|
|
|
XMLOpenElement root(doc, "jobs");
|
2009-03-06 16:55:19 +00:00
|
|
|
findJobs(state, doc, ATermMap(), autoArgs, e, "");
|
2009-03-05 14:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string programId = "eval-jobs";
|