Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed May 16, 2023
1 parent 22d04fc commit f55d717
Show file tree
Hide file tree
Showing 19 changed files with 1,297 additions and 395 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1731,12 +1731,12 @@
{
"group": "Python",
"command": "python.createEnvironment-button",
"when": "showCreateEnvButton && resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported && !inDiffEditor && pipDepsNotInstalled"
"when": "showCreateEnvButton && resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported && !inDiffEditor && pythonDepsNotInstalled"
},
{
"group": "Python",
"command": "python.createEnvironment-button",
"when": "showCreateEnvButton && resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
"when": "showCreateEnvButton && resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported && !inDiffEditor && pythonDepsNotInstalled"
}
],
"editor/context": [
Expand Down
113 changes: 85 additions & 28 deletions pythonFiles/installed_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
import os
import pathlib
import sys
from typing import Optional, Sequence
from typing import Dict, List, Optional, Sequence, Tuple, Union

LIB_ROOT = pathlib.Path(__file__).parent / "lib" / "python"
sys.path.insert(0, os.fspath(LIB_ROOT))

import tomli
from importlib_metadata import metadata
from packaging.requirements import Requirement

DEFAULT_SEVERITY = 3


def parse_args(argv: Optional[Sequence[str]] = None):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(
description="Check for installed packages against requirements"
)
parser.add_argument(
"REQUIREMENTS", type=str, help="Path to requirements.[txt, in]", nargs="+"
)
parser.add_argument("FILEPATH", type=str, help="Path to requirements.[txt, in]")

return parser.parse_args(argv)

Expand All @@ -39,32 +40,88 @@ def parse_requirements(line: str) -> Optional[Requirement]:
return None


def main():
args = parse_args()
def process_requirements(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]]:
diagnostics = []
for n, line in enumerate(req_file.read_text(encoding="utf-8").splitlines()):
if line.startswith(("#", "-", " ")) or line == "":
continue

req = parse_requirements(line)
if req:
try:
# Check if package is installed
metadata(req.name)
except:
diagnostics.append(
{
"line": n,
"character": 0,
"endLine": n,
"endCharacter": len(req.name),
"package": req.name,
"code": "not-installed",
"severity": DEFAULT_SEVERITY,
}
)
return diagnostics


def get_pos(lines: List[str], text: str) -> Tuple[int, int, int, int]:
for n, line in enumerate(lines):
index = line.find(text)
if index >= 0:
return n, index, n, index + len(text)
return (0, 0, 0, 0)


def process_pyproject(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]]:
diagnostics = []
for req_file in args.REQUIREMENTS:
req_file = pathlib.Path(req_file)
if req_file.exists():
lines = req_file.read_text(encoding="utf-8").splitlines()
for n, line in enumerate(lines):
if line.startswith(("#", "-", " ")) or line == "":
continue

req = parse_requirements(line)
if req:
try:
# Check if package is installed
metadata(req.name)
except:
diagnostics.append(
{
"line": n,
"package": req.name,
"code": "not-installed",
"severity": 3,
}
)
try:
raw_text = req_file.read_text(encoding="utf-8")
pyproject = tomli.loads(raw_text)
except:
return diagnostics

lines = raw_text.splitlines()
reqs = pyproject.get("project", {}).get("dependencies", [])
for raw_req in reqs:
req = parse_requirements(raw_req)
n, start, _, end = get_pos(lines, raw_req)
if req:
try:
# Check if package is installed
metadata(req.name)
except:
diagnostics.append(
{
"line": n,
"character": start,
"endLine": n,
"endCharacter": end,
"package": req.name,
"code": "not-installed",
"severity": DEFAULT_SEVERITY,
}
)
return diagnostics


def get_diagnostics(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]]:
diagnostics = []
if not req_file.exists():
return diagnostics

if req_file.name == "pyproject.toml":
diagnostics = process_pyproject(req_file)
else:
diagnostics = process_requirements(req_file)

return diagnostics


def main():
args = parse_args()
diagnostics = get_diagnostics(pathlib.Path(args.FILEPATH))
print(json.dumps(diagnostics, ensure_ascii=False))


Expand Down
Loading

0 comments on commit f55d717

Please sign in to comment.