From 39f50db731bb1924d3c18b153dfe4258208b8812 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 8 Feb 2006 13:21:16 +0000 Subject: * Refactoring: move derivation evaluation to libexpr. --- src/libexpr/Makefile.am | 3 ++- src/libexpr/get-drvs.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ src/libexpr/get-drvs.hh | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/libexpr/get-drvs.cc create mode 100644 src/libexpr/get-drvs.hh (limited to 'src/libexpr') diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am index dbc93abd5e..ad95dff1d6 100644 --- a/src/libexpr/Makefile.am +++ b/src/libexpr/Makefile.am @@ -3,7 +3,8 @@ lib_LTLIBRARIES = libexpr.la libexpr_la_SOURCES = nixexpr.cc nixexpr.hh parser.cc parser.hh \ eval.cc eval.hh primops.cc \ lexer-tab.c lexer-tab.h parser-tab.c parser-tab.h \ - nixexpr-ast.hh + nixexpr-ast.hh \ + get-drvs.cc get-drvs.hh EXTRA_DIST = lexer.l parser.y nixexpr-ast.def nixexpr-ast.cc diff --git a/src/libexpr/get-drvs.cc b/src/libexpr/get-drvs.cc new file mode 100644 index 0000000000..c158a71dff --- /dev/null +++ b/src/libexpr/get-drvs.cc @@ -0,0 +1,67 @@ +#include "get-drvs.hh" +#include "nixexpr-ast.hh" + + +bool getDerivation(EvalState & state, Expr e, DrvInfo & drv) +{ + ATermList es; + e = evalExpr(state, e); + if (!matchAttrs(e, es)) return false; + + ATermMap attrs; + queryAllAttrs(e, attrs, false); + + Expr a = attrs.get("type"); + if (!a || evalString(state, a) != "derivation") return false; + + a = attrs.get("name"); + if (!a) throw badTerm("derivation name missing", e); + drv.name = evalString(state, a); + + a = attrs.get("system"); + if (!a) + drv.system = "unknown"; + else + drv.system = evalString(state, a); + + drv.attrs = attrs; + + return true; +} + + +void getDerivations(EvalState & state, Expr e, DrvInfos & drvs) +{ + ATermList es; + DrvInfo drv; + + e = evalExpr(state, e); + + if (getDerivation(state, e, drv)) + drvs.push_back(drv); + + else if (matchAttrs(e, es)) { + ATermMap drvMap; + queryAllAttrs(e, drvMap); + for (ATermIterator i(drvMap.keys()); i; ++i) { + debug(format("evaluating attribute `%1%'") % aterm2String(*i)); + if (getDerivation(state, drvMap.get(*i), drv)) + drvs.push_back(drv); + else + ; + // parseDerivations(state, drvMap.get(*i), drvs); + } + } + + else if (matchList(e, es)) { + for (ATermIterator i(es); i; ++i) { + debug(format("evaluating list element")); + if (getDerivation(state, *i, drv)) + drvs.push_back(drv); + else + getDerivations(state, *i, drvs); + } + } +} + + diff --git a/src/libexpr/get-drvs.hh b/src/libexpr/get-drvs.hh new file mode 100644 index 0000000000..bfcff8cca3 --- /dev/null +++ b/src/libexpr/get-drvs.hh @@ -0,0 +1,64 @@ +#ifndef __GET_DRVS_H +#define __GET_DRVS_H + +#include +#include + +#include "eval.hh" + + +struct DrvInfo +{ +private: + string drvPath; + string outPath; + +public: + string name; + string system; + + ATermMap attrs; + + string queryDrvPath(EvalState & state) const + { + if (drvPath == "") { + Expr a = attrs.get("drvPath"); + (string &) drvPath = a ? evalPath(state, a) : ""; + } + return drvPath; + } + + string queryOutPath(EvalState & state) const + { + if (outPath == "") { + Expr a = attrs.get("outPath"); + if (!a) throw Error("output path missing"); + (string &) outPath = evalPath(state, a); + } + return outPath; + } + + void setDrvPath(const string & s) + { + drvPath = s; + } + + void setOutPath(const string & s) + { + outPath = s; + } +}; + + +typedef list DrvInfos; + + +/* Evaluate expression `e'. If it evaluates to a derivation, store + information about the derivation in `drv' and return true. + Otherwise, return false. */ +bool getDerivation(EvalState & state, Expr e, DrvInfo & drv); + +void getDerivations(EvalState & state, Expr e, DrvInfos & drvs); + + +#endif /* !__GET_DRVS_H */ -- cgit 1.4.1