Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
Automatically determine package name
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Johnston committed Oct 29, 2020
1 parent 72cebef commit a0a0361
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 27 deletions.
12 changes: 10 additions & 2 deletions pips3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,26 @@ def upload_index(self, package_name: str, index: Union[str, None] = None):
ContentType="text/html")


def publish_packages(endpoint: str, bucket: str, package_name: str):
def publish_packages(endpoint: str, bucket: str):
"""Publish current package files
Args:
endpoint (str): The endpoint for the S3-like service
bucket (str): The name of the bucket to use
package_name (str): The name of the package
"""

uploader = PipS3(endpoint, bucket)

package_name = None

for upload_file in PipS3.find_package_files():

# Get the package name
if package_name is None:

package_name = os.path.basename(upload_file).split('-')[0]
package_name = package_name.replace('_', '-')

uploader.upload_package(upload_file, package_name)

# Update the index
Expand Down
9 changes: 2 additions & 7 deletions pips3/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
@click.command()
@click.option('--endpoint', default=None, help='S3 Endpoint')
@click.option('--bucket', default=None, help='S3 Bucket')
@click.option('--package', default=None, help='Package Name')
def main(endpoint, bucket, package):
def main(endpoint, bucket):
"""Console script for pips3."""

# Try a number of options for determining configuration values
endpoint = os.getenv('PIPS3_ENDPOINT') if endpoint is None else endpoint
bucket = os.getenv('PIPS3_BUCKET') if bucket is None else bucket
package = os.getenv('PIPS3_PACKAGE') if package is None else package

# TODO: #2 Allow retrieving of values from pip.conf

Expand All @@ -30,10 +28,7 @@ def main(endpoint, bucket, package):
if bucket is None:
raise InvalidConfig("Error!!! S3 bucket not specified")

if package is None:
raise InvalidConfig("Error!!! S3 package name not specified")

publish_packages(endpoint, bucket, package)
publish_packages(endpoint, bucket)
return 0


Expand Down
Empty file.
Empty file added tests/assets/pips3-0.1.0.whl
Empty file.
13 changes: 6 additions & 7 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,29 @@ def test_upload_index():
@mock_s3
@patch('pips3.base.PipS3.find_package_files',
return_value=[
'docs/index.rst',
'docs/readme.rst',
'tests/assets/pips3-0.1.0.dev0.whl',
'tests/assets/pips3-0.1.0.whl',
])
def test_publish_packages(files_mock):
"""Publish packages"""

s3_client = boto3.client('s3', region_name='us-east-1')
s3_client.create_bucket(Bucket=BUCKET)

package = "pips3"
prefix = "simple"

publish_packages(ENDPOINT_URL, BUCKET, package)
publish_packages(ENDPOINT_URL, BUCKET)

# Get the index
index = s3_client.get_object(Bucket=BUCKET,
Key=f'{prefix}/{package}/index.html')
Key=f'{prefix}/pips3/index.html')
index = index['Body'].read().decode('utf-8')

expected_index = f"""<!DOCTYPE html>
<html>
<body>
<a href="{ ENDPOINT_URL }/simple/pips3/index.rst">index.rst</a>
<a href="{ ENDPOINT_URL }/simple/pips3/readme.rst">readme.rst</a>
<a href="{ ENDPOINT_URL }/simple/pips3/pips3-0.1.0.dev0.whl">pips3-0.1.0.dev0.whl</a>
<a href="{ ENDPOINT_URL }/simple/pips3/pips3-0.1.0.whl">pips3-0.1.0.whl</a>
</body>
</html>"""

Expand Down
15 changes: 4 additions & 11 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ def test_command_line_interface(publish_mock):
"""Test the CLI."""
runner = CliRunner()

result = runner.invoke(
cli.main,
['--endpoint', URL, '--bucket', BUCKET, '--package', PACKAGE])
result = runner.invoke(cli.main, ['--endpoint', URL, '--bucket', BUCKET])

assert result.exit_code == 0
publish_mock.assert_called_with(URL, BUCKET, PACKAGE)
publish_mock.assert_called_with(URL, BUCKET)


@patch('pips3.cli.publish_packages')
Expand All @@ -34,14 +32,13 @@ def test_command_line_interface_envars(publish_mock, monkeypatch):

monkeypatch.setenv('PIPS3_ENDPOINT', URL)
monkeypatch.setenv('PIPS3_BUCKET', BUCKET)
monkeypatch.setenv('PIPS3_PACKAGE', PACKAGE)

runner = CliRunner()

result = runner.invoke(cli.main)

assert result.exit_code == 0
publish_mock.assert_called_with(URL, BUCKET, PACKAGE)
publish_mock.assert_called_with(URL, BUCKET)


def test_cli_errors(monkeypatch):
Expand All @@ -63,8 +60,4 @@ def test_cli_errors(monkeypatch):
assert 'bucket not specified' in str(result.exception)

monkeypatch.setenv('PIPS3_BUCKET', URL)
result = runner.invoke(cli.main)

assert result.exit_code != 0
assert isinstance(result.exception, InvalidConfig)
assert 'package name not specified' in str(result.exception)
result = runner.invoke(cli.main)

0 comments on commit a0a0361

Please sign in to comment.