Esempio n. 1
0
    def run(self, src_location, dest_location):
        from bzrlib.bzrdir import BzrDir, format_registry
        from bzrlib.errors import NoRepositoryPresent, NotBranchError
        from bzrlib.repository import Repository
        source_repo = Repository.open(src_location)
        format = format_registry.make_bzrdir('rich-root-pack')
        try:
            target_bzrdir = BzrDir.open(dest_location)
        except NotBranchError:
            target_bzrdir = BzrDir.create(dest_location, format=format)
        try:
            target_repo = target_bzrdir.open_repository()
        except NoRepositoryPresent:
            target_repo = target_bzrdir.create_repository(shared=True)

        target_repo.fetch(source_repo)
        for name, ref in source_repo._git.heads().iteritems():
            head_loc = os.path.join(dest_location, name)
            try:
                head_bzrdir = BzrDir.open(head_loc)
            except NotBranchError:
                head_bzrdir = BzrDir.create(head_loc, format=format)
            try:
                head_branch = head_bzrdir.open_branch()
            except NotBranchError:
                head_branch = head_bzrdir.create_branch()
            head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
Esempio n. 2
0
    def run(self, src_location, dest_location):
        from bzrlib.bzrdir import BzrDir, format_registry
        from bzrlib.errors import NoRepositoryPresent, NotBranchError
        from bzrlib.repository import Repository
        source_repo = Repository.open(src_location)
        format = format_registry.make_bzrdir('rich-root-pack')
        try:
            target_bzrdir = BzrDir.open(dest_location)
        except NotBranchError:
            target_bzrdir = BzrDir.create(dest_location, format=format)
        try:
            target_repo = target_bzrdir.open_repository()
        except NoRepositoryPresent:
            target_repo = target_bzrdir.create_repository(shared=True)

        target_repo.fetch(source_repo)
        for name, ref in source_repo._git.heads().iteritems():
            head_loc = os.path.join(dest_location, name)
            try:
                head_bzrdir = BzrDir.open(head_loc)
            except NotBranchError:
                head_bzrdir = BzrDir.create(head_loc, format=format)
            try:
                head_branch = head_bzrdir.open_branch()
            except NotBranchError:
                head_branch = head_bzrdir.create_branch()
            head_branch.generate_revision_history(
                source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
Esempio n. 3
0
def get_branch(repo, relpath, format=None):
    """Return existing branch in destination repo. Create new if don't exist.

    @param  format:     force create new branch in specified format.
    """
    repo_trans = repo.bzrdir.root_transport
    try:
        br_dir = BzrDir.open(repo_trans.abspath(relpath))
        branch = br_dir.open_branch()
    except errors.NotBranchError:
        # create destination branch directory, creating parents as needed.
        needed = [relpath]
        while needed:
            try:
                repo_trans.mkdir(needed[-1])
                needed.pop()
            except errors.NoSuchFile:
                parent = urlutils.dirname(needed[-1])
                if parent == '':
                    raise errors.BzrCommandError('Could not create branch dir')
                needed.append(parent)
        br_dir = BzrDir.create(repo_trans.abspath(relpath))
        if format is None:
            format = BranchFormat.get_default_format()
        branch = format.initialize(br_dir)

        note('Created destination branch %s' % relpath)

    if branch.repository.bzrdir.root_transport.base != repo_trans.base:
        raise errors.BzrCommandError('Branch %s does not use repository %s'
                                     % (relpath, repo_trans.base))
    # XXX: hack to make sure the branch is using the same repository
    # instance, for locking purposes
    branch.repository = repo
    return branch
Esempio n. 4
0
def get_branch(repo, relpath, format=None):
    """Return existing branch in destination repo. Create new if don't exist.

    @param  format:     force create new branch in specified format.
    """
    repo_trans = repo.bzrdir.root_transport
    try:
        br_dir = BzrDir.open(repo_trans.abspath(relpath))
        branch = br_dir.open_branch()
    except errors.NotBranchError:
        # create destination branch directory, creating parents as needed.
        needed = [relpath]
        while needed:
            try:
                repo_trans.mkdir(needed[-1])
                needed.pop()
            except errors.NoSuchFile:
                parent = urlutils.dirname(needed[-1])
                if parent == '':
                    raise errors.BzrCommandError('Could not create branch dir')
                needed.append(parent)
        br_dir = BzrDir.create(repo_trans.abspath(relpath))
        if format is None:
            format = BranchFormat.get_default_format()
        branch = format.initialize(br_dir)

        note('Created destination branch %s' % relpath)

    if branch.repository.bzrdir.root_transport.base != repo_trans.base:
        raise errors.BzrCommandError('Branch %s does not use repository %s' %
                                     (relpath, repo_trans.base))
    # XXX: hack to make sure the branch is using the same repository
    # instance, for locking purposes
    branch.repository = repo
    return branch
Esempio n. 5
0
    def apply_pack(self, refs, read):
        """ apply pack from client to current repository """

        fd, path = tempfile.mkstemp(suffix=".pack")
        f = os.fdopen(fd, 'w')
        f.write(read())
        f.close()

        p = PackData(path)
        entries = p.sorted_entries()
        write_pack_index_v2(path[:-5] + ".idx", entries,
                            p.calculate_checksum())

        def get_objects():
            pack = Pack(path[:-5])
            for obj in pack.iterobjects():
                yield obj

        target = Repository.open(self.directory)

        target.lock_write()
        try:
            target.start_write_group()
            try:
                import_git_objects(target, self.mapping, iter(get_objects()))
            finally:
                target.commit_write_group()
        finally:
            target.unlock()

        for oldsha, sha, ref in refs:
            if ref[:11] == 'refs/heads/':
                branch_nick = ref[11:]

                try:
                    target_dir = BzrDir.open(self.directory + "/" +
                                             branch_nick)
                except:
                    target_dir = BzrDir.create(self.directory + "/" +
                                               branch_nick)

                try:
                    target_branch = target_dir.open_branch()
                except:
                    target_branch = target_dir.create_branch()

                rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
                target_branch.generate_revision_history(rev_id)
Esempio n. 6
0
    def __init__(self, directory=None, config=None, mr_compat=True):
        self.directory = directory or os.getcwd()
        self.control_dir = os.path.join(self.directory, '.bzr')
        self.config_file = config or os.path.join(self.directory, '.mrconfig')
        self.mr_compat = mr_compat

        if mr_compat:
            if self.__is_repository():
                self.config = self.__read_cfg()
                self.bzr_dir = Repository.open(self.directory)
            else:
                self.config = ConfigParser.RawConfigParser()
                r = BzrDir.create(self.directory)
                self.bzr_dir = r.create_repository(shared=True)
        else:
            self.config = ConfigParser.RawConfigParser()
            self.bzr_dir = None
Esempio n. 7
0
    def __init__(self, directory=None, config=None, mr_compat=True):
        self.directory = directory or os.getcwd()
        self.control_dir = os.path.join(self.directory, '.bzr')
        self.config_file = config or os.path.join(self.directory, '.mrconfig')
        self.mr_compat = mr_compat

        if mr_compat:
            if self.__is_repository():
                self.config = self.__read_cfg()
                self.bzr_dir = Repository.open(self.directory)
            else:
                self.config = ConfigParser.RawConfigParser()
                r = BzrDir.create(self.directory)
                self.bzr_dir = r.create_repository(shared=True)
        else:
            self.config = ConfigParser.RawConfigParser()
            self.bzr_dir = None
Esempio n. 8
0
    def create_upgraded_repository(self):
        """Create a repository in an upgraded format.

        :param upgrade_dir: The directory to create the repository in.
        :return: The created repository.
        """
        self.logger.info('Converting repository with fetch.')
        upgrade_dir = mkdtemp(dir=self.target_dir)
        try:
            bzrdir = BzrDir.create(upgrade_dir, self.get_target_format())
            repository = bzrdir.create_repository()
            repository.fetch(self.bzr_branch.repository)
        except:
            rmtree(upgrade_dir)
            raise
        else:
            os.rename(upgrade_dir, self.target_subdir)
Esempio n. 9
0
    def create_upgraded_repository(self):
        """Create a repository in an upgraded format.

        :param upgrade_dir: The directory to create the repository in.
        :return: The created repository.
        """
        self.logger.info('Converting repository with fetch.')
        upgrade_dir = mkdtemp(dir=self.target_dir)
        try:
            bzrdir = BzrDir.create(upgrade_dir, self.get_target_format())
            repository = bzrdir.create_repository()
            repository.fetch(self.bzr_branch.repository)
        except:
            rmtree(upgrade_dir)
            raise
        else:
            os.rename(upgrade_dir, self.target_subdir)
Esempio n. 10
0
    def apply_pack(self, refs, read):
        """ apply pack from client to current repository """

        fd, path = tempfile.mkstemp(suffix=".pack")
        f = os.fdopen(fd, 'w')
        f.write(read())
        f.close()

        p = PackData(path)
        entries = p.sorted_entries()
        write_pack_index_v2(path[:-5]+".idx", entries, p.calculate_checksum())

        def get_objects():
            pack = Pack(path[:-5])
            for obj in pack.iterobjects():
                yield obj

        target = Repository.open(self.directory)

        target.lock_write()
        try:
            target.start_write_group()
            try:
                import_git_objects(target, self.mapping, iter(get_objects()))
            finally:
                target.commit_write_group()
        finally:
            target.unlock()

        for oldsha, sha, ref in refs:
            if ref[:11] == 'refs/heads/':
                branch_nick = ref[11:]

                try:
                    target_dir = BzrDir.open(self.directory + "/" + branch_nick)
                except:
                    target_dir = BzrDir.create(self.directory + "/" + branch_nick)

                try:
                    target_branch = target_dir.open_branch()
                except:
                    target_branch = target_dir.create_branch()

                rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
                target_branch.generate_revision_history(rev_id)
Esempio n. 11
0
    def createBranchReference(self, url):
        """Create a pure branch reference that points to the specified URL.

        :param url: target of the branch reference.
        :return: file url to the created pure branch reference.
        """
        # XXX DavidAllouche 2007-09-12 bug=139109:
        # We do this manually because the bzrlib API does not support creating
        # a branch reference without opening it.
        t = get_transport(self.get_url('.'))
        t.mkdir('reference')
        a_bzrdir = BzrDir.create(self.get_url('reference'))
        branch_reference_format = BranchReferenceFormat()
        branch_transport = a_bzrdir.get_branch_transport(
            branch_reference_format)
        branch_transport.put_bytes('location', url)
        branch_transport.put_bytes(
            'format', branch_reference_format.get_format_string())
        return a_bzrdir.root_transport.base
Esempio n. 12
0
    def createBranchReference(self, url):
        """Create a pure branch reference that points to the specified URL.

        :param url: target of the branch reference.
        :return: file url to the created pure branch reference.
        """
        # XXX DavidAllouche 2007-09-12 bug=139109:
        # We do this manually because the bzrlib API does not support creating
        # a branch reference without opening it.
        t = get_transport(self.get_url('.'))
        t.mkdir('reference')
        a_bzrdir = BzrDir.create(self.get_url('reference'))
        branch_reference_format = BranchReferenceFormat()
        branch_transport = a_bzrdir.get_branch_transport(
            branch_reference_format)
        branch_transport.put_bytes('location', url)
        branch_transport.put_bytes('format',
                                   branch_reference_format.get_format_string())
        return a_bzrdir.root_transport.base
def enable_branch_deployment(buildout):

    load_plugins()

    branch_cache_dir = buildout._buildout_path('branch-cache')

    if not os.path.exists(branch_cache_dir):
        buildout._logger.info("Creating branch cache directory")
        os.mkdir(branch_cache_dir)

    # Do a couple of quick checks.
    allow_picked_versions = buildout['buildout'].get_bool(
        'allow-picked-versions')
    versions = buildout['buildout'].get('versions')
    if versions:
        versions = buildout[versions]
    else:
        versions = {}

    for egg_name, branch_loc in buildout['branches'].items():
        rev = buildout.get('revisions', {}).get(egg_name)
        if rev is None and not allow_picked_versions:
            raise zc.buildout.UserError('No revision specified for %s' %
                                        (egg_name, ))
        if egg_name in versions:
            raise zc.buildout.UserError(
                '%s is sourced from a branch and cannot also have a required '
                'version' % (egg_name, ))

    # XXX There are some potential races if more than one instance is running
    # at once.
    for egg_name, branch_loc in buildout['branches'].items():
        rev = buildout.get('revisions', {}).get(egg_name)
        if rev is not None:
            buildout._logger.info('using %s at revision %s of %s', egg_name,
                                  rev, branch_loc)
        else:
            buildout._logger.info('using %s at tip of %s', egg_name,
                                  branch_loc)

        repo = os.path.join(branch_cache_dir, egg_name)
        if not os.path.exists(repo):
            branch_bzrdir = BzrDir.create(repo)
            branch_bzrdir.create_repository(shared=True)
            os.mkdir(os.path.join(repo, 'source-branches'))
            os.mkdir(os.path.join(repo, 'checkouts'))

        source_branch_dir = os.path.join(repo, 'source-branches',
                                         urllib.quote(branch_loc, ''))
        remote_branch = None
        if not os.path.exists(source_branch_dir):
            remote_branch = BzrBranch.open(branch_loc)
            bzr_branch = remote_branch.create_clone_on_transport(
                get_transport(source_branch_dir), no_tree=True)
            created = True
        else:
            created = False
            bzr_branch = BzrBranch.open(source_branch_dir)

        if rev is not None:
            try:
                revno, revid = revision_info(bzr_branch, rev)
            except BzrError:
                # If the specified revision was not in the branch, try pulling
                # again.
                if remote_branch is None:
                    remote_branch = BzrBranch.open(branch_loc)
                bzr_branch.pull(remote_branch, overwrite=True)
                try:
                    revno, revid = revision_info(bzr_branch, rev)
                except BzrError, e:
                    raise zc.buildout.UserError(
                        'Finding revid for revision %s of %s failed:\n%s' %
                        (rev, branch_loc, e))
        else:
            if buildout['buildout'].get_bool('newest') and not created:
                remote_branch = BzrBranch.open(branch_loc)
                bzr_branch.pull(remote_branch, overwrite=True)
            revno, revid = revision_info(bzr_branch, '-1')

        checkout_dir = os.path.join(repo, 'checkouts',
                                    '%s-%s' % (revno, revid))
        if not os.path.exists(checkout_dir):
            rev_branch = bzr_branch.create_clone_on_transport(
                get_transport(checkout_dir), revision_id=revid, no_tree=False)
            if not rev_branch.bzrdir.has_workingtree():
                rev_branch.bzrdir.create_workingtree()

        zc.buildout.easy_install.develop(
            buildout._buildout_path(checkout_dir),
            buildout['buildout']['develop-eggs-directory'])