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 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 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 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 test_check_bin(): assert config.check_bin("sh") is True assert config.check_bin("agtasdytfhgasdfsudyghaushdgj") is False