Skip to content

Commit

Permalink
Merge pull request #6 from jeremiah-k/fix-extraction
Browse files Browse the repository at this point in the history
Fix extraction
  • Loading branch information
jeremiah-k authored Oct 14, 2024
2 parents c23985e + b4e5b49 commit d4e5b71
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
16 changes: 10 additions & 6 deletions app/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ def main():
subparsers = parser.add_subparsers(dest='command')

# Command to run setup
parser_setup = subparsers.add_parser('setup', help='Run the setup process')
subparsers.add_parser('setup', help='Run the setup process')

# Command to download firmware and APKs
parser_download = subparsers.add_parser('download', help='Download firmware and APKs')
subparsers.add_parser('download', help='Download firmware and APKs')

# Command to display NTFY topic
parser_topic = subparsers.add_parser('topic', help='Display the current NTFY topic')
subparsers.add_parser('topic', help='Display the current NTFY topic')

# Command to clean/remove Fetchtastic files and settings
parser_clean = subparsers.add_parser('clean', help='Remove Fetchtastic configuration, downloads, and cron jobs')
subparsers.add_parser('clean', help='Remove Fetchtastic configuration, downloads, and cron jobs')

args = parser.parse_args()

Expand All @@ -35,8 +35,12 @@ def main():
elif args.command == 'topic':
# Display the NTFY topic
config = setup_config.load_config()
if config and config.get('NTFY_SERVER'):
print(f"Current NTFY topic URL: {config['NTFY_SERVER']}")
if config and config.get('NTFY_SERVER') and config.get('NTFY_TOPIC'):
ntfy_server = config['NTFY_SERVER'].rstrip('/')
ntfy_topic = config['NTFY_TOPIC']
full_url = f"{ntfy_server}/{ntfy_topic}"
print(f"Current NTFY topic URL: {full_url}")
print(f"Topic name: {ntfy_topic}")
else:
print("Notifications are not set up. Run 'fetchtastic setup' to configure notifications.")
elif args.command == 'clean':
Expand Down
25 changes: 19 additions & 6 deletions app/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
selected_apk_assets = config.get('SELECTED_APK_ASSETS', [])
selected_firmware_assets = config.get('SELECTED_FIRMWARE_ASSETS', [])

download_dir = config.get('DOWNLOAD_DIR', os.path.join(os.path.expanduser("~"), "Downloads", "Fetchtastic"))
download_dir = config.get('DOWNLOAD_DIR', os.path.join(os.path.expanduser("~"), "storage", "downloads", "Meshtastic"))
firmware_dir = os.path.join(download_dir, "firmware")
apks_dir = os.path.join(download_dir, "apks")
latest_android_release_file = os.path.join(apks_dir, "latest_android_release.txt")
Expand Down Expand Up @@ -92,14 +92,27 @@ def download_file(url, download_path):
except requests.exceptions.RequestException as e:
log_message(f"Error downloading {url}: {e}")

# Function to extract files based on the given patterns
# Updated extract_files function
def extract_files(zip_path, extract_dir, patterns):
try:
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
for file_name in zip_ref.namelist():
if any(pattern in file_name for pattern in patterns):
zip_ref.extract(file_name, extract_dir)
log_message(f"Extracted {file_name} to {extract_dir}")
matched_files = []
for file_info in zip_ref.infolist():
file_name = file_info.filename
base_name = os.path.basename(file_name)
log_message(f"Checking file: {base_name}")
for pattern in patterns:
if pattern in base_name:
# Extract and flatten directory structure
source = zip_ref.open(file_info)
target_path = os.path.join(extract_dir, base_name)
with open(target_path, 'wb') as target_file:
target_file.write(source.read())
log_message(f"Extracted {base_name} to {extract_dir}")
matched_files.append(base_name)
break # Stop checking patterns for this file
if not matched_files:
log_message(f"No files matched the extraction patterns in {zip_path}.")
except zipfile.BadZipFile:
log_message(f"Error: {zip_path} is a bad zip file and cannot be opened.")
except Exception as e:
Expand Down
39 changes: 23 additions & 16 deletions app/setup_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
from . import menu_firmware
from . import downloader # Import downloader to perform first run

# Define the default configuration directory
HOME_DIR = os.path.expanduser("~")

# Try to find the Downloads directory
DOWNLOADS_DIR = os.path.join(HOME_DIR, 'Downloads')
if not os.path.exists(DOWNLOADS_DIR):
# Try other common locations
DOWNLOADS_DIR = os.path.join(HOME_DIR, 'Download')
if not os.path.exists(DOWNLOADS_DIR):
# Use HOME_DIR if Downloads directory is not found
DOWNLOADS_DIR = HOME_DIR

DEFAULT_CONFIG_DIR = os.path.join(DOWNLOADS_DIR, 'Fetchtastic')
def get_downloads_dir():
# For Termux, use ~/storage/downloads
if 'com.termux' in os.environ.get('PREFIX', ''):
storage_downloads = os.path.expanduser("~/storage/downloads")
if os.path.exists(storage_downloads):
return storage_downloads
# For other environments, use standard Downloads directories
home_dir = os.path.expanduser("~")
downloads_dir = os.path.join(home_dir, 'Downloads')
if os.path.exists(downloads_dir):
return downloads_dir
downloads_dir = os.path.join(home_dir, 'Download')
if os.path.exists(downloads_dir):
return downloads_dir
# Fallback to home directory
return home_dir

DOWNLOADS_DIR = get_downloads_dir()
DEFAULT_CONFIG_DIR = os.path.join(DOWNLOADS_DIR, 'Meshtastic')
CONFIG_FILE = os.path.join(DEFAULT_CONFIG_DIR, 'fetchtastic.yaml')

def config_exists():
Expand Down Expand Up @@ -56,7 +62,6 @@ def run_setup():
config['SAVE_FIRMWARE'] = save_firmware

# Run the menu scripts based on user choices
# Adjust SAVE_APKS and SAVE_FIRMWARE based on selections
if save_apks:
apk_selection = menu_apk.run_menu()
if not apk_selection:
Expand All @@ -74,7 +79,7 @@ def run_setup():
else:
config['SELECTED_FIRMWARE_ASSETS'] = firmware_selection['selected_assets']

# If both save_apks and save_firmware are False, inform the user and restart setup
# If both save_apks and save_firmware are False, inform the user and exit setup
if not save_apks and not save_firmware:
print("You must select at least one asset to download (APK or firmware).")
print("Please run 'fetchtastic setup' again and select at least one asset.")
Expand Down Expand Up @@ -138,8 +143,10 @@ def run_setup():
with open(CONFIG_FILE, 'w') as f:
yaml.dump(config, f)

full_topic_url = f"{ntfy_server.rstrip('/')}/{topic_name}"
print(f"Notifications have been set up using the topic: {topic_name}")
print(f"You can subscribe to this topic in the ntfy app by pasting the topic name.")
print(f"You can subscribe to this topic in the ntfy app easily by pasting the topic name.")
print(f"Full topic URL: {full_topic_url}")
# Ask if the user wants to copy the topic name to the clipboard
copy_to_clipboard = input("Do you want to copy the topic name to the clipboard? [y/n] (default: y): ").strip().lower() or 'y'
if copy_to_clipboard == 'y':
Expand Down

0 comments on commit d4e5b71

Please sign in to comment.