mesaport.Installer.downloader

  1import os
  2
  3import requests
  4import time
  5from rich import print, progress
  6
  7progress_columns = (progress.DownloadColumn(), 
  8                    *progress.Progress.get_default_columns(),
  9                    progress.TimeElapsedColumn())
 10
 11from . import mesaurls
 12
 13class Download:
 14    """Class for downloading the MESA SDK and MESA zip files."""
 15    def __init__(self, directory, version, ostype):
 16        """Initialize the Downloader class.
 17
 18        Args:
 19            ver (str): Version of MESA to install. 
 20            directory (str): Path to the directory where the MESA SDK and MESA zip files will be downloaded.
 21            ostype (str): Operating system type.
 22        """        
 23        self.ostype = ostype
 24        self.directory = directory
 25        sdk_url, mesa_url = self.prep_urls(version)
 26        self.sdk_download, self.mesa_zip = self.download(sdk_url, mesa_url)
 27        # if self.ostype == "macOS-Intel":
 28        if 'macOS' in self.ostype:
 29            xquartz = os.path.join(directory, mesaurls.url_xquartz.split('/')[-1])
 30            self.check_n_download(xquartz, mesaurls.url_xquartz, "[green b]Downloading XQuartz...")
 31
 32
 33    def prep_urls(self, version):
 34        """Prepare the URLs for the MESA SDK and MESA zip files.
 35
 36        Args:
 37            ver (str): Version of MESA to install. 
 38
 39        Returns:
 40            tuple: URLs for the MESA SDK and MESA zip files.
 41        """      
 42        if self.ostype == "Linux":
 43            sdk_url = mesaurls.linux_sdk_urls.get(version)
 44            mesa_url = mesaurls.mesa_urls.get(version)
 45        elif self.ostype == "macOS-Intel":
 46            sdk_url = mesaurls.mac_intel_sdk_urls.get(version)
 47            mesa_url = mesaurls.mesa_urls.get(version)
 48        elif self.ostype == "macOS-ARM":
 49            sdk_url = mesaurls.mac_arm_sdk_urls.get(version)
 50            mesa_url = mesaurls.mesa_urls.get(version)
 51        return sdk_url, mesa_url
 52
 53
 54    def check_n_download(self, filepath, url, text="Downloading..."):
 55        """Check if a file has already been downloaded, and if not, download it.
 56
 57        Args:
 58            filepath (str): Path to the file to be downloaded. 
 59            url (str): URL of the file to be downloaded.
 60            text (str, optional): Text to be displayed while downloading the file. Defaults to "Downloading...".
 61        """    
 62        headers = {
 63                        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
 64                    }
 65        response = requests.get(url, headers=headers, stream=True)
 66        response.raise_for_status()
 67        total = float(response.headers.get('content-length', 0))  
 68        if os.path.exists(filepath) and total == os.path.getsize(filepath):
 69            print(text)
 70            print("[blue]File already downloaded. Skipping download.[/blue]\n")
 71        else:
 72            chunk_size = 1024*1024
 73            size_done = 0
 74            with open(filepath, 'wb') as file, progress.Progress(*progress_columns) as progressbar:
 75                task = progressbar.add_task(text, total=int(total))
 76                for chunk in response.raw.stream(chunk_size, decode_content=False):
 77                    if chunk:
 78                        size_ = file.write(chunk)
 79                        progressbar.update(task_id=task, advance=size_)
 80                        size_done += size_
 81                time.sleep(5)
 82                if total == size_done:
 83                    progressbar.update(task, description=text+"[bright_blue b]Done![/bright_blue b]")
 84                else:
 85                    progressbar.update(task, description=text+"[red b]Failed![/red b]")
 86                    raise Exception("Download failed.")
 87            print("\n", end="")
 88            
 89
 90    def download(self, sdk_url, mesa_url):
 91        """Download the MESA SDK and MESA zip files.
 92
 93        Args:
 94            sdk_url (str): URL of the MESA SDK.
 95            mesa_url (str): URL of the MESA zip file.
 96
 97        Returns:
 98            tuple: Paths to the downloaded MESA SDK and MESA zip files.
 99        """        
100        sdk_download = os.path.join(self.directory, sdk_url.split('/')[-1])
101        self.check_n_download(sdk_download, sdk_url, "[green b]Downloading MESA SDK...[/green b]")
102
103        mesa_zip = os.path.join(self.directory, mesa_url.split('/')[-1])
104        self.check_n_download(mesa_zip, mesa_url, "[green b]Downloading MESA...[/green b]")
105        return sdk_download, mesa_zip
progress_columns = (<rich.progress.DownloadColumn object>, <rich.progress.TextColumn object>, <rich.progress.BarColumn object>, <rich.progress.TaskProgressColumn object>, <rich.progress.TimeRemainingColumn object>, <rich.progress.TimeElapsedColumn object>)
class Download:
 14class Download:
 15    """Class for downloading the MESA SDK and MESA zip files."""
 16    def __init__(self, directory, version, ostype):
 17        """Initialize the Downloader class.
 18
 19        Args:
 20            ver (str): Version of MESA to install. 
 21            directory (str): Path to the directory where the MESA SDK and MESA zip files will be downloaded.
 22            ostype (str): Operating system type.
 23        """        
 24        self.ostype = ostype
 25        self.directory = directory
 26        sdk_url, mesa_url = self.prep_urls(version)
 27        self.sdk_download, self.mesa_zip = self.download(sdk_url, mesa_url)
 28        # if self.ostype == "macOS-Intel":
 29        if 'macOS' in self.ostype:
 30            xquartz = os.path.join(directory, mesaurls.url_xquartz.split('/')[-1])
 31            self.check_n_download(xquartz, mesaurls.url_xquartz, "[green b]Downloading XQuartz...")
 32
 33
 34    def prep_urls(self, version):
 35        """Prepare the URLs for the MESA SDK and MESA zip files.
 36
 37        Args:
 38            ver (str): Version of MESA to install. 
 39
 40        Returns:
 41            tuple: URLs for the MESA SDK and MESA zip files.
 42        """      
 43        if self.ostype == "Linux":
 44            sdk_url = mesaurls.linux_sdk_urls.get(version)
 45            mesa_url = mesaurls.mesa_urls.get(version)
 46        elif self.ostype == "macOS-Intel":
 47            sdk_url = mesaurls.mac_intel_sdk_urls.get(version)
 48            mesa_url = mesaurls.mesa_urls.get(version)
 49        elif self.ostype == "macOS-ARM":
 50            sdk_url = mesaurls.mac_arm_sdk_urls.get(version)
 51            mesa_url = mesaurls.mesa_urls.get(version)
 52        return sdk_url, mesa_url
 53
 54
 55    def check_n_download(self, filepath, url, text="Downloading..."):
 56        """Check if a file has already been downloaded, and if not, download it.
 57
 58        Args:
 59            filepath (str): Path to the file to be downloaded. 
 60            url (str): URL of the file to be downloaded.
 61            text (str, optional): Text to be displayed while downloading the file. Defaults to "Downloading...".
 62        """    
 63        headers = {
 64                        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
 65                    }
 66        response = requests.get(url, headers=headers, stream=True)
 67        response.raise_for_status()
 68        total = float(response.headers.get('content-length', 0))  
 69        if os.path.exists(filepath) and total == os.path.getsize(filepath):
 70            print(text)
 71            print("[blue]File already downloaded. Skipping download.[/blue]\n")
 72        else:
 73            chunk_size = 1024*1024
 74            size_done = 0
 75            with open(filepath, 'wb') as file, progress.Progress(*progress_columns) as progressbar:
 76                task = progressbar.add_task(text, total=int(total))
 77                for chunk in response.raw.stream(chunk_size, decode_content=False):
 78                    if chunk:
 79                        size_ = file.write(chunk)
 80                        progressbar.update(task_id=task, advance=size_)
 81                        size_done += size_
 82                time.sleep(5)
 83                if total == size_done:
 84                    progressbar.update(task, description=text+"[bright_blue b]Done![/bright_blue b]")
 85                else:
 86                    progressbar.update(task, description=text+"[red b]Failed![/red b]")
 87                    raise Exception("Download failed.")
 88            print("\n", end="")
 89            
 90
 91    def download(self, sdk_url, mesa_url):
 92        """Download the MESA SDK and MESA zip files.
 93
 94        Args:
 95            sdk_url (str): URL of the MESA SDK.
 96            mesa_url (str): URL of the MESA zip file.
 97
 98        Returns:
 99            tuple: Paths to the downloaded MESA SDK and MESA zip files.
100        """        
101        sdk_download = os.path.join(self.directory, sdk_url.split('/')[-1])
102        self.check_n_download(sdk_download, sdk_url, "[green b]Downloading MESA SDK...[/green b]")
103
104        mesa_zip = os.path.join(self.directory, mesa_url.split('/')[-1])
105        self.check_n_download(mesa_zip, mesa_url, "[green b]Downloading MESA...[/green b]")
106        return sdk_download, mesa_zip

Class for downloading the MESA SDK and MESA zip files.

Download(directory, version, ostype)
16    def __init__(self, directory, version, ostype):
17        """Initialize the Downloader class.
18
19        Args:
20            ver (str): Version of MESA to install. 
21            directory (str): Path to the directory where the MESA SDK and MESA zip files will be downloaded.
22            ostype (str): Operating system type.
23        """        
24        self.ostype = ostype
25        self.directory = directory
26        sdk_url, mesa_url = self.prep_urls(version)
27        self.sdk_download, self.mesa_zip = self.download(sdk_url, mesa_url)
28        # if self.ostype == "macOS-Intel":
29        if 'macOS' in self.ostype:
30            xquartz = os.path.join(directory, mesaurls.url_xquartz.split('/')[-1])
31            self.check_n_download(xquartz, mesaurls.url_xquartz, "[green b]Downloading XQuartz...")

Initialize the Downloader class.

Arguments:
  • ver (str): Version of MESA to install.
  • directory (str): Path to the directory where the MESA SDK and MESA zip files will be downloaded.
  • ostype (str): Operating system type.
ostype
directory
def prep_urls(self, version):
34    def prep_urls(self, version):
35        """Prepare the URLs for the MESA SDK and MESA zip files.
36
37        Args:
38            ver (str): Version of MESA to install. 
39
40        Returns:
41            tuple: URLs for the MESA SDK and MESA zip files.
42        """      
43        if self.ostype == "Linux":
44            sdk_url = mesaurls.linux_sdk_urls.get(version)
45            mesa_url = mesaurls.mesa_urls.get(version)
46        elif self.ostype == "macOS-Intel":
47            sdk_url = mesaurls.mac_intel_sdk_urls.get(version)
48            mesa_url = mesaurls.mesa_urls.get(version)
49        elif self.ostype == "macOS-ARM":
50            sdk_url = mesaurls.mac_arm_sdk_urls.get(version)
51            mesa_url = mesaurls.mesa_urls.get(version)
52        return sdk_url, mesa_url

Prepare the URLs for the MESA SDK and MESA zip files.

Arguments:
  • ver (str): Version of MESA to install.
Returns:

tuple: URLs for the MESA SDK and MESA zip files.

def check_n_download(self, filepath, url, text='Downloading...'):
55    def check_n_download(self, filepath, url, text="Downloading..."):
56        """Check if a file has already been downloaded, and if not, download it.
57
58        Args:
59            filepath (str): Path to the file to be downloaded. 
60            url (str): URL of the file to be downloaded.
61            text (str, optional): Text to be displayed while downloading the file. Defaults to "Downloading...".
62        """    
63        headers = {
64                        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
65                    }
66        response = requests.get(url, headers=headers, stream=True)
67        response.raise_for_status()
68        total = float(response.headers.get('content-length', 0))  
69        if os.path.exists(filepath) and total == os.path.getsize(filepath):
70            print(text)
71            print("[blue]File already downloaded. Skipping download.[/blue]\n")
72        else:
73            chunk_size = 1024*1024
74            size_done = 0
75            with open(filepath, 'wb') as file, progress.Progress(*progress_columns) as progressbar:
76                task = progressbar.add_task(text, total=int(total))
77                for chunk in response.raw.stream(chunk_size, decode_content=False):
78                    if chunk:
79                        size_ = file.write(chunk)
80                        progressbar.update(task_id=task, advance=size_)
81                        size_done += size_
82                time.sleep(5)
83                if total == size_done:
84                    progressbar.update(task, description=text+"[bright_blue b]Done![/bright_blue b]")
85                else:
86                    progressbar.update(task, description=text+"[red b]Failed![/red b]")
87                    raise Exception("Download failed.")
88            print("\n", end="")

Check if a file has already been downloaded, and if not, download it.

Arguments:
  • filepath (str): Path to the file to be downloaded.
  • url (str): URL of the file to be downloaded.
  • text (str, optional): Text to be displayed while downloading the file. Defaults to "Downloading...".
def download(self, sdk_url, mesa_url):
 91    def download(self, sdk_url, mesa_url):
 92        """Download the MESA SDK and MESA zip files.
 93
 94        Args:
 95            sdk_url (str): URL of the MESA SDK.
 96            mesa_url (str): URL of the MESA zip file.
 97
 98        Returns:
 99            tuple: Paths to the downloaded MESA SDK and MESA zip files.
100        """        
101        sdk_download = os.path.join(self.directory, sdk_url.split('/')[-1])
102        self.check_n_download(sdk_download, sdk_url, "[green b]Downloading MESA SDK...[/green b]")
103
104        mesa_zip = os.path.join(self.directory, mesa_url.split('/')[-1])
105        self.check_n_download(mesa_zip, mesa_url, "[green b]Downloading MESA...[/green b]")
106        return sdk_download, mesa_zip

Download the MESA SDK and MESA zip files.

Arguments:
  • sdk_url (str): URL of the MESA SDK.
  • mesa_url (str): URL of the MESA zip file.
Returns:

tuple: Paths to the downloaded MESA SDK and MESA zip files.