From 725c37b4d350c9c040c29efd8146619f77c897f5 Mon Sep 17 00:00:00 2001 From: Frederik Rietdijk Date: Wed, 31 Aug 2016 10:22:19 +0200 Subject: [PATCH] Python: move wrapPython into own file --- .../interpreters/python/wrap-python.nix | 51 +++++++++++++++++++ pkgs/top-level/python-packages.nix | 46 +---------------- 2 files changed, 52 insertions(+), 45 deletions(-) create mode 100644 pkgs/development/interpreters/python/wrap-python.nix diff --git a/pkgs/development/interpreters/python/wrap-python.nix b/pkgs/development/interpreters/python/wrap-python.nix new file mode 100644 index 00000000000..b965ff5350b --- /dev/null +++ b/pkgs/development/interpreters/python/wrap-python.nix @@ -0,0 +1,51 @@ +{ lib +, python +, makeSetupHook +, makeWrapper }: + +with lib; + +makeSetupHook { + deps = makeWrapper; + substitutions.libPrefix = python.libPrefix; + substitutions.executable = python.interpreter; + substitutions.python = python; + substitutions.magicalSedExpression = let + # Looks weird? Of course, it's between single quoted shell strings. + # NOTE: Order DOES matter here, so single character quotes need to be + # at the last position. + quoteVariants = [ "'\"'''\"'" "\"\"\"" "\"" "'\"'\"'" ]; # hey Vim: '' + + mkStringSkipper = labelNum: quote: let + label = "q${toString labelNum}"; + isSingle = elem quote [ "\"" "'\"'\"'" ]; + endQuote = if isSingle then "[^\\\\]${quote}" else quote; + in '' + /^[a-z]?${quote}/ { + /${quote}${quote}|${quote}.*${endQuote}/{n;br} + :${label}; n; /^${quote}/{n;br}; /${endQuote}/{n;br}; b${label} + } + ''; + + # This preamble does two things: + # * Sets argv[0] to the original application's name; otherwise it would be .foo-wrapped. + # Python doesn't support `exec -a`. + # * Adds all required libraries to sys.path via `site.addsitedir`. It also handles *.pth files. + preamble = '' + import sys + import site + import functools + sys.argv[0] = '"'$(basename "$f")'"' + functools.reduce(lambda k, p: site.addsitedir(p, k), ['"$([ -n "$program_PYTHONPATH" ] && (echo "'$program_PYTHONPATH'" | sed "s|:|','|g") || true)"'], site._init_pathinfo()) + ''; + + in '' + 1 { + :r + /\\$|,$/{N;br} + /__future__|^ |^ *(#.*)?$/{n;br} + ${concatImapStrings mkStringSkipper quoteVariants} + /^[^# ]/i ${replaceStrings ["\n"] [";"] preamble} + } + ''; +} ./wrap.sh diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix index 991ceaf1f12..9b70ab87dc2 100644 --- a/pkgs/top-level/python-packages.nix +++ b/pkgs/top-level/python-packages.nix @@ -38,51 +38,7 @@ in modules // { # helpers - wrapPython = pkgs.makeSetupHook - { deps = pkgs.makeWrapper; - substitutions.libPrefix = python.libPrefix; - substitutions.executable = python.interpreter; - substitutions.python = python; - substitutions.magicalSedExpression = let - # Looks weird? Of course, it's between single quoted shell strings. - # NOTE: Order DOES matter here, so single character quotes need to be - # at the last position. - quoteVariants = [ "'\"'''\"'" "\"\"\"" "\"" "'\"'\"'" ]; # hey Vim: '' - - mkStringSkipper = labelNum: quote: let - label = "q${toString labelNum}"; - isSingle = elem quote [ "\"" "'\"'\"'" ]; - endQuote = if isSingle then "[^\\\\]${quote}" else quote; - in '' - /^[a-z]?${quote}/ { - /${quote}${quote}|${quote}.*${endQuote}/{n;br} - :${label}; n; /^${quote}/{n;br}; /${endQuote}/{n;br}; b${label} - } - ''; - - # This preamble does two things: - # * Sets argv[0] to the original application's name; otherwise it would be .foo-wrapped. - # Python doesn't support `exec -a`. - # * Adds all required libraries to sys.path via `site.addsitedir`. It also handles *.pth files. - preamble = '' - import sys - import site - import functools - sys.argv[0] = '"'$(basename "$f")'"' - functools.reduce(lambda k, p: site.addsitedir(p, k), ['"$([ -n "$program_PYTHONPATH" ] && (echo "'$program_PYTHONPATH'" | sed "s|:|','|g") || true)"'], site._init_pathinfo()) - ''; - - in '' - 1 { - :r - /\\$|,$/{N;br} - /__future__|^ |^ *(#.*)?$/{n;br} - ${concatImapStrings mkStringSkipper quoteVariants} - /^[^# ]/i ${replaceStrings ["\n"] [";"] preamble} - } - ''; - } - ../development/interpreters/python/wrap.sh; + wrapPython = callPackage ../development/interpreters/python/wrap-python.nix {inherit python; inherit (pkgs) makeSetupHook makeWrapper; }; # specials