From 6cd13b5b860c4edc615f111017fac614e28d9fed Mon Sep 17 00:00:00 2001
From: Michel Alexandre Salim <salimma@fedoraproject.org>
Date: Wed, 29 Jun 2011 14:31:45 +0200
Subject: [PATCH] 0alias: new-style launcher scripts
alias.py:
- generate new-style launcher script without --versions handling
- modify detection code to detect both variants of launcher scripts
testalias.py:
- updated to match new launcher script template
- also test parsing of old-style launcher scripts
---
tests/testalias.py | 27 ++++++++++++++++++++++++++-
zeroinstall/alias.py | 28 +++++++++++++++++++++-------
2 files changed, 47 insertions(+), 8 deletions(-)
diff --git a/tests/testalias.py b/tests/testalias.py
index a96aea4..43863a8 100755
--- a/tests/testalias.py
+++ b/tests/testalias.py
@@ -9,14 +9,22 @@ sys.path.insert(0, '..')
from zeroinstall import alias
expected_script = """#!/bin/sh
+exec 0launch 'http://example.com/foo.xml' "$@"
+"""
+
+old_script = """#!/bin/sh
if [ "$*" = "--versions" ]; then
exec 0launch -gd 'http://example.com/foo.xml' "$@"
else
exec 0launch 'http://example.com/foo.xml' "$@"
fi
+ """
+
+expected_script_main = """#!/bin/sh
+exec 0launch --main 'a'\\'''\\''\\test' 'http://example.com/foo.xml' "$@"
"""
-expected_script_main = """#!/bin/sh
+old_script_main = """#!/bin/sh
if [ "$*" = "--versions" ]; then
exec 0launch -gd 'http://example.com/foo.xml' "$@"
else
@@ -53,6 +61,23 @@ class TestAlias(BaseTest):
uri, main = alias.parse_script(tmp.name)
self.assertEquals('http://example.com/foo.xml', uri)
self.assertEquals('a\'\'\\test', main)
+
+ def testParseOld(self):
+ tmp = tempfile.NamedTemporaryFile()
+ tmp.write(old_script)
+ tmp.flush()
+ tmp.seek(0)
+ uri, main = alias.parse_script(tmp.name)
+ self.assertEquals('http://example.com/foo.xml', uri)
+ self.assertEquals(None, main)
+
+ tmp = tempfile.NamedTemporaryFile()
+ tmp.write(old_script_main)
+ tmp.flush()
+ tmp.seek(0)
+ uri, main = alias.parse_script(tmp.name)
+ self.assertEquals('http://example.com/foo.xml', uri)
+ self.assertEquals('a\'\'\\test', main)
def testParseException(self):
tmp = tempfile.NamedTemporaryFile()
diff --git a/zeroinstall/alias.py b/zeroinstall/alias.py
index a7189ab..9b9fa89 100644
--- a/zeroinstall/alias.py
+++ b/zeroinstall/alias.py
@@ -8,7 +8,7 @@ Support code for 0alias scripts.
from zeroinstall import _
-_template = '''#!/bin/sh
+_old_template = '''#!/bin/sh
if [ "$*" = "--versions" ]; then
exec 0launch -gd '%s' "$@"
else
@@ -16,6 +16,10 @@ else
fi
'''
+_template = '''#!/bin/sh
+exec 0launch %s '%s' "$@"
+'''
+
class NotAnAliasScript(Exception):
pass
@@ -27,12 +31,22 @@ def parse_script(pathname):
@raise NotAnAliasScript: if we can't parse the script
"""
stream = file(pathname)
- template_header = _template[:_template.index("-gd '")]
+ template_header = _template[:_template.index("%s '")]
actual_header = stream.read(len(template_header))
- if template_header != actual_header:
- raise NotAnAliasScript(_("'%s' does not look like a script created by 0alias") % pathname)
- rest = stream.read() # If it's a 0alias script, it should be quite short!
- line = rest.split('\n')[2]
+ stream.seek(0)
+ if template_header == actual_header:
+ # If it's a 0alias script, it should be quite short!
+ rest = stream.read()
+ line = rest.split('\n')[1]
+ else:
+ old_template_header = \
+ _old_template[:_old_template.index("-gd '")]
+ actual_header = stream.read(len(old_template_header))
+ if old_template_header != actual_header:
+ raise NotAnAliasScript(_("'%s' does not look like a script created by 0alias") % pathname)
+ rest = stream.read()
+ line = rest.split('\n')[2]
+
split = line.rfind("' '")
if split != -1:
# We have a --main
@@ -57,4 +71,4 @@ def write_script(stream, interface_uri, main = None):
else:
main_arg = ""
- stream.write(_template % (interface_uri, main_arg, interface_uri))
+ stream.write(_template % (main_arg, interface_uri))
--
1.7.5.4