GithubHelp home page GithubHelp logo

cloud-custodian / poetry-plugin-freeze Goto Github PK

View Code? Open in Web Editor NEW
20.0 9.0 3.0 89 KB

poetry plugin to freeze dependency versions in wheels

Python 99.54% Makefile 0.46%
poetry poetry-plugin poetry-python

poetry-plugin-freeze's Introduction

Freeze Wheel Plugin

Poetry plugin for creating frozen wheels using lockfiles.

Why

A common issue when publishing a Python application's release into PyPI, is whether or not the dependencies specified will continue to work over time. This tends to happen due to a confluence of reasons, poor dependency specification, bad observance of semantic versioning, or poor release management by the dependency. That translates to a reality where installing an older release of the application is unlikely to work, due to changes in the underlying dependency graph.

The dependency ecosystem is both complex and fragile. The emergence of lock files to ensure repeatability is testimony both to the problem and one solution. Yet when we go to publish in the packaging ecosystem we do so with non frozen dependencies specifications not with lockfiles. That means the testing pipelines that goes to produce and validate a release is against a lockfile but the release artifact is divorced of the lockfile contents, and starts to diverge from the moment of publication.

The various language package distribution channels (npm, pypi, rubygems, etc) are used for two different primary distribution purposes, for both libraries and applications. Generally speaking the extant behavior is reasonable for a library. Libraries should be relatively liberal on their own dependencies baring perhaps major versions to minimize conflicts for applications depending on them and ideally consist of minimal dependencies graphs. But for applications distribution, repeatable and verifyable installs are fundamental goals with potentially large dependency graphs. Using a frozen dependency graph versus version specifications is the only way to ensure repeatiblity of installation over time. Fundamentally the two different distribution purposes have different audiences, ie. libraries have developers and applications as consumers, applications have users as consumers.

What

A post build / pre publish command to allow for creating wheels with frozen dependencies. Basically we update wheel metadata for Requires-Dist to replace the pyproject.toml based version specification to a frozen (ie. ==version) one based on the version from the poetry lock information.

Note we can't use poetry to publish because the frozen wheel because it uses metadata from pyproject.toml instead of frozen wheel metadata.

Optional Dependencies

Frozen wheel metadata will contain Provides-Extra entries for any extra / optional dependencies. Frozen Requires-Dist lines will specify extra names _for packages that appear only in the optional/extra dependency graph.

If a package appears as both a nested "main" dependency and also as an "extra" dependency, its Requires-Dist entry in the frozen wheel will not specify an extra name.

To define this behavior in relation to poetry's export plugin, these two flows should result in the same installed package set:

# Export Flow
poetry export -f requirements.txt > requirements.txt && pip install -r requirements.txt

# Freeze-wheel Flow
poetry build && poetry freeze-wheel && pip install my_frozen_wheel

And introducing extras:

# Export Flow
poetry export --extras gcp -f requirements.txt && pip install -r requirements.txt

# Freeze-wheel Flow
poetry build && poetry freeze-wheel && pip install my_frozen_wheel[gcp]

The difference is in when to choose which extras to install - export does that at freeze time. freeze-wheel embeds the extra context at freeze time, but defers the actual extra selection until install time.

Usage

# install plugin
poetry self add poetry-plugin-freeze

# build per normal
poetry build

# add freeze step
poetry freeze-wheel

# avoid freezing specific packages
poetry freeze-wheel --exclude boto3 -e attrs

# Note we can't use poetry to publish because it uses metadata from pyproject.toml instead
# of frozen wheel metadata.

