2009-03-05 14:59:43 +00:00
|
|
|
|
#include <map>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2011-01-14 12:53:54 +00:00
|
|
|
|
#include <gc/gc_allocator.h>
|
|
|
|
|
|
2009-03-05 14:59:43 +00:00
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "eval.hh"
|
2012-10-04 19:24:25 +00:00
|
|
|
|
#include "eval-inline.hh"
|
2009-03-05 15:41:43 +00:00
|
|
|
|
#include "util.hh"
|
|
|
|
|
#include "xml-writer.hh"
|
2009-03-06 15:16:29 +00:00
|
|
|
|
#include "get-drvs.hh"
|
2011-12-05 12:08:43 +00:00
|
|
|
|
#include "common-opts.hh"
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
|
|
2009-03-15 11:56:11 +00:00
|
|
|
|
static Path gcRootsDir;
|
|
|
|
|
|
|
|
|
|
|
2010-11-19 11:01:31 +00:00
|
|
|
|
typedef std::map<Symbol, std::pair<unsigned int, Value *> > ArgsUsed;
|
|
|
|
|
typedef std::list<Value *, traceable_allocator<Value *> > ValueList;
|
|
|
|
|
typedef std::map<Symbol, ValueList> AutoArgs;
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void findJobs(EvalState & state, XMLWriter & doc,
|
2010-05-18 09:57:37 +00:00
|
|
|
|
const ArgsUsed & argsUsed, const AutoArgs & argsLeft,
|
|
|
|
|
Value & v, const string & attrPath);
|
2009-03-06 16:55:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void tryJobAlts(EvalState & state, XMLWriter & doc,
|
2010-05-18 09:57:37 +00:00
|
|
|
|
const ArgsUsed & argsUsed, const AutoArgs & argsLeft,
|
|
|
|
|
const string & attrPath, Value & fun,
|
|
|
|
|
Formals::Formals_::iterator cur,
|
|
|
|
|
Formals::Formals_::iterator last,
|
|
|
|
|
const Bindings & actualArgs)
|
2009-03-06 16:55:19 +00:00
|
|
|
|
{
|
2010-05-18 09:57:37 +00:00
|
|
|
|
if (cur == last) {
|
2010-11-19 11:01:31 +00:00
|
|
|
|
Value v, * arg = state.allocValue();
|
|
|
|
|
state.mkAttrs(*arg, 0);
|
|
|
|
|
*arg->attrs = actualArgs;
|
|
|
|
|
mkApp(v, fun, *arg);
|
2010-05-18 09:57:37 +00:00
|
|
|
|
findJobs(state, doc, argsUsed, argsLeft, v, attrPath);
|
2009-03-06 16:55:19 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-03 09:21:27 +00:00
|
|
|
|
AutoArgs::const_iterator a = argsLeft.find(cur->name);
|
2010-05-18 09:57:37 +00:00
|
|
|
|
|
2011-05-09 08:57:19 +00:00
|
|
|
|
Formals::Formals_::iterator next = cur; ++next;
|
2010-05-18 09:57:37 +00:00
|
|
|
|
|
2013-03-29 00:14:24 +00:00
|
|
|
|
if (a == argsLeft.end()) {
|
|
|
|
|
if (!cur->def)
|
|
|
|
|
throw TypeError(format("job `%1%' requires an argument named `%2%'")
|
|
|
|
|
% attrPath % cur->name);
|
|
|
|
|
tryJobAlts(state, doc, argsUsed, argsLeft, attrPath, fun, next, last, actualArgs);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-18 09:57:37 +00:00
|
|
|
|
int n = 0;
|
2010-11-19 11:01:31 +00:00
|
|
|
|
foreach (ValueList::const_iterator, i, a->second) {
|
2010-05-18 09:57:37 +00:00
|
|
|
|
Bindings actualArgs2(actualArgs); // !!! inefficient
|
|
|
|
|
ArgsUsed argsUsed2(argsUsed);
|
|
|
|
|
AutoArgs argsLeft2(argsLeft);
|
2010-11-19 11:01:31 +00:00
|
|
|
|
actualArgs2.push_back(Attr(cur->name, *i));
|
|
|
|
|
actualArgs2.sort(); // !!! inefficient
|
|
|
|
|
argsUsed2[cur->name] = std::pair<unsigned int, Value *>(n, *i);
|
2010-05-18 09:57:37 +00:00
|
|
|
|
argsLeft2.erase(cur->name);
|
|
|
|
|
tryJobAlts(state, doc, argsUsed2, argsLeft2, attrPath, fun, next, last, actualArgs2);
|
|
|
|
|
++n;
|
|
|
|
|
}
|
2009-03-06 16:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 14:06:10 +00:00
|
|
|
|
|
2010-05-18 09:57:37 +00:00
|
|
|
|
static void showArgsUsed(XMLWriter & doc, const ArgsUsed & argsUsed)
|
2009-03-07 14:06:10 +00:00
|
|
|
|
{
|
2010-05-18 09:57:37 +00:00
|
|
|
|
foreach (ArgsUsed::const_iterator, i, argsUsed) {
|
2009-03-07 14:06:10 +00:00
|
|
|
|
XMLAttrs xmlAttrs2;
|
2010-05-18 09:57:37 +00:00
|
|
|
|
xmlAttrs2["name"] = i->first;
|
2010-11-19 11:01:31 +00:00
|
|
|
|
xmlAttrs2["value"] = (format("%1%") % *i->second.second).str();
|
2010-05-18 09:57:37 +00:00
|
|
|
|
xmlAttrs2["altnr"] = int2String(i->second.first);
|
2009-03-07 14:06:10 +00:00
|
|
|
|
doc.writeEmptyElement("arg", xmlAttrs2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-07 13:20:00 +00:00
|
|
|
|
|
2014-01-12 16:37:56 +00:00
|
|
|
|
static string queryMetaStrings(EvalState & state, DrvInfo & drv, const string & name)
|
2009-07-07 13:20:00 +00:00
|
|
|
|
{
|
2014-01-12 16:37:56 +00:00
|
|
|
|
Value * v = drv.queryMeta(name);
|
|
|
|
|
if (v) {
|
|
|
|
|
state.forceValue(*v);
|
|
|
|
|
if (v->type == tString)
|
|
|
|
|
return v->string.s;
|
|
|
|
|
else if (v->type == tList) {
|
|
|
|
|
string res = "";
|
|
|
|
|
for (unsigned int n = 0; n < v->list.length; ++n) {
|
|
|
|
|
Value v2(*v->list.elems[n]);
|
|
|
|
|
state.forceValue(v2);
|
|
|
|
|
if (v2.type == tString) {
|
|
|
|
|
if (res.size() != 0) res += ", ";
|
|
|
|
|
res += v2.string.s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
2013-11-04 21:50:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-12 16:37:56 +00:00
|
|
|
|
return "";
|
2013-11-04 21:50:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-07 14:06:10 +00:00
|
|
|
|
static void findJobsWrapped(EvalState & state, XMLWriter & doc,
|
2010-05-18 09:57:37 +00:00
|
|
|
|
const ArgsUsed & argsUsed, const AutoArgs & argsLeft,
|
|
|
|
|
Value & v, const string & attrPath)
|
2009-03-05 15:41:43 +00:00
|
|
|
|
{
|
2009-03-06 16:55:19 +00:00
|
|
|
|
debug(format("at path `%1%'") % attrPath);
|
2010-11-19 11:01:31 +00:00
|
|
|
|
|
2012-10-04 18:31:47 +00:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
2010-05-18 09:57:37 +00:00
|
|
|
|
state.forceValue(v);
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
2010-05-18 09:57:37 +00:00
|
|
|
|
if (v.type == tAttrs) {
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
2014-01-12 16:37:56 +00:00
|
|
|
|
DrvInfo drv(state);
|
2013-01-22 13:41:02 +00:00
|
|
|
|
|
2012-10-04 19:24:25 +00:00
|
|
|
|
if (getDerivation(state, v, drv, false)) {
|
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
|
|
|
|
|
2014-01-12 16:37:56 +00:00
|
|
|
|
DrvInfo::Outputs outputs = drv.queryOutputs();
|
2013-02-13 16:49:28 +00:00
|
|
|
|
|
2009-03-09 13:04:46 +00:00
|
|
|
|
xmlAttrs["jobName"] = attrPath;
|
|
|
|
|
xmlAttrs["nixName"] = drv.name;
|
2009-03-06 15:16:29 +00:00
|
|
|
|
xmlAttrs["system"] = drv.system;
|
2014-01-12 16:37:56 +00:00
|
|
|
|
xmlAttrs["drvPath"] = drvPath = drv.queryDrvPath();
|
|
|
|
|
xmlAttrs["description"] = drv.queryMetaString("description");
|
|
|
|
|
xmlAttrs["longDescription"] = drv.queryMetaString("longDescription");
|
|
|
|
|
xmlAttrs["license"] = queryMetaStrings(state, drv, "license");
|
|
|
|
|
xmlAttrs["homepage"] = drv.queryMetaString("homepage");
|
|
|
|
|
xmlAttrs["maintainers"] = queryMetaStrings(state, drv, "maintainers");
|
|
|
|
|
|
|
|
|
|
xmlAttrs["schedulingPriority"] = int2String(drv.queryMetaInt("schedulingPriority", 100));
|
|
|
|
|
|
|
|
|
|
xmlAttrs["timeout"] = int2String(drv.queryMetaInt("timeout", 36000));
|
|
|
|
|
|
2014-08-25 09:35:56 +00:00
|
|
|
|
xmlAttrs["maxSilent"] = int2String(drv.queryMetaInt("maxSilent", 7200));
|
2010-05-26 08:03:59 +00:00
|
|
|
|
|
2013-08-15 00:33:10 +00:00
|
|
|
|
/* If this is an aggregate, then get its constituents. */
|
2013-08-13 23:59:29 +00:00
|
|
|
|
Bindings::iterator a = v.attrs->find(state.symbols.create("_hydraAggregate"));
|
|
|
|
|
if (a != v.attrs->end() && state.forceBool(*a->value)) {
|
2013-08-15 00:33:10 +00:00
|
|
|
|
Bindings::iterator a = v.attrs->find(state.symbols.create("constituents"));
|
2013-08-13 23:59:29 +00:00
|
|
|
|
if (a == v.attrs->end())
|
2013-08-15 00:33:10 +00:00
|
|
|
|
throw EvalError("derivation must have a ‘constituents’ attribute");
|
2013-08-13 23:59:29 +00:00
|
|
|
|
PathSet context;
|
2014-04-08 15:08:09 +00:00
|
|
|
|
state.coerceToString(*a->pos, *a->value, context, true, false);
|
2013-08-13 23:59:29 +00:00
|
|
|
|
PathSet drvs;
|
|
|
|
|
foreach (PathSet::iterator, i, context)
|
|
|
|
|
if (i->at(0) == '!') {
|
|
|
|
|
size_t index = i->find("!", 1);
|
|
|
|
|
drvs.insert(string(*i, index + 1));
|
|
|
|
|
}
|
2013-08-15 00:33:10 +00:00
|
|
|
|
xmlAttrs["constituents"] = concatStringsSep(" ", drvs);
|
2013-08-13 23:59:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
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. */
|
2010-06-01 11:20:05 +00:00
|
|
|
|
if (gcRootsDir != "") {
|
|
|
|
|
Path root = gcRootsDir + "/" + baseNameOf(drvPath);
|
2011-09-15 08:34:08 +00:00
|
|
|
|
if (!pathExists(root)) addPermRoot(*store, drvPath, root, false);
|
2010-06-01 11:20:05 +00:00
|
|
|
|
}
|
2013-01-22 13:41:02 +00:00
|
|
|
|
|
2009-03-05 15:41:43 +00:00
|
|
|
|
XMLOpenElement _(doc, "job", xmlAttrs);
|
2013-02-13 16:49:28 +00:00
|
|
|
|
|
|
|
|
|
foreach (DrvInfo::Outputs::iterator, j, outputs) {
|
|
|
|
|
XMLAttrs attrs2;
|
|
|
|
|
attrs2["name"] = j->first;
|
|
|
|
|
attrs2["path"] = j->second;
|
|
|
|
|
doc.writeEmptyElement("output", attrs2);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 14:06:10 +00:00
|
|
|
|
showArgsUsed(doc, argsUsed);
|
2009-03-05 15:41:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
2012-10-04 18:31:47 +00:00
|
|
|
|
if (!state.isDerivation(v)) {
|
|
|
|
|
foreach (Bindings::iterator, i, *v.attrs)
|
|
|
|
|
findJobs(state, doc, argsUsed, argsLeft, *i->value,
|
|
|
|
|
(attrPath.empty() ? "" : attrPath + ".") + (string) i->name);
|
|
|
|
|
}
|
2009-03-05 15:41:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-18 09:57:37 +00:00
|
|
|
|
else if (v.type == tLambda && v.lambda.fun->matchAttrs) {
|
|
|
|
|
tryJobAlts(state, doc, argsUsed, argsLeft, attrPath, v,
|
|
|
|
|
v.lambda.fun->formals->formals.begin(),
|
|
|
|
|
v.lambda.fun->formals->formals.end(),
|
|
|
|
|
Bindings());
|
2009-03-05 15:41:43 +00:00
|
|
|
|
}
|
2009-03-06 16:55:19 +00:00
|
|
|
|
|
2012-02-16 10:14:22 +00:00
|
|
|
|
else if (v.type == tNull) {
|
2012-02-16 10:03:22 +00:00
|
|
|
|
// allow null values, meaning 'do nothing'
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 16:55:19 +00:00
|
|
|
|
else
|
2010-05-18 09:57:37 +00:00
|
|
|
|
throw TypeError(format("unsupported value: %1%") % v);
|
2009-03-07 14:06:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void findJobs(EvalState & state, XMLWriter & doc,
|
2010-05-18 09:57:37 +00:00
|
|
|
|
const ArgsUsed & argsUsed, const AutoArgs & argsLeft,
|
|
|
|
|
Value & v, const string & attrPath)
|
2009-03-07 14:06:10 +00:00
|
|
|
|
{
|
|
|
|
|
try {
|
2010-05-18 09:57:37 +00:00
|
|
|
|
findJobsWrapped(state, doc, argsUsed, argsLeft, v, attrPath);
|
2009-10-08 12:29:20 +00:00
|
|
|
|
} catch (EvalError & e) {
|
2009-03-07 14:06:10 +00:00
|
|
|
|
XMLAttrs xmlAttrs;
|
|
|
|
|
xmlAttrs["location"] = attrPath;
|
|
|
|
|
xmlAttrs["msg"] = e.msg();
|
|
|
|
|
XMLOpenElement _(doc, "error", xmlAttrs);
|
|
|
|
|
showArgsUsed(doc, argsUsed);
|
|
|
|
|
}
|
2009-03-05 15:41:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
int main(int argc, char * * argv)
|
2009-03-05 14:59:43 +00:00
|
|
|
|
{
|
2012-05-23 18:44:10 +00:00
|
|
|
|
/* Prevent undeclared dependencies in the evaluation via
|
|
|
|
|
$NIX_PATH. */
|
|
|
|
|
unsetenv("NIX_PATH");
|
2013-01-22 13:41:02 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
|
initNix();
|
|
|
|
|
|
|
|
|
|
Strings searchPath;
|
|
|
|
|
Path releaseExpr;
|
|
|
|
|
std::map<string, Strings> autoArgs_;
|
|
|
|
|
|
|
|
|
|
parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
|
|
|
|
|
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. */
|
|
|
|
|
auto what = *arg;
|
|
|
|
|
string name = getArg(what, arg, end);
|
|
|
|
|
string value = getArg(what, arg, end);
|
|
|
|
|
autoArgs_[name].push_back((what == "--arg" ? 'E' : 'S') + value);
|
|
|
|
|
}
|
|
|
|
|
else if (parseSearchPathArg(arg, end, searchPath))
|
|
|
|
|
;
|
|
|
|
|
else if (*arg == "--gc-roots-dir")
|
|
|
|
|
gcRootsDir = getArg(*arg, arg, end);
|
|
|
|
|
else if (*arg != "" && arg->at(0) == '-')
|
|
|
|
|
return false;
|
2010-05-18 09:57:37 +00:00
|
|
|
|
else
|
2014-08-13 14:24:26 +00:00
|
|
|
|
releaseExpr = absPath(*arg);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
if (releaseExpr == "") throw UsageError("no expression specified");
|
2013-01-22 13:41:02 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
if (gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
|
2013-01-22 13:41:02 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
EvalState state(searchPath);
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
AutoArgs autoArgs;
|
|
|
|
|
for (auto & i : autoArgs_) {
|
|
|
|
|
for (auto & j : i.second) {
|
|
|
|
|
Value * v = state.allocValue();
|
|
|
|
|
if (j[0] == 'E')
|
|
|
|
|
state.eval(state.parseExprFromString(string(j, 1), absPath(".")), *v);
|
|
|
|
|
else
|
|
|
|
|
mkString(*v, string(j, 1));
|
|
|
|
|
autoArgs[state.symbols.create(i.first)].push_back(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//evalAutoArgs(state, autoArgs_, autoArgs);
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
store = openStore();
|
2010-10-19 14:19:50 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
Value v;
|
|
|
|
|
state.evalFile(releaseExpr, v);
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
XMLWriter doc(true, std::cout);
|
|
|
|
|
XMLOpenElement root(doc, "jobs");
|
|
|
|
|
findJobs(state, doc, ArgsUsed(), autoArgs, v, "");
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
2014-08-13 14:24:26 +00:00
|
|
|
|
state.printStats();
|
|
|
|
|
});
|
|
|
|
|
}
|