CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pip

The PyPA recommended tool for installing Python packages.

91

1.07x
Overview
Eval results
Files

task.mdevals/scenario-7/

Package Distribution Installer

Build a command-line tool that installs Python packages with different distribution format preferences and reports the installation details.

Background

Package managers provide options to control whether pre-built binary distributions (wheels) or source distributions are used during installation. These options help with reproducible builds, debugging compatibility issues, and optimizing installation speed.

Requirements

Create a Python script package_installer.py that:

  1. Accepts command-line arguments:

    • --package <name>: Package name to install (required)
    • --version <version>: Specific version to install (optional, defaults to latest)
    • --format <format>: Distribution format preference - one of binary-only, source-only, or prefer-binary (required)
    • --target <path>: Target directory for installation (required)
    • --report <path>: Output file path for the installation report (default: install_report.json)
  2. Installs packages with format preferences:

    • Use the package installer to install packages to the specified target directory
    • Apply the appropriate flags based on --format:
      • binary-only: Install only from pre-built binary distributions
      • source-only: Force installation from source distributions
      • prefer-binary: Prefer binary distributions when available
    • Capture whether a binary or source distribution was actually used
  3. Generates a JSON report containing:

    • package: The package name
    • version: The installed version
    • format_preference: The format preference requested
    • installed_from: The distribution type used (wheel or source)
    • install_path: The target installation directory
    • timestamp: ISO 8601 formatted timestamp
  4. Error handling:

    • Exit with status code 1 if required arguments are missing
    • Exit with status code 2 if an invalid format preference is provided
    • Exit with status code 3 if installation fails
    • Print clear error messages to stderr

Test Cases { .test-cases }

Test Case 1: Binary-only installation { .test-case @test }

Input:

python package_installer.py --package wheel --version 0.37.0 --format binary-only --target ./test_install1 --report test1.json

Expected Output File (test1.json):

{
  "package": "wheel",
  "version": "0.37.0",
  "format_preference": "binary-only",
  "installed_from": "wheel",
  "install_path": "./test_install1",
  "timestamp": "2024-01-15T10:30:00Z"
}

Test Case 2: Source-only installation { .test-case @test }

Input:

python package_installer.py --package six --version 1.16.0 --format source-only --target ./test_install2 --report test2.json

Expected Output File (test2.json):

{
  "package": "six",
  "version": "1.16.0",
  "format_preference": "source-only",
  "installed_from": "source",
  "install_path": "./test_install2",
  "timestamp": "2024-01-15T10:31:00Z"
}

Test Case 3: Prefer binary installation { .test-case @test }

Input:

python package_installer.py --package click --format prefer-binary --target ./test_install3

Expected Output File (install_report.json):

{
  "package": "click",
  "version": "8.1.7",
  "format_preference": "prefer-binary",
  "installed_from": "wheel",
  "install_path": "./test_install3",
  "timestamp": "2024-01-15T10:32:00Z"
}

Test Case 4: Invalid format error { .test-case @test }

Input:

python package_installer.py --package requests --format invalid --target ./test_install4

Expected Behavior:

  • Exit with status code 2
  • Print error to stderr: "Error: Invalid format preference 'invalid'"

Implementation Notes

  • Use subprocess to invoke the package installer with appropriate command-line flags
  • Parse the installation output or logs to determine if a wheel or source distribution was used
  • Ensure the target directory exists or create it before installation
  • The version in the report should reflect the actually installed version

Dependencies { .dependencies }

pip { .dependency }

The Python Package Installer - provides package installation and management capabilities.

Deliverables

  • package_installer.py: Main implementation file
  • test_package_installer.py: Test file containing automated tests for the test cases

Install with Tessl CLI

npx tessl i tessl/pypi-pip

tile.json