1414the minimal installation archive.
1515
1616"""
17- import io
1817import os
1918import sys
20- import zipfile
19+ import tempfile
20+ import urllib .request
2121
22+ from distutils .spawn import find_executable
2223from pathlib import Path
23- from urllib import request
24+ from subprocess import call
2425from urllib .error import URLError
25- from zipfile import ZipFile , ZipInfo
26-
27-
28- class ZipFileWithPermissions (ZipFile ):
29- """Custom ZipFile class handling file permissions.
30-
31- Code from https://stackoverflow.com/a/54748564
32- """
33-
34- def _extract_member (self , member , targetpath , pwd ):
35- if not isinstance (member , ZipInfo ):
36- member = self .getinfo (member )
37-
38- targetpath = super ()._extract_member (member , str (targetpath ), pwd )
39- attr = member .external_attr >> 16
40-
41- if attr != 0 :
42- Path (targetpath ).chmod (attr )
43-
44- return targetpath
4526
4627
4728def main ():
4829 rti_minimal_package_url = os .getenv ("RTI_MIN_PACKAGE_URL" )
49-
30+ temp_dir = Path ( tempfile . gettempdir ())
5031 if not rti_minimal_package_url :
5132 sys .exit (
5233 "Environment variable RTI_MIN_PACKAGE_URL not found, skipping..."
@@ -59,23 +40,38 @@ def main():
5940 except FileNotFoundError :
6041 sys .exit ("The RTI_INSTALLATION_PATH does not exist." )
6142
43+ cmake_command = find_executable ("cmake" )
44+ if cmake_command is None :
45+ sys .exit ("CMake must be installed in order to use the script." )
46+
6247 try :
63- resp = request .urlopen (rti_minimal_package_url )
48+ rti_zipped_file_name = Path (
49+ urllib .request .url2pathname (rti_minimal_package_url )
50+ ).name
51+ rti_zipped_file_path = temp_dir .joinpath (rti_zipped_file_name )
52+ urllib .request .urlretrieve (
53+ rti_minimal_package_url , rti_zipped_file_path
54+ )
6455 except URLError as e :
6556 sys .exit ("Error opening the URL: {}" .format (e ))
6657
67- print ("Extracting minimal installation." )
68-
6958 try :
70- with ZipFileWithPermissions (io .BytesIO (resp .read ())) as rti_zipfile :
71- bad_file_name = rti_zipfile .testzip ()
72-
73- if bad_file_name :
74- sys .exit ("Bad file found in the archive." )
59+ return_value = call (
60+ [
61+ cmake_command ,
62+ "-E" ,
63+ "tar" ,
64+ "xf" ,
65+ str (rti_zipped_file_path ),
66+ "--format=zip" ,
67+ ],
68+ cwd = rti_installation_path ,
69+ )
70+ except FileNotFoundError :
71+ sys .exit ("The CMake executable could not be found." )
7572
76- rti_zipfile .extractall (rti_installation_path )
77- except zipfile .BadZipFile as e :
78- sys .exit ("Error opening zip file: {}" .format (e ))
73+ if return_value :
74+ sys .exit ("There were error extracting the package" )
7975
8076
8177if __name__ == "__main__" :
0 commit comments