summary refs log tree commit diff
diff options
context:
space:
mode:
authoraszlig <aszlig@redmoonstudios.org>2014-07-29 06:29:27 +0200
committeraszlig <aszlig@redmoonstudios.org>2014-07-29 06:40:39 +0200
commitff15c31c37dc7f1b68432a9ea74b84e89d3462b3 (patch)
treeff1b82d3f164d822998115737685f13782fa5442
parent62596693556e2080092864113aefe60b12376084 (diff)
downloadnixlib-ff15c31c37dc7f1b68432a9ea74b84e89d3462b3.tar
nixlib-ff15c31c37dc7f1b68432a9ea74b84e89d3462b3.tar.gz
nixlib-ff15c31c37dc7f1b68432a9ea74b84e89d3462b3.tar.bz2
nixlib-ff15c31c37dc7f1b68432a9ea74b84e89d3462b3.tar.lz
nixlib-ff15c31c37dc7f1b68432a9ea74b84e89d3462b3.tar.xz
nixlib-ff15c31c37dc7f1b68432a9ea74b84e89d3462b3.tar.zst
nixlib-ff15c31c37dc7f1b68432a9ea74b84e89d3462b3.zip
python-wrapper: Fix handling __future__ imports.
The bazaar package is still broken even with 5f01cc7, because __future__
imports need to be the first imports before anything else. So this time
I'm going to make the sed expression with explicit branching so we can
properly match all the occasions we want to skip and insert the line
modifying sys.argv[0] only _once_ and leave the command block after
that one substitution. So no ugly swaps between hold and pattern space.

The label which is resonsible for not escaping the command block is "r"
and we jump to it as long as we need to skip something from the start of
the file.

While at it, I'm not only skipping every line with __future__ in it but
also backslashes at the end of the line, so for example:

```python
from __future__ import shiny_feature1, \
                       shiny_feature2, \
                       shiny_feature3
```

... will now be properly skipped as well.

Tested against bazaar and nixops.

Thanks to @edolstra for reporting this.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
-rw-r--r--pkgs/development/python-modules/generic/wrap.sh9
1 files changed, 6 insertions, 3 deletions
diff --git a/pkgs/development/python-modules/generic/wrap.sh b/pkgs/development/python-modules/generic/wrap.sh
index f81adf0bae93..0193bd828371 100644
--- a/pkgs/development/python-modules/generic/wrap.sh
+++ b/pkgs/development/python-modules/generic/wrap.sh
@@ -26,9 +26,12 @@ wrapPythonProgramsIn() {
             # dont wrap EGG-INFO scripts since they are called from python
             if echo "$i" | grep -v EGG-INFO/scripts; then
                 echo "wrapping \`$i'..."
-                sed -i "$i" -e '1,/#!/ { /^#\!/,/^[^#]/ {
-                    /^[^#]/i import sys; sys.argv[0] = '"'$(basename "$i")'"'
-                }}'
+                sed -i "$i" -re '1 {
+                    /^#!/!b; :r
+                    /\\$/{n;n;n;b r}
+                    /__future__|^ *(#.*)?$/{n;b r}
+                    /^ *[^# ]/i import sys; sys.argv[0] = '"'$(basename "$i")'"'
+                }'
                 wrapProgram "$i" \
                     --prefix PYTHONPATH ":" $program_PYTHONPATH \
                     --prefix PATH ":" $program_PATH