aboutsummaryrefslogtreecommitdiff
path: root/infra/libkookie/nixpkgs/pkgs/development/interpreters/python/wrap-python.nix
diff options
context:
space:
mode:
authorMx Kookie <kookie@spacekookie.de>2020-10-31 19:35:09 +0100
committerMx Kookie <kookie@spacekookie.de>2020-10-31 19:35:09 +0100
commitc4625b175f8200f643fd6e11010932ea44c78433 (patch)
treebce3f89888c8ac3991fa5569a878a9eab6801ccc /infra/libkookie/nixpkgs/pkgs/development/interpreters/python/wrap-python.nix
parent49f735974dd103039ddc4cb576bb76555164a9e7 (diff)
parentd661aa56a8843e991261510c1bb28fdc2f6975ae (diff)
Add 'infra/libkookie/' from commit 'd661aa56a8843e991261510c1bb28fdc2f6975ae'
git-subtree-dir: infra/libkookie git-subtree-mainline: 49f735974dd103039ddc4cb576bb76555164a9e7 git-subtree-split: d661aa56a8843e991261510c1bb28fdc2f6975ae
Diffstat (limited to 'infra/libkookie/nixpkgs/pkgs/development/interpreters/python/wrap-python.nix')
-rw-r--r--infra/libkookie/nixpkgs/pkgs/development/interpreters/python/wrap-python.nix52
1 files changed, 52 insertions, 0 deletions
diff --git a/infra/libkookie/nixpkgs/pkgs/development/interpreters/python/wrap-python.nix b/infra/libkookie/nixpkgs/pkgs/development/interpreters/python/wrap-python.nix
new file mode 100644
index 000000000000..6a19a2152419
--- /dev/null
+++ b/infra/libkookie/nixpkgs/pkgs/development/interpreters/python/wrap-python.nix
@@ -0,0 +1,52 @@
+{ lib
+, python
+, makeSetupHook
+, makeWrapper }:
+
+with lib;
+
+makeSetupHook {
+ deps = makeWrapper;
+ substitutions.sitePackages = python.sitePackages;
+ substitutions.executable = python.interpreter;
+ substitutions.python = python.pythonForBuild;
+ substitutions.pythonHost = 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] = '"'$(readlink -f "$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