def test_run(self): bad_param = 100 runner = SudoRun() try: _are_params_valid = runner.run(command=bad_param, desired_user=bad_param) except ValueError: self.assertTrue(True) else: self.assertTrue(False) try: _can_kate_run = runner.run(command="uname -a", desired_user="******") except UnknownUserException: self.assertTrue(True) else: self.assertTrue(False) try: _can_jared_run_as_root = runner.run(command="whoami", desired_user="******") except PrivilageExecutionException: self.assertTrue(True) else: self.assertTrue(False)
def list_git_configuration() -> list: """ Retrieve Git configuration information about the current user """ keeper = SudoRun() if not((git := shutil.which("git"))): raise EnvironmentError(f'could not find git path')
def configure_git(self, username=None, mail=None): """ GOAL: Configure Git """ keeper = SudoRun() whoami = keeper.whoami username = input("Git username: "******"Git email: ") if not mail else mail git_conf_file = pathlib.Path(f'/home/{whoami}/.gitconfig') commands = [ f'git config --file {git_conf_file} user.name {username}', f'git config --file {git_conf_file} user.email {mail}' ] for command in commands: keeper.run(command, whoami) print(colored("Successfully configured git", 'green'))
def configure_ppa(self): """ Goal: Install PPA """ # this is done in the installer pass gpg_url = "https://www.tuffix.xyz/repo/KEY.gpg" tuffix_list = pathlib.Path("/etc/apt/sources.list.d/tuffix.list") gpg_dest = pathlib.Path("/tmp/tuffix.gpg") executor = SudoRun() content = requests.get(gpg_url).content with open(gpg_dest, "wb") as gd, open(tuffix_list, "w") as tl: gd.write(content) tl.write("deb https://www.tuffix.xyz/repo focal main") executor.run(f'sudo apt-key add {gpg_dest.resolve()}', executor.whoami)
def test_check_user(self): param = 100 runner = SudoRun() try: _is_root_valid = runner.check_user("root") _is_kate_valid = runner.check_user("kate") except EnvironmentError as error: # cannot find /etc/passwd raise AssertionError(f'{error}') try: _is_params_valid = runner.check_user(param) except ValueError: # incorrect parameters self.assertTrue(True) else: self.assertTrue(False) self.assertTrue(_is_root_valid and isinstance(_is_root_valid, bool)) self.assertFalse(_is_kate_valid and isinstance(_is_kate_valid, bool))
def system_terminal_emulator() -> str: """ Goal: find the default terminal emulator Source: https://unix.stackexchange.com/questions/264329/get-the-terminal-emulator-name-inside-the-shell-script This is some next level stuff """ _command = """ sid=$(ps -o sid= -p "$$") sid_as_integer=$((sid)) # strips blanks if any session_leader_parent=$(ps -o ppid= -p "$sid_as_integer") session_leader_parent_as_integer=$((session_leader_parent)) emulator=$(ps -o comm= -p "$session_leader_parent_as_integer") echo "$emulator" """ keeper = SudoRun() output = keeper.run(command=_command, desired_user=keeper.whoami) if not(output): raise ValueError('error when parsing ps output') return ' '.join(output)
def __init__(self, build_config: BuildConfig, name: str, description: str): super().__init__(build_config, name, description) self.build_config = build_config self.executor = SudoRun()