diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index e504682..61e5567 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -150,14 +150,27 @@ def local_crate(crate, version): cratename, version = os.path.basename(crate)[:-6].rsplit("-", 1) return crate, cratename, version +def query_newest_version(crate): + url = requests.compat.urljoin(API_URL, f"crates/{crate}/versions") + req = requests.get(url, headers={"User-Agent": "rust2rpm"}) + req.raise_for_status() + versions = req.json()["versions"] + for struct in versions: + version = struct["num"] + if struct["yanked"]: + print(f'Ignoring yanked version {version}') + elif re.search('alpha|beta|rc|pre', version): + print(f'Ignoring pre-release version {version}') + else: + print(f'Found version {version}') + return version + + raise ValueError("Couldn't find any release versions. Specify a version explicitly.") + def download(crate, version): if version is None: # Now we need to get latest version - url = requests.compat.urljoin(API_URL, f"crates/{crate}/versions") - req = requests.get(url, headers={"User-Agent": "rust2rpm"}) - req.raise_for_status() - versions = req.json()["versions"] - version = next(version["num"] for version in versions if not version["yanked"]) + version = query_newest_version(crate) os.makedirs(CACHEDIR, exist_ok=True) cratef_base = f"{crate}-{version}.crate"