1{ lib
2, python
3, makeSetupHook
4, makeWrapper }:
5
6with lib;
7
8makeSetupHook {
9 deps = makeWrapper;
10 substitutions.libPrefix = python.libPrefix;
11 substitutions.executable = python.interpreter;
12 substitutions.python = python;
13 substitutions.magicalSedExpression = let
14 # Looks weird? Of course, it's between single quoted shell strings.
15 # NOTE: Order DOES matter here, so single character quotes need to be
16 # at the last position.
17 quoteVariants = [ "'\"'''\"'" "\"\"\"" "\"" "'\"'\"'" ]; # hey Vim: ''
18
19 mkStringSkipper = labelNum: quote: let
20 label = "q${toString labelNum}";
21 isSingle = elem quote [ "\"" "'\"'\"'" ];
22 endQuote = if isSingle then "[^\\\\]${quote}" else quote;
23 in ''
24 /^[a-z]?${quote}/ {
25 /${quote}${quote}|${quote}.*${endQuote}/{n;br}
26 :${label}; n; /^${quote}/{n;br}; /${endQuote}/{n;br}; b${label}
27 }
28 '';
29
30 # This preamble does two things:
31 # * Sets argv[0] to the original application's name; otherwise it would be .foo-wrapped.
32 # Python doesn't support `exec -a`.
33 # * Adds all required libraries to sys.path via `site.addsitedir`. It also handles *.pth files.
34 preamble = ''
35 import sys
36 import site
37 import functools
38 sys.argv[0] = '"'$(readlink -f "$f")'"'
39 functools.reduce(lambda k, p: site.addsitedir(p, k), ['"$([ -n "$program_PYTHONPATH" ] && (echo "'$program_PYTHONPATH'" | sed "s|:|','|g") || true)"'], site._init_pathinfo())
40 '';
41
42 in ''
43 1 {
44 :r
45 /\\$|,$/{N;br}
46 /__future__|^ |^ *(#.*)?$/{n;br}
47 ${concatImapStrings mkStringSkipper quoteVariants}
48 /^[^# ]/i ${replaceStrings ["\n"] [";"] preamble}
49 }
50 '';
51} ./wrap.sh