|
| 1 | +import yaml |
| 2 | +import pathlib |
| 3 | + |
| 4 | +import feedparser |
| 5 | +from packaging.version import Version |
| 6 | + |
| 7 | +# Go through the yaml files |
| 8 | +simtools_dir = pathlib.Path("simtools") |
| 9 | +yaml_files = simtools_dir.glob("**/*.yaml") |
| 10 | +for yaml_file in yaml_files: |
| 11 | + with open(yaml_file, "r") as file: |
| 12 | + data = yaml.safe_load(file) |
| 13 | + name = data["name"] |
| 14 | + release_info = data.get("release", {}) |
| 15 | + if ( |
| 16 | + release_info.get("source", None) == "pypi" |
| 17 | + ): # Only source supported at the moment |
| 18 | + current_version = Version(release_info.get("version", "0")) |
| 19 | + package_name = release_info.get("package_name", name) |
| 20 | + etag = release_info.get("etag", None) |
| 21 | + print(f"Checking updates for '{name}', latest known version: {current_version}") |
| 22 | + pypi_feed = feedparser.parse( |
| 23 | + f"https://pypi.org/rss/project/{package_name}/releases.xml", etag=etag |
| 24 | + ) |
| 25 | + if pypi_feed.status == 304: |
| 26 | + print(" PyPI feed has not changed") |
| 27 | + continue |
| 28 | + # Go through entries until finding a version that is not a dev release or pre-release |
| 29 | + for entry in pypi_feed.entries: |
| 30 | + entry_version = Version(entry.title) |
| 31 | + if entry_version.is_prerelease or entry_version.is_devrelease: |
| 32 | + continue |
| 33 | + if entry_version > current_version: |
| 34 | + print(f" New version found: {entry_version}") |
| 35 | + release_info["version"] = str(entry_version) |
| 36 | + release_info[ |
| 37 | + "published" |
| 38 | + ] = f"{entry.published_parsed.tm_year}-{entry.published_parsed.tm_mon:02d}-{entry.published_parsed.tm_mday:02d}" |
| 39 | + release_info["etag"] = pypi_feed.etag |
| 40 | + with open(yaml_file, "w") as file: |
| 41 | + yaml.safe_dump(data, file, sort_keys=False) |
| 42 | + break |
| 43 | + else: |
| 44 | + print(f" No new version found for '{name}'") |
| 45 | + else: |
| 46 | + print(f"Skipping '{name}' as it is not a PyPI package") |
0 commit comments