Skip to content

Commit

Permalink
Merge pull request #118 from martinholmer/add-cli
Browse files Browse the repository at this point in the history
Add pbrelease CLI to release function
  • Loading branch information
martinholmer authored Nov 12, 2018
2 parents 3df8025 + 537ebdd commit 3f78bbd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
8 changes: 8 additions & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package:
name: pkgbld
version: 0.0.0

build:
entry_points:
- pbrelease = pkgbld.cli:main

requirements:
build:
- python=3.6
Expand All @@ -16,6 +20,10 @@ requirements:
test:
imports:
- pkgbld
commands:
- which pbrelease # [unix]
- where pbrelease # [win]
- pbrelease --help

about:
home: https://github.com/open-source-economics/Package-Builder
1 change: 1 addition & 0 deletions pkgbld/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from pkgbld.utils import *
from pkgbld.release import *
from pkgbld.cli import *

from pkgbld._version import get_versions
__version__ = get_versions()['version']
Expand Down
62 changes: 62 additions & 0 deletions pkgbld/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Command-line interface (CLI) to Package-Builder response function,
which can be accessed as 'pbrelease' from an installed pkgbld conda package.
"""
# CODING-STYLE CHECKS:
# pycodestyle cli.py
# pylint --disable=locally-disabled cli.py

import sys
import argparse
import pkgbld


def main():
"""
Contains command-line interface (CLI) to Package-Builder response function.
"""
# parse command-line arguments:
usage_str = ('pbrelease REPOSITORY_NAME PACKAGE_NAME MODEL_VERSION\n'
' [--help] [--version]')
parser = argparse.ArgumentParser(
prog='',
usage=usage_str,
description=('Creates conda packages named PACKAGE_NAME for the PSL '
'model in REPOSITORY_NAME that has a MODEL_VERSION '
'release. The packages are build locally in a '
'temporary workspace and then uploaded to the '
'Anaconda Cloud PSLmodels channel for public '
'distribution.')
)
parser.add_argument('REPOSITORY_NAME', nargs='?',
help=('Name of repository in the GitHub organization '
'called pslmodels (nee open-source-economics). '
'Example: Tax-Calculator'),
default=None)
parser.add_argument('PACKAGE_NAME', nargs='?',
help=('Name of packages to build and upload. '
'Example: taxcalc'),
default=None)
parser.add_argument('MODEL_VERSION', nargs='?',
help=('Model release string that is consistent '
'with semantic-versioning rules. '
'Example: 0.22.2'),
default=None)
parser.add_argument('--version',
help=('optional flag that writes Package-Builder '
'release version to stdout and quits'),
default=False,
action="store_true")
args = parser.parse_args()
# show Package-Builder version and quit if --version option specified
if args.version:
version = pkgbld.__version__
if version == 'unknown':
version = 'locally.generated.package'
sys.stdout.write('Package-Builder {}\n'.format(version))
return 0
# call pkgbld release function with specified parameters
pkgbld.release(repo_name=args.REPOSITORY_NAME,
pkg_name=args.PACKAGE_NAME,
version=args.MODEL_VERSION)
return 0
17 changes: 10 additions & 7 deletions pkgbld/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,31 @@ def release(repo_name, pkg_name, version):
os.chdir(WORKING_DIR)

# clone model repository and checkout model version
print(': ... cloning repository')
print(': Package-Builder is cloning repository')
cmd = 'git clone {}/{}/'.format(GITHUB_URL, repo_name)
u.os_call(cmd)
os.chdir(repo_name)
cmd = 'git checkout -b v{ver} {ver}'.format(ver=version)
u.os_call(cmd)

# specify version in repository's conda.recipe/meta.yaml file
print(': ... setting version')
print(': Package-Builder is setting version')
u.specify_version(version)

# build and upload model package for each Python version and OS platform
local_platform = u.conda_platform_name()
for pyver in PYTHON_VERSIONS:
# ... build for local_platform
print(': ... building package for Python {}'.format(pyver))
print((': Package-Builder is building package '
'for Python {}').format(pyver))
cmd = ('conda build --python {} --old-build-string '
'--channel {} --override-channels '
'--no-anaconda-upload --output-folder {} '
'conda.recipe').format(pyver, ANACONDA_CHANNEL, BUILDS_DIR)
u.os_call(cmd)
# ... convert local build to other OS_PLATFORMS
print(': ... converting package for Python {}'.format(pyver))
print((': Package-Builder is converting package '
'for Python {}').format(pyver))
pyver_parts = pyver.split('.')
pystr = pyver_parts[0] + pyver_parts[1]
pkgfile = '{}-{}-py{}_0.tar.bz2'.format(pkg_name, version, pystr)
Expand All @@ -133,7 +135,8 @@ def release(repo_name, pkg_name, version):
)
u.os_call(cmd)
# ... upload to Anaconda Cloud
print(': ... uploading packages for Python {}'.format(pyver))
print((': Package-Builder is uploading packages '
'for Python {}').format(pyver))
for platform in OS_PLATFORMS:
pkgpath = os.path.join(BUILDS_DIR, platform, pkgfile)
cmd = 'anaconda --token {} upload --user {} {}'.format(
Expand All @@ -142,13 +145,13 @@ def release(repo_name, pkg_name, version):
try:
u.os_call(cmd)
except OSError:
msg = (': ... Package-Builder WARNING: anaconda upload '
msg = (': Package-Builder WARNING: anaconda upload '
'FAILED for {}/{} perhaps because package already '
'exists in the Anaconda Cloud ... '
'continuing').format(platform, pkgfile)
print(msg)

print(': ... cleaning-up')
print(': Package-Builder is cleaning-up')

# remove working directory and its contents
os.chdir(HOME_DIR)
Expand Down

0 comments on commit 3f78bbd

Please sign in to comment.