def finish_install(program_internal_name): """End of Install. Ran after every program install. Args: program_internal_name (str): Name of program as stored in the database """ config.vprint("Removing temporary install directory (if it exists)") try: rmtree("/tmp/hamstall-temp") except FileNotFoundError: pass config.vprint("Adding program to hamstall list of programs") config.db["programs"].update({program_internal_name: {"desktops": []}}) config.write_db() yn = generic.get_input('Would you like to add the program to your PATH? [Y/n]', ['y', 'n'], 'y') if yn == 'y': pathify(program_internal_name) yn = generic.get_input('Would you like to create a binlink? [y/N]', ['y', 'n'], 'n') if yn == 'y': binlink(program_internal_name) yn = generic.get_input('Would you like to create a desktop file? [y/N]', ['y', 'n'], 'n') if yn == 'y': create_desktop(program_internal_name) print("Install complete!") generic.leave()
def create_command(file_extension, program): """Create Extraction Command. Args: file_extension (str): File extension of program (including .) program (str): Program name overwrite_files (bool): Whether or not the command should overwrite files. Defaults to False. Returns: str: Command to run """ if config.vcheck(): # Creates the command to run to extract the archive if file_extension == '.tar.gz' or file_extension == '.tar.xz': vflag = 'v' elif file_extension == '.zip': vflag = '' elif file_extension == '.7z': vflag = '' elif file_extension == '.rar': vflag = '' else: if file_extension == '.tar.gz' or file_extension == '.tar.xz': vflag = '' elif file_extension == '.zip': vflag = '-qq' elif file_extension == '.7z': vflag = '-bb0 -bso0 -bd ' elif file_extension == '.rar': vflag = '-idcdpq ' if file_extension == '.tar.gz' or file_extension == '.tar.xz': command_to_go = "tar " + vflag + "xf " + program + " -C /tmp/hamstall-temp/" if which("tar") is None: print("tar not installed; please install it to install .tar.gz and .tar.xz files!") generic.leave() elif file_extension == '.zip': command_to_go = 'unzip ' + vflag + ' ' + program + ' -d /tmp/hamstall-temp/' if which("unzip") is None: print("unzip not installed; please install it to install ZIP files!") generic.leave() elif file_extension == '.7z': command_to_go = '7z x ' + vflag + program + ' -o/tmp/hamstall-temp/' if which("7z") is None: print("7z not installed; please install it to install 7z files!") generic.leave() elif file_extension == '.rar': command_to_go = 'unrar x ' + vflag + program + ' /tmp/hamstall-temp/' if which("unrar") is None: print("unrar not installed; please install it to install RAR files!") generic.leave() else: print('Error! File type not supported!') generic.leave(1) config.vprint("Running command: " + command_to_go) return command_to_go
def pathify(program_internal_name): """Add Program to Path. Adds a program to PATH through ~/.hamstall/.bashrc Args: program_internal_name (str): Name of program to add to PATH """ config.vprint('Adding program to PATH') line_to_write = "export PATH=$PATH:~/.hamstall/bin/" + program_internal_name + ' # ' + program_internal_name + '\n' config.add_line(line_to_write, "~/.hamstall/.bashrc") return
def dirinstall(program_path, program_internal_name, overwrite=False): """Install Directory. Installs a directory as a program Args: program_path (str): Path to directory to install program_internal_name (str): Name of program overwrite (bool): Whether or not to assume the program is already installed and to overwite it """ if not config.check_bin("rsync") and overwrite: print("rsync not installed! Please install it.") generic.leave(1) config.vprint("Moving folder to hamstall destination") if overwrite: call(["rsync", "-a", program_path, config.full("~/.hamstall/bin/{}".format(program_internal_name))]) rmtree(program_path) else: move(program_path, config.full("~/.hamstall/bin/")) finish_install(program_internal_name)
def erase(): """Remove hamstall.""" if not (config.exists(config.full("~/.hamstall/hamstall.py"))): print("hamstall not detected so not removed!") generic.leave() config.vprint('Removing source line from bashrc') config.remove_line("~/.hamstall/.bashrc", "~/{}".format(config.read_config("ShellFile")), "word") config.vprint("Removing .desktop files") for prog in config.db["programs"]: if config.db["programs"][prog]["desktops"]: for d in config.db["programs"][prog]["desktops"]: try: os.remove(config.full("~/.local/share/applications/{}.desktop".format(d))) except FileNotFoundError: pass config.vprint('Removing hamstall directory') rmtree(config.full('~/.hamstall')) try: rmtree("/tmp/hamstall-temp") except FileNotFoundError: pass print("Hamstall has been removed from your system.") print('Please restart your terminal.') config.unlock() sys.exit(0)
def update(silent=False): """Update Hamstall. Checks to see if we should update hamstall, then does so if one is available Args: silent (bool): Whether or not to not provide user feedback. Defaults to False. """ if not can_update: print("requests not found! Can't update!") if silent: return else: generic.leave(1) """Update hamstall after checking for updates""" prog_version_internal = config.get_version('prog_internal_version') config.vprint("Checking version on GitHub") final_version = get_online_version('prog') config.vprint('Installed internal version: ' + str(prog_version_internal)) config.vprint('Version on GitHub: ' + str(final_version)) if final_version > prog_version_internal: print("An update has been found! Installing...") config.vprint('Removing old hamstall pys...') os.chdir(config.full("~/.hamstall")) files = os.listdir() for i in files: i_num = len(i) - 3 if i[i_num:len(i)] == '.py': os.remove(config.full('~/.hamstall/' + i)) config.vprint("Downloading new hamstall pys..") download_files(['hamstall.py', 'generic.py', 'config.py', 'config.py', 'prog_manage.py'], '~/.hamstall/') config.db["version"]["prog_internal_version"] = final_version elif final_version < prog_version_internal: if not silent: print("hamstall version newer than latest online version! Something might be wrong...") else: if not silent: print("No update found!")
def binlink(program_internal_name): """Link Program. Creates an alias that cd's into a program directory before running a file in the program Args: program_internal_name (str): Name of program to create a binlink for """ while True: files = os.listdir(config.full('~/.hamstall/bin/' + program_internal_name + '/')) print(' '.join(files)) file_chosen = 'Cool fact. This line was originally written on line 163.' while file_chosen not in files: # Get file to binlink from user file_chosen = input('Please enter a file listed above. If you would like to cancel, type exit: ') if file_chosen == "exit": return line_to_add = 'alias ' + file_chosen + "='cd " + config.full('~/.hamstall/bin/' + program_internal_name) + \ '/ && ./' + file_chosen + "' # " + program_internal_name + "\n" config.vprint("Adding alias to bashrc") config.add_line(line_to_add, "~/.hamstall/.bashrc") yn = generic.get_input('Would you like to continue adding files to be run directly? [y/N]', ['y', 'n'], 'n') if yn == 'n': return
def uninstall(program): """Uninstall a Program. Args: program (str): Name of program to uninstall """ config.vprint("Removing program files") rmtree(config.full("~/.hamstall/bin/" + program + '/')) config.vprint("Removing program from PATH and any binlinks for the program") config.remove_line(program, "~/.hamstall/.bashrc", 'poundword') config.vprint("Removing program desktop files") if config.db["programs"][program]["desktops"]: for d in config.db["programs"][program]["desktops"]: try: os.remove(config.full("~/.local/share/applications/{}.desktop".format(d))) except FileNotFoundError: pass config.vprint("Removing program from hamstall list of programs") del config.db["programs"][program] print("Uninstall complete!") return
def gitinstall(git_url, program_internal_name, overwrite=False): """Git Install. Installs a program from a URL to a Git repository Args: git_url (str): URL to Git repository program_internal_name (str): Name of program to use overwrite (bool): Whether or not to assume the program is already installed and to overwite it """ if not config.check_bin("rsync") and overwrite: print("rsync not installed! Please install it.") generic.leave(1) config.vprint("Verifying that the input is a URL...") if re.match(r"https://\w.\w", git_url) is None or " " in git_url or "\\" in git_url: print("Invalid URL!") generic.leave() config.vprint("Checking for .git extension") if config.extension(git_url) != ".git": print("The URL must end in .git!") generic.leave(1) config.vprint("Downloading git repository") if overwrite: try: rmtree(config.full("/tmp/hamstall-temp")) # Removes temp directory (used during installs) except FileNotFoundError: pass os.mkdir("/tmp/hamstall-temp") os.chdir("/tmp/hamstall-temp") else: os.chdir(config.full("~/.hamstall/bin")) err = call(["git", "clone", git_url]) if err != 0: print("Error detected! Installation halted.") generic.leave(1) if overwrite: call(["rsync", "-a", "/tmp/hamstall-temp/{}/".format(program_internal_name), config.full("~/.hamstall/bin/{}".format(program_internal_name))]) finish_install(program_internal_name)
def install(program, overwrite=False): """Install Archive. Takes an archive and installs it. Args: program (str): Path to archive to install overwrite (bool): Whether or not to assume the program is already installed and to overwite it """ if not config.check_bin("rsync") and overwrite: print("rsync not installed! Please install it.") generic.leave(1) program_internal_name = config.name(program) if config.char_check(program_internal_name): print("Error! Archive name contains a space or #!") generic.leave(1) config.vprint("Removing old temp directory (if it exists!)") try: rmtree(config.full("/tmp/hamstall-temp")) # Removes temp directory (used during installs) except FileNotFoundError: pass config.vprint("Creating new temp directory") os.mkdir(config.full("/tmp/hamstall-temp")) # Creates temp directory for extracting archive config.vprint("Extracting archive to temp directory") file_extension = config.extension(program) program = config.spaceify(program) command_to_go = create_command(file_extension, program) config.vprint('File type detected: ' + file_extension) try: os.system(command_to_go) # Extracts program archive except: print('Failed to run command: ' + command_to_go + "!") print("Program installation halted!") generic.leave(1) config.vprint('Checking for folder in folder') if os.path.isdir(config.full('/tmp/hamstall-temp/' + program_internal_name + '/')): config.vprint('Folder in folder detected! Using that directory instead...') source = config.full('/tmp/hamstall-temp/' + program_internal_name) + '/' dest = config.full('~/.hamstall/bin/') else: config.vprint('Folder in folder not detected!') source = config.full('/tmp/hamstall-temp') + '/' dest = config.full('~/.hamstall/bin/' + program_internal_name + "/") config.vprint("Moving program to directory") if overwrite: if verbose: verbose_flag = "v" else: verbose_flag = "" call(["rsync", "-a{}".format(verbose_flag), source, dest]) else: move(source, dest) config.vprint("Adding program to hamstall list of programs") config.vprint('Removing old temp directory...') try: rmtree(config.full("/tmp/hamstall-temp")) except FileNotFoundError: config.vprint('Temp folder not found so not deleted!') finish_install(program_internal_name)
def branch_wizard(): """Switch Branches.""" if get_online_version("prog", "master") <= 18: extra_warning = """ hamstall stable release 1.2.0 hasn't happened yet! You cannot reset to the older version of hamstall! You must stay on this version! ############### """ else: extra_warning = "" print("""\n\n ####WARNING#### WARNING: You are changing branches of hamstall! Changing from master to beta means you may receive updates that contain bugs, some extremely severe! Changing from beta to master means you will either HAVE ALL OF YOUR HAMSTALL PROGRAMS DELETED or you will have to STAY ON THE UPDATE YOU CURRENTLY HAVE UNTIL MASTER CATCHES UP! Switching branches will trigger an immediate update of hamstall! ############### {extra_warning} Select a branch: m - Master branch. Less bugs, more stable, wait for updates. b - Beta branch. More bugs, less stable, updates asap. E - Exit branch wizard and don't change branches. """.format(extra_warning=extra_warning)) ans = generic.get_input("[m/b/E] ", ['m', 'b', 'e'], 'e') if ans == 'e': print("Not changing branches!") generic.leave() elif ans == 'm' and config.db["version"]["branch"] == "master": print("Already on the master branch, not switching!") generic.leave() elif ans == 'b' and config.db["version"]["branch"] == "beta": print("Already on the beta branch, not switching!") generic.leave() else: check = input('Type "YES" (without the quotes) to confirm the branch switch! ') if check != "YES": print("Cancelling branch switch.") generic.leave() if ans == 'm': branch = "master" if not config.check_bin("git"): print("Git is not installed! Your branch can be switched, but downgrading is impossible! " "If you would like to exit here, type \"y\"!") if generic.get_input("", ['y','n'], 'n') == 'y': print("Branch has not been changed!") generic.leave(1) else: print("Continuing with branch switch.") elif ans == 'b': branch = "beta" print("Changing branches and updating hamstall!") config.vprint("Switching branch and writing change to file") config.db["version"]["branch"] = branch config.write_db() if branch == "beta": config.vprint("Updating hamstall...") update(True) generic.leave(0) elif branch == "master": if get_online_version("prog", "master") <= 18: print("Cannot downgrade; staying on this version until master catches up!") generic.leave(0) print("Would you like to downgrade? If you do, all hamstall programs will be deleted!") dr = generic.get_input("If you don't, hamstall will remain at its current version until master is at a newer release! [y/N]", ['y', 'n'], 'n') if dr == 'y': config.vprint("Deleting and re-installing hamstall.") os.chdir(config.full("~/.hamstall")) config.vprint("Removing old hamstall .pys") for i in os.listdir(): i_num = len(i) - 3 if i[i_num:len(i)] == '.py': try: os.remove(i) except FileNotFoundError: pass try: rmtree("/tmp/hamstall-temp") except FileNotFoundError: pass os.mkdir("/tmp/hamstall-temp") os.chdir("/tmp/hamstall-temp") config.vprint("Cloning hamstall from the master branch") call(["git", "clone", "https://github.com/hammy3502/hamstall.git"]) os.chdir("/tmp/hamstall-temp/hamstall") config.vprint("Adding new hamstall .pys") for i in os.listdir(): i_num = len(i) - 3 if i[i_num:len(i)] == '.py': copyfile(i, config.full('~/.hamstall/' + i)) config.vprint("Removing old database and programs.") try: os.remove(config.full("~/.hamstall/database")) except FileNotFoundError: pass try: rmtree(config.full("~/.hamstall/bin")) except FileNotFoundError: pass os.mkdir(config.full("~/.hamstall/bin")) print("Please run hamstall again to re-create the database!") config.unlock() config.db = {"refresh": True} config.write_db() sys.exit(0)
def create_desktop(program_internal_name): """Create Desktop. Walks the user through creating a .desktop file for a program Args: program_internal_name (str): Name of program as stored in the database """ files = os.listdir(config.full('~/.hamstall/bin/' + program_internal_name + '/')) print(' '.join(files)) program_file = '/Placeholder/' config.vprint("Getting user inputs") while program_file not in files: # Get file to binlink from user program_file = input('Please enter a file listed above. If you would like to cancel, type exit: ') if program_file == "exit": return desktop_name = "{}-{}".format(program_file, program_internal_name) if config.exists("~/.local/share/applications/{}.desktop".format(desktop_name)): print("Desktop file already exists!") return exec_path = config.full("~/.hamstall/bin/{}/{}".format(program_internal_name, program_file)) path = config.full("~/.hamstall/bin/{}/".format(program_internal_name)) comment = "/" while not comment.replace(" ", "").isalnum() and comment != "": comment = input("Please input a comment for the application: ") if comment == "": comment = program_internal_name icon = ";" while not icon.replace("-", "").replace("_", "").replace("/", "").isalnum() and icon != "": icon = input("Enter the path to an icon, the name of the icon, or press ENTER for no icon! ") if icon != "": icon = "Icon=" + icon terminal = generic.get_input("Should this program launch a terminal to run it in? [y/N]", ['y', 'n'], 'n') if terminal.lower() == 'y': should_terminal = "True" else: should_terminal = "False" name = "/" while not name.replace(" ", "").isalnum() and name != "": name = input("Please enter a name: ") if name == "": name = program_internal_name ans = " " chosen_categories = [] categories = ["audio", "video", "development", "education", "game", "graphics", "network", "office", "science", "settings", "system", "utility", "end"] while ans.lower() != "end": print("Please enter categories, one at a time, from the list of .desktop categories below (defaults to " "Utility). Type \"end\" to end category selection. \n") print(", ".join(categories)) ans = generic.get_input("", categories, "Utility") if ans.capitalize() in chosen_categories or ans == "end": pass else: ans = ans.capitalize() chosen_categories.append(ans) if ans in ["Audio", "Video"] and not ("AudioVideo" in chosen_categories): chosen_categories.append("AudioVideo") if not chosen_categories: chosen_categories = ["Utility"] cats = ";".join(chosen_categories) + ";" # Get categories for the .desktop to_write = """ [Desktop Entry] Name={name} Comment={comment} Path={path} Exec={exec_path} {icon} Terminal={should_terminal} Type=Application Categories={categories} """.format(name=name, comment=comment, exec_path=exec_path, should_terminal=should_terminal, categories=cats, icon=icon, path=path) os.chdir(config.full("~/.local/share/applications/")) config.create("./{}.desktop".format(desktop_name)) with open(config.full("./{}.desktop".format(desktop_name)), 'w') as f: f.write(to_write) config.db["programs"][program_internal_name]["desktops"].append(desktop_name) print("\nDesktop file created!")
group.add_argument('-m', '--manage', help="Manage an installed program") group.add_argument( '-k', '--remove-lock', help="Remove hamstall lock file (only do this if hamstall isn't already " "running)", action="store_true") group.add_argument('-c', '--config', help="Change hamstall options", action="store_true") args = parser.parse_args() # Parser stuff if config.locked(): # Lock check config.vprint( "Lock file detected at /tmp/hamstall-lock. " + "Delete this file if you are completely sure no other instances of hamstall are running!" ) if args.remove_lock: try: os.remove(config.full("/tmp/hamstall-lock")) print("Lock removed!") except FileNotFoundError: print("Lock doesn't exist, so not removed!") generic.leave() else: print( "Another instance of hamstall is probably running! Execution halted!" ) sys.exit(2) else: config.lock()