Exemplo n.º 1
0
	def testParse(self):
		tmp = tempfile.NamedTemporaryFile()
		tmp.write(expected_script)
		tmp.flush()
		tmp.seek(0)
		uri, main = alias.parse_script(tmp.name)
		self.assertEqual('http://example.com/foo.xml', uri)
		self.assertEqual(None, main)

		tmp = tempfile.NamedTemporaryFile()
		tmp.write(expected_script_main)
		tmp.flush()
		tmp.seek(0)
		uri, main = alias.parse_script(tmp.name)
		self.assertEqual('http://example.com/foo.xml', uri)
		self.assertEqual('a\'\'\\test', main)

		tmp = tempfile.NamedTemporaryFile()
		tmp.write(expected_script_command)
		tmp.flush()
		tmp.seek(0)
		info = alias.parse_script(tmp.name)
		self.assertEqual('http://example.com/foo.xml', info.uri)
		self.assertEqual('a\'\'\\test', info.command)
		self.assertEqual(None, info.main)
Exemplo n.º 2
0
 def testParseException(self):
     tmp = tempfile.NamedTemporaryFile()
     tmp.write('hi' + expected_script)
     tmp.flush()
     tmp.seek(0)
     try:
         alias.parse_script(tmp.name)
         assert False
     except alias.NotAnAliasScript:
         pass
Exemplo n.º 3
0
	def testParseException(self):
		tmp = tempfile.NamedTemporaryFile()
		tmp.write('hi' + expected_script)
		tmp.flush()
		tmp.seek(0)
		try:
			alias.parse_script(tmp.name)
			assert False
		except alias.NotAnAliasScript:
			pass
Exemplo n.º 4
0
def canonical_iface_uri(uri):
	"""If uri is a relative path, convert to an absolute one.
	A "file:///foo" URI is converted to "/foo".
	An "alias:prog" URI expands to the URI in the 0alias script
	Otherwise, return it unmodified.
	@rtype: str
	@raise SafeException: if uri isn't valid
	"""
	if uri.startswith('http://') or uri.startswith('https://'):
		if uri.count("/") < 3:
			raise SafeException(_("Missing / after hostname in URI '%s'") % uri)
		return uri
	elif uri.startswith('file:///'):
		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
		alias_prog = uri[6:]
		if not os.path.isabs(alias_prog):
			full_path = support.find_in_path(alias_prog)
			if not full_path:
				raise alias.NotAnAliasScript("Not found in $PATH: " + alias_prog)
		else:
			full_path = alias_prog
		return alias.parse_script(full_path).uri
	else:
		path = os.path.realpath(uri)

	if os.path.isfile(path):
		return path

	if '/' not in uri:
		alias_path = support.find_in_path(uri)
		if alias_path is not None:
			from zeroinstall import alias
			try:
				alias.parse_script(alias_path)
			except alias.NotAnAliasScript:
				pass
			else:
				raise SafeException(_("Bad interface name '{uri}'.\n"
					"(hint: try 'alias:{uri}' instead)".format(uri = uri)))

	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': path})
Exemplo n.º 5
0
	def testParseOld(self):
		tmp = tempfile.NamedTemporaryFile()
		tmp.write(old_script)
		tmp.flush()
		tmp.seek(0)
		uri, main = alias.parse_script(tmp.name)
		self.assertEqual('http://example.com/foo.xml', uri)
		self.assertEqual(None, main)

		tmp = tempfile.NamedTemporaryFile()
		tmp.write(old_script_main)
		tmp.flush()
		tmp.seek(0)
		uri, main = alias.parse_script(tmp.name)
		self.assertEqual('http://example.com/foo.xml', uri)
		self.assertEqual('a\'\'\\test', main)
Exemplo n.º 6
0
def canonical_iface_uri(uri):
	"""If uri is a relative path, convert to an absolute one.
	A "file:///foo" URI is converted to "/foo".
	An "alias:prog" URI expands to the URI in the 0alias script
	Otherwise, return it unmodified.
	@rtype: str
	@raise SafeException: if uri isn't valid
	"""
	if uri.startswith('http://') or uri.startswith('https://'):
		if uri.count("/") < 3:
			raise SafeException(_("Missing / after hostname in URI '%s'") % uri)
		return uri
	elif uri.startswith('file:///'):
		return uri[7:]
	elif uri.startswith('alias:'):
		from zeroinstall import alias, support
		alias_prog = uri[6:]
		if not os.path.isabs(alias_prog):
			full_path = support.find_in_path(alias_prog)
			if not full_path:
				raise alias.NotAnAliasScript("Not found in $PATH: " + alias_prog)
		else:
			full_path = alias_prog
		interface_uri, main = alias.parse_script(full_path)
		return interface_uri
	else:
		iface_uri = os.path.realpath(uri)
		if os.path.isfile(iface_uri):
			return iface_uri
	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})
Exemplo n.º 7
0
    def testParse(self):
        tmp = tempfile.NamedTemporaryFile()
        tmp.write(expected_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(expected_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)
Exemplo n.º 8
0
	def testParseException(self):
		tmp = tempfile.NamedTemporaryFile()
		tmp.write('hi' + expected_script)
		tmp.flush()
		tmp.seek(0)
		try:
			alias.parse_script(tmp.name)
			assert False
		except alias.NotAnAliasScript:
			pass

		tmp = tempfile.NamedTemporaryFile()
		tmp.write(expected_script_command.replace('command', 'bob'))
		tmp.flush()
		tmp.seek(0)
		try:
			alias.parse_script(tmp.name)
			assert False
		except alias.NotAnAliasScript, ex:
			assert 'bob' in str(ex)
			pass
Exemplo n.º 9
0
	def testParseException(self):
		tmp = tempfile.NamedTemporaryFile(mode = 'wb', delete = False)
		tmp.write(bytes([240]))
		tmp.close()
		try:
			alias.parse_script(tmp.name)
			assert False
		except alias.NotAnAliasScript:
			pass
		os.unlink(tmp.name)

		tmp = tempfile.NamedTemporaryFile(mode = 'wt')
		tmp.write('hi' + expected_script)
		tmp.flush()
		tmp.seek(0)
		try:
			alias.parse_script(tmp.name)
			assert False
		except alias.NotAnAliasScript:
			pass

		tmp = tempfile.NamedTemporaryFile(mode = 'wt')
		tmp.write(expected_script_command.replace('command', 'bob'))
		tmp.flush()
		tmp.seek(0)
		try:
			alias.parse_script(tmp.name)
			assert False
		except alias.NotAnAliasScript as ex:
			assert 'does not look like a script created by 0alias' in str(ex)
			pass
Exemplo n.º 10
0
def canonical_iface_uri(uri):
	"""If uri is a relative path, convert to an absolute one.
	A "file:///foo" URI is converted to "/foo".
	An "alias:prog" URI expands to the URI in the 0alias script
	Otherwise, return it unmodified.
	@rtype: str
	@raise SafeException: if uri isn't valid
	"""
	if uri.startswith('http://') or uri.startswith('https://'):
		if uri.count("/") < 3:
			raise SafeException(_("Missing / after hostname in URI '%s'") % uri)
		return uri
	elif uri.startswith('file:///'):
		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:]
		if not os.path.isabs(alias_prog):
			full_path = support.find_in_path(alias_prog)
			if not full_path:
				raise alias.NotAnAliasScript("Not found in $PATH: " + alias_prog)
		else:
			full_path = alias_prog
		return alias.parse_script(full_path).uri
	else:
		path = os.path.realpath(uri)

	# Might be local sweet directory
	if os.path.exists(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': path})