def pkg_names_list_versions(session, packagename, suite=""): """ return all versions of a packagename. if suite is specified, only versions contained in that suite are returned. """ try: name_id = session.query(PackageName) \ .filter(PackageName.name == packagename) \ .first().id except Exception: raise InvalidPackageOrVersionError(packagename) try: if not suite: versions = session.query(Package) \ .filter(Package.name_id == name_id).all() else: versions = (session.query(Package).filter( Package.name_id == name_id).filter( sql_func.lower(Suite.suite) == suite).filter( Suite.package_id == Package.id).all()) except Exception: raise InvalidPackageOrVersionError(packagename) # we sort the versions according to debian versions rules versions = sorted(versions, cmp=version_compare) return versions
def pkg_names_list_versions(session, packagename, suite="", suite_order=None): """ return all versions of a packagename. If suite is specified, only versions contained in that suite are returned. If suite order is specified, then ordering is suite first and then by debian version. """ def compare_with_suite_order(a, b): try: latest_a = max(suite_order.index(x.lower()) for x in a.suites) except ValueError: latest_a = -1 try: latest_b = max(suite_order.index(x.lower()) for x in b.suites) except ValueError: latest_b = -1 if latest_a == latest_b: return version_compare(a.version, b.version) else: return 1 if latest_a > latest_b else -1 try: name_id = session.query(PackageName) \ .filter(PackageName.name == packagename) \ .first().id except Exception: raise InvalidPackageOrVersionError(packagename) try: if not suite: if not suite_order: versions = session.query(Package) \ .filter(Package.name_id == name_id).all() versions = sorted(versions, cmp=version_compare) else: versions_w_suites = pkg_names_list_versions_w_suites( session, packagename, as_object=True) versions = sorted(versions_w_suites, cmp=compare_with_suite_order) else: versions = (session.query(Package).filter( Package.name_id == name_id).filter( sql_func.lower(Suite.suite) == suite).filter( Suite.package_id == Package.id).all()) versions = sorted(versions, cmp=version_compare) except Exception: raise InvalidPackageOrVersionError(packagename) # we sort the versions according to debian versions rules return versions
def pkg_names_list_versions_w_suites(session, packagename, suite="", reverse=False): """ return versions with suites. if suite is provided, then only return versions contained in that suite. """ # FIXME a left outer join on (Package, Suite) is more preferred. # However, per https://stackoverflow.com/a/997467, custom aggregation # function to concatenate the suite names for the group_by should be # defined on database connection level. versions = pkg_names_list_versions(session, packagename, suite) versions_w_suites = [] try: for v in versions: suites = session.query(Suite) \ .filter(Suite.package_id == v.id) \ .all() # sort the suites according to debsources.consts.SUITES # use keyfunc to make it py3 compatible suites.sort(key=lambda s: SUITES['all'].index(s.suite)) suites = [s.suite for s in suites] v = v.to_dict() v['suites'] = suites versions_w_suites.append(v) except Exception: raise InvalidPackageOrVersionError(packagename) if reverse: versions_w_suites.reverse() return versions_w_suites
def _get_debian_path(self, session, package, version, sources_dir) -> Path: """ Returns the Debian path of a package version. For example: main/h contrib/libz It's the path of a *version*, since a package can have multiple versions in multiple areas (ie main/contrib/nonfree). sources_dir: the sources directory, usually comes from the app config """ prefix = SourcePackage.pkg_prefix(package) try: p_id = (session.query(PackageName).filter( PackageName.name == package).first().id) varea = (session.query(Package).filter( and_(Package.name_id == p_id, Package.version == version)).first().area) except Exception: # the package or version doesn't exist in the database # BUT: packages are stored for a longer time in the filesystem # to allow codesearch.d.n and others less up-to-date platforms # to point here. # Problem: we don't know the area of such a package # so we try in main, contrib and non-free. for area in AREAS: if Path.exists( Path(sources_dir) / area / prefix / package / version): return Path(area) / prefix raise InvalidPackageOrVersionError("%s %s" % (package, version)) return Path(varea) / prefix