Beispiel #1
0
    def get_objects(self, path_to):
        path_dict = path_to.split('/')

        package = path_dict[0]
        version = path_dict[1]
        path = '/'.join(path_dict[2:])

        if version == "latest":  # we search the latest available version
            return self._handle_latest_version(request.endpoint, package, path)

        versions = self.handle_versions(version, package, path)
        if versions:
            redirect_url_parts = [package, versions[-1]]
            if path:
                redirect_url_parts.append(path)
            redirect_url = '/'.join(redirect_url_parts)
            return self._redirect_to_url(request.endpoint,
                                         redirect_url,
                                         redirect_code=302)

        try:
            sources_path = helper.get_sources_path(session, package, version,
                                                   current_app.config)
        except (FileOrFolderNotFound, InvalidPackageOrVersionError) as e:
            if isinstance(e, FileOrFolderNotFound):
                raise Http404ErrorSuggestions(package, version,
                                              'debian/copyright')
            else:
                raise Http404ErrorSuggestions(package, version, '')

        try:
            c = helper.parse_license(sources_path)
        except Exception:
            # non machine readable license
            sourcefile = SourceCodeIterator(sources_path)
            return dict(
                package=package,
                version=version,
                code=sourcefile,
                dump='True',
                nlines=sourcefile.get_number_of_lines(),
            )
        renderer = RenderLicense(c, 'jinja')
        return dict(package=package,
                    version=version,
                    dump='False',
                    header=renderer.render_header(),
                    files=renderer.render_files("/src/" + package + "/" +
                                                version + "/"),
                    licenses=renderer.render_licenses())
Beispiel #2
0
    def get_objects(self, packagename, version):
        try:
            sources_path = helper.get_sources_path(session, packagename,
                                                   version, current_app.config)
        except (FileOrFolderNotFound, InvalidPackageOrVersionError) as e:
            if isinstance(e, FileOrFolderNotFound):
                raise Http404MissingCopyright(packagename, version, "")
            else:
                raise Http404ErrorSuggestions(packagename, version, "")

        try:
            c = helper.parse_license(sources_path)
        except Exception:
            # non machine readable license
            sourcefile = SourceCodeIterator(sources_path)
            return dict(
                package=packagename,
                version=version,
                code=sourcefile,
                dump="True",
                nlines=sourcefile.get_number_of_lines(),
            )
        return dict(
            package=packagename,
            version=version,
            dump="False",
            header=helper.get_copyright_header(c),
            files=helper.parse_copyright_paragraphs_for_html_render(
                c, "/src/" + packagename + "/" + version + "/"),
            licenses=helper.parse_licenses_for_html_render(c),
        )
Beispiel #3
0
 def _get_license_dict(self, files):
     result = []
     for f in files:
         try:
             # once DB full, remove license path
             try:
                 license_path = helper.get_sources_path(
                     session, f['package'], f['version'],
                     current_app.config)
             except (FileOrFolderNotFound, InvalidPackageOrVersionError):
                 raise Http404ErrorSuggestions(f['package'], f['version'],
                                               '')
             l = helper.get_license(session, f['package'], f['version'],
                                    f['path'], license_path)
             result.append(
                 dict(oracle='debian',
                      path=f['path'],
                      package=f['package'],
                      version=f['version'],
                      license=l,
                      origin=helper.license_url(f['package'],
                                                f['version'])))
         except Exception:
             result.append(
                 dict(oracle='debian',
                      path=f['path'],
                      package=f['package'],
                      version=f['version'],
                      license=None,
                      origin=helper.license_url(f['package'],
                                                f['version'])))
     return result
Beispiel #4
0
    def get_objects(self, packagename, version, path_to):
        try:
            serie_path, loc = helper.get_sources_path(
                session, packagename, version, current_app.config,
                'debian/patches/' + path_to.rstrip())
        except (FileOrFolderNotFound, InvalidPackageOrVersionError):
            raise Http404ErrorSuggestions(packagename, version,
                                          'debian/patches/' + path_to.rstrip())
        if 'api' in request.endpoint:
            summary = helper.get_file_deltas(serie_path)
            description, bug = helper.get_patch_details(serie_path)
            return dict(package=packagename,
                        version=version,
                        url=loc.get_raw_url(),
                        name=path_to,
                        description=description,
                        bug=bug,
                        file_deltas=summary)
        sourcefile = SourceCodeIterator(serie_path)

        return dict(package=packagename,
                    version=version,
                    nlines=sourcefile.get_number_of_lines(),
                    file_language='diff',
                    code=sourcefile)
