Skip to content

--constraint support #1364

@orsinium

Description

@orsinium

What's the problem this feature will solve?

There are 2 projects:

  • lib is an internal library. It has setup.py with dependencies of the library.
  • srv is an internal service that uses lib. It has a lock file requirements.txt.

The goal is to generate lib/requirements.txt which has only packages from lib/setup.py but the same versions as specified in srv/requirements.txt. Motivation:

  • The same version is needed to make sure that if the tests have passed for lib, it will work as expected in the environment of srv.
  • Running tests for lib in the environment of srv is complicated on CI: the env of srv is too big, and we want to avoid making it for pipelines in lib.

Describe the solution you'd like

The idea is the same as in pip's Constraints Files (-c/--constraint option). So, it makes sense to introduce the same key for pip-tools.

Alternative Solutions

pip-tools already uses the output file as the constraint. So, a workaround I found is to specify the constraint file as the output file and then restore it. The PoC:

import sys
import shutil
import subprocess
from argparse import ArgumentParser


def main():
    parser = ArgumentParser()
    parser.add_argument('-c', '--constraint', required=True)
    parser.add_argument('--output-file', default='requirements.txt')
    args, rest = parser.parse_known_args()
    shutil.copy(args.constraint, '/tmp/c.txt')
    cmd = [sys.executable, '-m', 'piptools', 'compile', '--output-file', args.constraint]
    try:
        code = subprocess.call(cmd + rest)
        shutil.copy(args.constraint, args.output_file)
    finally:
        shutil.copy('/tmp/c.txt', args.constraint)
    sys.exit(code)

if __name__ == '__main__':
    main()

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliRelated to command line interface thingsfeatureRequest for a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions