black: Reformat skipping string normalization.

This commit is contained in:
PIG208 2021-05-28 17:03:46 +08:00 committed by Tim Abbott
parent 5580c68ae5
commit fba21bb00d
178 changed files with 6562 additions and 4469 deletions

View file

@ -15,32 +15,39 @@ green = '\033[92m'
end_format = '\033[0m'
bold = '\033[1m'
def main():
usage = """./tools/provision
Creates a Python virtualenv. Its Python version is equal to
the Python version this command is executed with."""
parser = argparse.ArgumentParser(usage=usage)
parser.add_argument('--python-interpreter', '-p',
metavar='PATH_TO_PYTHON_INTERPRETER',
default=os.path.abspath(sys.executable),
help='Path to the Python interpreter to use when provisioning.')
parser.add_argument('--force', '-f', action='store_true',
help='create venv even with outdated Python version.')
parser.add_argument(
'--python-interpreter',
'-p',
metavar='PATH_TO_PYTHON_INTERPRETER',
default=os.path.abspath(sys.executable),
help='Path to the Python interpreter to use when provisioning.',
)
parser.add_argument(
'--force', '-f', action='store_true', help='create venv even with outdated Python version.'
)
options = parser.parse_args()
base_dir = os.path.abspath(os.path.join(__file__, '..', '..'))
py_version_output = subprocess.check_output([options.python_interpreter, '--version'],
stderr=subprocess.STDOUT, universal_newlines=True)
py_version_output = subprocess.check_output(
[options.python_interpreter, '--version'], stderr=subprocess.STDOUT, universal_newlines=True
)
# The output has the format "Python 1.2.3"
py_version_list = py_version_output.split()[1].split('.')
py_version = tuple(int(num) for num in py_version_list[0:2])
venv_name = 'zulip-api-py{}-venv'.format(py_version[0])
if py_version <= (3, 1) and (not options.force):
print(red + "Provision failed: Cannot create venv with outdated Python version ({}).\n"
"Maybe try `python3 tools/provision`."
.format(py_version_output.strip()) + end_format)
print(
red + "Provision failed: Cannot create venv with outdated Python version ({}).\n"
"Maybe try `python3 tools/provision`.".format(py_version_output.strip()) + end_format
)
sys.exit(1)
venv_dir = os.path.join(base_dir, venv_name)
@ -48,18 +55,24 @@ the Python version this command is executed with."""
try:
return_code = subprocess.call([options.python_interpreter, '-m', 'venv', venv_dir])
except OSError:
print("{red}Installation with venv failed. Probable errors are: "
"You are on Ubuntu and you haven't installed python3-venv,"
"or you are running an unsupported python version"
"or python is not installed properly{end_format}"
.format(red=red, end_format=end_format))
print(
"{red}Installation with venv failed. Probable errors are: "
"You are on Ubuntu and you haven't installed python3-venv,"
"or you are running an unsupported python version"
"or python is not installed properly{end_format}".format(
red=red, end_format=end_format
)
)
sys.exit(1)
raise
else:
# subprocess.call returns 0 if a script executed successfully
if return_code:
raise OSError("The command `{} -m venv {}` failed. Virtualenv not created!"
.format(options.python_interpreter, venv_dir))
raise OSError(
"The command `{} -m venv {}` failed. Virtualenv not created!".format(
options.python_interpreter, venv_dir
)
)
print("New virtualenv created.")
else:
print("Virtualenv already exists.")
@ -85,10 +98,21 @@ the Python version this command is executed with."""
pip_path = os.path.join(venv_dir, venv_exec_dir, 'pip')
# We first install a modern version of pip that supports --prefix
subprocess.call([pip_path, 'install', 'pip>=9.0'])
if subprocess.call([pip_path, 'install', '--prefix', venv_dir, '-r',
os.path.join(base_dir, requirements_filename)]):
raise OSError("The command `pip install -r {}` failed. Dependencies not installed!"
.format(os.path.join(base_dir, requirements_filename)))
if subprocess.call(
[
pip_path,
'install',
'--prefix',
venv_dir,
'-r',
os.path.join(base_dir, requirements_filename),
]
):
raise OSError(
"The command `pip install -r {}` failed. Dependencies not installed!".format(
os.path.join(base_dir, requirements_filename)
)
)
install_dependencies('requirements.txt')
@ -105,10 +129,7 @@ the Python version this command is executed with."""
print(green + 'Success!' + end_format)
activate_command = os.path.join(base_dir,
venv_dir,
venv_exec_dir,
'activate')
activate_command = os.path.join(base_dir, venv_dir, venv_exec_dir, 'activate')
# We make the path look like a Unix path, because most Windows users
# are likely to be running in a bash shell.
activate_command = activate_command.replace(os.sep, '/')