Example #1
0
def iter_version_links(html, name):
    """
    Iterate through version links (in order) within HTML.

    Filtering out links that don't "look" like versions.

    Either yields hrefs to be recursively searches or tuples of (name, href)
    that match the given name.
    """
    soup = BeautifulSoup(html)
    for node in soup.findAll("a"):
        if node.get("href") is None:
            continue
        try:
            guessed_name, _ = guess_name_and_version(node.text)
        except ValueError:
            href = node["href"]
            for extension in [".tar.gz", ".zip"]:
                if href.endswith(extension):
                    yield basename(href), href
                    break
            else:
                if node.get("rel") == "download":
                    # Might be a recursive link.
                    yield href
            # else couldn't parse name and version, probably the wrong kind of link
        else:
            if guessed_name.replace("_", "-").lower() != name.replace("_", "-").lower():
                continue
            yield node.text, node["href"]
Example #2
0
    def _get_metadata(self, path, filename):
        metadata = read_metadata(path)

        # make sure metadata validates
        if not self.validate_metadata(metadata):
            abort(codes.bad_request)

        # make sure nothing fishy is going on
        if metadata.get(Version.FILENAME) not in [None, filename]:
            abort(codes.bad_request)

        # make sure metadata is consistent with filename
        expected_name, expected_version = guess_name_and_version(filename)
        if metadata["name"] != expected_name or metadata["version"] != expected_version:
            self.logger.warn("Aborting upload of: {}; conflicting filename and metadata".format(filename))
            abort(codes.bad_request)

        # include local path in metadata
        metadata[Version.FILENAME] = filename
        # add upload timestamp
        metadata["_uploaded_timestamp"] = time()

        return metadata
Example #3
0
 def validate_guess(basename, expected_name, expected_version):
     name, version = guess_name_and_version(basename)
     eq_(name, expected_name)
     eq_(version, expected_version)