Example #1
0
def main(requirements_path):
    this_requirements_file = os.path.basename(requirements_path)
    parsed = parse_requirements(requirements_path,
                                session=pip.download.PipSession())
    requirements = [
        req for req in parsed
        # Skip packages from other requirements files
        if this_requirements_file in req.comes_from
    ]

    reverse_requirements = {}
    nested_requirements = set()

    # Fetch nested reqirements lines, this is mostly copied from
    # pip so that we support stuff "correctly". Unfortunately there
    # isn't any good API in pip for it :-/
    parser = build_parser()
    defaults = parser.get_default_values()

    with open(requirements_path) as fobj:
        for line in fobj:
            args_str, options_str = break_args_options(line)
            opts, _ = parser.parse_args(shlex.split(options_str), defaults)
            if opts.requirements:
                nested_requirements.update(opts.requirements)

    # Build reverse requirements to be able to add a note on who is depending
    # on what
    for req in requirements:
        reverse_requirements[safe_name(req.name)] = rdeps(req.name)

    output = []

    output.extend('-r %s' % req for req in nested_requirements)
    output.append('')

    # Let's output the updated, fixed and more correct requirements version
    for req in sorted(requirements, key=lambda x: safe_name(x.name)):
        if reverse_requirements.get(safe_name(req.name)):
            msg = '# %s is required by %s' % (safe_name(req.name), ', '.join(
                reverse_requirements[safe_name(req.name)]))
            output.append(msg)

        output.append('%s%s' % (safe_name(req.name), str(req.specifier)))

    with open(requirements_path, 'wb') as fobj:
        fobj.write('\n'.join(output))

    with open(requirements_path, 'a') as fobj:
        fobj.write('\n')

    for req in requirements:
        run_single_package(
            '%s%s' % (safe_name(req.name), str(req.specifier)),
            requirements_path,
            'sha256',
            # Workaround a bug or feature in hashin which would avoid
            # fetching wheels e.g for some packages.
            python_versions=['py27', '2.7'],
            verbose=True)
Example #2
0
def main(requirements_path):
    this_requirements_file = os.path.basename(requirements_path)
    parsed = parse_requirements(
        requirements_path, session=pip.download.PipSession())
    requirements = [
        req for req in parsed
        # Skip packages from other requirements files
        if this_requirements_file in req.comes_from]

    reverse_requirements = {}
    nested_requirements = set()

    # Fetch nested reqirements lines, this is mostly copied from
    # pip so that we support stuff "correctly". Unfortunately there
    # isn't any good API in pip for it :-/
    parser = build_parser()
    defaults = parser.get_default_values()

    with open(requirements_path) as fobj:
        for line in fobj:
            args_str, options_str = break_args_options(line)
            opts, _ = parser.parse_args(shlex.split(options_str), defaults)
            if opts.requirements:
                nested_requirements.update(opts.requirements)

    # Build reverse requirements to be able to add a note on who is depending
    # on what
    for req in requirements:
        reverse_requirements[safe_name(req.name)] = rdeps(req.name)

    output = []

    output.extend('-r %s' % req for req in nested_requirements)
    output.append('')

    # Let's output the updated, fixed and more correct requirements version
    for req in sorted(requirements, key=lambda x: safe_name(x.name)):
        if reverse_requirements.get(safe_name(req.name)):
            msg = '# %s is required by %s' % (
                safe_name(req.name),
                ', '.join(reverse_requirements[safe_name(req.name)]))
            output.append(msg)

        output.append('%s%s' % (safe_name(req.name), str(req.specifier)))

    with open(requirements_path, 'wb') as fobj:
        fobj.write('\n'.join(output))

    with open(requirements_path, 'a') as fobj:
        fobj.write('\n')

    for req in requirements:
        run_single_package(
            '%s%s' % (safe_name(req.name), str(req.specifier)),
            requirements_path,
            'sha256',
            # Workaround a bug or feature in hashin which would avoid
            # fetching wheels e.g for some packages.
            python_versions=['py27', '2.7'],
            verbose=True)