about summary refs log tree commit diff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-02-08T13·21+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-02-08T13·21+0000
commit39f50db731bb1924d3c18b153dfe4258208b8812 (patch)
treeb0a4c793d826353580de19e0151cfa6a8096ca3a /src/libexpr
parent4db4b61380d94c2626b7c4ee2e613b6b6223f803 (diff)
* Refactoring: move derivation evaluation to libexpr.
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/Makefile.am3
-rw-r--r--src/libexpr/get-drvs.cc67
-rw-r--r--src/libexpr/get-drvs.hh64
3 files changed, 133 insertions, 1 deletions
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 <string>
+#include <map>
+
+#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<DrvInfo> 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 */