3 # Utility to generate or verify `php.bzl` from `php.txt`
9 def _read_file_and_gen_bzl(path
: str) -> str:
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/"
16 for line
in f
.readlines():
18 if not line
or line
.startswith("#"):
20 if line
.startswith(prefix
):
21 line
= line
[len(prefix
) :]
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
:
35 def _verify(php_txt
: str, php_bzl
: str):
36 with
open(php_bzl
, mode
="rb") as f
:
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
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
)
53 raise Exception("unknown mode")