1717import subprocess
1818import sys
1919import sysconfig
20+ import tarfile
2021import urllib .request
2122from ctypes .util import find_library
2223from pathlib import Path
@@ -67,9 +68,16 @@ def libzim_major(self) -> str:
6768 def found_libzim (self ) -> str :
6869 return find_library ("zim" )
6970
71+ @property
72+ def is_latest_nightly (self ) -> bool :
73+ """will use redirect to latest available nightly"""
74+ return self .libzim_dl_version == "nightly"
75+
7076 @property
7177 def is_nightly (self ) -> bool :
72- return re .match (r"\d{4}-\d{2}-\d{2}" , self .libzim_dl_version )
78+ return self .is_latest_nightly or re .match (
79+ r"\d{4}-\d{2}-\d{2}" , self .libzim_dl_version
80+ )
7381
7482 @property
7583 def platform (self ) -> str :
@@ -122,7 +130,7 @@ def is_musl(self) -> bool:
122130 ["/usr/bin/env" , "ldd" , "--version" ], capture_output = True , text = True
123131 )
124132 try :
125- return "musl libc" in ps .stdout . readlines ()[0 ]
133+ return "musl libc" in ps .stderr . splitlines ()[0 ]
126134 except Exception :
127135 return False
128136
@@ -143,8 +151,13 @@ def get_download_filename(self, arch: Optional[str] = None) -> str:
143151 if self .platform == "Linux" :
144152 variant = "-musl" if self .is_musl else "-bionic"
145153
154+ if self .is_latest_nightly :
155+ version_suffix = ""
156+ else :
157+ version_suffix = f"-{ self .libzim_dl_version } "
158+
146159 return pathlib .Path (
147- f"libzim_{ lzplatform } -{ arch } { variant } - { self . libzim_dl_version } .tar.gz"
160+ f"libzim_{ lzplatform } -{ arch } { variant } { version_suffix } .tar.gz"
148161 ).name
149162
150163 def download_to_dest (self ):
@@ -196,7 +209,9 @@ def _download_and_extract(self, filename: str) -> pathlib.Path:
196209
197210 fpath = self .base_dir / filename
198211 source_url = "http://download.openzim.org/release/libzim"
199- if self .is_nightly :
212+ if self .is_latest_nightly :
213+ source_url = "http://download.openzim.org/nightly"
214+ elif self .is_nightly :
200215 source_url = f"http://download.openzim.org/nightly/{ self .libzim_dl_version } "
201216 url = f"{ source_url } /{ fpath .name } "
202217
@@ -213,7 +228,14 @@ def _download_and_extract(self, filename: str) -> pathlib.Path:
213228 print ("> extracting archive" )
214229 # extract into current folder (all files are inside an in-tar folder)
215230 shutil .unpack_archive (fpath , self .base_dir , "gztar" )
216- folder = fpath .with_name (fpath .name .replace (".tar.gz" , "" ))
231+
232+ # nightly have different download name and extracted folder name as it
233+ # uses a redirect
234+ if self .is_latest_nightly :
235+ tar = tarfile .open (fpath )
236+ folder = pathlib .Path (pathlib .Path (tar .firstmember .name ).parts [0 ])
237+ else :
238+ folder = fpath .with_name (fpath .name .replace (".tar.gz" , "" ))
217239
218240 return folder
219241
0 commit comments