diff --git a/README.md b/README.md index 4184e91..19d011c 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,23 @@ Installation is easy, just a "simple" one-liner sudo wget https://raw.githubusercontent.com/stautonico/python-color-scripts/main/python-colorscript -P /usr/bin/ && sudo chmod +x /usr/bin/python-colorscript && sudo mkdir /etc/python-colorscript && sudo wget https://raw.githubusercontent.com/stautonico/python-color-scripts/main/art.json -P /etc/python-colorscript && sudo chmod 666 /etc/python-colorscript/art.json ``` +# Updating + +This script has a built-in updater + +To update, simply: + +```shell + sudo python-colorscript --update +``` + +If a new version exists, it will automatically install + # Usage ```shell - usage: python-colorscript [-h] [--16] [--256] [--left-padding LEFT_PADDING] [--top-padding TOP_PADDING] [--art ART] - [--list] [--random-color-mode] [--not-sus] [--ignore-distro] [--uninstall] + usage: python-colorscript [-h] [--16] [--256] [--left-padding LEFT_PADDING] [--top-padding TOP_PADDING] [--art ART] [--list] + [--random-color-mode] [--not-sus] [--ignore-distro] [--version] [--update] [--uninstall] Draw some ASCII art! @@ -42,6 +54,8 @@ Installation is easy, just a "simple" one-liner Use a random color mode --not-sus Guarantees you won't be the imposter --ignore-distro, -i Ignore distro specific artworks + --version, -V Show version information + --update Try to update to the latest version --uninstall Uninstall the script from your system ``` @@ -50,6 +64,13 @@ and more secret arguments that you'll just have to discover on your own! ### Recommended Use Case You can add this file to the end of your .zshrc or .bashrc to get some ASCII artwork every time you open a terminal! +# Uninstallation +To uninstall, simply: + +```shell + sudo python-colorscript --uninstall +``` + # Adding New Art Since the printing of this art is all handled by kui's amazing [ANSI Pixels](https://kui.github.io/ansi_pixels) tool, diff --git a/python-colorscript b/python-colorscript index c9804ef..e0d53eb 100755 --- a/python-colorscript +++ b/python-colorscript @@ -9,6 +9,11 @@ import zlib from json import loads from random import choice from shutil import rmtree +from subprocess import DEVNULL, STDOUT, check_call + +import requests + +__VERSION__ = "1.0.1" # This portion of the script belongs to the user "kui" (https://github.com/kui) on github @@ -53,9 +58,18 @@ def ansi_color(code): # END KUI'S CODE + +def is_outdated(latest, current): + latest = tuple([int(x) for x in latest.split(".")]) + current = tuple([int(x) for x in current.split(".")]) + + return latest > current + + if __name__ == '__main__': - with open("/etc/python-colorscript/art.json", "r") as f: - # with open("art.json", "r") as f: + artjson_location = "art.json" if os.getenv( + "PYTHONCOLORSCRIPTDEBUG") == "true" else "/etc/python-colorscript/art.json" + with open(artjson_location, "r") as f: data = loads(f.read()) parser = argparse.ArgumentParser(description="Draw some ASCII art!") @@ -74,11 +88,57 @@ if __name__ == '__main__': help="Guarantees you won't be the imposter") parser.add_argument("--ignore-distro", "-i", action="store_true", dest="ignoredistro", required=False, help="Ignore distro specific artworks") + parser.add_argument("--version", "-V", action="store_true", required=False, help="Show version information") + parser.add_argument("--update", action="store_true", required=False, help="Try to update to the latest version") parser.add_argument("--uninstall", action="store_true", required=False, help="Uninstall the script from your system") args = parser.parse_args() + if args.version: + print("python-colorscript {}".format(__VERSION__)) + exit() + + if args.update: + if os.getuid() != 0: + print("Only root has the permissions to update the required files, please run with sudo") + exit() + else: + response = requests.get("https://api.github.com/repos/stautonico/python-color-scripts/releases/latest") + + latest_version = response.json()["name"] + + old = is_outdated(latest_version, __VERSION__) + + if old: + print("An update is available! {} -> {}".format(__VERSION__, latest_version)) + print("Downloading new version...") + check_call( + ["wget", "{}".format(response.json()['tarball_url']), "-O", "/tmp/python-colorscript.tar.gz"], + stdout=DEVNULL, stderr=STDOUT) + print("Extracting...") + check_call(["tar", "-xzvf", "/tmp/python-colorscript.tar.gz", "-C", "/tmp"], stdout=DEVNULL, + stderr=STDOUT) + # Find the name of the new file + new_filenames = [file for file in os.listdir("/tmp/") if + file.startswith("stautonico-python-color-scripts")] + if len(new_filenames) != 1: + print("Something went wrong when trying to update!") + exit(1) + else: + # Copy the script and the art.json file + print("Installing..") + check_call( + ["cp", "/tmp/{}/python-colorscript".format(new_filenames[0]), "/usr/bin/python-colorscript"], + stdout=DEVNULL, stderr=STDOUT) + check_call(["cp", "/tmp/{}/art.json".format(new_filenames[0]), "/etc/python-colorscript/art.json"], + stdout=DEVNULL, stderr=STDOUT) + + print("Successfully updated to version {}!".format(latest_version)) + else: + print("Your software is up to date! ({})".format(__VERSION__)) + exit() + if args.uninstall: if os.getuid() != 0: print("Only root has the permissions to delete the required files, please run with sudo") @@ -101,10 +161,19 @@ if __name__ == '__main__': choices = list(data.keys()) if not args.ignoredistro: - with open("/etc/lsb-release", "r") as f: - distro_file = f.read().replace("\n", "=") - split_distro_file = distro_file.split("=") - distro = split_distro_file[split_distro_file.index("DISTRIB_ID") + 1] + try: + with open("/etc/lsb-release", "r") as f: + distro_file = f.read().replace("\n", "=") + split_distro_file = distro_file.split("=") + distro = split_distro_file[split_distro_file.index("DISTRIB_ID") + 1] + except FileNotFoundError: + try: + with open("/etc/os-release", "r") as f: + distro_file = f.read().replace("\n", "=") + split_distro_file = distro_file.split("=") + distro = split_distro_file[split_distro_file.index("ID") + 1] + except FileNotFoundError: + distro = "Generic" if distro.lower() != "arch": choices.remove("archbtw")