forked from lix-project/nix-eval-jobs
Merge pull request #56 from jsoo1/refactor-drv-parsing
Refactor derivation representation.
This commit is contained in:
commit
7a9f13831c
|
@ -173,6 +173,65 @@ Value * topLevelValue(EvalState & state, Bindings & autoArgs) {
|
||||||
: releaseExprTopLevelValue(state, autoArgs);
|
: releaseExprTopLevelValue(state, autoArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* The fields of a derivation that are printed in json form */
|
||||||
|
struct Drv {
|
||||||
|
std::string name;
|
||||||
|
std::string system;
|
||||||
|
std::string drvPath;
|
||||||
|
std::map<std::string, std::string> outputs;
|
||||||
|
std::optional<nlohmann::json> meta;
|
||||||
|
|
||||||
|
Drv (EvalState & state, DrvInfo & drvInfo) {
|
||||||
|
if (drvInfo.querySystem() == "unknown")
|
||||||
|
throw EvalError("derivation must have a 'system' attribute");
|
||||||
|
|
||||||
|
auto localStore = state.store.dynamic_pointer_cast<LocalFSStore>();
|
||||||
|
|
||||||
|
for (auto out : drvInfo.queryOutputs(true)) {
|
||||||
|
if (out.second)
|
||||||
|
outputs[out.first] = localStore->printStorePath(*out.second);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (myArgs.meta) {
|
||||||
|
nlohmann::json meta_;
|
||||||
|
for (auto & name : drvInfo.queryMetaNames()) {
|
||||||
|
PathSet context;
|
||||||
|
std::stringstream ss;
|
||||||
|
|
||||||
|
auto metaValue = drvInfo.queryMeta(name);
|
||||||
|
// Skip non-serialisable types
|
||||||
|
// TODO: Fix serialisation of derivations to store paths
|
||||||
|
if (metaValue == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
printValueAsJSON(state, true, *metaValue, noPos, ss, context);
|
||||||
|
|
||||||
|
meta_[name] = nlohmann::json::parse(ss.str());
|
||||||
|
}
|
||||||
|
meta = meta_;
|
||||||
|
}
|
||||||
|
|
||||||
|
name = drvInfo.queryName();
|
||||||
|
system = drvInfo.querySystem();
|
||||||
|
drvPath = localStore->printStorePath(drvInfo.requireDrvPath());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static void to_json(nlohmann::json & json, const Drv & drv) {
|
||||||
|
json = nlohmann::json{
|
||||||
|
{ "name", drv.name },
|
||||||
|
{ "system", drv.system },
|
||||||
|
{ "drvPath", drv.drvPath },
|
||||||
|
{ "outputs", drv.outputs },
|
||||||
|
};
|
||||||
|
|
||||||
|
if (drv.meta.has_value())
|
||||||
|
json["meta"] = drv.meta.value();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
static void worker(
|
static void worker(
|
||||||
EvalState & state,
|
EvalState & state,
|
||||||
Bindings & autoArgs,
|
Bindings & autoArgs,
|
||||||
|
@ -193,62 +252,29 @@ static void worker(
|
||||||
debug("worker process %d at '%s'", getpid(), attrPath);
|
debug("worker process %d at '%s'", getpid(), attrPath);
|
||||||
|
|
||||||
/* Evaluate it and send info back to the collector. */
|
/* Evaluate it and send info back to the collector. */
|
||||||
nlohmann::json reply;
|
nlohmann::json reply = nlohmann::json{ { "attr", attrPath } };
|
||||||
reply["attr"] = attrPath;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto vTmp = findAlongAttrPath(state, attrPath, autoArgs, *vRoot).first;
|
auto vTmp = findAlongAttrPath(state, attrPath, autoArgs, *vRoot).first;
|
||||||
|
|
||||||
auto v = state.allocValue();
|
auto v = state.allocValue();
|
||||||
state.autoCallFunction(autoArgs, *vTmp, *v);
|
state.autoCallFunction(autoArgs, *vTmp, *v);
|
||||||
|
|
||||||
if (auto drv = getDerivation(state, *v, false)) {
|
if (auto drvInfo = getDerivation(state, *v, false)) {
|
||||||
|
|
||||||
if (drv->querySystem() == "unknown")
|
auto drv = Drv(state, *drvInfo);
|
||||||
throw EvalError("derivation must have a 'system' attribute");
|
reply.update(drv);
|
||||||
|
|
||||||
auto localStore = state.store.dynamic_pointer_cast<LocalFSStore>();
|
|
||||||
auto drvPath = localStore->printStorePath(drv->requireDrvPath());
|
|
||||||
auto storePath = localStore->parseStorePath(drvPath);
|
|
||||||
auto outputs = drv->queryOutputs();
|
|
||||||
|
|
||||||
reply["name"] = drv->queryName();
|
|
||||||
reply["system"] = drv->querySystem();
|
|
||||||
reply["drvPath"] = drvPath;
|
|
||||||
for (auto out : outputs){
|
|
||||||
if (out.second) {
|
|
||||||
reply["outputs"][out.first] = localStore->printStorePath(*out.second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (myArgs.meta) {
|
|
||||||
nlohmann::json meta;
|
|
||||||
for (auto & name : drv->queryMetaNames()) {
|
|
||||||
PathSet context;
|
|
||||||
std::stringstream ss;
|
|
||||||
|
|
||||||
auto metaValue = drv->queryMeta(name);
|
|
||||||
// Skip non-serialisable types
|
|
||||||
// TODO: Fix serialisation of derivations to store paths
|
|
||||||
if (metaValue == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
printValueAsJSON(state, true, *metaValue, noPos, ss, context);
|
|
||||||
nlohmann::json field = nlohmann::json::parse(ss.str());
|
|
||||||
meta[name] = field;
|
|
||||||
}
|
|
||||||
reply["meta"] = meta;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Register the derivation as a GC root. !!! This
|
/* Register the derivation as a GC root. !!! This
|
||||||
registers roots for jobs that we may have already
|
registers roots for jobs that we may have already
|
||||||
done. */
|
done. */
|
||||||
if (myArgs.gcRootsDir != "") {
|
if (myArgs.gcRootsDir != "") {
|
||||||
Path root = myArgs.gcRootsDir + "/" + std::string(baseNameOf(drvPath));
|
Path root = myArgs.gcRootsDir + "/" + std::string(baseNameOf(drv.drvPath));
|
||||||
if (!pathExists(root))
|
if (!pathExists(root)) {
|
||||||
|
auto localStore = state.store.dynamic_pointer_cast<LocalFSStore>();
|
||||||
|
auto storePath = localStore->parseStorePath(drv.drvPath);
|
||||||
localStore->addPermRoot(storePath, root);
|
localStore->addPermRoot(storePath, root);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue