Esempio n. 1
0
    def _render_directory(self, location):
        """
        renders a directory, lists subdirs and subfiles
        """
        hidden_files = app.config['HIDDEN_FILES'].split(" ")
        directory = Directory(location, hidden_files)

        pkg_infos = Infobox(session, location.get_package(),
                            location.get_version()).get_infos()

        content = directory.get_listing()
        path = location.get_path_to()

        if self.d.get('api'):
            self.render_func = jsonify
        else:
            self.render_func = bind_render(
                'sources/source_folder.html',
                subdirs=[x for x in content if x['type'] == "directory"],
                subfiles=[x for x in content if x['type'] == "file"],
                nb_hidden_files=sum(1 for f in content if f['hidden']),
                pathl=qry.location_get_path_links(".source", path),)

        return dict(type="directory",
                    directory=location.get_deepest_element(),
                    package=location.get_package(),
                    version=location.get_version(),
                    content=content,
                    path=path,
                    pkg_infos=pkg_infos,
                    )
Esempio n. 2
0
    def get_objects(self, packagename):
        suite = request.args.get("suite") or ""
        suite = suite.lower()
        if suite == "all":
            suite = ""
        # we list the version with suites it belongs to
        try:
            versions_w_suites = qry.pkg_names_list_versions_w_suites(
                session, packagename, suite, reverse=True)
        except InvalidPackageOrVersionError:
            raise Http404Error("%s not found" % packagename)

        # we simply add pathl (for use with "You are here:")
        if request.blueprint == 'sources':
            endpoint = '.source'
        elif request.blueprint == 'copyright':
            endpoint = '.versions'

        pathl = qry.location_get_path_links(endpoint, packagename)
        return dict(type="package",
                    package=packagename,
                    versions=versions_w_suites,
                    path=packagename,
                    suite=suite,
                    pathl=pathl
                    )
Esempio n. 3
0
    def get_objects(self, packagename):
        suite = request.args.get("suite") or ""
        suite = suite.lower()
        if suite == "all":
            suite = ""
        # we list the version with suites it belongs to
        try:
            versions_w_suites = qry.pkg_names_list_versions_w_suites(
                session, packagename, suite, reverse=True)
        except InvalidPackageOrVersionError:
            raise Http404Error("%s not found" % packagename)

        # we simply add pathl (for use with "You are here:")
        if request.blueprint == 'sources':
            endpoint = '.source'
        elif request.blueprint == 'copyright':
            endpoint = '.versions'

        pathl = qry.location_get_path_links(endpoint, packagename)
        return dict(type="package",
                    package=packagename,
                    versions=versions_w_suites,
                    path=packagename,
                    suite=suite,
                    pathl=pathl
                    )
Esempio n. 4
0
    def _render_package(self, packagename, path_to):
        """
        renders the package page (which lists available versions)
        """
        suite = request.args.get("suite") or ""
        suite = suite.lower()
        if suite == "all":
            suite = ""
        # we list the version with suites it belongs to
        try:
            versions_w_suites = qry.pkg_names_list_versions_w_suites(
                session, packagename, suite)
        except InvalidPackageOrVersionError:
            raise Http404Error("%s not found" % packagename)

        if self.d.get('api'):
            self.render_func = jsonify
        else:
            self.render_func = bind_render(
                'sources/source_package.html',
                # we simply add pathl (for use with "You are here:")
                pathl=qry.location_get_path_links('.source', path_to))

        return dict(type="package",
                    package=packagename,
                    versions=versions_w_suites,
                    path=path_to,
                    suite=suite,
                    )
Esempio n. 5
0
    def _render_file(self, location):
        """
        renders a file
        """
        file_ = SourceFile(location)
        checksum = file_.get_sha256sum(session)
        number_of_duplicates = (qry.count_files_checksum(session, checksum)
                                .first()[0]
                                )
        pkg_infos = Infobox(session,
                            location.get_package(),
                            location.get_version()).get_infos()
        text_file = file_.istextfile()
        raw_url = file_.get_raw_url()
        path = location.get_path_to()

        if self.d.get('api'):
            self.render_func = jsonify
            return dict(type="file",
                        file=location.get_deepest_element(),
                        package=location.get_package(),
                        version=location.get_version(),
                        mime=file_.get_mime(),
                        raw_url=raw_url,
                        path=path,
                        text_file=text_file,
                        stat=qry.location_get_stat(location.sources_path),
                        checksum=checksum,
                        number_of_duplicates=number_of_duplicates,
                        pkg_infos=pkg_infos
                        )
        # prepare the non-api render func
        self.render_func = None
        # more work to do with files
        # as long as 'lang' is in keys, then it's a text_file
        lang = None
        if 'lang' in request.args:
            lang = request.args['lang']
        # if the file is not a text file, we redirect to it
        elif not text_file:
            self.render_func = bind_redirect(raw_url)

        # set render func (non-api form)
        if not self.render_func:
            sources_path = raw_url.replace(
                current_app.config['SOURCES_STATIC'],
                current_app.config['SOURCES_DIR'],
                1)
            # ugly, but better than global variable,
            # and better than re-requesting the db
            # TODO: find proper solution for retrieving souces_path
            # (without putting it in kwargs, we don't want it in
            # json rendering eg)

            # we get the variables for highlighting and message (if they exist)
            try:
                highlight = request.args.get('hl')
            except (KeyError, ValueError, TypeError):
                highlight = None
            try:
                msg = request.args.getlist('msg')
                if msg == "":
                    msg = None  # we don't want empty messages
            except (KeyError, ValueError, TypeError):
                msg = None

            # we preprocess the file with SourceCodeIterator
            sourcefile = SourceCodeIterator(
                sources_path, hl=highlight, msg=msg, lang=lang)

            self.render_func = bind_render(
                self.d['templatename'],
                nlines=sourcefile.get_number_of_lines(),
                pathl=qry.location_get_path_links(".source", path),
                file_language=sourcefile.get_file_language(),
                msgs=sourcefile.get_msgdict(),
                code=sourcefile)

        return dict(type="file",
                    file=location.get_deepest_element(),
                    package=location.get_package(),
                    version=location.get_version(),
                    mime=file_.get_mime(),
                    raw_url=raw_url,
                    path=path,
                    text_file=text_file,
                    stat=qry.location_get_stat(location.sources_path),
                    checksum=checksum,
                    number_of_duplicates=number_of_duplicates,
                    pkg_infos=pkg_infos
                    )