# publish per normal
twine upload dist/*.whl

Mono-Repo Support

To support mono repos consisting of multiple libraries/applications, when creating a frozen wheel, main group dependencies specified by path can be optionally substituted out for references to their release artifact versions.

This assumes automation to run build and publish across the various subpackages, ie typically via make or just.

poetry-plugin-freeze's People

Contributors

ajkerrigan avatar kapilt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

poetry-plugin-freeze's Issues

Failing in project without dependencies

The command poetry freeze-wheel is failing in projects without dependencies. Example:

[tool.poetry.dependencies]
python = "^3.9"

This happens, if you are using this in a generic pipeline template for much projects. Stdout/Stderr example:

freezing wheels
'NoneType' object is not iterable

Imho, this shouldnt be an (unhandled) error. A warning log message is okay, but the exit code should be 0 anyway.

support freeze for relative dependencies only

We're interested in this plugin for a multimodule repository that publishes library packages. We're looking for a way to turn relative dependency paths into proper package versions. The whole repo is monoversioned. We don't want to freeze third party dependencies and would like to keep defining supported dependency ranges for them. Would it be possible to support this use case with a --relative-only option for example?

fails when given a dependency specification for a non installed version

freezing wheels                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Dependency walk failed at urllib3 (>=1.25.4,<1.27) 

with traceback

Traceback (most recent call last):                                                                                                                            
  File "/Users/kapilt/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry_plugin_freeze/app.py", line 44, in handle                 
    return self._handle()                                                                                                                                                                                                                                                                                                    
           ^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                                    
  File "/Users/kapilt/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry_plugin_freeze/app.py", line 63, in _handle                                                                                                                                                                               
    for w in iced.freeze():                                                                                                                                                                                                                                                                                                  
             ^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                                   
  File "/Users/kapilt/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry_plugin_freeze/app.py", line 135, in freeze                                                                                                                                                                               
    self.freeze_wheel(w, dep_package_map)                                                                                                                     
  File "/Users/kapilt/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry_plugin_freeze/app.py", line 305, in freeze_wheel                                                                                                                                                                         
    dep_lines = self.get_frozen_deps(deps, self.exclude_packages)                                                                                                                                                                                                                                                            
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                            
  File "/Users/kapilt/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry_plugin_freeze/app.py", line 222, in get_frozen_deps                                                                                                                                                                      
    dependency_sources = self.get_dependency_sources()                                                                                                                                                                                                                                                                       
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                       
  File "/Users/kapilt/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry_plugin_freeze/app.py", line 180, in get_dependency_sources                                                                                                                                                               
    base_nested_dependencies = walk_dependencies(                                                                                                                                                                                                                                                                            
                               ^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                            
  File "/Users/kapilt/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry_plugin_export/walker.py", line 155, in walk_dependencies                                                                                                                                                                 
    raise RuntimeError(f"Dependency walk failed at {requirement}")                                                                                                                                                                                                                                                           
RuntimeError: Dependency walk failed at urllib3 (>=1.25.4,<1.27)                                                                                                                                                                                                                                                             
> /Users/kapilt/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry_plugin_export/walker.py(155)walk_dependencies()                                                                                                                                                                                
-> raise RuntimeError(f"Dependency walk failed at {requirement}")                                                                                                                                                                                                                                                            
(Pdb) l                                                                                                                                                                                                                                                                                                                      
150             locked_package = get_locked_package(                                                                                                          
151                 requirement, packages_by_name, nested_dependencies                                                                                                                                                                                                                                                       
152             )                                                                                                                                                                                                                                                                                                            
153                                                                                                                                                                                                                                                                                                                          
154             if not locked_package:                                                                                                                                                                                                                                                                                       
155  ->             raise RuntimeError(f"Dependency walk failed at {requirement}")                                                                            
156                                                                                                                                                           
157             if requirement.extras:                                                                                                                        
158                 locked_package = locked_package.with_features(requirement.extras)                                                                                                                                                                                                                                        
159                                                                                                                                                                                                                                                                                                                          
160             # create dependency from locked package to retain dependency metadata   

the dependency arises from botocore which uses these markers

    'urllib3>=1.25.4,<1.27 ; python_version < "3.10"',
    'urllib3>=1.25.4,<2.1 ; python_version >= "3.10"',

Fails if project directory contains a `pyproject.toml` file without tool.poetry section

The plugin fails if project directory contains a pyproject.toml file without tool.poetry section.

It can easily occurs if you are using poetry config virtualenvs.in-project true with created a .venv directory inside the project directory and will probably contains pyproject.toml files

[tool.poetry] section not found in <project>/.venv/lib/python3.11/site-packages/ddtrace/appsec/_iast/_taint_tracking/_vendor/pybind11/tools/pyproject.toml

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.