Example #1
0
    def handle_args(self, parser, cfg, options, args):
        super(Main, self).handle_args(parser, cfg, options, args)

        os.umask(0022)

        log.info('Reading SSH public key...')
        pubkey = read_ssh_pubkey()
        user = ssh_extract_user(pubkey)
        if user is None:
            log.error('Cannot parse user from SSH public key.')
            sys.exit(1)
        log.info('Admin user is %r', user)
        log.info('Creating generated files directory...')
        generated = util.getGeneratedFilesDir(config=cfg)
        util.mkdir(generated)
        log.info('Creating repository structure...')
        repositories = util.getRepositoryDir(cfg)
        util.mkdir(repositories)
        #admin_repository = os.path.join(repositories, 'gitosis-admin.git')
        #init_admin_repository(
        #    git_dir=admin_repository,
        #    pubkey=pubkey,
        #    user=user,
        #    )
        #log.info('Running post-update hook...')
        #util.mkdir(os.path.expanduser('~/.ssh'), 0700)
        #run_hook.post_update(cfg=cfg, git_dir=admin_repository)
        #log.info('Symlinking ~/.gitosis.conf to repository...')
        #symlink_config(git_dir=admin_repository)
        log.info('Done.')
Example #2
0
def walk_repos(config):
    repositories = util.getRepositoryDir(config)

    def _error(e):
        if e.errno == errno.ENOENT:
            pass
        else:
            raise e

    for (dirpath, dirnames, filenames) \
            in os.walk(repositories, onerror=_error):
        # oh how many times i have wished for os.walk to report
        # topdir and reldir separately, instead of dirpath
        reldir = _extract_reldir(
            topdir=repositories,
            dirpath=dirpath,
        )

        log.debug('Walking %r, seeing %r', reldir, dirnames)

        to_recurse = []
        repos = []
        for dirname in dirnames:
            if dirname.endswith('.git'):
                repos.append(dirname)
            else:
                to_recurse.append(dirname)
        dirnames[:] = to_recurse

        for repo in repos:
            name, ext = os.path.splitext(repo)
            if reldir != '.':
                name = os.path.join(reldir, name)
            assert ext == '.git'
            yield (dirpath, repo, name)
Example #3
0
def set_export_ok(config):
    repositories = util.getRepositoryDir(config)

    try:
        global_enable = config.getboolean('gitosis', 'daemon')
    except (NoSectionError, NoOptionError):
        global_enable = False
    log.debug(
        'Global default is %r',
        {
            True: 'allow',
            False: 'deny'
        }.get(global_enable),
    )

    def _error(e):
        if e.errno == errno.ENOENT:
            pass
        else:
            raise e

    for (dirpath, dirnames, filenames) \
            in os.walk(repositories, onerror=_error):
        # oh how many times i have wished for os.walk to report
        # topdir and reldir separately, instead of dirpath
        reldir = _extract_reldir(
            topdir=repositories,
            dirpath=dirpath,
        )

        log.debug('Walking %r, seeing %r', reldir, dirnames)

        to_recurse = []
        repos = []
        for dirname in dirnames:
            if dirname.endswith('.git'):
                repos.append(dirname)
            else:
                to_recurse.append(dirname)
        dirnames[:] = to_recurse

        for repo in repos:
            name, ext = os.path.splitext(repo)
            if reldir != '.':
                name = os.path.join(reldir, name)
            assert ext == '.git'
            try:
                enable = config.getboolean('repo %s' % name, 'daemon')
            except (NoSectionError, NoOptionError):
                enable = global_enable

            if enable:
                log.debug('Allow %r', name)
                allow_export(os.path.join(dirpath, repo))
            else:
                log.debug('Deny %r', name)
                deny_export(os.path.join(dirpath, repo))
