-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathversion.py
More file actions
70 lines (55 loc) · 2.32 KB
/
version.py
File metadata and controls
70 lines (55 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
##############################################################################
#
# (c) 2025 The Trustees of Columbia University in the City of New York.
# All rights reserved.
#
# File coded by: Billinge Group members and community contributors.
#
# See GitHub contributions for a more detailed list of contributors.
# https://github.com/diffpy/diffpy.pdffit2/graphs/contributors # noqa: E501
#
# See LICENSE.rst for license information.
#
##############################################################################
"""Definition of __version__."""
# We do not use the other three variables, but can be added back if needed.
# __all__ = ["__date__", "__git_commit__", "__timestamp__", "__version__"]
import datetime
import json
import urllib.request
# obtain version information
from importlib.metadata import PackageNotFoundError, version
from pathlib import Path
def get_pypi_release_date(package_name, timeout=5):
package_file = Path(__file__).resolve()
try:
with open(package_file, "r", encoding="utf-8") as f:
lines = f.readlines()
for line in reversed(lines):
if line.startswith("# Release date:"):
return line.split(":", 1)[1].strip()
url = f"https://pypi.org/pypi/{package_name}/json"
with urllib.request.urlopen(url, timeout=timeout) as response:
data = json.loads(response.read().decode("utf-8"))
installed_version = version(package_name)
release_data = data["releases"].get(installed_version, [])
if not release_data:
raise ValueError(
f"No release data found for version {installed_version}"
)
release_date_str = release_data[-1]["upload_time"]
release_date = datetime.datetime.fromisoformat(release_date_str).date()
with open(package_file, "a", encoding="utf-8") as f:
f.write(f"\n# Release date: {release_date}")
except (ValueError, OSError) as e:
print(f"Warning: Could not fetch release date from PyPI: {e}")
release_date = datetime.datetime.fromtimestamp(
package_file.stat().st_ctime
).isoformat()
return str(release_date)
__date__ = get_pypi_release_date("diffpy.pdffit2")
try:
__version__ = version("diffpy.pdffit2")
except PackageNotFoundError:
__version__ = "unknown"