git.iko.soy/typeable/nixpkgs
on master help sign in
python-wrapper: Handle strings at the beginning.

This should fix #7366 for now, but using the (IMHO) pragmatic approach of extending the sed expression to recognize strings. However, this approach is obviously not parsing the full AST, nor does it wrap Python itself (as pointed out by @spwhitt in #7366) but tries to match Python strings as best as possible without getting TOO unreadable. We also use a little bit of Nix to help generating the SED expression, because doing the whole quote matching block over and over again would be quite repetitious and error-prone to change. The reason why I'm using imap here is that we need to have unique labels to avoid jumping into the wrong branch. So the new expression is not only able to match continous regions of triple-quoted strings, but also regions with only one quote character (even with escaped inner quotes) and empty strings. However, what it doesn't correctly recognize is something like this: "string1" "string2" "multi line string" Which is very unlikely that we'll find something like this in the wild. Of course, we could handle it as well, but it would mean that we need to substitute the current line into hold space until we're finished parsing the strings, branch off to another label where we match multiline strings of all sorts and swap hold/pattern space and finally print the result. So to summarize: The SED expression would be 3 to 4 times bigger than now and we gain very little from that. Signed-off-by: aszlig <aszlig@redmoonstudios.org>

commit311aa5d8d960dcfb64a762a5f597244001b94023 authoraszlig <aszlig@redmoonstudios.org> dateTue 14 Apr 2015, 06:55 (12 years ago) parenta9e364a
2 files changed+27−6
pkgs/development/python-modules/generic/wrap.sh+1−6 pkgs/top-level/python-packages.nix+26
pkgs/development/python-modules/generic/wrap.sh+1−6
@@ -26,12 +26,7 @@ wrapPythonProgramsIn() {
2626 # dont wrap EGG-INFO scripts since they are called from python
2727 if echo "$i" | grep -v EGG-INFO/scripts; then
2828 echo "wrapping \`$i'..."
29− sed -i "$i" -re '1 {
30− /^#!/!b; :r
31− /\\$/{N;b r}
32− /__future__|^ *(#.*)?$/{n;b r}
33− /^ *[^# ]/i import sys; sys.argv[0] = '"'$(basename "$i")'"'
34− }'
29+ sed -i "$i" -re '@magicalSedExpression@'
3530 wrapProgram "$i" \
3631 --prefix PYTHONPATH ":" $program_PYTHONPATH \
3732 --prefix PATH ":" $program_PATH
pkgs/top-level/python-packages.nix+26
@@ -50,6 +50,32 @@ let
5050 { deps = pkgs.makeWrapper;
5151 substitutions.libPrefix = python.libPrefix;
5252 substitutions.executable = "${python}/bin/${python.executable}";
53+ substitutions.magicalSedExpression = let
54+ # Looks weird? Of course, it's between single quoted shell strings.
55+ # NOTE: Order DOES matter here, so single character quotes need to be
56+ # at the last position.
57+ quoteVariants = [ "'\"'''\"'" "\"\"\"" "\"" "'\"'\"'" ]; # hey Vim: ''
58+
59+ mkStringSkipper = labelNum: quote: let
60+ label = "q${toString labelNum}";
61+ isSingle = elem quote [ "\"" "'\"'\"'" ];
62+ endQuote = if isSingle then "[^\\\\]${quote}" else quote;
63+ in ''
64+ /^ *[a-z]?${quote}/ {
65+ /${quote}${quote}|${quote}.*${endQuote}/{n;br}
66+ :${label}; n; /^${quote}/{n;br}; /${endQuote}/{n;br}; b${label}
67+ }
68+ '';
69+
70+ in ''
71+ 1 {
72+ /^#!/!b; :r
73+ /\\$/{N;br}
74+ /__future__|^ *(#.*)?$/{n;br}
75+ ${concatImapStrings mkStringSkipper quoteVariants}
76+ /^ *[^# ]/i import sys; sys.argv[0] = '"'$(basename "$i")'"'
77+ }
78+ '';
5379 }
5480 ../development/python-modules/generic/wrap.sh;
5581