diff --git a/pythonFiles/create_conda.py b/pythonFiles/create_conda.py index 15320a8a1ce6..4228c108b336 100644 --- a/pythonFiles/create_conda.py +++ b/pythonFiles/create_conda.py @@ -48,17 +48,17 @@ def parse_args(argv: Sequence[str]) -> argparse.Namespace: def file_exists(path: Union[str, pathlib.PurePath]) -> bool: - return os.path.exists(path) + return pathlib.Path.exists(path) def conda_env_exists(name: Union[str, pathlib.PurePath]) -> bool: - return os.path.exists(CWD / name) + return pathlib.Path.exists(CWD / name) def run_process(args: Sequence[str], error_message: str) -> None: try: print("Running: " + " ".join(args)) - subprocess.run(args, cwd=os.getcwd(), check=True) + subprocess.run(args, cwd=CWD, check=True) except subprocess.CalledProcessError: raise VenvError(error_message) @@ -89,11 +89,10 @@ def install_packages(env_path: str) -> None: def add_gitignore(name: str) -> None: - git_ignore = os.fspath(CWD / name / ".gitignore") + git_ignore = CWD / name / ".gitignore" if not file_exists(git_ignore): print(f"Creating: {git_ignore}") - with open(git_ignore, "w") as f: - f.write("*") + git_ignore.write_text("*") def main(argv: Optional[Sequence[str]] = None) -> None: diff --git a/pythonFiles/create_microvenv.py b/pythonFiles/create_microvenv.py index 10eae38ab977..3a305e186dfb 100644 --- a/pythonFiles/create_microvenv.py +++ b/pythonFiles/create_microvenv.py @@ -20,7 +20,7 @@ class MicroVenvError(Exception): def run_process(args: Sequence[str], error_message: str) -> None: try: print("Running: " + " ".join(args)) - subprocess.run(args, cwd=os.getcwd(), check=True) + subprocess.run(args, cwd=CWD, check=True) except subprocess.CalledProcessError: raise MicroVenvError(error_message) diff --git a/pythonFiles/create_venv.py b/pythonFiles/create_venv.py index 092286f986cf..d7355d6ecc9f 100644 --- a/pythonFiles/create_venv.py +++ b/pythonFiles/create_venv.py @@ -71,17 +71,17 @@ def is_installed(module: str) -> bool: def file_exists(path: Union[str, pathlib.PurePath]) -> bool: - return os.path.exists(path) + return pathlib.Path.exists(path) def venv_exists(name: str) -> bool: - return os.path.exists(CWD / name) and file_exists(get_venv_path(name)) + return pathlib.Path.exists(CWD / name) and file_exists(get_venv_path(name)) def run_process(args: Sequence[str], error_message: str) -> None: try: print("Running: " + " ".join(args)) - subprocess.run(args, cwd=os.getcwd(), check=True) + subprocess.run(args, cwd=CWD, check=True) except subprocess.CalledProcessError: raise VenvError(error_message) @@ -129,9 +129,8 @@ def upgrade_pip(venv_path: str) -> None: def add_gitignore(name: str) -> None: git_ignore = CWD / name / ".gitignore" if not file_exists(git_ignore): - print("Creating: " + os.fspath(git_ignore)) - with open(git_ignore, "w") as f: - f.write("*") + print(f"Creating: {git_ignore}") + git_ignore.write_text("*") def download_pip_pyz(name: str): @@ -140,11 +139,9 @@ def download_pip_pyz(name: str): try: with url_lib.urlopen(url) as response: - pip_pyz_path = os.fspath(CWD / name / "pip.pyz") - with open(pip_pyz_path, "wb") as out_file: - data = response.read() - out_file.write(data) - out_file.flush() + pip_pyz_path = CWD / name / "pip.pyz" + pip_pyz_path.write_bytes(response.read()) + except Exception: raise VenvError("CREATE_VENV.DOWNLOAD_PIP_FAILED") diff --git a/pythonFiles/download_get_pip.py b/pythonFiles/download_get_pip.py index b8238d60f261..3a863dc94276 100644 --- a/pythonFiles/download_get_pip.py +++ b/pythonFiles/download_get_pip.py @@ -22,7 +22,7 @@ def _get_package_data(): def _download_and_save(root, version): - root = os.getcwd() if root is None or root == "." else root + root = pathlib.Path.cwd() if root is None or root == "." else root url = f"https://raw.githubusercontent.com/pypa/get-pip/{version}/public/get-pip.py" print(url) with url_lib.urlopen(url) as response: diff --git a/pythonFiles/printEnvVariablesToFile.py b/pythonFiles/printEnvVariablesToFile.py index a4e0d24abbe0..827ac0e89c41 100644 --- a/pythonFiles/printEnvVariablesToFile.py +++ b/pythonFiles/printEnvVariablesToFile.py @@ -2,11 +2,12 @@ # Licensed under the MIT License. import os +import pathlib import sys # Last argument is the target file into which we'll write the env variables line by line. -output_file = sys.argv[-1] +output_file = pathlib.Path(sys.argv[-1]) -with open(output_file, "w") as outfile: +with output_file.open("w") as outfile: for key, val in os.environ.items(): outfile.write(f"{key}={val}\n") diff --git a/pythonFiles/run-jedi-language-server.py b/pythonFiles/run-jedi-language-server.py index 31095121409f..0ced77afa759 100644 --- a/pythonFiles/run-jedi-language-server.py +++ b/pythonFiles/run-jedi-language-server.py @@ -1,9 +1,14 @@ -import sys +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import os +import sys +from pathlib import Path + # Add the lib path to our sys path so jedi_language_server can find its references -EXTENSION_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -sys.path.insert(0, os.path.join(EXTENSION_ROOT, "pythonFiles", "lib", "jedilsp")) +EXTENSION_ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, os.fspath(EXTENSION_ROOT / "pythonFiles" / "lib" / "jedilsp")) from jedi_language_server.cli import cli diff --git a/pythonFiles/shell_exec.py b/pythonFiles/shell_exec.py index 4987399a53ea..c7a970dfcdc3 100644 --- a/pythonFiles/shell_exec.py +++ b/pythonFiles/shell_exec.py @@ -3,6 +3,7 @@ import subprocess import sys +from pathlib import Path # This is a simple solution to waiting for completion of commands sent to terminal. # 1. Intercept commands send to a terminal @@ -11,12 +12,12 @@ # 4. Calling code monitors the contents of the file to determine state of execution. # Last argument is a file that's used for synchronizing the actions in the terminal with the calling code in extension. -lock_file = sys.argv[-1] +lock_file = Path(sys.argv[-1]) shell_args = sys.argv[1:-1] print("Executing command in shell >> " + " ".join(shell_args)) -with open(lock_file, "w") as fp: +with lock_file.open("w") as fp: try: # Signal start of execution. fp.write("START\n") @@ -36,7 +37,8 @@ fp.flush() try: # ALso log the error for use from the other side. - with open(lock_file + ".error", "w") as fpError: + error_path = lock_file.with_suffix(".error") + with error_path.open("w") as fpError: fpError.write(traceback.format_exc()) except Exception: pass diff --git a/pythonFiles/testing_tools/run_adapter.py b/pythonFiles/testing_tools/run_adapter.py index 1eeef194f8f5..b47604c22782 100644 --- a/pythonFiles/testing_tools/run_adapter.py +++ b/pythonFiles/testing_tools/run_adapter.py @@ -2,17 +2,11 @@ # Licensed under the MIT License. # Replace the "." entry. -import os.path +import os import sys +from pathlib import Path -sys.path.insert( - 1, - os.path.dirname( # pythonFiles - os.path.dirname( # pythonFiles/testing_tools - os.path.abspath(__file__) # this file - ) - ), -) +sys.path.insert(1, os.fspath(Path(__file__).resolve().parent.parent)) from testing_tools.adapter.__main__ import parse_args, main diff --git a/pythonFiles/tests/__init__.py b/pythonFiles/tests/__init__.py index 4f762cd1f81a..e2b66ad4edae 100644 --- a/pythonFiles/tests/__init__.py +++ b/pythonFiles/tests/__init__.py @@ -1,11 +1,12 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -import os.path +import os +from pathlib import Path -TEST_ROOT = os.path.dirname(__file__) -SRC_ROOT = os.path.dirname(TEST_ROOT) -PROJECT_ROOT = os.path.dirname(SRC_ROOT) -TESTING_TOOLS_ROOT = os.path.join(SRC_ROOT, "testing_tools") -DEBUG_ADAPTER_ROOT = os.path.join(SRC_ROOT, "debug_adapter") +TEST_ROOT = os.fspath(Path(__file__).resolve().parent) +SRC_ROOT = os.fspath(Path(TEST_ROOT).parent) +PROJECT_ROOT = os.fspath(Path(SRC_ROOT).parent) +TESTING_TOOLS_ROOT = os.fspath(Path(SRC_ROOT) / "testing_tools") +DEBUG_ADAPTER_ROOT = os.fspath(Path(SRC_ROOT) / "debug_adapter") -PYTHONFILES = os.path.join(SRC_ROOT, "lib", "python") +PYTHONFILES = os.fspath(Path(SRC_ROOT) / "lib" / "python") diff --git a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py index 2d86710e776b..9d3855d3bfa3 100644 --- a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py +++ b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py @@ -1,4 +1,8 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import os +from pathlib import Path from .helpers import TEST_DATA_PATH, find_test_line_number, get_absolute_test_id @@ -845,10 +849,10 @@ "children": [ { "name": "test_a_function", - "path": os.fspath(os.path.join(tests_path, "test_a.py")), + "path": os.fspath(tests_path / "test_a.py"), "lineno": find_test_line_number( "test_a_function", - os.path.join(tests_path, "test_a.py"), + os.fspath(tests_path / "test_a.py"), ), "type_": "test", "id_": get_absolute_test_id( @@ -868,10 +872,10 @@ "children": [ { "name": "test_b_function", - "path": os.fspath(os.path.join(tests_path, "test_b.py")), + "path": os.fspath(tests_path / "test_b.py"), "lineno": find_test_line_number( "test_b_function", - os.path.join(tests_path, "test_b.py"), + os.fspath(tests_path / "test_b.py"), ), "type_": "test", "id_": get_absolute_test_id( diff --git a/pythonFiles/tests/pytestadapter/helpers.py b/pythonFiles/tests/pytestadapter/helpers.py index 2d36da59956b..e011ad8ad474 100644 --- a/pythonFiles/tests/pytestadapter/helpers.py +++ b/pythonFiles/tests/pytestadapter/helpers.py @@ -10,7 +10,7 @@ import sys import threading import uuid -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Dict, List, Optional, Tuple, Union script_dir = pathlib.Path(__file__).parent.parent.parent sys.path.append(os.fspath(script_dir)) @@ -195,7 +195,9 @@ def _run_test_code( return result -def find_test_line_number(test_name: str, test_file_path) -> str: +def find_test_line_number( + test_name: str, test_file_path: Union[str, pathlib.Path] +) -> str: """Function which finds the correct line number for a test by looking for the "test_marker--[test_name]" string. The test_name is split on the "[" character to remove the parameterization information. @@ -205,9 +207,9 @@ def find_test_line_number(test_name: str, test_file_path) -> str: test_file_path: The path to the test file where the test is located. """ test_file_unique_id: str = "test_marker--" + test_name.split("[")[0] - with open(test_file_path) as f: - for i, line in enumerate(f): - if test_file_unique_id in line: - return str(i + 1) + lines = pathlib.Path(test_file_path).read_text().splitlines() + for i, line in enumerate(lines): + if test_file_unique_id in line: + return str(i + 1) error_str: str = f"Test {test_name!r} not found on any line in {test_file_path}" raise ValueError(error_str) diff --git a/pythonFiles/tests/run_all.py b/pythonFiles/tests/run_all.py index ce5a62649962..12b19e048a41 100644 --- a/pythonFiles/tests/run_all.py +++ b/pythonFiles/tests/run_all.py @@ -2,10 +2,11 @@ # Licensed under the MIT License. # Replace the "." entry. -import os.path +import os import sys +from pathlib import Path -sys.path[0] = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path[0] = os.fspath(Path(__file__).resolve().parent.parent) from tests.__main__ import main, parse_args diff --git a/pythonFiles/tests/test_installed_check.py b/pythonFiles/tests/test_installed_check.py index dae019359e08..8672c8c86c3b 100644 --- a/pythonFiles/tests/test_installed_check.py +++ b/pythonFiles/tests/test_installed_check.py @@ -20,13 +20,12 @@ def generate_file(base_file: pathlib.Path): basename = "pyproject.toml" if "pyproject" in base_file.name else "requirements.txt" fullpath = base_file.parent / basename - if fullpath.exists(): - os.unlink(os.fspath(fullpath)) + fullpath.unlink(missing_ok=True) fullpath.write_text(base_file.read_text(encoding="utf-8")) try: yield fullpath finally: - os.unlink(str(fullpath)) + fullpath.unlink(missing_ok=True) def run_on_file( diff --git a/pythonFiles/unittestadapter/discovery.py b/pythonFiles/unittestadapter/discovery.py index db06004e02c9..6ff33f11a60c 100644 --- a/pythonFiles/unittestadapter/discovery.py +++ b/pythonFiles/unittestadapter/discovery.py @@ -76,9 +76,9 @@ def discover_tests( "status": "error", } """ - cwd = os.path.abspath(start_dir) + cwd = os.fspath(pathlib.Path(start_dir).resolve()) if "/" in start_dir: # is a subdir - parent_dir = os.path.dirname(start_dir) + parent_dir = os.fspath(pathlib.Path(start_dir).parent) sys.path.insert(0, parent_dir) else: sys.path.insert(0, cwd) @@ -95,7 +95,7 @@ def discover_tests( top_level_dir = start_dir # Get abspath of top level directory for build_test_tree. - top_level_dir = os.path.abspath(top_level_dir) + top_level_dir = os.fspath(pathlib.Path(top_level_dir).resolve()) tests, error = build_test_tree( suite, top_level_dir diff --git a/pythonFiles/unittestadapter/execution.py b/pythonFiles/unittestadapter/execution.py index 22451c25bf1f..46dd95d68e2d 100644 --- a/pythonFiles/unittestadapter/execution.py +++ b/pythonFiles/unittestadapter/execution.py @@ -171,7 +171,7 @@ def run_tests( failfast: Optional[bool], locals: Optional[bool] = None, ) -> PayloadDict: - cwd = os.path.abspath(start_dir) + cwd = os.fspath(pathlib.Path(start_dir).resolve()) status = TestExecutionStatus.error error = None payload: PayloadDict = {"cwd": cwd, "status": status, "result": None} @@ -180,8 +180,8 @@ def run_tests( # If it's a file, split path and file name. start_dir = cwd if cwd.endswith(".py"): - start_dir = os.path.dirname(cwd) - pattern = os.path.basename(cwd) + start_dir = pathlib.Path(cwd).parent + pattern = pathlib.Path(cwd).name # Discover tests at path with the file name as a pattern (if any). loader = unittest.TestLoader() @@ -232,7 +232,7 @@ def run_tests( def send_run_data(raw_data, port, uuid): status = raw_data["outcome"] - cwd = os.path.abspath(START_DIR) + cwd = os.fspath(pathlib.Path(START_DIR).resolve()) if raw_data["subtest"]: test_id = raw_data["subtest"] else: @@ -349,7 +349,7 @@ def post_response( locals, ) else: - cwd = os.path.abspath(start_dir) + cwd = os.fspath(pathlib.Path(start_dir).resolve()) status = TestExecutionStatus.error payload: PayloadDict = { "cwd": cwd, diff --git a/pythonFiles/unittestadapter/pvsc_utils.py b/pythonFiles/unittestadapter/pvsc_utils.py index 5632e69b09c7..323b9a551b71 100644 --- a/pythonFiles/unittestadapter/pvsc_utils.py +++ b/pythonFiles/unittestadapter/pvsc_utils.py @@ -166,7 +166,7 @@ def build_test_tree( components = test_id.split(".") class_name = f"{components[-1]}.py" # Find/build class node. - file_path = os.fsdecode(os.path.join(directory_path, class_name)) + file_path = os.fsdecode(directory_path / class_name) current_node = get_child_node( class_name, file_path, TestNodeTypeEnum.file, root ) diff --git a/pythonFiles/vscode_datascience_helpers/tests/logParser.py b/pythonFiles/vscode_datascience_helpers/tests/logParser.py index e021853fee7a..697368a6ef09 100644 --- a/pythonFiles/vscode_datascience_helpers/tests/logParser.py +++ b/pythonFiles/vscode_datascience_helpers/tests/logParser.py @@ -44,8 +44,8 @@ def printTestOutput(testlog): def splitByPid(testlog): # Split testlog into prefixed logs based on pid - baseFile = os.path.splitext(testlog[0])[0] p = Path(testlog[0]) + baseFile = p.parent / p.stem pids = set() logs = {} pid = None @@ -64,7 +64,7 @@ def splitByPid(testlog): # See if we've created a log for this pid or not if pid not in pids: pids.add(pid) - logFile = "{}_{}.log".format(baseFile, pid) + logFile = f"{os.fspath(baseFile)}_{pid}.log" print("Writing to new log: " + logFile) logs[pid] = Path(logFile).open(mode="w") diff --git a/pythonFiles/vscode_pytest/run_pytest_script.py b/pythonFiles/vscode_pytest/run_pytest_script.py index 8005f3a32c0e..9d712cdd930e 100644 --- a/pythonFiles/vscode_pytest/run_pytest_script.py +++ b/pythonFiles/vscode_pytest/run_pytest_script.py @@ -21,7 +21,7 @@ # Add the root directory to the path so that we can import the plugin. directory_path = pathlib.Path(__file__).parent.parent sys.path.append(os.fspath(directory_path)) - sys.path.insert(0, os.getcwd()) + sys.path.insert(0, os.fspath(pathlib.Path.cwd())) # Get the rest of the args to run with pytest. args = sys.argv[1:] run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT") diff --git a/src/test/pythonFiles/debugging/wait_for_file.py b/src/test/pythonFiles/debugging/wait_for_file.py index 72dc90bda61e..7565e40e0c9f 100644 --- a/src/test/pythonFiles/debugging/wait_for_file.py +++ b/src/test/pythonFiles/debugging/wait_for_file.py @@ -1,35 +1,34 @@ -import os.path import sys import time - +from pathlib import Path try: _, filename = sys.argv except ValueError: _, filename, outfile = sys.argv - sys.stdout = open(outfile, 'w') -print('waiting for file {!r}'.format(filename)) + sys.stdout = Path(outfile).open("w") +print("waiting for file {!r}".format(filename)) # We use sys.stdout.write() instead of print() because Python 2... -if not os.path.exists(filename): +if not Path.exists(filename): time.sleep(0.1) - sys.stdout.write('.') + sys.stdout.write(".") sys.stdout.flush() i = 1 -while not os.path.exists(filename): +while not Path.exists(filename): time.sleep(0.1) if i % 10 == 0: - sys.stdout.write(' ') + sys.stdout.write(" ") if i % 600 == 0: if i == 600: - sys.stdout.write('\n = 1 minute =\n') + sys.stdout.write("\n = 1 minute =\n") else: - sys.stdout.write('\n = {} minutes =\n'.format(i // 600)) + sys.stdout.write("\n = {} minutes =\n".format(i // 600)) elif i % 100 == 0: - sys.stdout.write('\n') - sys.stdout.write('.') + sys.stdout.write("\n") + sys.stdout.write(".") sys.stdout.flush() i += 1 -print('\nfound file {!r}'.format(filename)) -print('done!') +print("\nfound file {!r}".format(filename)) +print("done!")