Beispiel #5
0
def get_license(session, package, version, path):
    try:
        sources_path = get_sources_path(session, package, version,
                                        current_app.config)
    except (FileOrFolderNotFound, InvalidPackageOrVersionError):
        raise Http404ErrorSuggestions(package, version, '')

    try:
        c = parse_license(sources_path)
    except Exception:
        return None

    # search for path in globs
    path_dict = path.split('/')
    for par in c.all_files_paragraphs():
        for glob in par.files:
            if glob == path_dict[-1]:
                try:
                    return par.license.synopsis
                except AttributeError:
                    logging.warn("Path %s in Package %s with version %s is"
                                 " missing a license field" %
                                 (path, package, version))
                    return None
    # search for folder/* containing our file
    # search in reverse order as we can have f1/f2/filename
    # where f1/* with license1 and f2/* in another
    for folder in reversed(path_dict):
        for par in c.all_files_paragraphs():
            for glob in par.files:
                if glob.replace('/*', '') == folder:
                    try:
                        return par.license.synopsis
                    except AttributeError:
                        logging.warn("Path %s Package %s with version %s is"
                                     " missing a license field" %
                                     (path, package, version))
                        return None
    # TODO search for /*
    for par in c.all_files_paragraphs():
        for glob in par.files:
            if glob == '*':
                try:
                    return par.license.synopsis
                except AttributeError:
                    logging.warn("Path %s Package %s with version %s is"
                                 " missing a license field" %
                                 (path, package, version))
                    return None
Beispiel #6
0
 def _license_of_files(self, f):
     # once DB full, remove license path
     try:
         license_path = helper.get_sources_path(session, f.package,
                                                f.version,
                                                current_app.config)
     except (FileOrFolderNotFound, InvalidPackageOrVersionError):
         raise Http404ErrorSuggestions(f.package, f.version, '')
     return dict(oracle='debian',
                 path=f.path,
                 package=f.package,
                 version=f.version,
                 license=helper.get_license(session, f.package, f.version,
                                            f.path, license_path),
                 origin=helper.license_url(f.package, f.version))
Beispiel #7
0
    def get_objects(self, packagename, version):
        path_to = packagename + '/' + version

        try:
            format_file = helper.get_patch_format(session, packagename,
                                                  version, current_app.config)
        except InvalidPackageOrVersionError:
            raise Http404ErrorSuggestions(packagename, version, '')
        except FileOrFolderNotFound:
            return dict(package=packagename,
                        version=version,
                        path=path_to,
                        patches=[],
                        format='unknown')
        if not helper.is_supported(format_file):
            return dict(package=packagename,
                        version=version,
                        path=path_to,
                        format=format_file,
                        patches=[],
                        supported=False)

        # are there any patches for the package?
        try:
            series = helper.get_patch_series(session, packagename, version,
                                             current_app.config)
        except (FileOrFolderNotFound, InvalidPackageOrVersionError):
            return dict(package=packagename,
                        version=version,
                        path=path_to,
                        format=format_file,
                        patches=[],
                        supported=True)

        info = self.parse_patch_series(session, packagename, version,
                                       current_app.config, series)
        if 'api' in request.endpoint:
            return dict(package=packagename,
                        version=version,
                        format=format_file,
                        patches=[key.rstrip() for key in info.keys()])
        return dict(package=packagename,
                    version=version,
                    path=path_to,
                    format=format_file,
                    series=info.keys(),
                    patches=info,
                    supported=True)
