* Re: [PATCH] Makefile: Remove excess backslashes from sed
2010-04-09 5:57 ` Junio C Hamano
@ 2010-04-09 8:02 ` David Aguilar
2010-04-09 8:42 ` [PATCH] Makefile: Avoid multi-line sed when building python scripts David Aguilar
2010-04-09 14:33 ` [PATCH] Makefile: Remove excess backslashes from sed Brian Gernhardt
2010-04-09 15:34 ` Sverre Rabbelier
2010-04-09 15:35 ` [PATCH] Makefile: Simplify handling of python scripts Brian Gernhardt
2 siblings, 2 replies; 8+ messages in thread
From: David Aguilar @ 2010-04-09 8:02 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Brian Gernhardt, Git List
On Thu, Apr 08, 2010 at 10:57:56PM -0700, Junio C Hamano wrote:
> Brian Gernhardt <brian@gernhardtsoftware.com> writes:
>
> > The sed script that was intended to add lines altering the sys.path
> > had extra backslashes in them. Instead resulting in
> >
> > import sys; import os; sys.path.insert( ... )
> >
> > It output
> >
> > import sys; \ import os; \ sys.path.insert( ... )
> >
> > Unfortunately this caused python (2.6.1 on OS X 10.6.3) to error
> >
> > SyntaxError: unexpected character after line continuation character
> >
> > Removing two of the backslashes solves this problem.
>
> Traditionally, multi-line sed statements written in the Makefile were
> portability nightmare, as the line folding rules (especially how the
> backslash is treated in the output) in make implementations were subtly
> different, and implementations of sed also got multi-line statement often
> wrong. These days, things might have gotten much better, but in olden
> days (back when BSD vs SysV war was still raging), the trick to write
> things like this portably was to invoke a shell script that has multi-line
> sed statement from the Makefile. It was painful.
>
> I wonder if we can make this a lot simpler to avoid multi-line sed script.
> For example, we could write the source Python script to always begin with:
>
> #!/usr/bin/python
> import sys;
> import os;
> sys.path.insert(0, os.getenv("GITPYTHONLIB","."));
>
> and then do something like this in the Makefile:
>
> INSTLIBDIR=... && \
> sed -e '1s|#!.*python|#!$(PYTHON_PATH_SQ)|' \
> -e 's|\(os\.getenv("GITPYTHONLIB"\)[^)]*)|\1,"'"$$INSTLIBDIR"'")|' \
> <$@.py >$@+
> mv $@+ $@
>
> Contributors can then run things in-place while developing the scripts,
> perhaps setting GITPYTHONLIB to the source area.
Here's what I cooked up so far...:
$(patsubst %.py,%,$(SCRIPT_PYTHON)): GIT-CFLAGS
$(patsubst %.py,%,$(SCRIPT_PYTHON)): % : %.py
$(QUIET_GEN)$(RM) $@ $@+ && \
INSTLIBDIR=`MAKEFLAGS= $(MAKE) -C git_remote_helpers -s \
--no-print-directory prefix='$(prefix_SQ)' DESTDIR='$(DESTDIR_SQ)' \
instlibdir` && \
sed -e '1{' \
-e ' s|#!.*python|#!$(PYTHON_PATH_SQ)\n@@STARTUP@@\n|' \
-e '}' \
-e 's|@@STARTUP@@|import os, sys; sys.path.insert(0, @@ENV@@)|' \
-e 's|@@ENV@@|os.getenv("GITPYTHONLIB", "@@INSTLIBDIR@@")|' \
-e 's|@@INSTLIBDIR@@|'"$$INSTLIBDIR"'|g' \
$@.py >$@+ && \
chmod +x $@+ && \
mv $@+ $@
It avoids the multi-line portability concerns by substituting variables
along each step.
This is also nicer in that we don't need to start scripts with
"import sys" or os.getenv() anything... all we require is a
shebang line. We inject @@STARTUP@@ when we rewrite the
shebang.
That seems nicer then the old expression which searched for
"import sys.*" when doing the replacements.
Is the \n@@STARTUP\n thing portable?
--
David
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] Makefile: Avoid multi-line sed when building python scripts
2010-04-09 8:02 ` David Aguilar
@ 2010-04-09 8:42 ` David Aguilar
2010-04-09 14:33 ` [PATCH] Makefile: Remove excess backslashes from sed Brian Gernhardt
1 sibling, 0 replies; 8+ messages in thread
From: David Aguilar @ 2010-04-09 8:42 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Brian Gernhardt, Git List, Johan Herland, Sverre Rabbelier
Instead of requiring scripts to start with "import sys",
install the sys.path initialization code immediately
after each script's new shebang line.
This works around portability issues with multi-line sed and
simplifies the work for contributors. There is no longer
the requirement that every script must "import sys" before
importing git-provided modules.
Signed-off-by: David Aguilar <davvid@gmail.com>
---
Makefile | 8 +++-----
1 files changed, 3 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
index 956e781..b763947 100644
--- a/Makefile
+++ b/Makefile
@@ -1608,12 +1608,10 @@ $(patsubst %.py,%,$(SCRIPT_PYTHON)): % : %.py
--no-print-directory prefix='$(prefix_SQ)' DESTDIR='$(DESTDIR_SQ)' \
instlibdir` && \
sed -e '1{' \
- -e ' s|#!.*python|#!$(PYTHON_PATH_SQ)|' \
+ -e ' s|#!.*python|#!$(PYTHON_PATH_SQ)\n@@STARTUP@@|' \
-e '}' \
- -e 's|^import sys.*|&; \\\
- import os; \\\
- sys.path.insert(0, os.getenv("GITPYTHONLIB",\
- "@@INSTLIBDIR@@"));|' \
+ -e 's|@@STARTUP@@|import os, sys; sys.path.insert(0, @@ENV@@)|' \
+ -e 's|@@ENV@@|os.getenv("GITPYTHONLIB", "@@INSTLIBDIR@@")|' \
-e 's|@@INSTLIBDIR@@|'"$$INSTLIBDIR"'|g' \
$@.py >$@+ && \
chmod +x $@+ && \
--
1.7.0.3.313.g87b3c
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] Makefile: Remove excess backslashes from sed
2010-04-09 8:02 ` David Aguilar
2010-04-09 8:42 ` [PATCH] Makefile: Avoid multi-line sed when building python scripts David Aguilar
@ 2010-04-09 14:33 ` Brian Gernhardt
1 sibling, 0 replies; 8+ messages in thread
From: Brian Gernhardt @ 2010-04-09 14:33 UTC (permalink / raw)
To: David Aguilar; +Cc: Junio C Hamano, Git List
On Apr 9, 2010, at 4:02 AM, David Aguilar wrote:
> Is the \n@@STARTUP\n thing portable?
No. OS X's sed renders it as
#!/usr/bin/pythonnimport os, sys; sys.path.insert(0, os.getenv("GITPYTHONLIB", "/usr/local/lib/python2.6/site-packages"))
So NAK for your patch.
~~ Brian
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] Makefile: Remove excess backslashes from sed
2010-04-09 5:57 ` Junio C Hamano
2010-04-09 8:02 ` David Aguilar
@ 2010-04-09 15:34 ` Sverre Rabbelier
2010-04-09 15:35 ` [PATCH] Makefile: Simplify handling of python scripts Brian Gernhardt
2 siblings, 0 replies; 8+ messages in thread
From: Sverre Rabbelier @ 2010-04-09 15:34 UTC (permalink / raw)
To: Junio C Hamano, Johan Herland, David Aguilar; +Cc: Brian Gernhardt, Git List
Heya,
On Fri, Apr 9, 2010 at 07:57, Junio C Hamano <gitster@pobox.com> wrote:
> I wonder if we can make this a lot simpler to avoid multi-line sed script.
> For example, we could write the source Python script to always begin with:
>
> #!/usr/bin/python
> import sys;
> import os;
> sys.path.insert(0, os.getenv("GITPYTHONLIB","."));
Thank you, I like this a lot better than our current approach. I think
it's very ugly that git (needs to) mangle(s) the python files when
installing, since that means that line-numbers don't match in
stacktraces. I'd definitely prefer using this.
--
Cheers,
Sverre Rabbelier
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] Makefile: Simplify handling of python scripts
2010-04-09 5:57 ` Junio C Hamano
2010-04-09 8:02 ` David Aguilar
2010-04-09 15:34 ` Sverre Rabbelier
@ 2010-04-09 15:35 ` Brian Gernhardt
2 siblings, 0 replies; 8+ messages in thread
From: Brian Gernhardt @ 2010-04-09 15:35 UTC (permalink / raw)
To: Git List; +Cc: David Aguilar
The sed script intended to add a standard opening to python scripts
was non-compatible and overly complex. Simplifying it down to a set
of one-liners removes the compatibility issues of newlines. Moving
the environment alterations from the Makefile to the python scripts
makes also makes the scripts easier to run in-place.
Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Brian Gernhardt <brian@gernhardtsoftware.com>
---
Makefile | 9 ++-------
git-remote-testgit.py | 2 ++
2 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/Makefile b/Makefile
index f0fe351..87c90d6 100644
--- a/Makefile
+++ b/Makefile
@@ -1629,13 +1629,8 @@ $(patsubst %.py,%,$(SCRIPT_PYTHON)): % : %.py
INSTLIBDIR=`MAKEFLAGS= $(MAKE) -C git_remote_helpers -s \
--no-print-directory prefix='$(prefix_SQ)' DESTDIR='$(DESTDIR_SQ)' \
instlibdir` && \
- sed -e '1{' \
- -e ' s|#!.*python|#!$(PYTHON_PATH_SQ)|' \
- -e '}' \
- -e 's|^import sys.*|&; \\\
- import os; \\\
- sys.path.insert(0, os.getenv("GITPYTHONLIB",\
- "@@INSTLIBDIR@@"));|' \
+ sed -e '1s|#!.*python|#!$(PYTHON_PATH_SQ)|' \
+ -e 's|\(os\.getenv("GITPYTHONLIB"\)[^)]*)|\1,"@@INSTLIBDIR@@")|' \
-e 's|@@INSTLIBDIR@@|'"$$INSTLIBDIR"'|g' \
$@.py >$@+ && \
chmod +x $@+ && \
diff --git a/git-remote-testgit.py b/git-remote-testgit.py
index f61624e..9253922 100644
--- a/git-remote-testgit.py
+++ b/git-remote-testgit.py
@@ -2,6 +2,8 @@
import hashlib
import sys
+import os
+sys.path.insert(0, os.getenv("GITPYTHONLIB","."))
from git_remote_helpers.util import die, debug, warn
from git_remote_helpers.git.repo import GitRepo
--
1.7.1.rc0.243.g2ce66
^ permalink raw reply related [flat|nested] 8+ messages in thread