Skip to content

Commit

Permalink
Permission set only on specified folder while unzipping
Browse files Browse the repository at this point in the history
  • Loading branch information
shashank40 committed Jul 5, 2024
1 parent 172d026 commit c81087e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
44 changes: 23 additions & 21 deletions openadapt/app/tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import multiprocessing
import os
import sys
import requests
import threading
from tqdm import tqdm
import time

from loguru import logger
from packaging.version import Version
from pyqttoast import Toast, ToastButtonAlignment, ToastIcon, ToastPosition, ToastPreset
from PySide6.QtCore import QMargins, QSize, Qt, Signal, QObject, Slot, QThread
from PySide6.QtCore import QMargins, QObject, QSize, Qt, QThread, Signal, Slot
from PySide6.QtGui import QAction, QColor, QFont, QIcon, QPixmap
from PySide6.QtWidgets import (
QApplication,
Expand All @@ -36,21 +36,22 @@
QVBoxLayout,
QWidget,
)
from tqdm import tqdm
import requests

from build_scripts.get_version import get_latest_version, get_version
from openadapt.app.cards import quick_record, stop_record
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.update_utils import unzip_file
from openadapt.db import crud
from openadapt.models import Recording
from openadapt.replay import replay
from openadapt.strategies.base import BaseReplayStrategy
from openadapt.update_utils import unzip_file
from openadapt.utils import get_posthog_instance
from openadapt.visualize import main as visualize
from build_scripts.get_version import get_version, get_latest_version
from packaging.version import Version

# ensure all strategies are registered
import openadapt.strategies # noqa: F401
Expand Down Expand Up @@ -117,9 +118,9 @@ class SystemTrayIcon(QObject):

# storing actions is required to prevent garbage collection
recording_actions = {"visualize": [], "replay": []}
download_complete = Signal(str)
download_start_toast = Signal(str)
unzipping_started_toast = Signal(str)
download_complete_signal = Signal(str)
download_start_toast_signal = Signal(str)
unzipping_started_toast_signal = Signal(str)

def __init__(self) -> None:
"""Initialize the system tray icon."""
Expand Down Expand Up @@ -185,11 +186,11 @@ def __init__(self) -> None:
)
self.download_update_action = TrackedQAction(self.download_button_text)
self.download_update_action.triggered.connect(self.download_latest_app_version)
# Connect download_complete signal to show_toast slot
self.download_complete.connect(self.download_complete_slot)
# Connect download_start_toast signal to download_start_toast_slot
self.download_start_toast.connect(self.download_start_toast_slot)
self.unzipping_started_toast.connect(self.unzipping_started_slot)
# Connect download_complete_signal signal to show_toast slot
self.download_complete_signal.connect(self.download_complete_slot)
# Connect download_start_toast_signal signal to download_start_toast_slot
self.download_start_toast_signal.connect(self.download_start_toast_slot)
self.unzipping_started_toast_signal.connect(self.unzipping_started_slot)

self.menu.addAction(self.download_update_action)
self.download_progress_toast = None
Expand Down Expand Up @@ -224,7 +225,7 @@ def _quit() -> None:
# for storing toasts that should be manually removed
self.sticky_toasts = {}

self.launch_dashboard()
# self.launch_dashboard()

def stop_download(self) -> None:
"""Stops download when button clicked."""
Expand Down Expand Up @@ -264,7 +265,8 @@ def download_latest_version(self, base_url: str, latest_version: str) -> None:
else:
file_ext = ".zip"

file_name = f"OpenAdapt-v{latest_version}{file_ext}"
file_base_name = f"OpenAdapt-v{latest_version}"
file_name = f"{file_base_name}{file_ext}"
download_url = base_url + f"/v{latest_version}/{file_name}"

downloads_path = os.path.join(os.path.expanduser("~"), "Downloads")
Expand Down Expand Up @@ -309,14 +311,14 @@ def download_latest_version(self, base_url: str, latest_version: str) -> None:
progress_message = (
f"Estimated time remaining: {eta_formatted} minutes"
)
self.download_start_toast.emit(progress_message)
self.download_start_toast_signal.emit(progress_message)

self.unzipping_started_toast.emit("Unzipping Started")
unzip_file(local_filename)
self.download_complete.emit("Download & Unzipping Complete")
self.unzipping_started_toast_signal.emit("Unzipping Started")
unzip_file(local_filename, file_base_name)
self.download_complete_signal.emit("Download Complete")
except Exception as e:
logger.error(e)
self.download_complete.emit("Download Failed")
self.download_complete_signal.emit("Download Failed")

def check_and_download_latest_version(self) -> None:
"""Checks and Download latest version."""
Expand Down
21 changes: 13 additions & 8 deletions openadapt/update_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import shutil

from loguru import logger


def set_permissions(path: str) -> None:
"""Set the permissions of all files to make the executable."""
Expand All @@ -12,22 +14,25 @@ def set_permissions(path: str) -> None:
try:
os.chmod(os.path.join(root, dir), 0o755)
except PermissionError:
print(f"Skipping directory due to PermissionError: {dir_path}")
logger.info(f"Skipping directory due to PermissionError: {dir_path}")
except Exception as e:
print(f"An error occurred for directory {dir_path}: {e}")
logger.info(f"An error occurred for directory {dir_path}: {e}")
for file in files:
file_path = os.path.join(root, file)
try:
os.chmod(os.path.join(root, file), 0o755)
except PermissionError:
print(f"Skipping file due to PermissionError: {file_path}")
logger.info(f"Skipping file due to PermissionError: {file_path}")
except Exception as e:
print(f"An error occurred for file {file_path}: {e}")
logger.info(f"An error occurred for file {file_path}: {e}")


def unzip_file(file_path: str) -> None:
def unzip_file(file_path: str, base_file_name: str) -> None:
"""Unzip a file to the given directory."""
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")
unzipping_directory_path = f"{os.path.dirname(file_path)}/{base_file_name}"
if not os.path.exists(unzipping_directory_path):
os.makedirs(unzipping_directory_path)
shutil.unpack_archive(file_path, unzipping_directory_path)
set_permissions(unzipping_directory_path)
logger.info("Unzipped")

0 comments on commit c81087e

Please sign in to comment.