示例#1
0
def buildPool():
    args = []
    kwargs = {'maxsize': config.xgetint('database', 'pool-maxsize', default=25)}
    dsn = config.xget('database', 'dsn')
    if dsn:
        args.append(dsn)
    else:
        for key in ('database', 'user', 'password', 'host'):
            val = config.xget('database', key)
            if val:
                kwargs[key] = val
        port = config.xgetint('database', 'port')
        if port:
            kwargs['port'] = port
    if not args and not kwargs:
        raise ValueError('database is not configured')
    pool = PostgresConnectionPool(*args, **kwargs)
    initSchema(pool)
    return pool
示例#2
0
文件: query.py 项目: fantix/ArchRepo
 def __init__(self, pool, key, sql, values):
     super(ReusableCursor, self).__init__(self.work)
     self.pool = pool
     self.key = self._formatted_info = key
     self.sql = sql
     self.values = values
     self.offset = 0
     self.queue = Queue()
     self._count = AsyncResult()
     self.last_access = time.time()
     self.idle = False
     self.listeners = []
     self.window = config.xgetint('web', 'query-reusable-window', 30)
示例#3
0
文件: web.py 项目: fantix/ArchRepo
 def __init__(self, pool):
     host = config.xget('web', 'host', default='*')
     port = config.xgetint('web', 'port', default=8080)
     super(ArchRepoWebServer, self).__init__('%s:%d' % (host, port), log=None)
     cherrypy.server.unsubscribe()
     static_dir = resource_filename('archrepo', 'templates/static')
     self.application = cherrypy.tree.mount(
         ArchRepoApplication(pool), config={
             '/': {'tools.sessions.on': True},
             '/static': {'tools.staticdir.on': True,
                         'tools.staticdir.dir': static_dir}})
     self.application.log.access_log.level = self.application.log.access_log.parent.level
     self.application.log.error_log.level = self.application.log.error_log.parent.level
示例#4
0
文件: web.py 项目: fantix/ArchRepo
    def __init__(self, pool):
        self.cursorPool = CursorPool(
            config.xgetint('web', 'concurrent-queries', 16))
        self.pool = pool
        self._env = Environment(loader=FileSystemLoader(
            resource_filename('archrepo', 'templates')),
                                extensions=['jinja2.ext.i18n'])
        try:
            self.trans = gettext.translation(
                'archrepo', os.path.join(
                    os.environ.get('ARCHREPO_PREFIX', sys.prefix),
                    'share/locale'))
        except IOError:
            self.trans = gettext.NullTranslations()
        #noinspection PyUnresolvedReferences
        self._env.install_gettext_translations(self.trans)

        if config.has_section('flux-sso'):
            self.auth = FluxAuth(pool)
        else:
            self.auth = Auth(pool)
示例#5
0
文件: repo.py 项目: fantix/ArchRepo
    def my_init(self, **kwargs):
        self._started_event = AsyncResult()
        self._repo_lock = defaultdict(RLock)
        self._same_pkg_locks = defaultdict(RLock)
        self._ignored_move_events = set()
        self._move_events = {}

        self._pool = kwargs.get('pool')

        self._repo_dir = config.get('repository', 'path')
        self._db_name = config.get('repository', 'name') + '.db.tar.gz'
        self._verify = config.xgetbool('repository', 'verify-tarball', True)
        self._auto_rename = config.xgetbool('repository', 'auto-rename', True)
        self._command_add = config.xget('repository', 'command-add',
                                        default='repo-add')
        self._command_remove = config.xget('repository', 'command-remove',
                                        default='repo-remove')
        self._command_fuser = config.xget('repository', 'command-fuser',
                                          default='fuser')
        self._command_pkginfo = os.path.join(
            os.environ.get('ARCHREPO_PREFIX', sys.prefix), 'bin',
            'read_pkginfo.py')
        self._semaphore = Semaphore(
            config.xgetint('repository', 'concurrent-jobs', default=256))
