Skip to content

Commit

Permalink
Clean up and API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AUNaseef committed Aug 19, 2021
1 parent eb76644 commit 68bf1bc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
18 changes: 10 additions & 8 deletions protonup/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from configparser import ConfigParser
import tarfile
import requests
from .utilities import download, sha512sum
from .constants import CONFIG_FILE, PROTONGE_URL, MIB
from .utilities import download, sha512sum, readable_size
from .constants import CONFIG_FILE, PROTONGE_URL
from .constants import TEMP_DIR, DEFAULT_INSTALL_DIR


def fetch_data(tag):
"""
Fetch ProtonGE release information from github
Expand Down Expand Up @@ -119,9 +120,9 @@ def get_proton(version=None, yes=True, dl_only=False, output=None):
# Confirmation
if not yes:
print(f"Ready to download Proton-{data['version']}",
f"\nSize : {round(data['size']/MIB, 2)} MiB",
f"\nSize : {readable_size(data['size'])}",
f"\nPublished : {data['date']}")
if not input("Continue? (Y/n): ") in ['y', 'Y', '']:
if input("Continue? (Y/n): ") not in ['y', 'Y', '']:
return

# Prepare Destination
Expand Down Expand Up @@ -159,11 +160,12 @@ def get_proton(version=None, yes=True, dl_only=False, output=None):
shutil.rmtree(TEMP_DIR, ignore_errors=True)


def remove_proton(version=None, yes=True):
def remove_proton(version=None):
"""Uninstall existing proton installation"""
target = install_directory() + "Proton-" + version
if not version.startswith("Proton-"):
version = "Proton-" + version
target = install_directory() + version
if os.path.exists(target):
if yes or input(f'Are you sure? (Y/n) ') in ['y', 'Y', '']:
shutil.rmtree(install_directory() + 'Proton-' + version)
shutil.rmtree(target)
return True
return False
16 changes: 11 additions & 5 deletions protonup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import argparse
from .api import install_directory, installed_versions
from .api import get_proton, remove_proton, fetch_releases
from .utilities import folder_size
from .constants import MIB
from .utilities import folder_size, readable_size


def parse_arguments():
Expand All @@ -26,17 +25,24 @@ def parse_arguments():
def main():
"""Start here"""
args = parse_arguments()

if args.dir:
print(f"Install directory set to '{install_directory(args.dir)}'")

if args.tag or not (args.rem or args.list or args.dir or args.releases):
get_proton(version=args.tag, yes=args.yes, dl_only=args.download,
output=args.output)
if args.rem:
if not remove_proton(version=args.rem, yes=args.yes):
if args.yes or input(f"Confirm remove {args.rem}? (Y/n): ") not in ['y', 'Y', '']:
return
if not remove_proton(version=args.rem):
print(f'Proton-{args.rem} not installed')

if args.list:
_install_directory = install_directory()
for item in installed_versions():
print(f"{item} - {round(folder_size(install_directory() + item)/MIB, 2)} MiB")
print(f"{item} - {readable_size(folder_size(_install_directory + item))}")

if args.releases:
for tag in fetch_releases():
print (tag)
print(tag)
1 change: 0 additions & 1 deletion protonup/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
DEFAULT_INSTALL_DIR = os.path.expanduser('~/.steam/root/compatibilitytools.d/')
TEMP_DIR = '/tmp/protonup/'
PROTONGE_URL = 'https://api.github.com/repos/GloriousEggroll/proton-ge-custom/releases'
MIB = 1048576 # One mebibyte in bytes
BUFFER_SIZE = 65536 # Work with 64 kb chunks
19 changes: 14 additions & 5 deletions protonup/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
import sys
import hashlib
import requests
from .constants import MIB, BUFFER_SIZE
from .constants import BUFFER_SIZE


def readable_size(num, suffix='B'):
""" Convert bytes to readable values """
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, 'Yi', suffix)


def download(url, destination, show_progress=False):
Expand All @@ -15,7 +24,7 @@ def download(url, destination, show_progress=False):

if show_progress:
f_size = int(file.headers.get('content-length'))
f_size_mib = round(f_size / MIB, 2)
f_size_r = readable_size(f_size)
c_count = int(f_size / BUFFER_SIZE)
c_current = 1
destination = os.path.expanduser(destination)
Expand All @@ -26,9 +35,9 @@ def download(url, destination, show_progress=False):
dest.write(chunk)
dest.flush()
if show_progress:
progress = min(round((c_current / c_count) * 100, 2), 100.00)
downloaded = round((c_current * BUFFER_SIZE) / MIB, 2)
sys.stdout.write(f'\rDownloaded {progress:.2f}% - {downloaded:.2f} MiB/{f_size_mib:.2f} MiB')
progress = min((c_current / c_count) * 100, 100.00)
downloaded = readable_size(c_current * BUFFER_SIZE)
sys.stdout.write(f'\rDownloaded {progress:.2f}% - {downloaded} / {f_size_r} ')
c_current += 1
if show_progress:
sys.stdout.write('\n')
Expand Down
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = protonup
version = 0.1.3
version = 0.1.4
description = Manage Proton-GE Installations
long_description = file: README.md
long_description_content_type = text/markdown
Expand All @@ -14,6 +14,8 @@ license_files =
packages = find:
install_requires =
requests
argparse
configparser
python_requires = >3.6

[options.entry_points]
Expand Down

0 comments on commit 68bf1bc

Please sign in to comment.