From faf7ca00d3bfc844aefdb4368343acb1dacb40e1 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Tue, 17 Jan 2012 19:29:09 +0000 Subject: [PATCH] Allow file:path syntax for relative paths Also, throw an error if the path doesn't exist for file: URIs. --- tests/testmodel.py | 26 ++++++++++++++++++++++++++ zeroinstall/alias.py | 4 ++-- zeroinstall/injector/model.py | 15 ++++++++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/tests/testmodel.py b/tests/testmodel.py index aecacfd..20105c7 100755 --- a/tests/testmodel.py +++ b/tests/testmodel.py @@ -261,6 +261,7 @@ class TestModel(BaseTest): assert 'Malformed arch' in str(ex) def testCanonical(self): + # HTTP try: model.canonical_iface_uri('http://foo') assert False @@ -274,6 +275,31 @@ class TestModel(BaseTest): assert False except model.SafeException as ex: assert 'Bad interface name' in str(ex) + + # Bare relative path + model.canonical_iface_uri('Command.xml') + try: + model.canonical_iface_uri('CommandMissing.xml') + assert False + except model.SafeException as ex: + assert "Bad interface name 'CommandMissing.xml'" in str(ex), ex + + # file:absolute + model.canonical_iface_uri('file://{path}/Command.xml'.format(path = mydir)) + try: + print model.canonical_iface_uri('file://{path}/CommandMissing.xml'.format(path = mydir)) + assert False + except model.SafeException as ex: + assert "Bad interface name 'file://" in str(ex), ex + + # file:relative + model.canonical_iface_uri('file:Command.xml') + try: + model.canonical_iface_uri('file:CommandMissing.xml') + assert False + except model.SafeException as ex: + assert "Bad interface name 'file:CommandMissing.xml'" in str(ex), ex + def testVersions(self): def pv(v): diff --git a/zeroinstall/alias.py b/zeroinstall/alias.py index f153223..f011822 100644 --- a/zeroinstall/alias.py +++ b/zeroinstall/alias.py @@ -6,7 +6,7 @@ Support code for 0alias scripts. # Copyright (C) 2009, Thomas Leonard # See the README file for details, or visit http://0install.net. -from zeroinstall import _ +from zeroinstall import _, SafeException _old_template = '''#!/bin/sh if [ "$*" = "--versions" ]; then @@ -20,7 +20,7 @@ _template = '''#!/bin/sh exec 0launch %s'%s' "$@" ''' -class NotAnAliasScript(Exception): +class NotAnAliasScript(SafeException): pass class ScriptInfo: diff --git a/zeroinstall/injector/model.py b/zeroinstall/injector/model.py index a08d9c1..f854a02 100644 --- a/zeroinstall/injector/model.py +++ b/zeroinstall/injector/model.py @@ -1212,7 +1212,11 @@ def canonical_iface_uri(uri): raise SafeException(_("Missing / after hostname in URI '%s'") % uri) return uri elif uri.startswith('file:///'): - return uri[7:] + path = uri[7:] + elif uri.startswith('file:'): + if uri[5] == '/': + raise SafeException(_('Use file:///path for absolute paths, not {uri}').format(uri = uri)) + path = os.path.abspath(uri[5:]) elif uri.startswith('alias:'): from zeroinstall import alias, support alias_prog = uri[6:] @@ -1224,13 +1228,14 @@ def canonical_iface_uri(uri): full_path = alias_prog return alias.parse_script(full_path).uri else: - iface_uri = os.path.realpath(uri) - if os.path.isfile(iface_uri): - return iface_uri + path = os.path.realpath(uri) + + if os.path.isfile(path): + return path raise SafeException(_("Bad interface name '%(uri)s'.\n" "(doesn't start with 'http:', and " "doesn't exist as a local file '%(interface_uri)s' either)") % - {'uri': uri, 'interface_uri': iface_uri}) + {'uri': uri, 'interface_uri': path}) _version_mod_to_value = { 'pre': -2, -- 2.11.4.GIT