def ensure_path(location: Path, *, force: bool) -> Tuple[bool, bool]: """Ensure location is in user's PATH or add it to PATH. Returns True if location was added to PATH """ location_str = str(location) path_added = False need_shell_restart = userpath.need_shell_restart(location_str) in_current_path = userpath.in_current_path(location_str) if force or (not in_current_path and not need_shell_restart): userpath.append(location_str, "pipx") print( pipx_wrap( f"Success! Added {location_str} to the PATH environment variable.", subsequent_indent=" " * 4, )) path_added = True need_shell_restart = userpath.need_shell_restart(location_str) elif not in_current_path and need_shell_restart: print( pipx_wrap( f""" {location_str} has been been added to PATH, but you need to open a new terminal or re-login for this PATH change to take effect. """, subsequent_indent=" " * 4, )) else: print( pipx_wrap(f"{location_str} is already in PATH.", subsequent_indent=" " * 4)) return (path_added, need_shell_restart)
def append(locations, shells, all_shells, home, force, quiet): """Appends to the user PATH. The shell must be restarted for the update to take effect. """ if not force: for location in locations: if up.in_current_path(location): echo_warning(( 'The directory `{}` is already in PATH! If you ' 'are sure you want to proceed, try again with ' 'the -f/--force flag.'.format(location) )) sys.exit(2) elif up.in_new_path(location, shells=shells, all_shells=all_shells, home=home): echo_warning(( 'The directory `{}` is already in PATH, pending a shell ' 'restart! If you are sure you want to proceed, try again ' 'with the -f/--force flag.'.format(location) )) sys.exit(2) try: up.append(locations, shells=shells, all_shells=all_shells, home=home, check=True) except Exception as e: echo_failure(str(e)) sys.exit(1) else: if not quiet: echo_success('Success!')
def ensure_path(location: Path, *, force: bool): location_str = str(location) post_install_message = ( "You likely need to open a new terminal or re-login for " "the changes to take effect.") if userpath.in_current_path(location_str) or userpath.need_shell_restart( location_str): if not force: if userpath.need_shell_restart(location_str): print(f"{location_str} has been already been added to PATH. " f"{post_install_message}") else: logging.warning(( f"The directory `{location_str}` is already in PATH. If you " "are sure you want to proceed, try again with " "the '--force' flag.\n\n" f"Otherwise pipx is ready to go! {stars}")) return userpath.append(location_str) print(f"Success! Added {location_str} to the PATH environment variable.") print("Consider adding shell completions for pipx. " "Run 'pipx completions' for instructions.") print() print(f"{post_install_message} {stars}")
def append(path, force): """Appends to the user PATH. The shell must be restarted for the update to take effect. """ if not force: if up.in_current_path(path): echo_warning(( 'The directory `{}` is already in PATH! If you ' 'are sure you want to proceed, try again with ' 'the -f/--force flag.'.format(path) )) sys.exit(2) elif up.in_new_path(path): echo_warning(( 'The directory `{}` is already in PATH, pending a shell ' 'restart! If you are sure you want to proceed, try again ' 'with the -f/--force flag.'.format(path) )) sys.exit(2) if up.append(path): echo_success('Success!') else: echo_failure('An unexpected failure seems to have occurred.') sys.exit(1)
def test_append_multiple(): location1 = urlsafe_b64encode(urandom(5)).decode() location2 = urlsafe_b64encode(urandom(5)).decode() assert not userpath.in_current_path([location1, location2]) assert userpath.append([location1, location2]) assert userpath.in_new_path([location1, location2]) assert userpath.need_shell_restart([location1, location2])
def __init__( self, bbin_path: str = "~/.config/binbin", bin_path: str = "~/bin", app_path: str = "~/app", ): bbin_dir = Path(bbin_path).expanduser() binaries_dir = Path(bin_path).expanduser() app_dir = Path(app_path).expanduser() self._bbin_path = bbin_dir self._app_path = app_dir self._bin_path = binaries_dir if not (bbin_dir.exists() and bbin_dir.is_dir()): interface.warn("Bbin's index is not initialized! Initalizing...") git.clone( BBIN_URL, str(bbin_dir), with_spinner=False, success_text= f"Finished initializing bbin's index at {bbin_dir}", ) else: self.update() if not (binaries_dir.exists() and binaries_dir.is_dir()): with halo.Halo(f"Creating binary directory at {bin_path}" ) as spinner: # type: ignore binaries_dir.mkdir(parents=True) # Make sure it is on the $PATH userpath.append(str(binaries_dir)) # type: ignore spinner.succeed("Done") # type: ignore if not (app_dir.exists() and app_dir.is_dir()): with halo.Halo(f"Creating app directory at {app_dir}" ) as spinner: # type: ignore app_dir.mkdir(parents=True) spinner.succeed("Done") # type: ignore assert bbin_dir.exists() and bbin_dir.is_dir() assert app_dir.exists() and app_dir.is_dir() assert binaries_dir.exists() and binaries_dir.is_dir()
def add_path_to_environment(path): path = str(path) post_install_message = ( "You likely need to open a new terminal or re-login for changes to your $PATH" "to take effect.") if userpath.in_current_path(path) or userpath.need_shell_restart(path): if userpath.need_shell_restart(path): print( f"{path} has already been added to PATH. " f"{post_install_message}", file=sys.stderr, ) return userpath.append(path) print(f"Success! Added {path} to the PATH environment variable.", file=sys.stderr) print(file=sys.stderr) print(post_install_message, file=sys.stderr)
def test_append_multiple(self, request, shell_test): if shell_test is None: locations = [get_random_path(), get_random_path()] assert not userpath.in_current_path(locations) assert userpath.append(locations, check=True) assert userpath.in_new_path(locations) assert userpath.need_shell_restart(locations) else: process = shell_test(request.node.name) stdout, stderr = process.communicate() assert process.returncode == 0, (stdout + stderr).decode('utf-8')
def test_append_multiple(): locations = [get_random_path(), get_random_path()] assert not userpath.in_current_path(locations) assert userpath.append(locations, check=True) assert userpath.in_new_path(locations) assert userpath.need_shell_restart(locations)
def test_append(): location = get_random_path() assert not userpath.in_current_path(location) assert userpath.append(location, check=True) assert userpath.in_new_path(location) assert userpath.need_shell_restart(location)
import ctypes, sys def is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() except: return False if is_admin(): url = "https://storage.googleapis.com/flutter_infra/releases/stable/windows/flutter_windows_v1.9.1+hotfix.6-stable.zip" print("Downloading FLutter.....") wget.download(url, 'flutter.zip') print("Extracting FLutter.....") with zipfile.ZipFile('flutter.zip', "r") as zip_ref: zip_ref.extractall("C:\\") location = "C:\\flutter\\bin" userpath.append(location) #os.system('setx /M path "%path%;C:\\flutter\\bin"') print( "Flutter has been added to Windows environment variables and will now restart. Please save your work!" ) input("Press Enter to reboot...") os.system("shutdown -t 0 -r -f") else: # Re-run the program with admin rights ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
#!/usr/bin/env python """ This simple meta script just adds the `script` directory to path. """ import userpath from rich.console import Console from pathlib import Path us = Path(__file__) scripts_dir = (us / ".." / "scripts").resolve() scripts = str(scripts_dir) console = Console() print = console.print with console.status("Checking if scripts directory is in path") as status: if not userpath.in_current_path(scripts): status.update("Adding scripts directory to path") userpath.append(scripts) status.update("Checking if scripts directory is now in path") if userpath.in_new_path(scripts): print("Added scripts directory to path.") else: print("Scripts directory is already in path.") status.update("Checking if shell needs restarting") if userpath.need_shell_restart(scripts): print("You need to restart your shell for this to take effect.")
def test_append(): location = urlsafe_b64encode(urandom(5)).decode() assert not userpath.in_current_path(location) assert userpath.append(location) assert userpath.in_new_path(location)