2020-06-11 00:54:34 +02:00
import glob
2016-10-14 00:57:08 +02:00
import os
2020-04-17 09:19:39 +02:00
import subprocess
2019-06-20 18:27:09 +02:00
import sys
2021-03-03 05:00:15 +01:00
from argparse import ArgumentParser
2017-03-26 04:48:25 +02:00
from distutils . version import LooseVersion
2020-06-11 00:54:34 +02:00
from typing import Iterable , List , Optional , Tuple
2017-10-18 05:02:44 +02:00
from scripts . lib . zulip_tools import get_dev_uuid_var_path
2020-06-11 00:54:34 +02:00
from version import PROVISION_VERSION
2016-10-14 00:57:08 +02:00
2020-04-17 09:19:39 +02:00
ZULIP_PATH = os . path . dirname ( os . path . dirname ( os . path . dirname ( os . path . abspath ( __file__ ) ) ) )
2021-02-12 08:19:30 +01:00
2020-04-17 08:46:45 +02:00
def get_major_version ( v : str ) - > int :
2021-02-12 08:20:45 +01:00
return int ( v . split ( " . " ) [ 0 ] )
2016-10-14 00:57:08 +02:00
2021-02-12 08:19:30 +01:00
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
def get_version_file ( ) - > str :
2017-10-18 05:02:44 +02:00
uuid_var_path = get_dev_uuid_var_path ( )
2021-02-12 08:20:45 +01:00
return os . path . join ( uuid_var_path , " provision_version " )
2016-10-14 00:57:08 +02:00
2021-02-12 08:19:30 +01:00
2021-02-12 08:20:45 +01:00
PREAMBLE = """
2016-10-14 00:57:08 +02:00
Before we run tests , we make sure your provisioning version
is correct by looking at var / provision_version , which is at
2020-07-10 01:57:43 +02:00
version { } , and we compare it to the version in source
control ( version . py ) , which is { } .
2021-02-12 08:20:45 +01:00
"""
2016-10-14 00:57:08 +02:00
2021-02-12 08:19:30 +01:00
2020-04-17 08:46:45 +02:00
def preamble ( version : str ) - > str :
2020-07-10 01:57:43 +02:00
text = PREAMBLE . format ( version , PROVISION_VERSION )
2021-02-12 08:20:45 +01:00
text + = " \n "
2016-10-14 00:57:08 +02:00
return text
2021-02-12 08:19:30 +01:00
2021-02-12 08:20:45 +01:00
NEED_TO_DOWNGRADE = """
2016-10-14 00:57:08 +02:00
It looks like you checked out a branch that expects an older
version of dependencies than the version you provisioned last .
This may be ok , but it ' s likely that you either want to rebase
2021-09-01 00:15:31 +02:00
your branch on top of upstream / main or re - provision your VM .
2016-12-10 17:57:41 +01:00
2017-01-14 11:19:26 +01:00
Do this : ` . / tools / provision `
2021-02-12 08:20:45 +01:00
"""
2016-10-14 00:57:08 +02:00
2021-02-12 08:20:45 +01:00
NEED_TO_UPGRADE = """
2016-10-14 00:57:08 +02:00
It looks like you checked out a branch that has added
2016-12-30 00:59:46 +01:00
dependencies beyond what you last provisioned . Your command
is likely to fail until you add dependencies by provisioning .
2016-12-10 17:57:41 +01:00
2017-01-14 11:19:26 +01:00
Do this : ` . / tools / provision `
2021-02-12 08:20:45 +01:00
"""
2016-10-14 00:57:08 +02:00
2021-02-12 08:19:30 +01:00
2020-04-17 08:46:45 +02:00
def get_provisioning_status ( ) - > Tuple [ bool , Optional [ str ] ] :
2016-10-14 00:57:08 +02:00
version_file = get_version_file ( )
if not os . path . exists ( version_file ) :
# If the developer doesn't have a version_file written by
# a previous provision, then we don't do any safety checks
# here on the assumption that the developer is managing
2017-01-14 11:19:26 +01:00
# their own dependencies and not running provision.
2016-10-14 00:57:08 +02:00
return True , None
2020-04-09 21:51:58 +02:00
with open ( version_file ) as f :
2019-07-14 21:37:08 +02:00
version = f . read ( ) . strip ( )
2016-10-14 00:57:08 +02:00
# Normal path for people that provision--we're all good!
if version == PROVISION_VERSION :
return True , None
# We may be more provisioned than the branch we just moved to. As
# long as the major version hasn't changed, then we should be ok.
2017-03-26 04:48:25 +02:00
if LooseVersion ( version ) > LooseVersion ( PROVISION_VERSION ) :
2016-10-14 00:57:08 +02:00
if get_major_version ( version ) == get_major_version ( PROVISION_VERSION ) :
return True , None
else :
return False , preamble ( version ) + NEED_TO_DOWNGRADE
return False , preamble ( version ) + NEED_TO_UPGRADE
2019-06-20 18:27:09 +02:00
2021-03-02 20:59:19 +01:00
def assert_provisioning_status_ok ( skip_provision_check : bool ) - > None :
if not skip_provision_check :
2019-06-20 18:27:09 +02:00
ok , msg = get_provisioning_status ( )
if not ok :
print ( msg )
2021-03-02 20:59:19 +01:00
print (
" If you really know what you are doing, use --skip-provision-check to run anyway. "
)
2019-06-20 18:27:09 +02:00
sys . exit ( 1 )
2020-04-17 08:51:06 +02:00
2021-03-03 05:00:15 +01:00
def add_provision_check_override_param ( parser : ArgumentParser ) - > None :
"""
2021-03-02 20:59:19 +01:00
Registers - - skip - provision - check argument to be used with various commands / tests in our tools .
2021-03-03 05:00:15 +01:00
"""
2021-03-02 20:49:17 +01:00
parser . add_argument (
" --skip-provision-check " ,
action = " store_true " ,
help = " Skip check that provision has been run; useful to save time if you know the dependency changes are not relevant to this command and will not cause it to fail " ,
)
2021-03-03 05:00:15 +01:00
2020-04-17 08:51:06 +02:00
def find_js_test_files ( test_dir : str , files : Iterable [ str ] ) - > List [ str ] :
test_files = [ ]
for file in files :
2021-04-10 11:18:55 +02:00
file = min (
2021-07-18 09:43:07 +02:00
(
os . path . join ( test_dir , file_name )
for file_name in os . listdir ( test_dir )
if file_name . startswith ( file )
) ,
default = file ,
2021-04-10 11:18:55 +02:00
)
2020-04-17 08:51:06 +02:00
test_files . append ( os . path . abspath ( file ) )
if not test_files :
2021-02-20 05:52:06 +01:00
test_files = sorted (
glob . glob ( os . path . join ( test_dir , " *.ts " ) ) + glob . glob ( os . path . join ( test_dir , " *.js " ) )
)
2020-04-17 08:51:06 +02:00
return test_files
2020-04-17 09:19:39 +02:00
2021-02-12 08:19:30 +01:00
2021-03-12 12:30:43 +01:00
def prepare_puppeteer_run ( is_firefox : bool = False ) - > None :
2020-04-17 09:19:39 +02:00
os . chdir ( ZULIP_PATH )
2021-03-12 12:30:43 +01:00
# This will determine if the browser will be firefox or chrome.
os . environ [ " PUPPETEER_PRODUCT " ] = " firefox " if is_firefox else " chrome "
2021-02-12 08:20:45 +01:00
subprocess . check_call ( [ " node " , " node_modules/puppeteer/install.js " ] )
os . makedirs ( " var/puppeteer " , exist_ok = True )
for f in glob . glob ( " var/puppeteer/puppeteer-failure*.png " ) :
2020-04-17 09:19:39 +02:00
os . remove ( f )