Skip to content

Instantly share code, notes, and snippets.

@tony
Forked from sobolevn/pipfile-to-poetry.py
Last active December 16, 2019 15:15
Show Gist options
  • Save tony/03e08da95b3c2217a4d64bade4feebe0 to your computer and use it in GitHub Desktop.
Save tony/03e08da95b3c2217a4d64bade4feebe0 to your computer and use it in GitHub Desktop.

Revisions

  1. tony revised this gist Dec 16, 2019. 1 changed file with 21 additions and 13 deletions.
    34 changes: 21 additions & 13 deletions pipfile-to-poetry.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import json

    import toml


    @@ -25,19 +26,26 @@ def to_name_version_notation(pipfile, lock, section):
    for dep in dependencies:
    if dep not in pipfile[category]:
    continue

    notations.update({
    dep: convert_version(dependencies[dep]['version']),
    })
    try:
    notations.update({dep: convert_version(dependencies[dep]['version'])})
    except KeyError as e:
    errors.append(f'{dep} - ({e.__class__.__name__}) {e} - {dependencies[dep]}')
    return notations


    pipfile = read_pipenv()
    lock = read_pipenv_lock()
    if __name__ == '__main__':
    errors = []
    pipfile = read_pipenv()
    lock = read_pipenv_lock()

    for section in ['default', 'develop']:
    print('Section:', section)
    current = to_name_version_notation(pipfile, lock, section)
    for dep, version in current.items():
    print(dep, '=', '"' + version + '"')
    print()

    for section in ['default', 'develop']:
    print('Section:', section)
    current = to_name_version_notation(pipfile, lock, section)
    for dep, version in current.items():
    print(dep, '=', '"' + version + '"')
    print()
    if len(errors):
    print('errors encountered:')
    for error in errors:
    print(f'- {error}')
  2. @sobolevn sobolevn created this gist Feb 18, 2019.
    43 changes: 43 additions & 0 deletions pipfile-to-poetry.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    # -*- coding: utf-8 -*-

    import json
    import toml


    def read_pipenv_lock():
    with open('Pipfile.lock') as lock:
    return json.loads(lock.read())


    def read_pipenv():
    with open('Pipfile') as pipfile:
    return toml.loads(pipfile.read())


    def convert_version(version):
    return version.replace('==', '^')


    def to_name_version_notation(pipfile, lock, section):
    category = 'packages' if section == 'default' else 'dev-packages'
    notations = {}
    dependencies = lock[section]
    for dep in dependencies:
    if dep not in pipfile[category]:
    continue

    notations.update({
    dep: convert_version(dependencies[dep]['version']),
    })
    return notations


    pipfile = read_pipenv()
    lock = read_pipenv_lock()

    for section in ['default', 'develop']:
    print('Section:', section)
    current = to_name_version_notation(pipfile, lock, section)
    for dep, version in current.items():
    print(dep, '=', '"' + version + '"')
    print()