Esempio n. 1
0
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()
Esempio n. 2
0
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)
Esempio n. 3
0
def configure():
    """Change hamstall Options."""
    while True:
        print("""
Select an option:
au - Enable/disable the ability to install updates when hamstall is run. Currently {au}.
v - Enable/disable verbose mode, showing more output when hamstall commands are run. Currently {v}.
b - Swap branches in hamstall. Allows you to get updates sooner at the cost of possible bugs. Current branch: {b}.
e - Exit hamstall
        """.format(
            au=generic.endi(config.read_config("AutoInstall")), v=generic.endi(config.read_config("Verbose")),
            b=config.db["version"]["branch"]
        ))
        option = generic.get_input("[au/v/b/E] ", ['au', 'v', 'b', 'e'], 'e')
        if option == 'au':
            if not can_update:
                print("requests isn't installed, so AutoInstall cannot be enabled!")
            else:
                key = "AutoInstall"
        elif option == 'v':
            key = "Verbose"
        elif option == 'b':
            branch_wizard()
            key = None
        elif option == 'e':
            generic.leave()
        if key is not None:
            new_value = config.change_config(key, "flip")
            print("\n{key} mode {value}!".format(key=key, value=generic.endi(new_value)))
Esempio n. 4
0
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
Esempio n. 5
0
def download_files(files, folder):
    """Download List of Files.
    
    Args:
        files (str[]): List of files to obtain from hamstall repo
        folder (str): Folder to put files in

    """
    if not can_update:
        print("Cannot download files if the request library isn't installed!")
        generic.leave(1)
    for i in files:
        r = requests.get(
            "https://raw.githubusercontent.com/hammy3502/hamstall/{}/".format(config.db["version"]["branch"]) + i)
        open(config.full(folder + i), 'wb').write(r.content)
Esempio n. 6
0
def manage(program):
    """Manage Installed Program.

    Args:
        program (str): Internal name of program to manage

    """
    while True:
        print("Enter an option to manage " + program + ":")
        print("b - Create binlinks for " + program)
        print("p - Add " + program + " to PATH")
        print("n - Rename " + program)
        print("u - Uninstall " + program)
        print("r - Remove all binlinks + PATHs for " + program)
        print("d - Create a .desktop file for " + program)
        print("rd - Remove a .desktop file for " + program)
        print("c - Run a command inside " + program + "'s directory")
        print("s - Launch a shell inside " + program + "'s directory")
        print("E - Exit program management")
        option = generic.get_input("[b/p/n/u/r/d/rd/c/s/E]", ['b', 'p', 'n', 'u', 'r', 'd', 'c', 'rd', 's', 'e'], 'e')
        if option == 'b':
            binlink(program)
        elif option == 'p':
            pathify(program)
        elif option == 'n':
            program = rename(program)
        elif option == 'u':
            uninstall(program)
            generic.leave()
        elif option == 'r':
            config.remove_line(program, "~/.hamstall/.bashrc", 'poundword')
        elif option == 'd':
            create_desktop(program)
        elif option == 'rd':
            remove_desktop(program)
        elif option == 'c':
            command(program)
        elif option == 's':
            print("When you exit the shell, you will be returned to here.")
            os.chdir(config.full("~/.hamstall/bin/" + program + "/"))
            call(["/bin/bash"])
        elif option == 'e':
            generic.leave()
Esempio n. 7
0
def get_online_version(type_of_replacement, branch=config.branch):
    """Get hamstall Version from GitHub.

    Args:
        type_of_replacement (str): Type of version to get (file or prog)
        branch (str): Branch to check version of (default: User's current branch)
    
    Returns:
        int: The specified version
    """
    if not can_update:
        print("requests library not installed! Exiting...")
        generic.leave(1)
    version_url = "https://raw.githubusercontent.com/hammy3502/hamstall/{}/version".format(branch)
    version_raw = requests.get(version_url)
    version = version_raw.text
    spot = version.find(".")
    if type_of_replacement == 'file':
        return int(version[0:spot])
    elif type_of_replacement == 'prog':
        return int(version[spot + 1:])
Esempio n. 8
0
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)
Esempio n. 9
0
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)
Esempio n. 10
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!")
Esempio n. 11
0
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)
Esempio n. 12
0
def first_time_setup(sym):
    """First Time Setup.

    Sets up hamstall for the first time.

    Args:
        sym (bool): Used for testing. If True, installed py's will be symlinked to originals, not copied.
        False means it will be copied and not symlinked.

    """
    if config.exists(config.full('~/.hamstall/hamstall.py')):
        print('Please don\'t run first time setup on an already installed system!')
        generic.leave()
    print('Installing hamstall to your system...')
    try:
        os.mkdir(config.full("~/.hamstall"))
    except FileExistsError:
        rmtree(config.full("~/.hamstall"))
        os.mkdir(config.full("~/.hamstall"))
    try:
        os.mkdir(config.full("/tmp/hamstall-temp/"))
    except FileExistsError:
        rmtree(config.full("/tmp/hamstall-temp"))
        os.mkdir(config.full("/tmp/hamstall-temp/"))
    os.mkdir(config.full("~/.hamstall/bin"))
    config.create("~/.hamstall/database")
    create_db()
    config.create("~/.hamstall/.bashrc")  # Create directories and files
    files = os.listdir()
    for i in files:
        i_num = len(i) - 3
        if i[i_num:len(i)] == '.py':
            if sym:
                os.symlink(os.getcwd() + "/" + i, config.full("~/.hamstall/" + i))
            else:
                try:
                    copyfile(i, config.full('~/.hamstall/' + i))
                except FileNotFoundError:
                    print("A file is missing that was attempted to be copied! Install halted!")
                    generic.leave(1)
    config.add_line("source ~/.hamstall/.bashrc\n", "~/{}".format(config.read_config("ShellFile")))
    config.add_line("alias hamstall='python3 ~/.hamstall/hamstall.py'\n", "~/.hamstall/.bashrc")  # Add bashrc line
    print('First time setup complete!')
    print('Please run the command "source ~/{}" or restart your terminal.'.format(config.read_config("ShellFile")))
    print('Afterwards, you may begin using hamstall with the hamstall command!')
    generic.leave()
Esempio n. 13
0
def list_programs():
    """List Installed Programs."""
    for prog in config.db["programs"].keys():
        print(prog)
    generic.leave()
Esempio n. 14
0
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)
Esempio n. 15
0
                   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()

username = getpass.getuser()  # Root check
if username == 'root':
    print(
        'Note: Running as root user will install programs for the root user to use!'
    )

if config.db == {"refresh": True}:  # Downgrade check