Skip to content

LayerDynamics/auto_dep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Auto-Dep

Auto-Dep is a command-line tool and Python package that scans Python projects to automatically detect dependencies. It then generates dependency files such as requirements.txt and environment.yml to facilitate easy installation of third-party packages.

Key Features

  • Scans Python files in a specified directory (recursively) to detect imported modules.
  • Filters out standard library modules to identify third-party packages only.
  • Optionally ignores specific folders or directories, such as virtual environments or build artifacts.
  • Writes output files:
    • requirements.txt (pip-style dependencies)
    • environment.yml (conda-style environment specification)
    • dependencies_summary.txt (optional summary of detected packages)
  • Offers a command-line interface (auto-dep) with various options for customization.

Project Structure (Overview)

auto_dep/
β”œβ”€β”€ auto_dep/
β”‚   β”œβ”€β”€ __init__.py      # Package definition with version
β”‚   β”œβ”€β”€ cli.py           # Command-line interface definitions
β”‚   β”œβ”€β”€ scanner.py       # Scans directories for Python files
β”‚   β”œβ”€β”€ parser.py        # Parses Python files (AST) to detect imports
β”‚   β”œβ”€β”€ filter.py        # Filters out standard library modules
β”‚   └── writer.py        # Writes requirements.txt, environment.yml, or summaries
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ test_cli.py      # Unit tests for CLI
β”‚   β”œβ”€β”€ test_edge_cases.py
β”‚   β”œβ”€β”€ test_filter.py
β”‚   β”œβ”€β”€ test_parser.py
β”‚   β”œβ”€β”€ test_scanner.py
β”‚   └── test_writer.py
└── ...

Installation

  1. Conda Users
    • (Optional) Create a clean environment:
      conda create -n auto-dep python=3.10
      conda activate auto-dep
      
    • Install dependencies using requirements.txt:
      pip install -r requirements.txt
      
    • Alternatively, install dependencies via environment.yml (if present):
      conda env create -f environment.yml
      conda activate autoenv
      
  2. Pip Users
    • Run:
      pip install -r requirements.txt
      

Basic Usage

From the terminal, navigate to the directory containing your Python project or specify it via the --project-dir argument:

auto-dep --project-dir /path/to/python/project

By default, this will:

  • Recursively scan /path/to/python/project for .py files.
  • Parse each .py file for imports.
  • Exclude standard library modules.
  • Write out a requirements.txt and environment.yml file in the same directory (unless overridden).

CLI Arguments

Use auto-dep --help to view all available options. Some of the key arguments include:

--project-dir, -p      Root directory of the Python project (default: current)
--output-dir, -o       Directory to write output files (default: same as project)
--env-name, -e         Environment name for environment.yml (default: autoenv)
--python-version, -v   Python version to use (e.g., 3.8)
--verbose              Enable verbose logging
--quiet                Suppress all output except errors
--confirm, -c          Ask for confirmation before writing files
--skip-environment-yml Skip generating environment.yml
--skip-requirements-txt
                       Skip generating requirements.txt
--generate-summary, -s Generate a dependencies_summary.txt file
--ignore-dir, -i       Additional directories to ignore (repeatable)

How It Works

  1. Scanner

    • Recursively walks through the project directory structure, respecting ignored directories (e.g., .git, __pycache__, node_modules, etc.).
    • Collects all .py files.
  2. Parser

    • For each .py file, uses Python's ast module to parse and identify import statements (import x or from x import y).
    • Skips or safely handles files with syntax errors, encoding issues, etc.
  3. Filter

    • Compares detected imports against lists of standard library modules for the configured Python version.
    • Excludes standard library imports, leaving only third-party packages.
  4. Writer

    • Generates requirements.txt listing sorted package names.
    • Generates environment.yml with a conda environment specification.
    • (Optional) Generates a summary text file with all detected third-party packages.
  5. CLI

    • Manages argument parsing, logging levels, confirmation prompts (--confirm), and orchestrates the scanning, parsing, filtering, and writing processes.

Contributing

Contributions are welcome! Feel free to:

  • Submit pull requests for new features, bug fixes, or improvements.
  • Add or improve test cases in the tests/ directory.
  • Suggest new or updated docstrings and documentation.

Testing

The tests/ folder uses unittest (and can be run via pytest or unittest). For example:

pytest

This executes all test modules, including:

  • test_scanner.py
  • test_parser.py
  • test_filter.py
  • test_writer.py
  • test_cli.py
  • test_edge_cases.py

License

(If applicable, choose an open-source license. Currently no explicit license is provided in the codebase.)


Happy coding with Auto-Dep! If you have questions or issues, please open an issue or submit a pull request.

About

🐍 AST-based Python dependency extractor that analyzes source code to automatically generate pip and conda dependency files. Supports project-wide scanning, stdlib filtering, and customizable output formats.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages