Skip to content

Commit

Permalink
Added functionality to unzip
Browse files Browse the repository at this point in the history
  • Loading branch information
shashank40 committed Jun 19, 2024
1 parent d4a1b34 commit 723d115
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 4 additions & 6 deletions openadapt/app/tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import os
import sys
import requests
from urllib.parse import urlparse
import threading
from tqdm import tqdm

Expand Down Expand Up @@ -42,7 +41,7 @@
from openadapt.app.dashboard.run import cleanup as cleanup_dashboard
from openadapt.app.dashboard.run import run as run_dashboard
from openadapt.app.main import FPATH # , start
from openadapt.build_utils import is_running_from_executable
from openadapt.build_utils import is_running_from_executable, unzip_file
from openadapt.db import crud
from openadapt.models import Recording
from openadapt.replay import replay
Expand Down Expand Up @@ -202,24 +201,23 @@ def download_latest_version(self, base_url: str, latest_version: str) -> None:
FILE_NAME = f"OpenAdapt-v{latest_version}.zip"
DOWNLOAD_URL = base_url + f"/v{latest_version}/{FILE_NAME}"

parsed_url = urlparse(DOWNLOAD_URL)
filename = os.path.basename(parsed_url.path)
downloads_path = os.path.join(os.path.expanduser("~"), "Downloads")
local_filename = os.path.join(downloads_path, filename)
local_filename = os.path.join(downloads_path, FILE_NAME)

response = requests.get(DOWNLOAD_URL, stream=True)
total_size = response.headers.get('content-length')
total_size = int(total_size) if total_size else None
block_size = 1024 # 1 Kilobyte
with open(local_filename, 'wb') as file, tqdm(
total=total_size, unit='B', unit_scale=True, desc=filename
total=total_size, unit='B', unit_scale=True, desc=FILE_NAME
) as progress_bar:
for data in response.iter_content(block_size):
if CANCEL_APP_DOWNLOAD:
CANCEL_APP_DOWNLOAD = False
break
file.write(data)
progress_bar.update(len(data))
unzip_file(local_filename)


def check_and_download_latest_version(self) -> None:
Expand Down
13 changes: 13 additions & 0 deletions openadapt/build_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
import os
import pathlib
import sys
import shutil

def set_permissions(path):
for root, dirs, files in os.walk(path):
for dir in dirs:
os.chmod(os.path.join(root, dir), 0o755)
for file in files:
os.chmod(os.path.join(root, file), 0o755)

def unzip_file(file_path: str) -> None:
if os.path.exists(file_path):
shutil.unpack_archive(file_path, os.path.dirname(file_path))
set_permissions(os.path.dirname(file_path))
print("Unzipped")

def get_root_dir_path() -> pathlib.Path:
"""Get the path to the project root directory."""
Expand Down

0 comments on commit 723d115

Please sign in to comment.