55from pathlib import Path
66import zipfile
77import io
8+ import shutil
9+ import platform
10+ import subprocess
811
912# Constants
1013REPO_OWNER = "contentauth"
1114REPO_NAME = "c2pa-rs"
1215GITHUB_API_BASE = "https://api.github.com"
13- ARTIFACTS_DIR = Path ("artifacts" )
16+ SCRIPTS_ARTIFACTS_DIR = Path ("scripts/artifacts" )
17+ ROOT_ARTIFACTS_DIR = Path ("artifacts" )
18+
19+ def detect_os ():
20+ """Detect the operating system and return the corresponding platform identifier."""
21+ system = platform .system ().lower ()
22+ if system == "darwin" :
23+ return "apple-darwin"
24+ elif system == "linux" :
25+ return "unknown-linux-gnu"
26+ elif system == "windows" :
27+ return "pc-windows-msvc"
28+ else :
29+ raise ValueError (f"Unsupported operating system: { system } " )
30+
31+ def detect_arch ():
32+ """Detect the CPU architecture and return the corresponding identifier."""
33+ machine = platform .machine ().lower ()
34+
35+ # Handle common architecture names
36+ if machine in ["x86_64" , "amd64" ]:
37+ return "x86_64"
38+ elif machine in ["arm64" , "aarch64" ]:
39+ return "aarch64"
40+ else :
41+ raise ValueError (f"Unsupported CPU architecture: { machine } " )
42+
43+ def get_platform_identifier ():
44+ """Get the full platform identifier (arch-os) for the current system,
45+ matching the identifiers used by the Github publisher.
46+ Returns one of:
47+ - universal-apple-darwin (for Mac)
48+ - x86_64-pc-windows-msvc (for Windows 64-bit)
49+ - x86_64-unknown-linux-gnu (for Linux 64-bit)
50+ """
51+ system = platform .system ().lower ()
52+
53+ if system == "darwin" :
54+ return "universal-apple-darwin"
55+ elif system == "windows" :
56+ return "x86_64-pc-windows-msvc"
57+ elif system == "linux" :
58+ return "x86_64-unknown-linux-gnu"
59+ else :
60+ raise ValueError (f"Unsupported operating system: { system } " )
1461
1562def get_release_by_tag (tag ):
1663 """Get release information for a specific tag from GitHub."""
1764 url = f"{ GITHUB_API_BASE } /repos/{ REPO_OWNER } /{ REPO_NAME } /releases/tags/{ tag } "
65+ print (f"Fetching release information from { url } ..." )
1866 response = requests .get (url )
1967 response .raise_for_status ()
2068 return response .json ()
2169
2270def download_and_extract_libs (url , platform_name ):
2371 """Download a zip artifact and extract only the libs folder."""
2472 print (f"Downloading artifact for { platform_name } ..." )
25- platform_dir = ARTIFACTS_DIR / platform_name
73+ platform_dir = SCRIPTS_ARTIFACTS_DIR / platform_name
2674 platform_dir .mkdir (parents = True , exist_ok = True )
2775
2876 response = requests .get (url )
@@ -31,12 +79,28 @@ def download_and_extract_libs(url, platform_name):
3179 with zipfile .ZipFile (io .BytesIO (response .content )) as zip_ref :
3280 # Extract only files inside the libs/ directory
3381 for member in zip_ref .namelist ():
82+ print (f" Processing zip member: { member } " )
3483 if member .startswith ("lib/" ) and not member .endswith ("/" ):
84+ print (f" Processing lib file from downloadedzip: { member } " )
3585 target_path = platform_dir / os .path .relpath (member , "lib" )
86+ print (f" Moving file to target path: { target_path } " )
3687 target_path .parent .mkdir (parents = True , exist_ok = True )
3788 with zip_ref .open (member ) as source , open (target_path , "wb" ) as target :
3889 target .write (source .read ())
39- print (f"Successfully downloaded and extracted libraries for { platform_name } " )
90+
91+ print (f"Done downloading and extracting libraries for { platform_name } " )
92+
93+ def copy_artifacts_to_root ():
94+ """Copy the artifacts folder from scripts/artifacts to the root of the repository."""
95+ if not SCRIPTS_ARTIFACTS_DIR .exists ():
96+ print ("No artifacts found in scripts/artifacts" )
97+ return
98+
99+ print ("Copying artifacts from scripts/artifacts to root..." )
100+ if ROOT_ARTIFACTS_DIR .exists ():
101+ shutil .rmtree (ROOT_ARTIFACTS_DIR )
102+ shutil .copytree (SCRIPTS_ARTIFACTS_DIR , ROOT_ARTIFACTS_DIR )
103+ print ("Done copying artifacts" )
40104
41105def main ():
42106 if len (sys .argv ) < 2 :
@@ -46,31 +110,43 @@ def main():
46110
47111 release_tag = sys .argv [1 ]
48112 try :
49- ARTIFACTS_DIR .mkdir (exist_ok = True )
113+ SCRIPTS_ARTIFACTS_DIR .mkdir (exist_ok = True )
50114 print (f"Fetching release information for tag { release_tag } ..." )
51115 release = get_release_by_tag (release_tag )
52- print (f"Found release: { release ['tag_name' ]} " )
116+ print (f"Found release: { release ['tag_name' ]} \n " )
53117
54- for asset in release ['assets' ]:
55- if not asset ['name' ].endswith ('.zip' ):
56- continue
118+ # Get the platform identifier for the current system
119+ env_platform = os .environ .get ("C2PA_LIBS_PLATFORM" )
120+ if env_platform :
121+ print (f"Using platform from environment variable C2PA_LIBS_PLATFORM: { env_platform } " )
122+ platform_id = env_platform or get_platform_identifier ()
123+ platform_source = "environment variable" if env_platform else "auto-detection"
124+ print (f"Target platform: { platform_id } (set through{ platform_source } )" )
57125
58- # Example asset name: c2pa-v0.49.5-aarch64-apple-darwin.zip
59- # Platform name: aarch64-apple-darwin
60- parts = asset ['name' ].split ('-' )
61- if len (parts ) < 4 :
62- continue # Unexpected naming, skip
63- platform_name = '-' .join (parts [3 :]).replace ('.zip' , '' )
126+ # Construct the expected asset name
127+ expected_asset_name = f"{ release_tag } -{ platform_id } .zip"
128+ print (f"Looking for asset: { expected_asset_name } " )
64129
65- download_and_extract_libs (asset ['browser_download_url' ], platform_name )
130+ # Find the matching asset in the release
131+ matching_asset = None
132+ for asset in release ['assets' ]:
133+ if asset ['name' ] == expected_asset_name :
134+ matching_asset = asset
135+ break
66136
67- print ("\n All artifacts have been downloaded and extracted successfully!" )
137+ if matching_asset :
138+ print (f"Found matching asset: { matching_asset ['name' ]} " )
139+ download_and_extract_libs (matching_asset ['browser_download_url' ], platform_id )
140+ print ("\n Artifacts have been downloaded and extracted successfully!" )
141+ copy_artifacts_to_root ()
142+ else :
143+ print (f"\n No matching asset found: { expected_asset_name } " )
68144
69145 except requests .exceptions .RequestException as e :
70- print (f"Error downloading artifacts : { e } " , file = sys . stderr )
146+ print (f"Error: { e } " )
71147 sys .exit (1 )
72148 except Exception as e :
73- print (f"Unexpected error : { e } " , file = sys . stderr )
149+ print (f"Error : { e } " )
74150 sys .exit (1 )
75151
76152if __name__ == "__main__" :
0 commit comments