def setup_repositories(self): """ set up repositories for software installation and return a package manager for performing software installation tasks """ repository_sections = self.xml_state.get_repository_sections() package_manager = self.xml_state.get_package_manager() repo = Repository(self.root_bind, package_manager) for xml_repo in repository_sections: repo_type = xml_repo.get_type() repo_source = xml_repo.get_source().get_path() repo_alias = xml_repo.get_alias() repo_priority = xml_repo.get_priority() log.info('Setting up repository %s', repo_source) log.info('--> Type: %s', repo_type) if repo_priority: log.info('--> Priority: %s', repo_priority) uri = Uri(repo_source, repo_type) repo_source_translated = uri.translate() log.info('--> Translated: %s', repo_source_translated) if not uri.is_remote(): self.root_bind.mount_shared_directory(repo_source_translated) if not repo_alias: repo_alias = uri.alias() log.info('--> Alias: %s', repo_alias) repo.add_repo(repo_alias, repo_source_translated, repo_type, repo_priority) self.uri_list.append(uri) repo.cleanup_unused_repos() return PackageManager(repo, package_manager)
def testHttpPathModification(self): myuri = Uri("http://example.com") myuri.path_segments = [] # there is no path in this url, so we need to init the array :( myuri.path_segments.append("test.html") self.assertEqual(str(myuri), "http://example.com/test.html") myuri.path_segments.insert(0, "directory") self.assertEqual(str(myuri), "http://example.com/directory/test.html") myuri.scheme = 'https' self.assertEqual(str(myuri), "https://example.com/directory/test.html")
def testHttpUri(self): http_url = "http://www.google.com/mail?locale=en_US#tab1" uri = Uri(http_url) self.assertEqual("http", uri.scheme) self.assertEqual("www.google.com", uri.authority) self.assertEqual("/mail", uri.path) self.assertEqual("locale=en_US", uri.query) self.assertEqual("tab1", uri.fragment)
def testBitlyUri(self): bitly_url = "http://bit.ly/f3fvFA" uri = Uri(bitly_url) self.assertEqual("http", uri.scheme) self.assertEqual("bit.ly", uri.authority) self.assertEqual("/f3fvFA", uri.path) self.assertEqual("", uri.query) self.assertEqual("", uri.fragment)
def testQuoraUri(self): q_uri = "http://www.quora.com/Is-there-a-polite-way-to-interrupt-or-stop-a-Death-by-Powerpoint-presentation" uri = Uri(q_uri) self.assertEqual("http", uri.scheme) self.assertEqual("www.quora.com", uri.authority) self.assertEqual("/Is-there-a-polite-way-to-interrupt-or-stop-a-Death-by-Powerpoint-presentation", uri.path) self.assertEqual("", uri.query) self.assertEqual("", uri.fragment)
def setup_repositories(self): """ set up repositories for software installation and return a package manager for performing software installation tasks """ repository_sections = self.xml_state.get_repository_sections() package_manager = self.xml_state.get_package_manager() repo = Repository( self.root_bind, package_manager ) for xml_repo in repository_sections: repo_type = xml_repo.get_type() repo_source = xml_repo.get_source().get_path() repo_alias = xml_repo.get_alias() repo_priority = xml_repo.get_priority() log.info('Setting up repository %s', repo_source) log.info('--> Type: %s', repo_type) if repo_priority: log.info('--> Priority: %s', repo_priority) uri = Uri(repo_source, repo_type) repo_source_translated = uri.translate() log.info('--> Translated: %s', repo_source_translated) if not uri.is_remote(): self.root_bind.mount_shared_directory(repo_source_translated) if not repo_alias: repo_alias = uri.alias() log.info('--> Alias: %s', repo_alias) repo.add_repo( repo_alias, repo_source_translated, repo_type, repo_priority ) self.uri_list.append(uri) repo.cleanup_unused_repos() return PackageManager( repo, package_manager )
def validate(self): self.type = "" self.rel = "alternate" self.hreflang = "" self.title = "" if self.attrs.has_key((None, "rel")): self.value = self.rel = self.attrs.getValue((None, "rel")) if self.rel.startswith( 'http://www.iana.org/assignments/relation/'): self.rel = self.rel[ len('http://www.iana.org/assignments/relation/'):] if self.rel in self.validRelations: self.log( ValidAtomLinkRel({ "parent": self.parent.name, "element": self.name, "attr": "rel", "value": self.rel })) elif rfc2396_full.rfc2396_re.match(self.rel.encode('idna')): self.log( ValidAtomLinkRel({ "parent": self.parent.name, "element": self.name, "attr": "rel", "value": self.rel })) else: self.log( UnregisteredAtomLinkRel({ "parent": self.parent.name, "element": self.name, "attr": "rel", "value": self.rel })) nonblank.validate(self, errorClass=AttrNotBlank, extraParams={"attr": "rel"}) if self.attrs.has_key((None, "type")): self.value = self.type = self.attrs.getValue((None, "type")) if not mime_re.match(self.type): self.log( InvalidMIMEType({ "parent": self.parent.name, "element": self.name, "attr": "type", "value": self.type })) elif self.rel == "self" and self.type not in [ "application/atom+xml", "application/rss+xml", "application/rdf+xml" ]: self.log( SelfNotAtom({ "parent": self.parent.name, "element": self.name, "attr": "type", "value": self.type })) else: self.log( ValidMIMEAttribute({ "parent": self.parent.name, "element": self.name, "attr": "type", "value": self.type })) if self.attrs.has_key((None, "title")): self.log( ValidTitle({ "parent": self.parent.name, "element": self.name, "attr": "title" })) self.value = self.title = self.attrs.getValue((None, "title")) nonblank.validate(self, errorClass=AttrNotBlank, extraParams={"attr": "title"}) nonhtml.validate(self) if self.attrs.has_key((None, "length")): self.value = self.hreflang = self.attrs.getValue((None, "length")) positiveInteger.validate(self) nonblank.validate(self) if self.attrs.has_key((None, "hreflang")): self.value = self.hreflang = self.attrs.getValue( (None, "hreflang")) iso639.validate(self) if self.attrs.has_key((None, "href")): self.value = self.attrs.getValue((None, "href")) xmlbase.validate(self, extraParams={"attr": "href"}) if self.rel == "self" and self.parent.name == "feed": from urlparse import urljoin if urljoin(self.xmlBase, self.value) not in self.dispatcher.selfURIs: if urljoin(self.xmlBase, self.value).split( '#')[0] != self.xmlBase.split('#')[0]: from uri import Uri value = Uri(self.value) for docbase in self.dispatcher.selfURIs: if value == Uri(docbase): break else: self.log( SelfDoesntMatchLocation({ "parent": self.parent.name, "element": self.name })) else: self.log( MissingHref({ "parent": self.parent.name, "element": self.name, "attr": "href" })) if self.attrs.has_key( (u'http://purl.org/syndication/thread/1.0', u'count')): if self.rel != "replies": self.log( UnexpectedAttribute({ "parent": self.parent.name, "element": self.name, "attribute": "thr:count" })) self.value = self.attrs.getValue( (u'http://purl.org/syndication/thread/1.0', u'count')) self.name = "thr:count" nonNegativeInteger.validate(self) if self.attrs.has_key( (u'http://purl.org/syndication/thread/1.0', u'when')): self.log( NoThrWhen({ "parent": self.parent.name, "element": self.name, "attribute": "thr:when" })) if self.attrs.has_key( (u'http://purl.org/syndication/thread/1.0', u'updated')): if self.rel != "replies": self.log( UnexpectedAttribute({ "parent": self.parent.name, "element": self.name, "attribute": "thr:updated" })) self.value = self.attrs.getValue( (u'http://purl.org/syndication/thread/1.0', u'updated')) self.name = "thr:updated" rfc3339.validate(self)
def getTypeFromURI(uri): # parse URI parsed = Uri(uri) # get scheme scheme = parsed.getScheme() return scheme.replace('+', ' ')
def validate(self): self.type = "" self.rel = "alternate" self.href = "" self.hreflang = "" self.title = "" if self.attrs.has_key((None, "rel")): self.value = self.rel = self.attrs.getValue((None, "rel")) if self.rel.startswith( 'http://www.iana.org/assignments/relation/'): self.rel = self.rel[ len('http://www.iana.org/assignments/relation/'):] if self.rel in self.validRelations: self.log( ValidAtomLinkRel({ "parent": self.parent.name, "element": self.name, "attr": "rel", "value": self.rel })) elif rfc2396_full.rfc2396_re.match(self.rel.encode('idna')): self.log( ValidAtomLinkRel({ "parent": self.parent.name, "element": self.name, "attr": "rel", "value": self.rel })) else: self.log( UnregisteredAtomLinkRel({ "parent": self.parent.name, "element": self.name, "attr": "rel", "value": self.rel })) nonblank.validate(self, errorClass=AttrNotBlank, extraParams={"attr": "rel"}) if self.rel in self.rfc5005 and self.parent.name == 'entry': self.log(FeedHistoryRelInEntry({"rel": self.rel})) if self.attrs.has_key((None, "type")): self.value = self.type = self.attrs.getValue((None, "type")) if not mime_re.match(self.type): self.log( InvalidMIMEType({ "parent": self.parent.name, "element": self.name, "attr": "type", "value": self.type })) elif self.rel == "self" and self.type not in [ "application/atom+xml", "application/rss+xml", "application/rdf+xml" ]: self.log( SelfNotAtom({ "parent": self.parent.name, "element": self.name, "attr": "type", "value": self.type })) else: self.log( ValidMIMEAttribute({ "parent": self.parent.name, "element": self.name, "attr": "type", "value": self.type })) if self.attrs.has_key((None, "title")): self.log( ValidTitle({ "parent": self.parent.name, "element": self.name, "attr": "title" })) self.value = self.title = self.attrs.getValue((None, "title")) nonblank.validate(self, errorClass=AttrNotBlank, extraParams={"attr": "title"}) nonhtml.validate(self) if self.attrs.has_key((None, "length")): self.name = 'length' self.value = self.attrs.getValue((None, "length")) nonNegativeInteger.validate(self) nonblank.validate(self) if self.attrs.has_key((None, "hreflang")): self.name = 'hreflang' self.value = self.hreflang = self.attrs.getValue( (None, "hreflang")) iso639.validate(self) if self.attrs.has_key((None, "href")): self.name = 'href' self.value = self.href = self.attrs.getValue((None, "href")) xmlbase.validate(self, extraParams={"attr": "href"}) if self.rel == "self" and self.parent.name in ["feed", "channel"]: # detect relative self values from urlparse import urlparse from xml.dom import XML_NAMESPACE absolute = urlparse(self.href)[1] element = self while not absolute and element and hasattr(element, 'attrs'): pattrs = element.attrs if pattrs and pattrs.has_key((XML_NAMESPACE, u'base')): absolute = urlparse( pattrs.getValue((XML_NAMESPACE, u'base')))[1] element = element.parent if not absolute: self.log(RelativeSelf({"value": self.href})) from urlparse import urljoin if urljoin(self.xmlBase, self.value) not in self.dispatcher.selfURIs: if urljoin(self.xmlBase, self.value).split( '#')[0] != self.xmlBase.split('#')[0]: from uri import Uri if self.value.startswith( 'http://feeds.feedburner.com/'): if self.value.endswith('?format=xml'): self.value = self.value.split('?')[0] value = Uri(self.value) for docbase in self.dispatcher.selfURIs: if value == Uri(docbase): break # don't complain when validating feedburner's xml view if docbase.startswith( 'http://feeds.feedburner.com/'): if docbase.endswith('?format=xml'): if value == Uri(docbase.split('?')[0]): break else: self.log( SelfDoesntMatchLocation({ "parent": self.parent.name, "element": self.name })) self.dispatcher.selfURIs.append( urljoin(self.xmlBase, self.value)) else: self.log( MissingHref({ "parent": self.parent.name, "element": self.name, "attr": "href" })) if self.attrs.has_key( (u'http://purl.org/syndication/thread/1.0', u'count')): if self.rel != "replies": self.log( UnexpectedAttribute({ "parent": self.parent.name, "element": self.name, "attribute": "thr:count" })) self.value = self.attrs.getValue( (u'http://purl.org/syndication/thread/1.0', u'count')) self.name = "thr:count" nonNegativeInteger.validate(self) if self.attrs.has_key( (u'http://purl.org/syndication/thread/1.0', u'when')): self.log( NoThrWhen({ "parent": self.parent.name, "element": self.name, "attribute": "thr:when" })) if self.attrs.has_key( (u'http://purl.org/syndication/thread/1.0', u'updated')): if self.rel != "replies": self.log( UnexpectedAttribute({ "parent": self.parent.name, "element": self.name, "attribute": "thr:updated" })) self.value = self.attrs.getValue( (u'http://purl.org/syndication/thread/1.0', u'updated')) self.name = "thr:updated" rfc3339.validate(self)
def getHostFromURI(uri): return Uri(uri).getAuthority().getHost()
def _set_uri(self, uri): if not isinstance(uri, Uri): uri = Uri(uri) self._uri = uri
def testInputOutput(self): fake_uri = "urn:example:animal:ferret:nose" uri = Uri(fake_uri) self.assertEqual(fake_uri, str(uri))
def testImaginaryUri(self): fake_uri = "urn:example:animal:ferret:nose" uri = Uri(fake_uri) self.assertEqual("urn", uri.scheme) self.assertEqual("example:animal:ferret:nose", uri.path)
def testHttpWithParamsToString(self): myuri = Uri("http://example.com/path/to/file.html") myuri.query_params["test"] = "bob" self.assertEqual(str(myuri), "http://example.com/path/to/file.html?test=bob") myuri.query_params["id"] = 1234 self.assertEqual(str(myuri), "http://example.com/path/to/file.html?test=bob&id=1234")