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