folly::coro::timeoutNoDiscard
[hiphop-php.git] / hphp / system / php_bzl.py
blob8b831ddc4d873f62817d9100232e10db48f5d7fa
1 #!/usr/bin/env python3
3 # Utility to generate or verify `php.bzl` from `php.txt`
5 import os.path
6 import sys
9 def _read_file_and_gen_bzl(path: str) -> str:
10 r = ""
11 r += f"# This file is {'@'}generated from `php.txt`.\n"
12 r += "# Do not edit manually, run `python3 hphp/system/php_bzl.py` instead.\n"
13 r += "SYSTEMLIB_SRCS = [\n"
14 prefix = "hphp/system/"
15 with open(path) as f:
16 for line in f.readlines():
17 line = line.strip()
18 if not line or line.startswith("#"):
19 continue
20 if line.startswith(prefix):
21 line = line[len(prefix) :]
22 r += f' "{line}",\n'
23 r += "]\n"
24 return r
27 def _generate():
28 php_txt = os.path.dirname(__file__) + "/php.txt"
29 php_bzl = os.path.dirname(__file__) + "/php.bzl"
30 contents = _read_file_and_gen_bzl(php_txt)
31 with open(php_bzl, mode="w") as f:
32 f.write(contents)
35 def _verify(php_txt: str, php_bzl: str):
36 with open(php_bzl, mode="rb") as f:
37 expected = f.read()
38 assert isinstance(expected, bytes)
39 actual = _read_file_and_gen_bzl(php_txt)
40 actual = bytes(actual, "utf-8")
41 assert expected == actual, "file need to be regenerated"
44 if __name__ == "__main__":
45 if len(sys.argv) == 1:
46 # This is to be invoked manually when `php.txt` changes
47 _generate()
48 elif sys.argv[1] == "verify":
49 # This is invoked as Buck test
50 [php_txt, php_bzl] = sys.argv[2:]
51 _verify(php_txt, php_bzl)
52 else:
53 raise Exception("unknown mode")