diff --git a/LICENSE b/LICENSE index a63e591..3c51476 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 madas +Copyright (c) 2024 Madars Batraks Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..f8cb294 --- /dev/null +++ b/__main__.py @@ -0,0 +1,3 @@ +from cli import main + +main() \ No newline at end of file diff --git a/cli.py b/cli.py new file mode 100644 index 0000000..65dda13 --- /dev/null +++ b/cli.py @@ -0,0 +1,38 @@ +import getopt +import sys +import os + +import discord +argumentList = sys.argv[1:] + +options = "hiu" + +long_options = ["help", "install", "uninstall"] + +def main(): + + if os.geteuid() == 0: + print("WARNING: This script is not meant to run as root as it installs discord to the user folder.") + i = input("Do you wish to proceed? (y/n)") + if str(i).upper() != "Y": + return exit() + + try: + # Parsing argument + arguments, values = getopt.getopt(argumentList, options, long_options) + + # checking each argument + for currentArgument, currentValue in arguments: + + if currentArgument in ("-h", "--help"): + print ("Displaying Help") + + elif currentArgument in ("-i", "--install"): + discord.install() + + elif currentArgument in ("-u", "--uninstall"): + discord.uninstall() + + except getopt.error as err: + # output error, and return with an error code + print (str(err)) diff --git a/config.py b/config.py new file mode 100644 index 0000000..872f9f3 --- /dev/null +++ b/config.py @@ -0,0 +1,19 @@ +import os + +config = { + "discordUrl" : "https://discord.com/api/download?platform=linux&format=tar.gz", + "asarUrl" : "https://github.com/GooseMod/OpenAsar/releases/download/nightly/app.asar", + "tmpDir" : "/tmp/dup/", + "confDir" : os.path.expanduser("~") + "/.config/", + "installDir" : os.path.expanduser("~") + "/.local/lib/Discord", + "linkDir" : os.path.expanduser("~") + "/.local/bin/Discord", + "localDir" : os.path.expanduser("~") + "/.local/share/dup" +} + +def get( name: str ): + try: + config[name] + except: + raise Exception("No such configuration variable: " + name) + else: + return config[name] \ No newline at end of file diff --git a/discord.py b/discord.py new file mode 100644 index 0000000..e1703e9 --- /dev/null +++ b/discord.py @@ -0,0 +1,41 @@ +import tarfile +import os +import shutil + +from util import download,rm,mkdir +import config + +tmpDir = config.get("tmpDir") +installDir = config.get("installDir") +discordUrl = config.get("discordUrl") + +def install(): + rm(tmpDir) + mkdir(tmpDir) + + print("Downloading Discord...") + download(discordUrl, "discord.tar.gz") + + print("Extracting..." ) + file = tarfile.open(tmpDir+"/discord.tar.gz") + mkdir("/tmp/dup/discord") + file.extractall(tmpDir+"/discord") + file.close() + + print("Installing...") + rm(installDir) + shutil.move(tmpDir+"/discord/Discord", installDir) + + os.symlink(installDir + "/Discord", config.get("linkDir")) + + print("Running post-install tasks... (Your Discord client will close)") + # TBA + + print("Cleaning up...") + rm(tmpDir) + +def uninstall(): + rm(config.get("installDir")) + rm(config.get("tmpDir")) + rm(config.get("confDir")) + rm(config.get("linkDir")) \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index d34d81b..0000000 --- a/main.py +++ /dev/null @@ -1,109 +0,0 @@ -import requests -import tqdm -import tarfile -import os -import shutil -import getopt -import sys - -discordUrl = "https://discord.com/api/download?platform=linux&format=tar.gz" -tmpDir = "/tmp/dup/" -confDir = "~/.config/" -installDir = os.path.expanduser("~") + "/.local/lib/Discord" -execDir = os.path.expanduser("~") + "/.local/bin/Discord" - -def download(url: str, dest: str): - with open(tmpDir + dest, 'wb') as f: - with requests.get(url, stream=True) as r: - r.raise_for_status() - totalLength = int(r.headers.get('content-length', 0)) - - tqdm_params = { - 'desc': dest, - 'total': totalLength, - 'miniters': 1, - 'bar_format' : "{desc}: |{bar:40}| {percentage:3.1f}% ", - } - with tqdm.tqdm(**tqdm_params) as pb: - for chunk in r.iter_content(chunk_size=8192): - pb.update(len(chunk)) - f.write(chunk) - -def rm(dir: str): - try: - if os.path.isdir(dir) == True: - shutil.rmtree(dir) - else: - os.remove(dir) - except Exception as err: - print(err) - -# def rm(folder: str): -# try: -# shutil.rmtree(folder) -# except Exception as err: -# print(err) - -def mkdir(name: str): - try: - os.mkdir(name) - except Exception as err: - print(err) - -def installDiscord(): - rm(tmpDir) - mkdir(tmpDir) - - print("Downloading Discord...") - download(discordUrl, "discord.tar.gz") - - print("Extracting..." ) - file = tarfile.open(tmpDir+"/discord.tar.gz") - mkdir("/tmp/dup/discord") - file.extractall(tmpDir+"/discord") - file.close() - - print("Installing...") - rm(installDir) - shutil.move(tmpDir+"/discord/Discord", installDir) - - os.symlink(installDir + "/Discord", execDir) - - print("Cleaning up...") - # os.remove(tmpDir+"discord.tar.gz") - rm(tmpDir) - -def uninstall(): - rm(installDir) - rm(tmpDir) - rm(confDir) - rm(execDir) - - -argumentList = sys.argv[1:] - -# Options -options = "hiu" - -# Long options -long_options = ["help", "install", "uninstall"] - -try: - # Parsing argument - arguments, values = getopt.getopt(argumentList, options, long_options) - - # checking each argument - for currentArgument, currentValue in arguments: - - if currentArgument in ("-h", "--help"): - print ("Displaying Help") - - elif currentArgument in ("-i", "--install"): - installDiscord() - - elif currentArgument in ("-u", "--uninstall"): - uninstall() - -except getopt.error as err: - # output error, and return with an error code - print (str(err)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fa9cf06 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +tqdm \ No newline at end of file diff --git a/util.py b/util.py new file mode 100644 index 0000000..53e98ec --- /dev/null +++ b/util.py @@ -0,0 +1,41 @@ +import tqdm +import os +import shutil +import requests +import config + +def download(url: str, dest: str): + with open(config.get("tmpDir") + dest, 'wb') as f: + with requests.get(url, stream=True) as r: + r.raise_for_status() + totalLength = int(r.headers.get('content-length', 0)) + + tqdm_params = { + 'desc': dest, + 'total': totalLength, + 'miniters': 1, + 'bar_format' : "{desc}: |{bar:40}| {percentage:3.1f}% ", + } + with tqdm.tqdm(**tqdm_params) as pb: + for chunk in r.iter_content(chunk_size=8192): + pb.update(len(chunk)) + f.write(chunk) + +def mkdir(name: str): + try: + os.mkdir(name) + except Exception as err: + print(err) + +def rm(dir: str): + try: + if os.path.isdir(dir) == True: + shutil.rmtree(dir) + else: + os.remove(dir) + except Exception as err: + print(err) + +def rmlist(list: list): + for dir in list: + rm(dir)