2018-07-11 20:41:39 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
import difflib
|
|
|
|
import filecmp
|
|
|
|
import os
|
2018-07-18 23:50:16 +02:00
|
|
|
import shutil
|
2020-06-11 00:54:34 +02:00
|
|
|
import subprocess
|
2019-01-15 02:58:48 +01:00
|
|
|
import tempfile
|
2018-07-11 20:41:39 +02:00
|
|
|
|
2020-04-22 01:19:45 +02:00
|
|
|
TOOLS_DIR = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
|
2018-07-11 20:41:39 +02:00
|
|
|
|
2019-01-15 02:58:48 +01:00
|
|
|
def generate_files(source_file: str, tmp_dir: str) -> None:
|
2018-07-11 20:41:39 +02:00
|
|
|
# Copy the source CSV file to out temporary test directory.
|
|
|
|
input_file_path = source_file
|
2019-01-15 02:58:48 +01:00
|
|
|
output_file_path = os.path.join(tmp_dir, "CSV_A")
|
2018-07-18 23:50:16 +02:00
|
|
|
shutil.copyfile(input_file_path, output_file_path)
|
2018-07-11 20:41:39 +02:00
|
|
|
|
|
|
|
# Generate the name map file in the temporary directory.
|
|
|
|
input_file_path = output_file_path
|
2019-01-15 02:58:48 +01:00
|
|
|
output_file_path = os.path.join(tmp_dir, 'NAME_MAP_A')
|
2018-07-11 20:41:39 +02:00
|
|
|
subprocess.check_call([os.path.join(TOOLS_DIR, 'setup', 'emoji', 'import_emoji_names_from_csv'),
|
|
|
|
'--input-file', input_file_path, '--output-file', output_file_path],
|
|
|
|
stdout=subprocess.DEVNULL)
|
|
|
|
|
|
|
|
# Regenerate the CSV file from name map.
|
|
|
|
input_file_path = output_file_path
|
2019-01-15 02:58:48 +01:00
|
|
|
output_file_path = os.path.join(tmp_dir, 'CSV_B')
|
2018-07-11 20:41:39 +02:00
|
|
|
subprocess.check_call([os.path.join(TOOLS_DIR, 'setup', 'emoji', 'export_emoji_names_to_csv'),
|
|
|
|
'--input-file', input_file_path, '--output-file', output_file_path],
|
|
|
|
stdout=subprocess.DEVNULL)
|
|
|
|
|
|
|
|
# Regenerate the name map file from the regenerated CSV file.
|
|
|
|
input_file_path = output_file_path
|
2019-01-15 02:58:48 +01:00
|
|
|
output_file_path = os.path.join(tmp_dir, 'NAME_MAP_B')
|
2018-07-11 20:41:39 +02:00
|
|
|
subprocess.check_call([os.path.join(TOOLS_DIR, 'setup', 'emoji', 'import_emoji_names_from_csv'),
|
|
|
|
'--input-file', input_file_path, '--output-file', output_file_path],
|
|
|
|
stdout=subprocess.DEVNULL)
|
|
|
|
|
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 print_diff(path_file1: str, path_file2: str) -> None:
|
2018-07-11 20:41:39 +02:00
|
|
|
with open(path_file1) as file1:
|
|
|
|
with open(path_file2) as file2:
|
|
|
|
diff = difflib.unified_diff(
|
|
|
|
file1.readlines(),
|
|
|
|
file2.readlines(),
|
|
|
|
fromfile=path_file1,
|
|
|
|
tofile=path_file2,
|
|
|
|
)
|
|
|
|
for line in diff:
|
|
|
|
print(line)
|
|
|
|
|
|
|
|
def compare_files(first_file: str, second_file: str) -> None:
|
|
|
|
same = True
|
|
|
|
same = same and filecmp.cmp(first_file, second_file, shallow=False)
|
|
|
|
if not same:
|
|
|
|
print_diff(first_file, second_file)
|
|
|
|
raise Exception("Round trip conversion failed!!")
|
|
|
|
|
2019-01-15 02:58:48 +01:00
|
|
|
def check_files(tmp_dir: str) -> None:
|
2018-07-11 20:41:39 +02:00
|
|
|
# Compare the original and regenerated CSV files.
|
2019-01-15 02:58:48 +01:00
|
|
|
first_file = os.path.join(tmp_dir, 'CSV_A')
|
|
|
|
second_file = os.path.join(tmp_dir, 'CSV_B')
|
2018-07-11 20:41:39 +02:00
|
|
|
compare_files(first_file, second_file)
|
|
|
|
|
|
|
|
# Compare the name map files.
|
2019-01-15 02:58:48 +01:00
|
|
|
first_file = os.path.join(tmp_dir, 'NAME_MAP_A')
|
|
|
|
second_file = os.path.join(tmp_dir, 'NAME_MAP_B')
|
2018-07-11 20:41:39 +02:00
|
|
|
compare_files(first_file, second_file)
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
description = ("This tool is used test the emoji tooling that we use to import/export "
|
|
|
|
"naming related data. This works by doing a round-trip conversion of "
|
|
|
|
"and then verifying that no changes were made in the process.")
|
|
|
|
parser = argparse.ArgumentParser(description=description)
|
|
|
|
parser.add_argument(
|
|
|
|
"--input-file", dest="input_file_path", type=str, metavar="<path>",
|
|
|
|
default=os.path.join(TOOLS_DIR, "setup", "emoji", "emoji_names.csv"),
|
|
|
|
help="Path to the CSV file to be used for round-trip conversion.")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2019-01-15 02:58:48 +01:00
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
|
|
generate_files(args.input_file_path, tmp_dir)
|
|
|
|
check_files(tmp_dir)
|
2018-07-11 20:41:39 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|