Beispiel #8
0
    def get_objects(self, path_to):
        path_dict = path_to.split('/')
        package = path_dict[0]
        version = path_dict[1]
        patch = '/'.join(path_dict[2:])

        if version == "latest":  # we search the latest available version
            return self._handle_latest_version(request.endpoint, package,
                                               'debian/patches/' + patch)

        versions = self.handle_versions(version, package,
                                        'debian/patches/' + patch)
        if versions and version:
            redirect_url_parts = [package, versions[-1]]
            if patch:
                redirect_url_parts.append(patch)
            redirect_url = '/'.join(redirect_url_parts)
            return self._redirect_to_url(request.endpoint,
                                         redirect_url,
                                         redirect_code=302)

        try:
            serie_path, loc = get_sources_path(
                session, package, version, current_app.config,
                'debian/patches/' + patch.rstrip())
        except (FileOrFolderNotFound, InvalidPackageOrVersionError):
            raise Http404ErrorSuggestions(package, version,
                                          'debian/patches/' + patch.rstrip())
        if 'api' in request.endpoint:
            summary = helper.get_file_deltas(serie_path)
            description, bug = helper.get_patch_details(serie_path)
            return dict(package=package,
                        version=version,
                        url=loc.get_raw_url(),
                        name=patch,
                        description=description,
                        bug=bug,
                        file_deltas=summary)
        sourcefile = SourceCodeIterator(serie_path)

        return dict(package=package,
                    version=version,
                    nlines=sourcefile.get_number_of_lines(),
                    file_language='diff',
                    code=sourcefile)
Beispiel #9
0
    def _license_of_files(self, f):
        # once DB full, remove license path
        try:
            license_path = helper.get_sources_path(session, f.package,
                                                   f.version,
                                                   current_app.config)
        except (FileOrFolderNotFound, InvalidPackageOrVersionError):
            raise Http404ErrorSuggestions(f.package, f.version, '')

        try:
            c = helper.parse_license(license_path)
            l = helper.get_license(f.package, f.version, f.path, c)
        except copyright.NotMachineReadableError:
            l = None
        return dict(oracle='debian',
                    path=f.path,
                    package=f.package,
                    version=f.version,
                    license=l,
                    origin=helper.license_url(f.package, f.version))
Beispiel #10
0
    def _render_location(self, package, version, path):
        """
        renders a location page, can be a folder or a file
        """
        try:
            location = Location(session,
                                current_app.config["SOURCES_DIR"],
                                current_app.config["SOURCES_STATIC"],
                                package, version, path)
        except (FileOrFolderNotFound, InvalidPackageOrVersionError):
            raise Http404ErrorSuggestions(package, version, path)

        if location.is_symlink():
            # check if it's secure
            symlink_dest = os.readlink(location.sources_path)
            dest = os.path.normpath(  # absolute, target file
                os.path.join(os.path.dirname(location.sources_path),
                             symlink_dest))
            # note: adding trailing slash because normpath drops them
            if dest.startswith(os.path.normpath(location.version_path) + '/'):
                # symlink is safe; redirect to its destination
                redirect_url = os.path.normpath(
                    os.path.join(os.path.dirname(location.path_to),
                                 symlink_dest))
                self.render_func = bind_redirect(url_for(request.endpoint,
                                                 path_to=redirect_url),
                                                 code=301)
                return dict(redirect=redirect_url)
            else:
                raise Http403Error(
                    'insecure symlink, pointing outside package/version/')

        if location.is_dir():  # folder, we list its content
            return self._render_directory(location)

        elif location.is_file():  # file
            return self._render_file(location)

        else:  # doesn't exist
            raise Http404Error(None)
Beispiel #11
0
 def _get_license_dict(self, files):
     result = []
     for f in files:
         try:
             # once DB full, remove license path
             try:
                 license_path = helper.get_sources_path(
                     session, f["package"], f["version"],
                     current_app.config)
             except (FileOrFolderNotFound, InvalidPackageOrVersionError):
                 raise Http404ErrorSuggestions(f["package"], f["version"],
                                               "")
             # parse file
             try:
                 c = helper.parse_license(license_path)
                 license = helper.get_license(f["package"], f["version"],
                                              f["path"], c)
             except copyright.NotMachineReadableError:
                 license = None
             result.append(
                 dict(
                     oracle="debian",
                     path=f["path"],
                     package=f["package"],
                     version=f["version"],
                     license=license,
                     origin=helper.license_url(f["package"], f["version"]),
                 ))
         except Exception:
             result.append(
                 dict(
                     oracle="debian",
                     path=f["path"],
                     package=f["package"],
                     version=f["version"],
                     license=None,
                     origin=helper.license_url(f["package"], f["version"]),
                 ))
     return result
