Completed
Pull Request — master (#127)
by Michael
10:36 queued 10:00
created

install_from_pypi()   B

Complexity

Conditions 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.0935

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
c 2
b 0
f 0
dl 0
loc 25
ccs 9
cts 16
cp 0.5625
crap 7.0935
rs 8.0894
1 3
import logging
2
3 3
from shutil import rmtree
4 3
from pathlib import Path
5
6 3
from changes import shell, util, venv, verification
7
8 3
log = logging.getLogger(__name__)
0 ignored issues
show
Coding Style Naming introduced by
The name log does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
9
10
11 3
def build_distributions(context):
12
    """Builds package distributions"""
13 3
    rmtree('dist', ignore_errors=True)
14
15 3
    build_package_command = 'python setup.py clean sdist bdist_wheel'
16 3
    result = shell.dry_run(build_package_command, context.dry_run)
17 3
    packages = Path('dist').files() if not context.dry_run else "nothing"
0 ignored issues
show
Bug introduced by
The Instance of Path does not seem to have a member named files.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
18
19 3
    if not result:
20
        raise Exception('Error building packages: %s' % result)
21
    else:
22 3
        log.info('Built %s' % ', '.join(packages))
23 3
    return packages
24
25
26
# tox
27 3
def install_package(context):
28
    """Attempts to install the sdist and wheel."""
29
30 3
    if not context.dry_run and build_distributions(context):
31
        with util.mktmpdir() as tmp_dir:
32
            venv.create_venv(tmp_dir=tmp_dir)
33
            for distribution in Path('dist').files():
0 ignored issues
show
Bug introduced by
The Instance of Path does not seem to have a member named files.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
34
                try:
35
                    venv.install(distribution, tmp_dir)
36
                    log.info('Successfully installed %s', distribution)
37
                    if context.test_command and verification.run_test_command(context.test_command):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (100/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
38
                        log.info('Successfully ran test command: %s',
39
                                 context.test_command)
40
                except Exception as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
41
                    raise Exception(
42
                        'Error installing distribution %s' % distribution, e)
43
    else:
44 3
        log.info('Dry run, skipping installation')
45
46
47
# twine
48 3
def upload_package(context):
49
    """Uploads your project packages to pypi with twine."""
50
51 3
    if not context.dry_run and build_distributions(context):
52
        upload_args = 'twine upload '
53
        upload_args += ' '.join(Path('dist').files())
0 ignored issues
show
Bug introduced by
The Instance of Path does not seem to have a member named files.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
54
        if context.pypi:
55
            upload_args += ' -r %s' % context.pypi
56
57
        upload_result = shell.dry_run(upload_args, context.dry_run)
58
        if not context.dry_run and not upload_result:
59
            raise Exception('Error uploading: %s' % upload_result)
60
        else:
61
            log.info('Successfully uploaded %s:%s',
62
                     context.module_name, context.new_version)
63
    else:
64 3
        log.info('Dry run, skipping package upload')
65
66
67 3
def install_from_pypi(context):
68
    """Attempts to install your package from pypi."""
69
70 3
    tmp_dir = venv.create_venv()
71 3
    install_cmd = '%s/bin/pip install %s' % (tmp_dir, context.module_name)
72
73 3
    package_index = 'pypi'
74 3
    if context.pypi:
75
        install_cmd += '-i %s' % context.pypi
76
        package_index = context.pypi
77
78 3
    try:
79 3
        result = shell.dry_run(install_cmd, context.dry_run)
80 3
        if not context.dry_run and not result:
81
            log.error('Failed to install %s from %s',
82
                      context.module_name, package_index)
83
        else:
84 3
            log.info('Successfully installed %s from %s',
85
                     context.module_name, package_index)
86
87
    except Exception as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
88
        error_msg = 'Error installing %s from %s' % (
89
            context.module_name, package_index)
90
        log.exception(error_msg)
91
        raise Exception(error_msg, e)
92