def fromCodeImport(cls, code_import): """Convert a `CodeImport` to an instance.""" branch = code_import.branch if branch.stacked_on is not None and not branch.stacked_on.private: stacked_path = branch_id_alias(branch.stacked_on) stacked_on_url = compose_public_url('http', stacked_path) else: stacked_on_url = None if code_import.rcs_type == RevisionControlSystems.SVN: return cls( branch.id, 'svn', str(code_import.url), stacked_on_url=stacked_on_url) elif code_import.rcs_type == RevisionControlSystems.BZR_SVN: return cls( branch.id, 'bzr-svn', str(code_import.url), stacked_on_url=stacked_on_url) elif code_import.rcs_type == RevisionControlSystems.CVS: return cls( branch.id, 'cvs', cvs_root=str(code_import.cvs_root), cvs_module=str(code_import.cvs_module)) elif code_import.rcs_type == RevisionControlSystems.GIT: return cls( branch.id, 'git', str(code_import.url), stacked_on_url=stacked_on_url) elif code_import.rcs_type == RevisionControlSystems.BZR: return cls( branch.id, 'bzr', str(code_import.url), stacked_on_url=stacked_on_url) else: raise AssertionError("Unknown rcstype %r." % code_import.rcs_type)
def test_branch_id_alias_private(self): # Private branches are not found at all (this is for anonymous access) owner = self.factory.makePerson() branch = self.factory.makeAnyBranch(owner=owner, information_type=InformationType.USERDATA) with person_logged_in(owner): path = branch_id_alias(branch) result = self.makeRewriter()._getBranchIdAndTrailingPath(path) self.assertEqual((None, None, "MISS"), result)
def test_translateControlPath(self): branch = self.factory.makeProductBranch(owner=self.requester) self.factory.enableDefaultStackingForProduct(branch.product, branch) transport, path = yield self.server.translateVirtualPath( '~%s/%s/.bzr/control.conf' % (branch.owner.name, branch.product.name)) self.assertEqual('default_stack_on = %s\n' % branch_id_alias(branch), transport.get_bytes(path))
def test_branch_id_alias_private(self): # Private branches are not found at all (this is for anonymous access) owner = self.factory.makePerson() branch = self.factory.makeAnyBranch( owner=owner, information_type=InformationType.USERDATA) with person_logged_in(owner): path = branch_id_alias(branch) result = self.makeRewriter()._getBranchIdAndTrailingPath(path) self.assertEqual((None, None, 'MISS'), result)
def test_rewriteLine_id_alias_private(self): # All requests for /+branch-id/$id/... for private branches return # 'NULL'. This is translated by apache to a 404. rewriter = self.makeRewriter() branch = self.factory.makeAnyBranch(information_type=InformationType.USERDATA) path = branch_id_alias(removeSecurityProxy(branch)) transaction.commit() output = [rewriter.rewriteLine("%s/changes" % path), rewriter.rewriteLine("%s/.bzr" % path)] self.assertEqual(["NULL", "NULL"], output)
def test_get_stacking_policy(self): # A stacking policy control file is served underneath product # directories for products that have a default stacked-on branch. product = self.factory.makeProduct() self.factory.enableDefaultStackingForProduct(product) transport = self.getTransport() control_file = transport.get_bytes('~%s/%s/.bzr/control.conf' % (self.requester.name, product.name)) stacked_on = IBranchTarget(product).default_stacked_on_branch self.assertEqual('default_stack_on = %s' % branch_id_alias(stacked_on), control_file.strip())
def test_rewriteLine_id_alias_private(self): # All requests for /+branch-id/$id/... for private branches return # 'NULL'. This is translated by apache to a 404. rewriter = self.makeRewriter() branch = self.factory.makeAnyBranch( information_type=InformationType.USERDATA) path = branch_id_alias(removeSecurityProxy(branch)) transaction.commit() output = [ rewriter.rewriteLine("%s/changes" % path), rewriter.rewriteLine("%s/.bzr" % path) ] self.assertEqual(['NULL', 'NULL'], output)
def test_get_stacking_policy(self): # A stacking policy control file is served underneath product # directories for products that have a default stacked-on branch. product = self.factory.makeProduct() self.factory.enableDefaultStackingForProduct(product) transport = self.getTransport() control_file = transport.get_bytes( '~%s/%s/.bzr/control.conf' % (self.requester.name, product.name)) stacked_on = IBranchTarget(product).default_stacked_on_branch self.assertEqual( 'default_stack_on = %s' % branch_id_alias(stacked_on), control_file.strip())
def test_rewriteLine_id_alias_logs_cache_hit(self): # The second request for a branch using the alias hits the cache. rewriter = self.makeRewriter() branch = self.factory.makeAnyBranch() transaction.commit() path = "%s/.bzr/README" % branch_id_alias(branch) rewriter.rewriteLine(path) rewriter.rewriteLine(path) logging_output_lines = self.getLoggerOutput(rewriter).strip().split("\n") self.assertEqual(2, len(logging_output_lines)) self.assertIsNot( None, re.match("INFO .* -> .* (.*s, cache: HIT)", logging_output_lines[-1]), "No hit found in %r" % logging_output_lines[-1], )
def test_rewriteLine_id_alias_found_dot_bzr(self): # Requests for /+branch-id/$id/.bzr/... are redirected to where the # branches are served from by ID if they are public. rewriter = self.makeRewriter() branches = [ self.factory.makeProductBranch(), self.factory.makePersonalBranch(), self.factory.makePackageBranch(), ] transaction.commit() output = [rewriter.rewriteLine("%s/.bzr/README" % branch_id_alias(branch)) for branch in branches] expected = [ "file:///var/tmp/bazaar.launchpad.dev/mirrors/%s/.bzr/README" % branch_id_to_path(branch.id) for branch in branches ] self.assertEqual(expected, output)
def updateBranches(self, branches): """Update the stacked_on_location for all branches in 'branches'. :param branches: An iterator yielding (branch_id, branch_type, unique_name, stacked_on_unique_name). """ for branch_info in branches: (branch_id, branch_type, unique_name, stacked_on_id, stacked_on_name) = branch_info if self.options.stack_on_id: branch = FakeBranch(stacked_on_id) stacked_on_location = branch_id_alias(branch) else: stacked_on_location = '/' + stacked_on_name self.updateStackedOn(branch_id, 'lp-internal:///' + unique_name, stacked_on_location)
def test_rewriteLine_id_alias_logs_cache_hit(self): # The second request for a branch using the alias hits the cache. rewriter = self.makeRewriter() branch = self.factory.makeAnyBranch() transaction.commit() path = "%s/.bzr/README" % branch_id_alias(branch) rewriter.rewriteLine(path) rewriter.rewriteLine(path) logging_output_lines = self.getLoggerOutput(rewriter).strip().split( '\n') self.assertEqual(2, len(logging_output_lines)) self.assertIsNot( None, re.match("INFO .* -> .* (.*s, cache: HIT)", logging_output_lines[-1]), "No hit found in %r" % logging_output_lines[-1])
def updateBranches(self, branches): """Update the stacked_on_location for all branches in 'branches'. :param branches: An iterator yielding (branch_id, branch_type, unique_name, stacked_on_unique_name). """ for branch_info in branches: (branch_id, branch_type, unique_name, stacked_on_id, stacked_on_name) = branch_info if self.options.stack_on_id: branch = FakeBranch(stacked_on_id) stacked_on_location = branch_id_alias(branch) else: stacked_on_location = '/' + stacked_on_name self.updateStackedOn( branch_id, 'lp-internal:///' + unique_name, stacked_on_location)
def _serializeControlDirectory(self, requester, lookup): trailing_path = lookup['trailing'].lstrip('/') if not ('.bzr' == trailing_path or trailing_path.startswith('.bzr/')): return target = self._get_product_target(lookup['control_name']) if target is None: target = self._get_package_target(lookup['control_name']) if target is None: return default_branch = IBranchTarget(target).default_stacked_on_branch if default_branch is None: return if not self._canRead(requester, default_branch): return path = branch_id_alias(default_branch) return (CONTROL_TRANSPORT, { 'default_stack_on': escape(path) }, trailing_path)
def _serializeControlDirectory(self, requester, lookup): trailing_path = lookup['trailing'].lstrip('/') if not ('.bzr' == trailing_path or trailing_path.startswith('.bzr/')): return target = self._get_product_target(lookup['control_name']) if target is None: target = self._get_package_target(lookup['control_name']) if target is None: return default_branch = IBranchTarget(target).default_stacked_on_branch if default_branch is None: return if not self._canRead(requester, default_branch): return path = branch_id_alias(default_branch) return ( CONTROL_TRANSPORT, {'default_stack_on': escape(path)}, trailing_path)
def test_rewriteLine_id_alias_found_dot_bzr(self): # Requests for /+branch-id/$id/.bzr/... are redirected to where the # branches are served from by ID if they are public. rewriter = self.makeRewriter() branches = [ self.factory.makeProductBranch(), self.factory.makePersonalBranch(), self.factory.makePackageBranch() ] transaction.commit() output = [ rewriter.rewriteLine("%s/.bzr/README" % branch_id_alias(branch)) for branch in branches ] expected = [ 'file:///var/tmp/bazaar.launchpad.dev/mirrors/%s/.bzr/README' % branch_id_to_path(branch.id) for branch in branches ] self.assertEqual(expected, output)
def _serializeControlDirectory(self, requester, lookup): try: namespace = lookup_branch_namespace(lookup['control_name']) except (InvalidNamespace, NotFoundError): return trailing_path = lookup['trailing'].lstrip('/') if not ('.bzr' == trailing_path or trailing_path.startswith('.bzr/')): # '.bzr' is OK, '.bzr/foo' is OK, '.bzrfoo' is not. return default_branch = namespace.target.default_stacked_on_branch if default_branch is None: return try: path = branch_id_alias(default_branch) except Unauthorized: return return (CONTROL_TRANSPORT, { 'default_stack_on': escape(path) }, escape(trailing_path))
def _serializeControlDirectory(self, requester, lookup): try: namespace = lookup_branch_namespace(lookup['control_name']) except (InvalidNamespace, NotFoundError): return trailing_path = lookup['trailing'].lstrip('/') if not ('.bzr' == trailing_path or trailing_path.startswith('.bzr/')): # '.bzr' is OK, '.bzr/foo' is OK, '.bzrfoo' is not. return default_branch = namespace.target.default_stacked_on_branch if default_branch is None: return try: path = branch_id_alias(default_branch) except Unauthorized: return return ( CONTROL_TRANSPORT, {'default_stack_on': escape(path)}, escape(trailing_path))
def test_getByUrl_with_id_alias(self): """getByUrl works with +branch-id aliases.""" branch = self.factory.makeAnyBranch() url = compose_public_url('bzr+ssh', branch_id_alias(branch)) branch_lookup = getUtility(IBranchLookup) self.assertEqual(branch, branch_lookup.getByUrl(url))
def test_branch_id_alias_public_slash(self): # A trailing slash is returned as the extra path. branch = self.factory.makeAnyBranch() path = '%s/' % branch_id_alias(branch) result = self.lookup.getByHostingPath(path.lstrip('/')) self.assertEqual((branch, '/'), result)
def check_control_file((transport, path)): self.assertEqual( 'default_stack_on = %s\n' % branch_id_alias(branch), transport.get_bytes(path))
def test_branch_id_alias_public(self): # Public branches can be accessed. branch = self.factory.makeAnyBranch() path = branch_id_alias(branch) result = self.lookup.getByHostingPath(path.lstrip('/')) self.assertEqual((branch, ''), result)
def test_branch_id_alias_public_with_path(self): # All the path after the number is returned as the trailing path. branch = self.factory.makeAnyBranch() path = '%s/foo' % branch_id_alias(branch) result = self.lookup.getByHostingPath(path.lstrip('/')) self.assertEqual((branch, '/foo'), result)