Esempio n. 1
0
def create_repository(app,
                      name,
                      type,
                      description,
                      long_description,
                      user_id,
                      category_ids=None,
                      remote_repository_url=None,
                      homepage_url=None):
    """Create a new ToolShed repository"""
    category_ids = category_ids or []
    sa_session = app.model.context.current
    # Add the repository record to the database.
    repository = app.model.Repository(
        name=name,
        type=type,
        remote_repository_url=remote_repository_url,
        homepage_url=homepage_url,
        description=description,
        long_description=long_description,
        user_id=user_id)
    # Flush to get the id.
    sa_session.add(repository)
    sa_session.flush()
    # Create an admin role for the repository.
    create_repository_admin_role(app, repository)
    # Determine the repository's repo_path on disk.
    dir = os.path.join(app.config.file_path,
                       *util.directory_hash_id(repository.id))
    # Create directory if it does not exist.
    if not os.path.exists(dir):
        os.makedirs(dir)
    # Define repo name inside hashed directory.
    repository_path = os.path.join(dir, "repo_%d" % repository.id)
    # Create local repository directory.
    if not os.path.exists(repository_path):
        os.makedirs(repository_path)
    # Create the local repository.
    init_repository(repo_path=repository_path)
    # Add an entry in the hgweb.config file for the local repository.
    lhs = f"repos/{repository.user.username}/{repository.name}"
    app.hgweb_config_manager.add_entry(lhs, repository_path)
    # Create a .hg/hgrc file for the local repository.
    create_hgrc_file(app, repository)
    flush_needed = False
    if category_ids:
        # Create category associations
        for category_id in category_ids:
            category = sa_session.query(app.model.Category) \
                                 .get(app.security.decode_id(category_id))
            rca = app.model.RepositoryCategoryAssociation(repository, category)
            sa_session.add(rca)
            flush_needed = True
    if flush_needed:
        sa_session.flush()
    # Update the repository registry.
    app.repository_registry.add_entry(repository)
    message = "Repository <b>%s</b> has been created." % escape(
        str(repository.name))
    return repository, message
Esempio n. 2
0
def create_repository(
    app,
    name,
    type,
    description,
    long_description,
    user_id,
    category_ids=[],
    remote_repository_url=None,
    homepage_url=None,
):
    """Create a new ToolShed repository"""
    sa_session = app.model.context.current
    # Add the repository record to the database.
    repository = app.model.Repository(
        name=name,
        type=type,
        remote_repository_url=remote_repository_url,
        homepage_url=homepage_url,
        description=description,
        long_description=long_description,
        user_id=user_id,
    )
    # Flush to get the id.
    sa_session.add(repository)
    sa_session.flush()
    # Create an admin role for the repository.
    repository_admin_role = create_repository_admin_role(app, repository)
    # Determine the repository's repo_path on disk.
    dir = os.path.join(app.config.file_path, *directory_hash_id(repository.id))
    # Create directory if it does not exist.
    if not os.path.exists(dir):
        os.makedirs(dir)
    # Define repo name inside hashed directory.
    repository_path = os.path.join(dir, "repo_%d" % repository.id)
    # Create local repository directory.
    if not os.path.exists(repository_path):
        os.makedirs(repository_path)
    # Create the local repository.
    repo = hg_util.get_repo_for_repository(app, repository=None, repo_path=repository_path, create=True)
    # Add an entry in the hgweb.config file for the local repository.
    lhs = "repos/%s/%s" % (repository.user.username, repository.name)
    app.hgweb_config_manager.add_entry(lhs, repository_path)
    # Create a .hg/hgrc file for the local repository.
    hg_util.create_hgrc_file(app, repository)
    flush_needed = False
    if category_ids:
        # Create category associations
        for category_id in category_ids:
            category = sa_session.query(app.model.Category).get(app.security.decode_id(category_id))
            rca = app.model.RepositoryCategoryAssociation(repository, category)
            sa_session.add(rca)
            flush_needed = True
    if flush_needed:
        sa_session.flush()
    # Update the repository registry.
    app.repository_registry.add_entry(repository)
    message = "Repository <b>%s</b> has been created." % escape(str(repository.name))
    return repository, message