Skip to content

Instantly share code, notes, and snippets.

@adamghill
Created February 7, 2020 23:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adamghill/8a96311b5cf1790c72a0b825ffe4f6d2 to your computer and use it in GitHub Desktop.
Save adamghill/8a96311b5cf1790c72a0b825ffe4f6d2 to your computer and use it in GitHub Desktop.
Convert pyproject.toml to setup.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego (gallegoj@uw.edu)
# @Date: 2019-12-18
# @Filename: create_setup.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)
# https://github.com/sdss/flicamera/blob/master/create_setup.py
# This is a temporary solution for the fact that pip install . fails with
# poetry when there is no setup.py and an extension needs to be compiled.
# See https://github.com/python-poetry/poetry/issues/1516. Running this
# script creates a setup.py filled out with information generated by
# poetry when parsing the pyproject.toml.
import os
import sys
# If there is a global installation of poetry, prefer that.
poetry_python_lib = os.path.expanduser('~/.poetry/lib')
sys.path.append(os.path.realpath(poetry_python_lib))
try:
from poetry.masonry.builders.sdist import SdistBuilder
from poetry.factory import Factory
except (ImportError, ModuleNotFoundError) as ee:
raise ImportError('install poetry by doing pip install poetry to use '
f'this script: {ee}')
# Generate a Poetry object that knows about the metadata in pyproject.toml
factory = Factory()
poetry = factory.create_poetry(os.path.dirname(__file__))
# Use the SdistBuilder to genrate a blob for setup.py
sdist_builder = SdistBuilder(poetry, None, None)
setuppy_blob = sdist_builder.build_setup()
with open('setup.py', 'wb') as unit:
unit.write(setuppy_blob)
unit.write(b'\n# This setup.py was autogenerated using poetry.\n')
@joepreludian
Copy link

Hey @adamghill. First of all, thanks for the great content! I got inspired on your project and I created a docker container with your command built in.

I had a need to provide editable poetry project in some pip only projects, so I found your script pretty useful! I just wrapped up your content in a docker container. If you want to check it out, please look at this repo: https://github.com/joepreludian/poetry-to-setup-py

Thank you!

@ycnakajsph
Copy link

Thanks to both of you! @joepreludian the integration works like a charm cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment