diff options
Diffstat (limited to 'pkgs/development/tools/poetry2nix/poetry2nix/pep508.nix')
-rw-r--r-- | pkgs/development/tools/poetry2nix/poetry2nix/pep508.nix | 147 |
1 files changed, 71 insertions, 76 deletions
diff --git a/pkgs/development/tools/poetry2nix/poetry2nix/pep508.nix b/pkgs/development/tools/poetry2nix/poetry2nix/pep508.nix index 67ffdddb4bd..773e0a60e09 100644 --- a/pkgs/development/tools/poetry2nix/poetry2nix/pep508.nix +++ b/pkgs/development/tools/poetry2nix/poetry2nix/pep508.nix @@ -7,7 +7,6 @@ let # Strip leading/trailing whitespace from string stripStr = s: lib.elemAt (builtins.split "^ *" (lib.elemAt (builtins.split " *$" s) 0)) 2; - findSubExpressionsFun = acc: c: ( if c == "(" then ( let @@ -15,23 +14,23 @@ let isOpen = acc.openP == 0; startPos = if isOpen then posNew else acc.startPos; in - acc // { - inherit startPos; - exprs = acc.exprs ++ [ (substr acc.exprPos (acc.pos - 1) acc.expr) ]; - pos = posNew; - openP = acc.openP + 1; - } + acc // { + inherit startPos; + exprs = acc.exprs ++ [ (substr acc.exprPos (acc.pos - 1) acc.expr) ]; + pos = posNew; + openP = acc.openP + 1; + } ) else if c == ")" then ( let openP = acc.openP - 1; exprs = findSubExpressions (substr acc.startPos acc.pos acc.expr); in - acc // { - inherit openP; - pos = acc.pos + 1; - exprs = if openP == 0 then acc.exprs ++ [ exprs ] else acc.exprs; - exprPos = if openP == 0 then acc.pos + 1 else acc.exprPos; - } + acc // { + inherit openP; + pos = acc.pos + 1; + exprs = if openP == 0 then acc.exprs ++ [ exprs ] else acc.exprs; + exprPos = if openP == 0 then acc.pos + 1 else acc.exprPos; + } ) else acc // { pos = acc.pos + 1; } ); @@ -39,7 +38,7 @@ let findSubExpressions = expr: let acc = builtins.foldl' findSubExpressionsFun { - exprs = []; + exprs = [ ]; expr = expr; pos = 0; openP = 0; @@ -47,18 +46,16 @@ let startPos = 0; } (lib.stringToCharacters expr); tailExpr = (substr acc.exprPos acc.pos expr); - tailExprs = if tailExpr != "" then [ tailExpr ] else []; + tailExprs = if tailExpr != "" then [ tailExpr ] else [ ]; in - acc.exprs ++ tailExprs; - + acc.exprs ++ tailExprs; parseExpressions = exprs: let splitCond = ( s: builtins.map - (x: stripStr (if builtins.typeOf x == "list" then (builtins.elemAt x 0) else x)) + (x: stripStr ( if builtins.typeOf x == "list" then (builtins.elemAt x 0) else x)) (builtins.split " (and|or) " (s + " ")) ); - mapfn = expr: ( if (builtins.match "^ ?$" expr != null) then null # Filter empty else if (builtins.elem expr [ "and" "or" ]) then { @@ -70,14 +67,12 @@ let value = expr; } ); - parse = expr: builtins.filter (x: x != null) (builtins.map mapfn (splitCond expr)); in - builtins.foldl' ( - acc: v: acc ++ ( - if builtins.typeOf v == "string" then parse v else [ (parseExpressions v) ] - ) - ) [] exprs; + builtins.foldl' + ( + acc: v: acc ++ ( if builtins.typeOf v == "string" then parse v else [ (parseExpressions v) ]) + ) [ ] exprs; # Transform individual expressions to structured expressions # This function also performs variable substitution, replacing environment markers with their explicit values @@ -94,9 +89,10 @@ let else throw "Unsupported platform" ); platform_machine = stdenv.platform.kernelArch; - platform_python_implementation = let - impl = python.passthru.implementation; - in + platform_python_implementation = + let + impl = python.passthru.implementation; + in ( if impl == "cpython" then "CPython" else if impl == "pypy" then "PyPy" @@ -115,34 +111,32 @@ let implementation_version = python.version; extra = ""; }; - substituteVar = value: if builtins.hasAttr value variables then (builtins.toJSON variables."${value}") else value; - processVar = value: builtins.foldl' (acc: v: v acc) value [ stripStr substituteVar ]; in - if builtins.typeOf exprs == "set" then ( - if exprs.type == "expr" then ( - let - mVal = ''[a-zA-Z0-9\'"_\. ]+''; - mOp = "in|[!=<>]+"; - e = stripStr exprs.value; - m = builtins.map stripStr (builtins.match ''^(${mVal}) *(${mOp}) *(${mVal})$'' e); - in - { - type = "expr"; - value = { - op = builtins.elemAt m 1; - values = [ - (processVar (builtins.elemAt m 0)) - (processVar (builtins.elemAt m 2)) - ]; - }; - } - ) else exprs - ) else builtins.map transformExpressions exprs; + if builtins.typeOf exprs == "set" then ( + if exprs.type == "expr" then ( + let + mVal = ''[a-zA-Z0-9\'"_\. ]+''; + mOp = "in|[!=<>]+"; + e = stripStr exprs.value; + m = builtins.map stripStr (builtins.match ''^(${mVal}) *(${mOp}) *(${mVal})$'' e); + in + { + type = "expr"; + value = { + op = builtins.elemAt m 1; + values = [ + (processVar (builtins.elemAt m 0)) + (processVar (builtins.elemAt m 2)) + ]; + }; + } + ) else exprs + ) else builtins.map transformExpressions exprs; # Recursively eval all expressions evalExpressions = exprs: @@ -165,32 +159,33 @@ let let parts = builtins.splitVersion c; pruned = lib.take ((builtins.length parts) - 1) parts; - upper = builtins.toString ( - (lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1 - ); + upper = builtins.toString + ( + (lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1 + ); upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned); in - op.">=" v c && op."<" v upperConstraint; + op.">=" v c && op."<" v upperConstraint; "===" = x: y: x == y; "in" = x: y: let values = builtins.filter (x: builtins.typeOf x == "string") (builtins.split " " (unmarshal y)); in - builtins.elem (unmarshal x) values; + builtins.elem (unmarshal x) values; }; in - if builtins.typeOf exprs == "set" then ( - if exprs.type == "expr" then ( - let - expr = exprs; - result = (op."${expr.value.op}") (builtins.elemAt expr.value.values 0) (builtins.elemAt expr.value.values 1); - in - { - type = "value"; - value = result; - } - ) else exprs - ) else builtins.map evalExpressions exprs; + if builtins.typeOf exprs == "set" then ( + if exprs.type == "expr" then ( + let + expr = exprs; + result = (op."${expr.value.op}") (builtins.elemAt expr.value.values 0) (builtins.elemAt expr.value.values 1); + in + { + type = "value"; + value = result; + } + ) else exprs + ) else builtins.map evalExpressions exprs; # Now that we have performed an eval all that's left to do is to concat the graph into a single bool reduceExpressions = exprs: @@ -217,18 +212,18 @@ let cond = "and"; } v; in - acc // { - value = cond."${acc.cond}" acc.value ret.value; - } + acc // { + value = cond."${acc.cond}" acc.value ret.value; + } ) else throw "Unsupported type" ); in - ( - builtins.foldl' reduceExpressionsFun { - value = true; - cond = "and"; - } exprs - ).value; + ( + builtins.foldl' reduceExpressionsFun { + value = true; + cond = "and"; + } exprs + ).value; in e: builtins.foldl' (acc: v: v acc) e [ findSubExpressions |