示例#6
0
文件: web.py 项目: fantix/ArchRepo
    def query(self, sort=None, arch=None, maintainer=None, q=None, limit=None,
              page=None, flagged=None, last_update=None):
        userinfo = self.auth.getUserInfo()

        sql = 'SELECT %s FROM packages' % ', '.join(FIELDS)
        sort_list = []
        where_list = ['latest']
        desc = True
        values = {}

        if arch is not None:
            if not isinstance(arch, list):
                arch = [arch]
            where_list.append('arch IN %(arch)s')
            values['arch'] = tuple(arch)
        else:
            arch = ()

        if last_update is not None and last_update.strip():
            where_list.append('last_update > %(last_update)s')
            values['last_update'] = last_update
        else:
            last_update = ''

        if flagged == '1':
            where_list.append('flag_date IS NOT NULL')
        elif flagged == '2':
            where_list.append('flag_date IS NULL')
        else:
            flagged = '0'

        if maintainer is not None and maintainer.isdigit():
            if int(maintainer):
                where_list.append('owner=%(owner)s')
                values['owner'] = int(maintainer)
            else:
                where_list.append('owner IS NULL')

        if sort:
            for part in sort.lower().split(','):
                if part in ('last_update', 'name', 'arch', 'flag_date'):
                    sort_list.append(part)
                if part in ('name', 'arch', 'asc'):
                    desc = False
                if part in ('desc',):
                    desc = True
        if not sort_list:
            sort = 'last_update'
            sort_list.append(sort)
            desc = True

        if q is not None and q.strip():
            sql = ', '.join((sql, "to_tsquery(%(lang)s, %(q)s) query"))
            values['lang'] = 'english'
            values['q'] = '&'.join(q.split())
            where_list.append('searchable @@ query')
            sort_list.append('ts_rank_cd(searchable, query)')
            desc = True
        else:
            q = ''

        if where_list:
            sql = ' WHERE '.join((sql, ' AND '.join(where_list)))

        if sort_list:
            sql = ' ORDER BY '.join((sql, ', '.join(sort_list)))
            if desc:
                sql = ' '.join((sql, 'DESC'))

        if limit not in AVAILABLE_LIMITS[1:]:
            limit = AVAILABLE_LIMITS[0]
        if limit != 'all':
            #noinspection PyUnresolvedReferences
            if 'sub_cursor_pool' in cherrypy.session:
                #noinspection PyUnresolvedReferences
                cpool = cherrypy.session['sub_cursor_pool']
            else:
                cpool = SubCursorPool(
                    self.cursorPool,
                    config.xgetint('web', 'concurrent-queries-per-session', 1))
                #noinspection PyUnresolvedReferences
                cherrypy.session['sub_cursor_pool'] = cpool

            cursor = cpool.getCursor(self.pool, sql % values, sql, values)
            count = cursor.count
            all_pages = int(math.ceil(float(count) / int(limit)))
            if page is not None and page.isdigit():
                page = min(all_pages, max(1, int(page)))
            else:
                page = 1
            offset = (page - 1) * int(limit)
            result = cursor.fetch(int(limit), offset)
        else:
            page = 1
            all_pages = 1
            with self.pool.cursor() as cur:
                logging.debug('SQL: %s, VALUES: %r', sql, values)
                cur.execute(sql, values)
                result = cur.fetchall()
            count = len(result)
        with self.pool.cursor() as cur:
            cur.execute('SELECT id, username FROM users')
            users = [(None, self.gettext('All')), ('0', self.gettext('Orphan'))]
            for val, label in cur.fetchall():
                users.append((str(val), label))
            users_dict = dict(users)

        result = [dict(zip(FIELDS, x)) for x in result]
        for row in result:
            row['last_update'] = format_date(row['last_update'])
            row['flag_date'] = ('' if row['flag_date'] is None else
                                format_date(row['flag_date']))
            row['maintainer'] = users_dict.get(str(row['owner'] or '0'))
        parts = cherrypy.request.query_string.split('&')
        pager = '&'.join([x for x in parts if not x.startswith('page=')] + ['page='])
        sorter = '&'.join([x for x in parts if not x.startswith('sort=')] + ['sort='])
        tmpl = self._env.get_template('packages.html')
        return tmpl.render(
            packages=result, userinfo=userinfo, q=q, arch=arch,
            last_update=last_update, flagged=flagged, page=page, count=count,
            all_pages=all_pages, limit=limit, all_limits=AVAILABLE_LIMITS,
            pager=pager, sorter=sorter, maintainer=maintainer, users=users,
            base_url=config.get('web', 'external-base-url').rstrip('/'),
            title=config.get('web', 'title'),
            favicon=config.get('web', 'favicon'),
            all_arch=('any', 'i686', 'x86_64'))