Beispiel #12
0
    def get_objects(self, path_to):
        path_dict = path_to.split('/')
        package = path_dict[0]
        version = path_dict[1]

        if len(path_dict) > 2:
            raise Http404ErrorSuggestions(package, version, '')

        if version == "latest":  # we search the latest available version
            return self._handle_latest_version(request.endpoint, package, "")

        versions = self.handle_versions(version, package, "")
        if versions:
            redirect_url_parts = [package, versions[-1]]
            redirect_url = '/'.join(redirect_url_parts)
            return self._redirect_to_url(request.endpoint,
                                         redirect_url,
                                         redirect_code=302)

        # identify patch format, accept only 3.0 quilt
        try:
            source_format, loc = get_sources_path(session, package, version,
                                                  current_app.config,
                                                  'debian/source/format')
        except (FileOrFolderNotFound, InvalidPackageOrVersionError):
            return dict(package=package,
                        version=version,
                        path=path_to,
                        patches=[],
                        format='unknown')

        with io.open(source_format, mode='r', encoding='utf-8') as f:
            format_file = f.read()
        if format_file.rstrip() not in ACCEPTED_FORMATS:
            return dict(package=package,
                        version=version,
                        path=path_to,
                        format=format_file,
                        patches=[],
                        supported=False)

        # are there any patches for the package?
        try:
            series, loc = get_sources_path(session, package, version,
                                           current_app.config,
                                           'debian/patches/series')
        except (FileOrFolderNotFound, InvalidPackageOrVersionError):
            return dict(package=package,
                        version=version,
                        path=path_to,
                        format=format_file,
                        patches=[],
                        supported=True)
        with io.open(series, mode='r', encoding='utf-8') as f:
            series = f.readlines()

        info = self.parse_patch_series(session, package, version,
                                       current_app.config, series)
        if 'api' in request.endpoint:
            return dict(package=package,
                        version=version,
                        format=format_file.rstrip(),
                        patches=[key.rstrip() for key in info.keys()])
        return dict(package=package,
                    version=version,
                    path=path_to,
                    format=format_file,
                    series=info.keys(),
                    patches=info,
                    supported=True)
Beispiel #13
0
    def get_objects(self, packagename, version):
        page = request.args.get("page", 1, type=int)
        path_to = packagename + "/" + version

        try:
            format_file = helper.get_patch_format(session, packagename,
                                                  version, current_app.config)
        except InvalidPackageOrVersionError:
            raise Http404ErrorSuggestions(packagename, version, "")
        except FileOrFolderNotFound:
            return dict(
                package=packagename,
                version=version,
                path=str(path_to),
                patches=[],
                format="unknown",
            )
        if not helper.is_supported(format_file):
            return dict(
                package=packagename,
                version=version,
                path=str(path_to),
                format=format_file,
                pagination=None,
                patches=[],
                supported=False,
            )

        # are there any patches for the package?
        try:
            series = helper.get_patch_series(session, packagename, version,
                                             current_app.config)
        except (FileOrFolderNotFound, InvalidPackageOrVersionError):
            return dict(
                package=packagename,
                version=version,
                path=path_to,
                format=format_file,
                pagination=None,
                patches=[],
                supported=True,
            )

        count = len(series)
        offset = int(current_app.config.get("LIST_OFFSET") or 60)
        start = (page - 1) * offset
        end = start + offset
        pagination = Pagination(page, offset, count, {
            "packagename": packagename,
            "version": version
        })
        info = self.parse_patch_series(session, packagename, version,
                                       current_app.config, series[start:end])
        if "api" in request.endpoint:
            return dict(
                package=packagename,
                version=version,
                format=format_file,
                count=count,
                patches=[key.rstrip() for key in info.keys()],
            )
        return dict(
            package=packagename,
            version=version,
            path=path_to,
            format=format_file,
            series=info.keys(),
            patches=info,
            supported=True,
            pagination=pagination,
        )