[Launcher] append --user if the bot isn't in a venv (#2027)

* [V3 Launcher] --user CLI flag

* Better handling of the sys arguments

* Black reformat to -l 99

* Update launcher to PR#2025

* Always append --user if not in a virtualenv

* Remove --user flag
This commit is contained in:
El Laggron 2018-08-24 14:33:57 +02:00 committed by Toby Harradine
parent bda7e08208
commit bc39a6741c

View File

@ -34,6 +34,14 @@ IS_WINDOWS = os.name == "nt"
IS_MAC = sys.platform == "darwin" IS_MAC = sys.platform == "darwin"
def is_venv():
"""Return True if the process is in a venv or in a virtualenv."""
# credit to @calebj
return hasattr(sys, "real_prefix") or (
hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix
)
def parse_cli_args(): def parse_cli_args():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Red - Discord Bot's launcher (V3)", allow_abbrev=False description="Red - Discord Bot's launcher (V3)", allow_abbrev=False
@ -71,7 +79,7 @@ def parse_cli_args():
return parser.parse_known_args() return parser.parse_known_args()
def update_red(dev=False, reinstall=False, voice=False, mongo=False, docs=False, test=False): def update_red(dev=False, voice=False, mongo=False, docs=False, test=False):
interpreter = sys.executable interpreter = sys.executable
print("Updating Red...") print("Updating Red...")
# If the user ran redbot-launcher.exe, updating with pip will fail # If the user ran redbot-launcher.exe, updating with pip will fail
@ -104,25 +112,21 @@ def update_red(dev=False, reinstall=False, voice=False, mongo=False, docs=False,
package = "Red-DiscordBot" package = "Red-DiscordBot"
if egg_l: if egg_l:
package += "[{}]".format(", ".join(egg_l)) package += "[{}]".format(", ".join(egg_l))
if reinstall: arguments = [
code = subprocess.call(
[
interpreter, interpreter,
"-m", "-m",
"pip", "pip",
"install", "install",
"-U", "-U",
"-I", "-I",
"--force-reinstall",
"--no-cache-dir", "--no-cache-dir",
"--force-reinstall",
"--process-dependency-links", "--process-dependency-links",
package, package,
] ]
) if not is_venv():
else: arguments.append("--user")
code = subprocess.call( code = subprocess.call(arguments)
[interpreter, "-m", "pip", "install", "-U", "--process-dependency-links", package]
)
if code == 0: if code == 0:
print("Red has been updated") print("Red has been updated")
else: else:
@ -320,7 +324,7 @@ def extras_selector():
return selected return selected
def development_choice(reinstall=False, can_go_back=True): def development_choice(can_go_back=True):
while True: while True:
print("\n") print("\n")
print("Do you want to install stable or development version?") print("Do you want to install stable or development version?")
@ -336,7 +340,6 @@ def development_choice(reinstall=False, can_go_back=True):
selected = extras_selector() selected = extras_selector()
update_red( update_red(
dev=False, dev=False,
reinstall=reinstall,
voice=True if "voice" in selected else False, voice=True if "voice" in selected else False,
docs=True if "docs" in selected else False, docs=True if "docs" in selected else False,
test=True if "test" in selected else False, test=True if "test" in selected else False,
@ -347,7 +350,6 @@ def development_choice(reinstall=False, can_go_back=True):
selected = extras_selector() selected = extras_selector()
update_red( update_red(
dev=True, dev=True,
reinstall=reinstall,
voice=True if "voice" in selected else False, voice=True if "voice" in selected else False,
docs=True if "docs" in selected else False, docs=True if "docs" in selected else False,
test=True if "test" in selected else False, test=True if "test" in selected else False,
@ -453,14 +455,14 @@ def main_menu():
print("0. Back") print("0. Back")
choice = user_choice() choice = user_choice()
if choice == "1": if choice == "1":
if development_choice(reinstall=True): if development_choice():
wait() wait()
elif choice == "2": elif choice == "2":
loop.run_until_complete(reset_red()) loop.run_until_complete(reset_red())
wait() wait()
elif choice == "3": elif choice == "3":
loop.run_until_complete(reset_red()) loop.run_until_complete(reset_red())
development_choice(reinstall=True, can_go_back=False) development_choice(can_go_back=False)
wait() wait()
elif choice == "0": elif choice == "0":
break break