Skip to content

Commit

Permalink
Merge pull request #311 from takluyver/opt-no-setup-py
Browse files Browse the repository at this point in the history
Add an option to make sdists without setup.py
  • Loading branch information
takluyver committed Jan 14, 2020
2 parents 7cd39b0 + bd294aa commit 645512f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 11 deletions.
12 changes: 12 additions & 0 deletions doc/cmdline.rst
Expand Up @@ -39,6 +39,12 @@ Build a wheel and an sdist (tarball) from the package.

Limit to building either ``wheel`` or ``sdist``.

.. option:: --no-setup-py

Don't generate a setup.py file in the sdist.
An sdist built without this will only work with tools that support PEP 517,
but the wheel will still be usable by any compatible tool.

.. _publish_cmd:

``flit publish``
Expand All @@ -54,6 +60,12 @@ or another repository.
Limit to publishing either ``wheel`` or ``sdist``.
You should normally publish the two formats together.

.. option:: --no-setup-py

Don't generate a setup.py file in the sdist.
An sdist built without this will only work with tools that support PEP 517,
but the wheel will still be usable by any compatible tool.

.. option:: --repository <repository>

Name of a repository to upload packages to. Should match a section in
Expand Down
20 changes: 18 additions & 2 deletions flit/__init__.py
Expand Up @@ -65,6 +65,13 @@ def main(argv=None):
help="Select a format to build. Options: 'wheel', 'sdist'"
)

parser_build.add_argument('--no-setup-py', action='store_false', dest='setup_py',
help=("Don't generate a setup.py file in the sdist. "
"The sdist will only work with tools that support PEP 517, "
"but the wheel will still be usable by any compatible tool."
)
)

# flit publish --------------------------------------------
parser_publish = subparsers.add_parser('publish',
help="Upload wheel and sdist",
Expand All @@ -74,6 +81,13 @@ def main(argv=None):
help="Select a format to publish. Options: 'wheel', 'sdist'"
)

parser_publish.add_argument('--no-setup-py', action='store_false', dest='setup_py',
help=("Don't generate a setup.py file in the sdist. "
"The sdist will only work with tools that support PEP 517, "
"but the wheel will still be usable by any compatible tool."
)
)

parser_publish.add_argument('--repository',
help="Name of the repository to upload to (must be in ~/.pypirc)"
)
Expand Down Expand Up @@ -139,12 +153,14 @@ def main(argv=None):
if args.subcmd == 'build':
from .build import main
try:
main(args.ini_file, formats=set(args.format or []))
main(args.ini_file, formats=set(args.format or []),
gen_setup_py=args.setup_py)
except(common.NoDocstringError, common.VCSError, common.NoVersionError) as e:
sys.exit(e.args[0])
elif args.subcmd == 'publish':
from .upload import main
main(args.ini_file, args.repository, formats=set(args.format or []))
main(args.ini_file, args.repository, formats=set(args.format or []),
gen_setup_py=args.setup_py)

elif args.subcmd == 'install':
from .install import Installer
Expand Down
4 changes: 2 additions & 2 deletions flit/build.py
Expand Up @@ -26,7 +26,7 @@ def unpacked_tarball(path):
assert len(files) == 1, files
yield os.path.join(tmpdir, files[0])

def main(ini_file: Path, formats=None):
def main(ini_file: Path, formats=None, gen_setup_py=True):
"""Build wheel and sdist"""
if not formats:
formats = ALL_FORMATS
Expand All @@ -41,7 +41,7 @@ def main(ini_file: Path, formats=None):

if 'sdist' in formats:
sb = SdistBuilder.from_ini_path(ini_file)
sdist_file = sb.build(ini_file.parent / 'dist')
sdist_file = sb.build(ini_file.parent / 'dist', gen_setup_py=gen_setup_py)
sdist_info = SimpleNamespace(builder=sb, file=sdist_file)
# When we're building both, build the wheel from the unpacked sdist.
# This helps ensure that the sdist contains all the necessary files.
Expand Down
4 changes: 2 additions & 2 deletions flit/sdist.py
Expand Up @@ -215,5 +215,5 @@ def make_setup_py(self):
extra='\n '.join(extra),
).encode('utf-8')

def build(self, target_dir):
return Path(super().build(str(target_dir)))
def build(self, target_dir, gen_setup_py=True):
return Path(super().build(str(target_dir), gen_setup_py=gen_setup_py))
4 changes: 2 additions & 2 deletions flit/upload.py
Expand Up @@ -261,10 +261,10 @@ def do_upload(file:Path, metadata:Metadata, repo_name=None):
log.info("Package is at %s/%s", repo['url'], metadata.name)


def main(ini_path, repo_name, formats=None):
def main(ini_path, repo_name, formats=None, gen_setup_py=True):
"""Build and upload wheel and sdist."""
from . import build
built = build.main(ini_path, formats=formats)
built = build.main(ini_path, formats=formats, gen_setup_py=gen_setup_py)

if built.wheel is not None:
do_upload(built.wheel.file, built.wheel.builder.metadata, repo_name)
Expand Down
5 changes: 3 additions & 2 deletions flit_core/flit_core/sdist.py
Expand Up @@ -164,7 +164,7 @@ def add_setup_py(self, files_to_add, target_tarfile):
def dir_name(self):
return '{}-{}'.format(self.metadata.name, self.metadata.version)

def build(self, target_dir):
def build(self, target_dir, gen_setup_py=True):
if not osp.isdir(target_dir):
os.makedirs(target_dir)
target = osp.join(
Expand All @@ -191,7 +191,8 @@ def build(self, target_dir):
else:
tf.addfile(ti) # Symlinks & ?

self.add_setup_py(files_to_add, tf)
if gen_setup_py:
self.add_setup_py(files_to_add, tf)

pkg_info = PKG_INFO.format(
name=self.metadata.name,
Expand Down
22 changes: 21 additions & 1 deletion tests/test_sdist.py
Expand Up @@ -4,6 +4,7 @@
import pytest
from shutil import which, copy, copytree
import sys
import tarfile
from tempfile import TemporaryDirectory
from testpath import assert_isfile, MockCommand

Expand All @@ -27,7 +28,26 @@ def test_make_sdist():
with TemporaryDirectory() as td:
td = Path(td)
builder.build(td)
assert_isfile(td / 'package1-0.1.tar.gz')
sdist_file = td / 'package1-0.1.tar.gz'
assert_isfile(sdist_file)

with tarfile.open(str(sdist_file)) as tf:
assert 'package1-0.1/setup.py' in tf.getnames()


def test_sdist_no_setup_py():
# Smoke test of making a complete sdist
if not which('git'):
pytest.skip("requires git")
builder = sdist.SdistBuilder.from_ini_path(samples_dir / 'package1' / 'flit.ini')
with TemporaryDirectory() as td:
td = Path(td)
builder.build(td, gen_setup_py=False)
sdist_file = td / 'package1-0.1.tar.gz'
assert_isfile(sdist_file)

with tarfile.open(str(sdist_file)) as tf:
assert 'package1-0.1/setup.py' not in tf.getnames()


LIST_FILES = """\
Expand Down

0 comments on commit 645512f

Please sign in to comment.