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 = """
test_script: Reword provision warning not to assume running tests.
Previously, running `./tools/run-dev.py` when provision was required
would lead to a warning along the lines of:
```
Before we run tests, we make sure your provisioning version
is correct by looking at var/provision_version, which is at
version 165.1, and we compare it to the version in source
control (version.py), which is 165.2.
It looks like you checked out a branch that has added
dependencies beyond what you last provisioned. Your command
is likely to fail until you add dependencies by provisioning.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
The assumption that we're trying to run tests might cause some
confusion, especially if its the first time you're seeing the
provision warning. Hence, we reword the first paragraph to avoid
making that assumption.
The second paragraph has also been slightly altered, since (1) it's
possible that we didn't checkout a different branch, but eg just
rebased with upstream and (2) we might not be on a VM.
The warning you'd get after this commit would be along the lines of:
```
Provisioning state check failed! This check compares
`var/provision_version` (currently 165.2) to the version in
source control (`version.py`), which is 164.6, to see if you
likely need to provision before this command can run
properly.
The branch you are currently on 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 your
branch on top of upstream/main or re-provision your machine.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
or along the lines of:
```
Provisioning state check failed! This check compares
`var/provision_version` (currently 165.2) to the version in
source control (`version.py`), which is 167.2, to see if you
likely need to provision before this command can run
properly.
The branch you are currently on has added dependencies beyond
what you last provisioned. Your command is likely to fail
until you add dependencies by provisioning.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
2021-11-16 06:53:07 +01:00
Provisioning state check failed ! This check compares
` var / provision_version ` ( currently { } ) to the version in
source control ( ` version . py ` ) , which is { } , to see if you
likely need to provision before this command can run
properly .
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 = """
test_script: Reword provision warning not to assume running tests.
Previously, running `./tools/run-dev.py` when provision was required
would lead to a warning along the lines of:
```
Before we run tests, we make sure your provisioning version
is correct by looking at var/provision_version, which is at
version 165.1, and we compare it to the version in source
control (version.py), which is 165.2.
It looks like you checked out a branch that has added
dependencies beyond what you last provisioned. Your command
is likely to fail until you add dependencies by provisioning.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
The assumption that we're trying to run tests might cause some
confusion, especially if its the first time you're seeing the
provision warning. Hence, we reword the first paragraph to avoid
making that assumption.
The second paragraph has also been slightly altered, since (1) it's
possible that we didn't checkout a different branch, but eg just
rebased with upstream and (2) we might not be on a VM.
The warning you'd get after this commit would be along the lines of:
```
Provisioning state check failed! This check compares
`var/provision_version` (currently 165.2) to the version in
source control (`version.py`), which is 164.6, to see if you
likely need to provision before this command can run
properly.
The branch you are currently on 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 your
branch on top of upstream/main or re-provision your machine.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
or along the lines of:
```
Provisioning state check failed! This check compares
`var/provision_version` (currently 165.2) to the version in
source control (`version.py`), which is 167.2, to see if you
likely need to provision before this command can run
properly.
The branch you are currently on has added dependencies beyond
what you last provisioned. Your command is likely to fail
until you add dependencies by provisioning.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
2021-11-16 06:53:07 +01:00
The branch you are currently on 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 your
branch on top of upstream / main or re - provision your machine .
2016-12-10 17:57:41 +01:00
2022-04-17 14:31:08 +02:00
Don ' t worry we are running provision for you `./tools/provision`
If you wish to ignore auto provision use ` - - skip - provision - check ` .
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 = """
test_script: Reword provision warning not to assume running tests.
Previously, running `./tools/run-dev.py` when provision was required
would lead to a warning along the lines of:
```
Before we run tests, we make sure your provisioning version
is correct by looking at var/provision_version, which is at
version 165.1, and we compare it to the version in source
control (version.py), which is 165.2.
It looks like you checked out a branch that has added
dependencies beyond what you last provisioned. Your command
is likely to fail until you add dependencies by provisioning.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
The assumption that we're trying to run tests might cause some
confusion, especially if its the first time you're seeing the
provision warning. Hence, we reword the first paragraph to avoid
making that assumption.
The second paragraph has also been slightly altered, since (1) it's
possible that we didn't checkout a different branch, but eg just
rebased with upstream and (2) we might not be on a VM.
The warning you'd get after this commit would be along the lines of:
```
Provisioning state check failed! This check compares
`var/provision_version` (currently 165.2) to the version in
source control (`version.py`), which is 164.6, to see if you
likely need to provision before this command can run
properly.
The branch you are currently on 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 your
branch on top of upstream/main or re-provision your machine.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
or along the lines of:
```
Provisioning state check failed! This check compares
`var/provision_version` (currently 165.2) to the version in
source control (`version.py`), which is 167.2, to see if you
likely need to provision before this command can run
properly.
The branch you are currently on has added dependencies beyond
what you last provisioned. Your command is likely to fail
until you add dependencies by provisioning.
Do this: `./tools/provision`
If you really know what you are doing, use --skip-provision-check to
run anyway.
```
2021-11-16 06:53:07 +01:00
The branch you are currently on has added 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
2022-04-17 14:31:08 +02:00
Don ' t worry we are running provision for you `./tools/provision`
If you wish to ignore auto provision use ` - - skip - provision - check ` .
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 )
2022-04-17 14:31:08 +02:00
p = subprocess . Popen (
[ " ./tools/provision " ] ,
stdout = subprocess . PIPE ,
stderr = subprocess . PIPE ,
universal_newlines = True ,
2021-03-02 20:59:19 +01:00
)
2022-04-17 14:31:08 +02:00
output , _ = p . communicate ( )
if p . returncode != 0 :
print ( f " Running provision failed, here are the details \n Output: { output } " )
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 :
2022-03-04 07:32:14 +01: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 )
) ,
2022-03-04 07:32:14 +01:00
default = file ,
2021-04-10 11:18:55 +02:00
)
2022-03-02 05:33:40 +01:00
2022-03-04 07:32:14 +01:00
if not os . path . isfile ( file ) :
2022-03-02 05:33:40 +01:00
raise Exception ( f " Cannot find a matching file for ' { file } ' in ' { test_dir } ' " )
2022-03-04 07:32:14 +01:00
test_files . append ( os . path . abspath ( file ) )
2020-04-17 08:51:06 +02:00
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 )