Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.venv
.idea

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
Expand Down
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"version": "0.2.0",
"configurations": [

{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
{
"name": "Pyright CLI",
"type": "node",
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"editor.formatOnSave": true
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"typescript.tsdk": "node_modules/typescript/lib"
}
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Start from a Node.js base image
FROM node:16-alpine

# Install necessary dependencies for building Python
RUN apk add --no-cache \
git \
build-base \
openssl-dev \
zlib-dev \
bzip2-dev \
readline-dev \
sqlite-dev \
xz-dev \
tk-dev \
libffi-dev \
ncurses-dev \
linux-headers

# Download, extract, and install Python 3.12
RUN wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tar.xz && \
tar -xf Python-3.12.0.tar.xz && \
cd Python-3.12.0 && \
./configure --enable-optimizations && \
make -j$(nproc) && \
make install && \
cd .. && \
rm -rf Python-3.12.0 Python-3.12.0.tar.xz && \
ln -sf /usr/local/bin/python3.12 /usr/local/bin/python && \
ln -sf /usr/local/bin/pip3.12 /usr/local/bin/pip

# Upgrade pip
RUN pip3.12 install --upgrade pip

# Add your required files
ADD packages/pyright-scip/sourcegraph-scip-python-0.6.0.tgz /

RUN npm --prefix /package install
COPY index.py /

WORKDIR /projects/data

# Set entrypoint
ENTRYPOINT ["python", "/index.py"]
6 changes: 0 additions & 6 deletions Dockerfile.autoindex

This file was deleted.

84 changes: 84 additions & 0 deletions index-old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import argparse
import subprocess
from pathlib import Path
import tomllib # Use tomllib for Python 3.11+, or install toml for older versions


def matches_pattern(package, patterns):
"""Check if a package matches any of the specified patterns."""
return any(pattern.lower() in package.lower() for pattern in patterns)


def install_package(package_with_constraints):
"""Attempt to install a package with constraints using pip."""
print(f"Installing package: {package_with_constraints}")
try:
subprocess.run(["pip", "install", package_with_constraints], check=True)
except subprocess.CalledProcessError:
print(f"Failed to install {package_with_constraints}, continuing...")


def extract_dependencies(data, patterns):
"""Recursively search for dependencies in nested data structures."""
if isinstance(data, list):
for item in data:
# Match package names in a list
if isinstance(item, str):
package_name = item.split(" ", 1)[0]
if matches_pattern(package_name, patterns):
yield item
elif isinstance(data, dict):
for key, value in data.items():
# Recurse into dictionaries
yield from extract_dependencies(value, patterns)


def process_pyproject_file(pyproject_file, patterns):
"""Process a pyproject.toml file and install matching packages."""
print(f"Processing pyproject.toml: {pyproject_file}")
with open(pyproject_file, "rb") as file:
pyproject_data = tomllib.load(file)

# Extract all dependencies recursively
for dependency in extract_dependencies(pyproject_data, patterns):
install_package(dependency)


def process_requirements_file(req_file, patterns):
"""Process a requirements file and install matching packages."""
print(f"Processing file: {req_file}")
with open(req_file, "r") as file:
for line in file:
package = line.strip()
# Ignore comments and empty lines
if not package or package.startswith("#"):
continue
# Install only if package matches any of the patterns
if matches_pattern(package, patterns):
install_package(package)


def main(index_name, patterns):
"""Main function to process files and run SCIP indexing command."""
# Process requirements-like files
for req_file in Path(".").rglob("requirements*.txt"):
process_requirements_file(req_file, patterns)

# Process pyproject.toml files
for pyproject_file in Path(".").rglob("pyproject.toml"):
process_pyproject_file(pyproject_file, patterns)

# Run the SCIP indexing command
print("Running SCIP indexing command...")
subprocess.run(["node", "/package/index.js", "index", ".",
"--project-version=0.1.0", f"--output={index_name}"], check=True)


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Install specific packages from requirements and pyproject.toml files")
parser.add_argument("index_name", help="The index name for SCIP indexing command")
parser.add_argument("patterns", nargs="+", help="Patterns to match package names (e.g., 'flask')")
args = parser.parse_args()

# Run main with index name and patterns
main(args.index_name, args.patterns)
195 changes: 195 additions & 0 deletions index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import argparse
import subprocess
import os
import venv
from pathlib import Path
import tomllib # Use tomllib for Python 3.11+, or install toml for older versions


def matches_pattern(package, patterns):
"""Check if a package matches any of the specified patterns."""
return any(pattern.lower() in package.lower() for pattern in patterns)



def setup_virtual_environment(project_path):
"""Create a virtual environment in the specified directory."""
venv_path = project_path / '.venv'
print(f"Creating virtual environment at: {venv_path}")
venv.create(venv_path, with_pip=True)
# subprocess.run([sys.executable, '-m', 'venv', str(venv_path)], check=True)
return venv_path

def activate_virtual_environment(venv_path):
"""Activate the virtual environment by adjusting the environment variables."""
venv_bin = venv_path / 'bin'
os.environ['VIRTUAL_ENV'] = str(venv_path)
os.environ['PATH'] = f"{venv_bin}:{os.environ['PATH']}"
print(f"Virtual environment activated: {os.environ['VIRTUAL_ENV']}")
print(f"Updated PATH: {os.environ['PATH']}")
return venv_bin / 'python'

def install_package(package_with_constraints, venv_python):
"""Attempt to install a package with constraints using pip within the virtual environment."""
print(f"Installing package: {package_with_constraints}")
try:
result = subprocess.run(
[venv_python, "-m", "pip", "install", package_with_constraints],
check=True,
capture_output=True,
text=True,
)
print(result.stdout)
print(result.stderr)
except subprocess.CalledProcessError as e:
print(f"Failed to install {package_with_constraints}. Error: {e.stderr}")


def extract_dependencies(data, patterns):
"""Recursively search for dependencies in nested data structures."""
if isinstance(data, list):
for item in data:
# Match package names in a list
if isinstance(item, str):
if matches_pattern(item, patterns):
yield item
elif isinstance(data, dict):
for key, value in data.items():
yield from extract_dependencies(key, patterns)
# Recurse into dictionaries
yield from extract_dependencies(value, patterns)
elif isinstance(data, str):
if matches_pattern(data, patterns):
yield data


def process_pyproject_file(pyproject_file, patterns, venv_python):
"""Process a pyproject.toml file and install matching packages."""
print(f"Processing pyproject.toml: {pyproject_file}")
with open(pyproject_file, "rb") as file:
pyproject_data = tomllib.load(file)

# Extract all dependencies recursively
for dependency in extract_dependencies(pyproject_data, patterns):
install_package(dependency, venv_python)


def _iter_requirements_lines(req_file: Path, visited: set[Path]):
req_file = req_file.resolve()
if req_file in visited:
return
visited.add(req_file)

print(f"Processing file: {req_file}")

try:
# Try UTF-8 first, fallback to latin-1 to avoid crash
try:
data = req_file.read_text(encoding="utf-8")
except UnicodeDecodeError:
print(f"⚠️ {req_file} is not UTF-8. Falling back to latin-1.")
data = req_file.read_text(encoding="latin-1")

for raw in data.splitlines():
line = raw.strip()
if not line or line.startswith("#"):
continue

# Remove inline comments unless it's part of a URL
if "#" in line and not line.lower().startswith(("http://", "https://")):
line = line.split("#", 1)[0].strip()
if not line:
continue

# -r / --requirement include
if line.startswith("-r ") or line.startswith("--requirement "):
parts = line.split(maxsplit=1)
if len(parts) == 2:
include_path = parts[1].strip().strip("'\"")
include_file = (req_file.parent / include_path).resolve()
if include_file.exists():
yield from _iter_requirements_lines(include_file, visited)
else:
print(f"Included file not found: {include_file}")
continue

# Skip constraints
if line.startswith("-c ") or line.startswith("--constraint "):
continue

yield line

except FileNotFoundError:
print(f"⚠️ Requirements file not found: {req_file}")



def process_requirements_file(req_file: Path, patterns, venv_python):
"""
Process a requirements file, following -r includes recursively.
Only install specs that match any of the provided patterns.
"""
visited: set[Path] = set()
for spec in _iter_requirements_lines(req_file, visited):
if matches_pattern(spec, patterns):
install_package(spec, venv_python)


def process_project(path, patterns):
"""Process the project: set up venv, install packages, and run Node.js script."""
project_path = Path(path).resolve()
venv_path = setup_virtual_environment(project_path)
venv_python = activate_virtual_environment(venv_path)

# Copy the current environment and update it for the virtual environment
env = os.environ.copy()

try:
# Install a test package or requirement to verify pip functionality
print("Verifying pip functionality in the virtual environment...")
subprocess.run(
[venv_python, "-m", "pip", "--version"],
check=True,
capture_output=True,
env=env,
)

# Process requirements-like files
for req_file in Path(path).rglob("requirements*.txt"):
process_requirements_file(req_file, patterns, venv_python)

# Process pyproject.toml files
for pyproject_file in Path(path).rglob("pyproject.toml"):
process_pyproject_file(pyproject_file, patterns, venv_python)

# Run the indexer
print("Running SCIP indexing command...")
result = subprocess.run(
["node", "/package/index.js", "index", ".", "--project-version=0.1.0", "--output=python.scip"],
cwd=path,
env=env,
check=True,
capture_output=True,
text=True,
)
print("Indexer output:")
print(result.stdout)
print(result.stderr)
except subprocess.CalledProcessError as e:
print(f"Command '{e.cmd}' failed with exit code {e.returncode}. Error output:\n{e.stderr}\nStandard output:\n{e.stdout}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
print("Deactivating virtual environment.")
os.environ.pop('VIRTUAL_ENV', None)
os.environ['PATH'] = os.environ['PATH'].split(":", 1)[1]


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Install specific packages from requirements and pyproject.toml files")
parser.add_argument("path", help="Relative path to project")
parser.add_argument("patterns", nargs="+", help="Patterns to match package names (e.g., 'flask')")
args = parser.parse_args()

# Run main with index name and patterns
process_project(args.path, args.patterns)
3 changes: 3 additions & 0 deletions index.scip
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

C
scip-python0.6.0)file:///Users/oinger/Projects/scip-python 
1 change: 1 addition & 0 deletions packages/pyright-internal/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/pyright-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"build": "tsc",
"clean": "shx rm -rf ./dist ./out",
"test": "jest --forceExit",
"typecheck": "tsc --noEmit",
"test:coverage": "jest --forceExit --reporters=jest-junit --reporters=default --coverage --coverageReporters=cobertura --coverageReporters=html --coverageReporters=json"
},
"dependencies": {
Expand Down
Loading