pytorch/tools/code_coverage/package/oss/utils.py
John Reese f625bb4bc9 [codemod][usort] apply import merging for fbcode (1 of 11) (#78973)
Summary:
Applies new import merging and sorting from µsort v1.0.

When merging imports, µsort will make a best-effort to move associated
comments to match merged elements, but there are known limitations due to
the diynamic nature of Python and developer tooling. These changes should
not produce any dangerous runtime changes, but may require touch-ups to
satisfy linters and other tooling.

Note that µsort uses case-insensitive, lexicographical sorting, which
results in a different ordering compared to isort. This provides a more
consistent sorting order, matching the case-insensitive order used when
sorting import statements by module name, and ensures that "frog", "FROG",
and "Frog" always sort next to each other.

For details on µsort's sorting and merging semantics, see the user guide:
https://usort.readthedocs.io/en/stable/guide.html#sorting

Test Plan: S271899

Reviewed By: lisroach

Differential Revision: D36402110

Pull Request resolved: https://github.com/pytorch/pytorch/pull/78973
Approved by: https://github.com/osalpekar
2022-06-06 23:44:28 +00:00

98 lines
3.1 KiB
Python

import os
import subprocess
from typing import List, Optional
from ..util.setting import CompilerType, TestType, TOOLS_FOLDER
from ..util.utils import print_error, remove_file
def get_oss_binary_folder(test_type: TestType) -> str:
assert test_type in {TestType.CPP, TestType.PY}
# TODO: change the way we get binary file -- binary may not in build/bin ?
return os.path.join(
get_pytorch_folder(), "build/bin" if test_type == TestType.CPP else "test"
)
def get_oss_shared_library() -> List[str]:
lib_dir = os.path.join(get_pytorch_folder(), "build", "lib")
return [
os.path.join(lib_dir, lib)
for lib in os.listdir(lib_dir)
if lib.endswith(".dylib")
]
def get_oss_binary_file(test_name: str, test_type: TestType) -> str:
assert test_type in {TestType.CPP, TestType.PY}
binary_folder = get_oss_binary_folder(test_type)
binary_file = os.path.join(binary_folder, test_name)
if test_type == TestType.PY:
# add python to the command so we can directly run the script by using binary_file variable
binary_file = "python " + binary_file
return binary_file
def get_llvm_tool_path() -> str:
return os.environ.get(
"LLVM_TOOL_PATH", "/usr/local/opt/llvm/bin"
) # set default as llvm path in dev server, on mac the default may be /usr/local/opt/llvm/bin
def get_pytorch_folder() -> str:
# TOOLS_FOLDER in oss: pytorch/tools/code_coverage
return os.path.abspath(
os.environ.get(
"PYTORCH_FOLDER", os.path.join(TOOLS_FOLDER, os.path.pardir, os.path.pardir)
)
)
def detect_compiler_type() -> Optional[CompilerType]:
# check if user specifies the compiler type
user_specify = os.environ.get("CXX", None)
if user_specify:
if user_specify in ["clang", "clang++"]:
return CompilerType.CLANG
elif user_specify in ["gcc", "g++"]:
return CompilerType.GCC
raise RuntimeError(f"User specified compiler is not valid {user_specify}")
# auto detect
auto_detect_result = subprocess.check_output(
["cc", "-v"], stderr=subprocess.STDOUT
).decode("utf-8")
if "clang" in auto_detect_result:
return CompilerType.CLANG
elif "gcc" in auto_detect_result:
return CompilerType.GCC
raise RuntimeError(f"Auto detected compiler is not valid {auto_detect_result}")
def clean_up_gcda() -> None:
gcda_files = get_gcda_files()
for item in gcda_files:
remove_file(item)
def get_gcda_files() -> List[str]:
folder_has_gcda = os.path.join(get_pytorch_folder(), "build")
if os.path.isdir(folder_has_gcda):
# TODO use glob
# output = glob.glob(f"{folder_has_gcda}/**/*.gcda")
output = subprocess.check_output(["find", folder_has_gcda, "-iname", "*.gcda"])
return output.decode("utf-8").split("\n")
else:
return []
def run_oss_python_test(binary_file: str) -> None:
# python test script
try:
subprocess.check_call(
binary_file, shell=True, cwd=get_oss_binary_folder(TestType.PY)
)
except subprocess.CalledProcessError:
print_error(f"Binary failed to run: {binary_file}")