-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage-thorium.py
164 lines (143 loc) · 6.01 KB
/
manage-thorium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
manage-thorium.py
Copyright (C) 2023 by Kai Froeb
This script is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Description:
This script automates the installation, update, or removal of the Thorium browser
on Debian-based Linux distributions. It checks for the latest version of Thorium
from its GitHub releases, downloads the .deb package, and manages the installation
or update using 'nala', a front-end for 'apt'. The script can also remove an
existing installation of Thorium.
Usage:
To install Thorium: python manage-thorium.py install
To update Thorium: python manage-thorium.py update
To remove Thorium: python manage-thorium.py remove
Dependencies:
This script uses 'nala' for package management, which is a front-end for 'apt'.
Ensure 'nala' is installed on your system. To install 'nala', use:
sudo apt install nala
"""
import requests
import os
import subprocess
import argparse
import tempfile
def get_installed_version():
"""
Retrieves the installed version of Thorium browser.
Returns:
str: Installed version number, or None if not installed.
"""
try:
result = subprocess.run(["thorium-browser", "--version"], capture_output=True, text=True, check=True)
version_line = result.stdout.strip()
if version_line.startswith("Thorium"):
return version_line.split()[1]
except subprocess.CalledProcessError:
print("Thorium browser is not installed or unable to get version.")
except FileNotFoundError:
print("Thorium browser is not installed.")
return None
def install_deb(file_path):
"""
Installs a .deb file using Nala, with a fallback to dpkg.
Args:
file_path (str): Path to the .deb file.
"""
try:
subprocess.run(["sudo", "nala", "install", file_path], check=True)
except subprocess.CalledProcessError:
print("Nala failed to install the package. Attempting with dpkg...")
subprocess.run(["sudo", "dpkg", "-i", file_path], check=True)
subprocess.run(["sudo", "nala", "install", "-f"], check=True)
def remove_package(package_name):
"""
Removes a package using Nala, with a fallback to dpkg.
Args:
package_name (str): Name of the package to remove.
"""
try:
subprocess.run(["sudo", "nala", "remove", package_name], check=True)
except subprocess.CalledProcessError:
print("Nala failed to remove the package. Attempting with dpkg...")
subprocess.run(["sudo", "dpkg", "-r", package_name], check=True)
def download_file(url, destination_folder):
"""
Downloads a file from a given URL to a specified destination.
Args:
url (str): URL of the file to download.
destination_folder (str): Folder where the file will be saved.
Returns:
str: Path to the downloaded file.
"""
local_filename = url.split('/')[-1]
path = os.path.join(destination_folder, local_filename)
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
return path
def get_latest_deb_release(owner, repo):
"""
Fetches the latest .deb release from a GitHub repository.
Args:
owner (str): GitHub username or organization name.
repo (str): GitHub repository name.
Returns:
str: URL of the latest .deb release, or None if not found.
"""
api_url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
response = requests.get(api_url)
if response.status_code != 200:
print("Failed to fetch release data from GitHub API")
return None
release_data = response.json()
for asset in release_data.get('assets', []):
if asset['name'].endswith('.deb'):
return asset['browser_download_url']
return None
def main():
"""
Manages the installation, removal, and updating of Thorium browser.
Supported commands: install, remove, update.
"""
parser = argparse.ArgumentParser(description='Manage Thorium Browser installation.')
subparsers = parser.add_subparsers(dest='command')
subparsers.required = True
# Add parsers for 'install', 'remove', and 'update' commands
install_parser = subparsers.add_parser('install')
remove_parser = subparsers.add_parser('remove')
update_parser = subparsers.add_parser('update')
args = parser.parse_args()
if args.command in ['install', 'update']:
installed_version = get_installed_version()
if args.command == 'update' and installed_version is None:
print("Thorium is not installed. Use 'install' command to install it.")
return
latest_deb_url = get_latest_deb_release("Alex313031", "Thorium")
if not latest_deb_url:
print("No .deb release found")
return
latest_version = latest_deb_url.split('/')[-1].split('_')[1]
if installed_version == latest_version:
print(f"Latest version {latest_version} is already installed.")
return
if args.command == 'update':
print(f"Updating Thorium from version {installed_version} to {latest_version}...")
print("Latest .deb release URL:", latest_deb_url)
# Define a general temporary directory for downloads
temp_dir = tempfile.gettempdir()
downloaded_file = download_file(latest_deb_url, temp_dir)
try:
install_deb(downloaded_file)
print(f"download {downloaded_file} and installed it from {temp_dir}")
except Error:
print(f"downloading {downloaded_file} and/or installing it from {temp_dir} failed")
elif args.command == 'remove':
remove_package("thorium-browser") # Replace with the actual package name if different
if __name__ == "__main__":
main()