forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Including psutil & setproctitle (ray-project#7031)
- Loading branch information
Showing
17 changed files
with
171 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import argparse | ||
import re | ||
import sys | ||
import tempfile | ||
from pathlib import Path | ||
|
||
exit_with_error = False | ||
|
||
|
||
def check_import(file): | ||
check_to_lines = { | ||
"import ray": -1, | ||
"import psutil": -1, | ||
"import setproctitle": -1 | ||
} | ||
|
||
with open(file) as f: | ||
for i, line in enumerate(f): | ||
for check in check_to_lines.keys(): | ||
if re.match(r"\s+" + check + r"(\s|$)", line): | ||
check_to_lines[check] = i | ||
|
||
for import_lib in ["import psutil", "import setproctitle"]: | ||
if check_to_lines[import_lib] != -1: | ||
import_psutil_line = check_to_lines[import_lib] | ||
import_ray_line = check_to_lines["import ray"] | ||
if import_ray_line == -1 or import_ray_line > import_psutil_line: | ||
print( | ||
"{}:{}".format(str(file), import_psutil_line + 1), | ||
"{} without explicit import ray before it.".format( | ||
import_lib)) | ||
global exit_with_error | ||
exit_with_error = True | ||
|
||
|
||
# Run the test with pytest file_name | ||
def test_check_import(): | ||
global exit_with_error | ||
_, path = tempfile.mkstemp() | ||
|
||
with open(path, "w") as f: | ||
f.write(""" | ||
import psutil | ||
import ray | ||
""") | ||
check_import(path) | ||
assert exit_with_error | ||
exit_with_error = False | ||
|
||
with open(path, "w") as f: | ||
f.write(""" | ||
import psutil | ||
""") | ||
check_import(path) | ||
assert exit_with_error | ||
exit_with_error = False | ||
|
||
with open(path, "w") as f: | ||
f.write(""" | ||
import random_lib | ||
""") | ||
check_import(path) | ||
assert not exit_with_error | ||
exit_with_error = False | ||
|
||
with open(path, "w") as f: | ||
f.write(""" | ||
import setproctitle | ||
import ray | ||
""") | ||
check_import(path) | ||
assert exit_with_error | ||
exit_with_error = False | ||
|
||
with open(path, "w") as f: | ||
f.write(""" | ||
import ray | ||
import psutil | ||
""") | ||
check_import(path) | ||
assert not exit_with_error | ||
exit_with_error = False | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("path", help="File path to check. e.g. '.' or './src'") | ||
parser.add_argument( | ||
"-s", "--skip", action="append", help="Skip certian directory") | ||
args = parser.parse_args() | ||
|
||
file_path = Path(args.path) | ||
if file_path.is_dir(): | ||
all_py_files = file_path.rglob("*.py") | ||
else: | ||
all_py_files = [file_path] | ||
|
||
if args.skip is not None: | ||
filtered_py_files = [] | ||
for py_file in all_py_files: | ||
should_skip = False | ||
for skip_dir in args.skip: | ||
if str(py_file).startswith(skip_dir): | ||
should_skip = True | ||
if not should_skip: | ||
filtered_py_files.append(py_file) | ||
all_py_files = filtered_py_files | ||
|
||
for py_file in all_py_files: | ||
check_import(py_file) | ||
|
||
if exit_with_error: | ||
print("check import ordering failed") | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.