# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
#!/usr/bin/env python3
import glob
import os
from glob import glob


SCRIPT_NAME = os.path.basename(__file__)
PYRO_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))

library_files = set()
os.chdir(f"{PYRO_DIR}/library")
for filename in glob("**/*.py", recursive=True):
    library_files.add(filename)

os.chdir(f"{PYRO_DIR}")
frozen_library_files = (
    glob("library/compiler/**/*.py", recursive=True)
    + glob("library/collections/*.py")
    + """
library/_builtins.py
library/_bytecode_utils.py
library/_codecs.py
library/_collections.py
library/_compiler.py
library/_compiler_opcode.py
library/_contextvars.py
library/_ctypes.py
library/_frozen_importlib.py
library/_frozen_importlib_external.py
library/_functools.py
library/_imp.py
library/_io.py
library/_json.py
library/_os.py
library/_path.py
library/_signal.py
library/_str_mod.py
library/_thread.py
library/_valgrind.py
library/_warnings.py
library/_weakref.py
library/array.py
library/builtins.py
library/faulthandler.py
library/gc.py
library/inspect.py
library/itertools.py
library/marshal.py
library/mmap.py
library/opcode.py
library/operator.py
library/sys.py
library/types.py
library/typing.py
library/unicodedata.py
third-party/cpython/Lib/__future__.py
third-party/cpython/Lib/_collections_abc.py
third-party/cpython/Lib/_py_abc.py
third-party/cpython/Lib/_weakrefset.py
third-party/cpython/Lib/abc.py
third-party/cpython/Lib/ast.py
third-party/cpython/Lib/codecs.py
third-party/cpython/Lib/collections/abc.py
third-party/cpython/Lib/contextlib.py
third-party/cpython/Lib/copyreg.py
third-party/cpython/Lib/dis.py
third-party/cpython/Lib/enum.py
third-party/cpython/Lib/functools.py
third-party/cpython/Lib/genericpath.py
third-party/cpython/Lib/heapq.py
third-party/cpython/Lib/importlib/__init__.py
third-party/cpython/Lib/importlib/abc.py
third-party/cpython/Lib/importlib/machinery.py
third-party/cpython/Lib/importlib/resources.py
third-party/cpython/Lib/importlib/util.py
third-party/cpython/Lib/io.py
third-party/cpython/Lib/keyword.py
third-party/cpython/Lib/os.py
third-party/cpython/Lib/posixpath.py
third-party/cpython/Lib/re.py
third-party/cpython/Lib/reprlib.py
third-party/cpython/Lib/runpy.py
third-party/cpython/Lib/sre_compile.py
third-party/cpython/Lib/sre_constants.py
third-party/cpython/Lib/sre_parse.py
third-party/cpython/Lib/stat.py
third-party/cpython/Lib/token.py
third-party/cpython/Lib/tokenize.py
third-party/cpython/Lib/warnings.py
third-party/cpython/Lib/weakref.py
third-party/cpython/Lib/zipimport.py
""".strip().split()
)
for filename in frozen_library_files:
    assert os.path.exists(filename)

cpython_library_files = set()
os.chdir(f"{PYRO_DIR}/third-party/cpython/Lib")
for filename in glob("**/*", recursive=True):
    if os.path.isdir(filename) or "__pycache__/" in filename:
        continue
    if filename in library_files:
        continue
    cpython_library_files.add(filename)

files = [
    (f"{PYRO_DIR}/library_files.cmake", "LIBRARY_FILES", library_files),
    (
        f"{PYRO_DIR}/cpython_library_files.cmake",
        "CPYTHON_LIBRARY_FILES",
        cpython_library_files,
    ),
    (
        f"{PYRO_DIR}/frozen_library_files.cmake",
        "FROZEN_LIBRARY_FILES",
        frozen_library_files,
    ),
]
for filename, define, filelist in files:
    with open(filename, "w") as fp:
        fp.write(
            f"""\
# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
# This file was generated by `util/{SCRIPT_NAME}`.
set({define}
"""
        )
        for filename in sorted(filelist):
            fp.write(filename + "\n")
        fp.write(
            """\
)
"""
        )
