-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathsetup.py
More file actions
84 lines (70 loc) · 2.77 KB
/
setup.py
File metadata and controls
84 lines (70 loc) · 2.77 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
import os
import platform
import shutil
import sys
import urllib.request
from pathlib import Path
from setuptools import setup
from setuptools.command.build_py import build_py
class CustomBuildPy(build_py):
"""Custom build command to download and set up Lensfun."""
def run(self):
self.download_and_extract_lensfun()
super().run()
def get_download_url(self, asset_name):
"""Gets the download URL for a given asset from the latest GitHub release."""
api_url = "https://api.github.com/repos/shenmintao/lensfun/releases/latest"
# Try both GITHUB_TOKEN and GH_TOKEN for compatibility
token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
headers = {}
if token:
headers["Authorization"] = f"token {token}"
req = urllib.request.Request(api_url, headers=headers)
try:
with urllib.request.urlopen(req) as response:
data = json.loads(response.read().decode())
for asset in data["assets"]:
if asset["name"] == asset_name:
return asset["browser_download_url"]
except Exception as e:
print(f"Error fetching release info from GitHub: {e}", file=sys.stderr)
return None
return None
def download_and_extract_lensfun(self):
"""Downloads and extracts the appropriate Lensfun library."""
vendor_dir = Path("src/raw_alchemy/vendor/lensfun")
vendor_dir.mkdir(parents=True, exist_ok=True)
system = platform.system().lower()
if system == "windows":
asset_name = "lensfun-windows.zip"
elif system == "linux":
asset_name = "lensfun-linux.tar.gz"
elif system == "darwin":
asset_name = "lensfun-macos.tar.gz"
else:
print(f"Unsupported system: {system}", file=sys.stderr)
sys.exit(1)
download_url = self.get_download_url(asset_name)
if not download_url:
print(f"Could not find download URL for {asset_name}", file=sys.stderr)
sys.exit(1)
archive_path = asset_name
try:
print(f"Downloading Lensfun for {system} from {download_url}...")
with urllib.request.urlopen(download_url) as response, open(
archive_path, "wb"
) as out_file:
shutil.copyfileobj(response, out_file)
print("Extracting Lensfun...")
shutil.unpack_archive(archive_path, vendor_dir)
os.remove(archive_path)
print("Lensfun setup complete.")
except Exception as e:
print(f"Error during Lensfun setup: {e}", file=sys.stderr)
sys.exit(1)
setup(
cmdclass={
"build_py": CustomBuildPy,
}
)