Example #4
0
def generate_project_list_fp(config, fp):
    """
    Generate projects list for ``gitweb``.

    :param config: configuration to read projects from
    :type config: RawConfigParser

    :param fp: writable for ``projects.list``
    :type fp: (file-like, anything with ``.write(data)``)
    """
    log = logging.getLogger('gitosis.gitweb.generate_projects_list')

    repositories = util.getRepositoryDir(config)

    try:
        global_enable = config.getboolean('gitosis', 'gitweb')
    except (NoSectionError, NoOptionError):
        global_enable = False

    for section in config.sections():
        l = section.split(None, 1)
        type_ = l.pop(0)
        if type_ != 'repo':
            continue
        if not l:
            continue

        try:
            enable = config.getboolean(section, 'gitweb')
        except (NoSectionError, NoOptionError):
            enable = global_enable

        if not enable:
            continue

        name, = l

        if not os.path.exists(os.path.join(repositories, name)):
            namedotgit = '%s.git' % name
            if os.path.exists(os.path.join(repositories, namedotgit)):
                name = namedotgit
            else:
                log.warning(
                    'Cannot find %(name)r in %(repositories)r'
                    % dict(name=name, repositories=repositories))

        response = [name]
        try:
            owner = config.get(section, 'owner')
        except (NoSectionError, NoOptionError):
            pass
        else:
            response.append(owner)

        line = ' '.join([urllib.parse.quote_plus(s) for s in response])
        print(line, file=fp)
Example #5
0
def set_descriptions(config):
    """
    Set descriptions for gitweb use.
    """
    log = logging.getLogger('gitosis.gitweb.set_descriptions')

    repositories = util.getRepositoryDir(config)

    for section in config.sections():
        l = section.split(None, 1)
        type_ = l.pop(0)
        if type_ != 'repo':
            continue
        if not l:
            continue

        try:
            description = config.get(section, 'description')
        except (NoSectionError, NoOptionError):
            continue

        if not description:
            continue

        name, = l

        if not os.path.exists(os.path.join(repositories, name)):
            namedotgit = '%s.git' % name
            if os.path.exists(os.path.join(repositories, namedotgit)):
                name = namedotgit
            else:
                log.warning(
                    'Cannot find %(name)r in %(repositories)r'
                    % dict(name=name, repositories=repositories))
                continue

        path = os.path.join(
            repositories,
            name,
            'description',
            )
        tmp = '%s.%d.tmp' % (path, os.getpid())
        f = open(tmp, 'w')
        try:
            print(description, file=f)
        finally:
            f.close()
        os.rename(tmp, path)
Example #6
0
def push_mirrors(config, git_dir):
    """
    Apply a push with the mirror option to all mirrors defined in gitosis.conf
    of the repository being updated.
    
    @param config: ConfiParser object loaded with gitosis.conf
    @param git_dir: Path the repository being updated. 
    """
    log = logging.getLogger('gitosis.mirror.push_mirrors')
    
    repository_dir = os.path.abspath(util.getRepositoryDir(config))
    git_dir = os.path.abspath(git_dir)
    git_name = get_git_name(repository_dir, git_dir)
    
    log.info('Updating %s\'s mirrors.' % git_name)
    
    for remote in get_mirrors(config, git_name):
        log.info('Updating %s.' % remote)
        repository.mirror(git_dir, remote)
Example #7
0
def set_repo_descriptions(config, path):
    log = logging.getLogger('gitosis.gitweb.set_descriptions')

    repositories = util.getRepositoryDir(config)
    if repositories[-1] != '/':
        repositories += '/'
    if path.startswith(repositories):
        path = path[len(repositories):]

    name, ext = os.path.splitext(path)
    assert ext == '.git'
    try:
        description = config.get('repo %s' % name, 'description')
    except (NoSectionError, NoOptionError):
        description = None

    if description is not None:
        if not os.path.exists(os.path.join(repositories, name)):
            namedotgit = '%s.git' % name
            if os.path.exists(os.path.join(repositories, namedotgit)):
                name = namedotgit
            else:
                log.warning('Cannot find %(name)r in %(repositories)r' %
                            dict(name=name, repositories=repositories))

        log.debug('Set description for %(name)r in %(repositories)r' %
                  dict(name=name, repositories=repositories))

        path = os.path.join(
            repositories,
            name,
            'description',
        )
        tmp = '%s.%d.tmp' % (path, os.getpid())
        f = file(tmp, 'w')
        try:
            print >> f, description
        finally:
            f.close()
        os.rename(tmp, path)
Example #8
0
def enum_cfg_repos(config):
    """
    Enumerates all repositories that have repo sections in the config.
    """
    repositories = util.getRepositoryDir(config)

    for section in config.sections():
        l = section.split(None, 1)
        type_ = l.pop(0)
        if type_ != 'repo':
            continue
        if not l:
            continue

        name, = l

        if not os.path.exists(os.path.join(repositories, name)):
            subpath = '%s.git' % name
        else:
            subpath = name

        yield (section, name, repositories, subpath)