Beispiel #1
0
 def get_latest_version_within_specs(specs, versions, prereleases=None):
     spec_set = SpecifierSet(",".join(["".join([x, y]) for x, y in specs]))
     candidates = []
     for version in versions:
         if spec_set.contains(version, prereleases=prereleases):
             candidates.append(version)
     candidates = sorted(candidates,
                         key=lambda v: parse_version(v),
                         reverse=True)
     if len(candidates) > 0:
         return candidates[0]
     return None
Beispiel #2
0
 def get_latest_version_within_specs(specs, versions, prereleases=None):
     # build up a spec set and convert compatible specs to pinned ones
     spec_set = SpecifierSet(
         ",".join(["".join([x.replace("~=", "=="), y]) for x, y in specs])
     )
     candidates = []
     for version in versions:
         if spec_set.contains(version, prereleases=prereleases):
             candidates.append(version)
     candidates = sorted(candidates, key=lambda v: parse_version(v), reverse=True)
     if len(candidates) > 0:
         return candidates[0]
     return None
Beispiel #3
0
 def get_latest_version_within_specs(specs, versions, prereleases=None):
     # build up a spec set and convert compatible specs to pinned ones
     spec_set = SpecifierSet(",".join(
         ["".join([x.replace("~=", "=="), y]) for x, y in specs]))
     candidates = []
     for version in versions:
         if spec_set.contains(version, prereleases=prereleases):
             candidates.append(version)
     candidates = sorted(candidates,
                         key=lambda v: parse_version(v),
                         reverse=True)
     if len(candidates) > 0:
         return candidates[0]
     return None
Beispiel #4
0
 def get_latest_version_within_specs(specs, versions, prereleases=None):
     spec_set = SpecifierSet(",".join(["".join([x, y]) for x, y in specs]))
     candidates = []
     # print("specs are", spec_set)
     for version in versions:
         if spec_set.contains(version, prereleases=prereleases):
             candidates.append(version)
             # else:
             # print(spec_set, "does not contain", version)
     candidates = sorted(candidates, key=lambda v: parse_version(v), reverse=True)
     if len(candidates) > 0:
         # print("candidates are", candidates)
         return candidates[0]
     return None
Beispiel #5
0
def package_data(name):
    """
    Creates detailed package data, see django.json
    :param name: package name
    """
    with open('index.json') as f:
        item = json.loads(f.read()).get(name, {})

    data = {
         "count": sum([n for _, n in item.items()]),
         "specs": {
            "unpinned": 0,
            "range": 0,
            "pinned": 0,
            "compatible": 0,
            "unknown": 0
         },
         "releases": {
             "unknown": 0
         },
         "major_releases": {},
         "security": {
             "secure": 0,
             "insecure": 0,
             "unknown": 0
         }
    }

    # calculate specs
    for spec, count in item.items():
        if spec == "":
            data['specs']['unpinned'] += count
        elif spec.startswith("=="):
            data['specs']['pinned'] += count
        elif spec.startswith("~="):
            data['specs']['compatible'] += count
        else:
            is_range_spec = False
            for range_spec in ('<', ">", "!="):
                if spec.startswith(range_spec):
                    data['specs']['range'] += count
                    is_range_spec = True
                    break
            if not is_range_spec:
                data['specs']['unknown'] += count

    # releases
    import requests
    r = requests.get(f"https://pypi.python.org/pypi/{name}/json")
    releases = sorted(r.json()["releases"].keys(), key=lambda v: parse_version(v), reverse=True)

    for spec, count in item.items():
        if not spec.startswith("=="):
            data['releases']['unknown'] += count
            continue
        spec_set = SpecifierSet(spec)
        candidates = []
        for release in releases:
            if spec_set.contains(release, prereleases=True):
                candidates.append(release)
        candidates = sorted(candidates, key=lambda v: parse_version(v), reverse=True)
        if len(candidates) > 0:
            key = str(candidates[0])
            if key not in data['releases']:
                data['releases'][key] = 0
            data['releases'][key] += count
        else:
            data['releases']['unknown'] += count

    from collections import OrderedDict
    data['releases'] = OrderedDict(sorted(data['releases'].items(), key=lambda v: parse_version(v[0]), reverse=True))

    # major releases
    for release, count in data['releases'].items():
        major_release = ".".join(release.split(".")[:2])
        if major_release not in data["major_releases"]:
            data["major_releases"][major_release] = 0
        data["major_releases"][major_release] += count
    data['major_releases'] = OrderedDict(sorted(data['major_releases'].items(), key=lambda v: parse_version(v[0]), reverse=True))

    # security
    for release, count in data['releases'].items():
        if release == 'unknown':
            data['security']['unknown'] = count
            continue
        vulns = safety_check(
            packages=safety_read_requirements(StringIO(f"{name}=={release}")),
            key="",
            db_mirror="",
            cached=True,
            ignore_ids=[]
        )
        if len(vulns) > 0:
            data['security']['insecure'] += count
        else:
            data['security']['secure'] += count

    return data