hydra/src/c/hydra_eval_jobs.cc

241 lines
7.5 KiB
C++
Raw Normal View History

#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"
#include "get-drvs.hh"
using namespace nix;
void printHelp()
{
std::cout << "Syntax: eval-jobs <expr>\n";
}
2009-03-15 11:56:11 +00:00
static Path gcRootsDir;
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,
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;
}
2009-07-07 13:20:00 +00:00
Expr name; ATerm def2; ATermList values;
if (!matchFormal(ATgetFirst(formals), name, def2)) abort();
if ((values = (ATermList) argsLeft.get(name))) {
2009-03-09 10:01:04 +00:00
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()));
2009-03-09 10:01:04 +00:00
argsUsed2.set(name, (ATerm) ATmakeList2(*i, (ATerm) ATmakeInt(n)));
argsLeft2.remove(name);
tryJobAlts(state, doc, argsUsed2, argsLeft2, attrPath, fun, ATgetNext(formals), actualArgs2);
}
}
2009-03-09 13:04:46 +00:00
else
throw TypeError(format("job `%1%' requires an argument named `%2%'")
% attrPath % aterm2String(name));
}
static void showArgsUsed(XMLWriter & doc, const ATermMap & argsUsed)
{
foreach (ATermMap::const_iterator, i, argsUsed) {
XMLAttrs xmlAttrs2;
xmlAttrs2["name"] = aterm2String(i->key);
2009-03-09 10:01:04 +00:00
xmlAttrs2["value"] = showValue(ATelementAt((ATermList) i->value, 0));
xmlAttrs2["altnr"] = int2String(ATgetInt((ATermInt) ATelementAt((ATermList) i->value, 1)));
doc.writeEmptyElement("arg", xmlAttrs2);
}
}
2009-07-07 13:20:00 +00:00
static string queryMetaFieldString(MetaInfo & meta, const string & name)
{
MetaValue value = meta[name];
if (value.type != MetaValue::tpString) return "";
return value.stringValue;
}
static int queryMetaFieldInt(MetaInfo & meta, const string & name, int def)
{
MetaValue value = meta[name];
if (value.type == MetaValue::tpInt) return value.intValue;
if (value.type == MetaValue::tpString) {
int n;
if (string2Int(value.stringValue, n)) return n;
}
return def;
}
static void findJobsWrapped(EvalState & state, XMLWriter & doc,
const ATermMap & argsUsed, const ATermMap & argsLeft,
2009-03-05 15:41:43 +00:00
Expr e, const string & attrPath)
{
debug(format("at path `%1%'") % attrPath);
2009-03-05 15:41:43 +00:00
e = evalExpr(state, e);
2009-07-07 13:20:00 +00:00
ATermList as, 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);
DrvInfo drv;
if (getDerivation(state, e, drv)) {
2009-03-05 15:41:43 +00:00
XMLAttrs xmlAttrs;
2009-03-15 11:56:11 +00:00
Path drvPath;
2009-03-05 15:41:43 +00:00
2009-03-09 13:04:46 +00:00
xmlAttrs["jobName"] = attrPath;
xmlAttrs["nixName"] = drv.name;
xmlAttrs["system"] = drv.system;
2009-03-15 11:56:11 +00:00
xmlAttrs["drvPath"] = drvPath = drv.queryDrvPath(state);
xmlAttrs["outPath"] = drv.queryOutPath(state);
2009-07-07 13:20:00 +00:00
MetaInfo meta = drv.queryMetaInfo(state);
xmlAttrs["description"] = queryMetaFieldString(meta, "description");
xmlAttrs["longDescription"] = queryMetaFieldString(meta, "longDescription");
xmlAttrs["license"] = queryMetaFieldString(meta, "license");
xmlAttrs["homepage"] = queryMetaFieldString(meta, "homepage");
int prio = queryMetaFieldInt(meta, "schedulingPriority", 100);
xmlAttrs["schedulingPriority"] = int2String(prio);
2009-07-07 14:33:51 +00:00
string maintainers;
MetaValue value = meta["maintainers"];
if (value.type == MetaValue::tpString)
maintainers = value.stringValue;
else if (value.type == MetaValue::tpStrings) {
foreach (Strings::const_iterator, i, value.stringValues) {
if (maintainers.size() != 0) maintainers += ", ";
maintainers += *i;
}
}
xmlAttrs["maintainers"] = maintainers;
2009-03-15 11:56:11 +00:00
/* Register the derivation as a GC root. !!! This
registers roots for jobs that we may have already
done. */
Path root = gcRootsDir + "/" + baseNameOf(drvPath);
if (!pathExists(root)) addPermRoot(drvPath, root, false);
2009-03-05 15:41:43 +00:00
XMLOpenElement _(doc, "job", xmlAttrs);
showArgsUsed(doc, argsUsed);
2009-03-05 15:41:43 +00:00
}
else {
foreach (ATermMap::const_iterator, i, attrs)
findJobs(state, doc, argsUsed, argsLeft, i->value,
2009-03-05 15:41:43 +00:00
(attrPath.empty() ? "" : attrPath + ".") + aterm2String(i->key));
}
}
else if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis)) {
tryJobAlts(state, doc, argsUsed, argsLeft, attrPath, e, formals, ATermMap());
2009-03-05 15:41:43 +00:00
}
else
throw TypeError(format("unknown value: %1%") % showValue(e));
}
static void findJobs(EvalState & state, XMLWriter & doc,
const ATermMap & argsUsed, const ATermMap & argsLeft,
Expr e, const string & attrPath)
{
try {
findJobsWrapped(state, doc, argsUsed, argsLeft, e, attrPath);
} catch (Error & e) {
XMLAttrs xmlAttrs;
xmlAttrs["location"] = attrPath;
xmlAttrs["msg"] = e.msg();
XMLOpenElement _(doc, "error", xmlAttrs);
showArgsUsed(doc, argsUsed);
}
2009-03-05 15:41:43 +00:00
}
void run(Strings args)
{
EvalState state;
Path releaseExpr;
ATermMap autoArgs;
for (Strings::iterator i = args.begin(); i != args.end(); ) {
string arg = *i++;
if (arg == "--arg" || arg == "--argstr") {
/* 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 = arg == "--arg"
? evalExpr(state, parseExprFromString(state, value, absPath(".")))
: makeStr(value);
autoArgs.set(toATerm(name), (ATerm) ATinsert(autoArgs.get(toATerm(name))
? (ATermList) autoArgs.get(toATerm(name))
: ATempty, e));
}
2009-03-15 11:56:11 +00:00
else if (arg == "--gc-roots-dir") {
if (i == args.end()) throw UsageError("missing argument");
gcRootsDir = *i++;
}
else if (arg[0] == '-')
throw UsageError(format("unknown flag `%1%'") % arg);
else
releaseExpr = arg;
}
2009-03-09 13:04:46 +00:00
if (releaseExpr == "") throw UsageError("no expression specified");
2009-03-15 11:56:11 +00:00
if (gcRootsDir == "") throw UsageError("--gc-roots-dir not specified");
store = openStore();
2009-03-05 15:41:43 +00:00
Expr e = parseExprFromFile(state, releaseExpr);
XMLWriter doc(true, std::cout);
XMLOpenElement root(doc, "jobs");
findJobs(state, doc, ATermMap(), autoArgs, e, "");
}
string programId = "eval-jobs";