Commit a43c3baf by Sébastien Eustace

Improve files to publish detection and improve publish command

parent bf061f93
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
- Improved the `show` command to make it easier to check if packages are properly installed. - Improved the `show` command to make it easier to check if packages are properly installed.
- The `script` command has been deprecated, use `run` instead. - The `script` command has been deprecated, use `run` instead.
- The `publish` command no longer build packages by default. Use `--build` to retrieve the previous behavior.
- Improved support for private repositories. - Improved support for private repositories.
- Expanded version constraints now keep the original version's precision. - Expanded version constraints now keep the original version's precision.
- The lock file hash no longer use the project's name and version. - The lock file hash no longer use the project's name and version.
......
...@@ -9,7 +9,7 @@ class PublishCommand(Command): ...@@ -9,7 +9,7 @@ class PublishCommand(Command):
{ --r|repository= : The repository to publish the package to. } { --r|repository= : The repository to publish the package to. }
{ --u|username= : The username to access the repository. } { --u|username= : The username to access the repository. }
{ --p|password= : The password to access the repository. } { --p|password= : The password to access the repository. }
{ --no-build : Do not build the package before publishing. } { --build : Build the package before publishing. }
""" """
help = """The publish command builds and uploads the package to a remote repository. help = """The publish command builds and uploads the package to a remote repository.
...@@ -24,13 +24,32 @@ the config command. ...@@ -24,13 +24,32 @@ the config command.
def handle(self): def handle(self):
from poetry.masonry.publishing.publisher import Publisher from poetry.masonry.publishing.publisher import Publisher
# Building package first, unless told otherwise publisher = Publisher(self.poetry, self.output)
if not self.option('no-build'):
# Building package first, if told
if self.option('build'):
if publisher.files:
if not self.confirm(
'There are <info>{}</info> files ready for publishing. '
'Build anyway?'.format(len(publisher.files))
):
self.line_error('<error>Aborted!</error>')
return 1
self.call('build') self.call('build')
files = publisher.files
if not files:
self.line_error(
'<error>No files to publish. '
'Run poetry build first or use the --build option.</error>'
)
return 1
self.line('') self.line('')
publisher = Publisher(self.poetry, self.output)
publisher.publish( publisher.publish(
self.option('repository'), self.option('repository'),
self.option('username'), self.option('username'),
......
...@@ -17,6 +17,10 @@ class Publisher: ...@@ -17,6 +17,10 @@ class Publisher:
self._io = io self._io = io
self._uploader = Uploader(poetry, io) self._uploader = Uploader(poetry, io)
@property
def files(self):
return self._uploader.files
def publish(self, repository_name, username, password): def publish(self, repository_name, username, password):
if repository_name: if repository_name:
self._io.writeln( self._io.writeln(
......
...@@ -2,6 +2,8 @@ import hashlib ...@@ -2,6 +2,8 @@ import hashlib
import io import io
import re import re
from typing import List
import requests import requests
from requests import adapters from requests import adapters
...@@ -52,6 +54,23 @@ class Uploader: ...@@ -52,6 +54,23 @@ class Uploader:
return adapters.HTTPAdapter(max_retries=retry) return adapters.HTTPAdapter(max_retries=retry)
@property
def files(self): # type: () -> List[str]
dist = self._poetry.file.parent / 'dist'
version = normalize_version(self._package.version.text)
wheels = list(dist.glob(
'{}-{}-*.whl'.format(
re.sub("[^\w\d.]+", "_", self._package.pretty_name, flags=re.UNICODE),
re.sub("[^\w\d.]+", "_", version, flags=re.UNICODE),
)
))
tars = list(dist.glob(
'{}-{}.tar.gz'.format(self._package.pretty_name, version)
))
return sorted(wheels + tars)
def auth(self, username, password): def auth(self, username, password):
self._username = username self._username = username
self._password = password self._password = password
...@@ -178,28 +197,7 @@ class Uploader: ...@@ -178,28 +197,7 @@ class Uploader:
raise raise
def _do_upload(self, session, url): def _do_upload(self, session, url):
dist = self._poetry.file.parent / 'dist' for file in self.files:
version = normalize_version(self._package.version.text)
packages = dist.glob(
'{}-{}*'.format(self._package.name, version)
)
files = (
i for i in packages if (
i.match(
'{}-{}-*.whl'.format(
self._package.name, version
)
)
or
i.match(
'{}-{}.tar.gz'.format(
self._package.name, version
)
)
)
)
for file in files:
# TODO: Check existence # TODO: Check existence
resp = self._upload_file(session, url, file) resp = self._upload_file(session, url, file)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment