Skip to content

Commit

Permalink
build: adopt black codestyle
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Laíns <[email protected]>
  • Loading branch information
FFY00 authored and gaborbernat committed Oct 7, 2020
1 parent a148d6a commit fcc5106
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 153 deletions.
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: double-quote-string-fixer
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
hooks:
Expand Down
34 changes: 20 additions & 14 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ def run_tests(session, env=None):
htmlcov_output = os.path.join(session.virtualenv.location, 'htmlcov')
xmlcov_output = os.path.join(session.virtualenv.location, f'coverage-{session.python}.xml')

session.run('pytest', '--cov', '--cov-config', 'setup.cfg',
f'--cov-report=html:{htmlcov_output}',
f'--cov-report=xml:{xmlcov_output}',
'tests/', *session.posargs, env=env)
session.run(
'pytest',
'--cov',
'--cov-config',
'setup.cfg',
f'--cov-report=html:{htmlcov_output}',
f'--cov-report=xml:{xmlcov_output}',
'tests/',
*session.posargs,
env=env,
)


@nox.session(python=['2.7', '3.5', '3.6', '3.7', '3.8', 'pypy2', 'pypy3'])
Expand All @@ -45,8 +52,13 @@ def docs(session):
session.install('.', '-r', os.path.join('docs', 'requirements.txt'))
output = session.create_tmp()
session.run(
'python', '-m', 'sphinx',
'-n', '-W', os.path.join('docs', 'source'), output,
'python',
'-m',
'sphinx',
'-n',
'-W',
os.path.join('docs', 'source'),
output,
)


Expand All @@ -55,10 +67,7 @@ def test_wheel(session):
session.install('-r', 'requirements-dev.txt')

with tempfile.TemporaryDirectory(prefix='python-build-wheel-') as dest:
session.run(
'python', '-m', 'build', '--wheel', '--no-isolation', '--outdir', dest,
env={'PYTHONPATH': 'src'}
)
session.run('python', '-m', 'build', '--wheel', '--no-isolation', '--outdir', dest, env={'PYTHONPATH': 'src'})
for target in os.listdir(dest):
if target.endswith('.whl'):
session.install(os.path.join(dest, target))
Expand All @@ -72,10 +81,7 @@ def test_sdist(session):
session.install('-r', 'requirements-dev.txt')

with tempfile.TemporaryDirectory(prefix='python-build-wheel-') as dest:
session.run(
'python', '-m', 'build', '--sdist', '--no-isolation', '--outdir', dest,
env={'PYTHONPATH': 'src'}
)
session.run('python', '-m', 'build', '--sdist', '--no-isolation', '--outdir', dest, env={'PYTHONPATH': 'src'})
for target in os.listdir(dest):
if target.endswith('.tar.gz'):
session.install(os.path.join(dest, target))
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[build-system]
requires = ['setuptools >= 40.8.0', 'wheel']
build-backend = 'setuptools.build_meta'

[tool.black]
line-length = 127
skip-string-normalization = true
target-version = ['py38', 'py37', 'py36', 'py35', 'py27']
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ norecursedirs = tests/integration/*
[flake8]
max-line-length = 127
max-complexity = 10
extend-ignore = E203

[mypy]
ignore_missing_imports = True
Expand Down
58 changes: 27 additions & 31 deletions src/build/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: MIT

'''
"""
python-build - A simple, correct PEP517 package builder
'''
"""
__version__ = '0.0.4'

import contextlib
Expand All @@ -28,42 +28,39 @@

_DEFAULT_BACKEND = {
'build-backend': 'setuptools.build_meta:__legacy__',
'requires': [
'setuptools >= 40.8.0',
'wheel'
]
'requires': ['setuptools >= 40.8.0', 'wheel'],
}


class BuildException(Exception):
'''
"""
Exception raised by ProjectBuilder
'''
"""


class BuildBackendException(Exception):
'''
"""
Exception raised when the backend fails
'''
"""


class TypoWarning(Warning):
'''
"""
Warning raised when a potential typo is found
'''
"""


class IncompleteCheckWarning(Warning):
'''
"""
Warning raised when we have an incomplete check
'''
"""


def check_version(requirement_string, extra=''): # type: (str, str) -> bool
'''
"""
:param requirement_string: Requirement string
:param extra: Extra (eg. test in myproject[test])
'''
"""
import packaging.requirements

if sys.version_info >= (3, 8):
Expand All @@ -72,9 +69,7 @@ def check_version(requirement_string, extra=''): # type: (str, str) -> bool
import importlib_metadata

req = packaging.requirements.Requirement(requirement_string)
env = {
'extra': extra
}
env = {'extra': extra}

if req.marker and not req.marker.evaluate(env):
return True
Expand All @@ -91,7 +86,7 @@ def check_version(requirement_string, extra=''): # type: (str, str) -> bool
warnings.warn(
"Verified that the '{}[{}]' extra is present but did not verify that it is active "
"(it's dependencies are met)".format(req.name, extra),
IncompleteCheckWarning
IncompleteCheckWarning,
)

if req.specifier:
Expand All @@ -106,7 +101,7 @@ def _find_typo(dictionary, expected): # type: (Mapping[str, str], str) -> None
if difflib.SequenceMatcher(None, expected, obj).ratio() >= 0.8:
warnings.warn(
"Found '{}' in pyproject.toml, did you mean '{}'?".format(obj, expected),
TypoWarning
TypoWarning,
)


Expand All @@ -124,9 +119,9 @@ def _working_directory(path): # type: (str) -> Iterator[None]

class ProjectBuilder(object):
def __init__(self, srcdir='.', config_settings=None): # type: (str, Optional[ConfigSettings]) -> None
'''
"""
:param srcdir: Source directory
'''
"""
self.srcdir = os.path.abspath(srcdir)
self.config_settings = config_settings if config_settings else {}

Expand Down Expand Up @@ -156,17 +151,18 @@ def __init__(self, srcdir='.', config_settings=None): # type: (str, Optional[Co

self._backend = self._build_system['build-backend']

self.hook = pep517.wrappers.Pep517HookCaller(self.srcdir, self._backend,
backend_path=self._build_system.get('backend-path'))
self.hook = pep517.wrappers.Pep517HookCaller(
self.srcdir, self._backend, backend_path=self._build_system.get('backend-path')
)

@property
def build_dependencies(self): # type: () -> Set[str]
return set(self._build_system['requires'])

def get_dependencies(self, distribution): # type: (str) -> Set[str]
'''
"""
Returns a set of dependencies
'''
"""
get_requires = getattr(self.hook, 'get_requires_for_build_{}'.format(distribution))

try:
Expand All @@ -178,23 +174,23 @@ def get_dependencies(self, distribution): # type: (str) -> Set[str]
raise BuildBackendException('Backend operation failed: {}'.format(e))

def check_dependencies(self, distribution): # type: (str) -> Set[str]
'''
"""
Returns a set of the missing dependencies
:param distribution: Distribution to build (sdist or wheel)
'''
"""
dependencies = self.get_dependencies(distribution)
dependencies.update(self.build_dependencies)

return {dep for dep in dependencies if not check_version(dep)}

def build(self, distribution, outdir): # type: (str, str) -> None
'''
"""
Builds a distribution
:param distribution: Distribution to build (sdist or wheel)
:param outdir: Output directory
'''
"""
build = getattr(self.hook, 'build_{}'.format(distribution))
outdir = os.path.abspath(outdir)

Expand Down
76 changes: 47 additions & 29 deletions src/build/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def _showwarning(message, category, filename, lineno, file=None, line=None): #


def _error(msg, code=1): # type: (str, int) -> None # pragma: no cover
'''
"""
Prints an error message and exits. Will color the output when writting to a TTY.
:param msg: Error message
:param code: Error code
'''
"""
prefix = 'ERROR'
if sys.stdout.isatty():
prefix = '\33[91m' + prefix + '\33[0m'
Expand Down Expand Up @@ -60,7 +60,7 @@ def _build_in_current_env(builder, outdir, distributions, skip_dependencies=Fals

def build(srcdir, outdir, distributions, config_settings=None, isolation=True, skip_dependencies=False):
# type: (str, str, List[str], Optional[ConfigSettings], bool, bool) -> None
'''
"""
Runs the build process
:param srcdir: Source directory
Expand All @@ -69,7 +69,7 @@ def build(srcdir, outdir, distributions, config_settings=None, isolation=True, s
:param config_settings: Configuration settings to be passed to the backend
:param isolation: Isolate the build in a separate environment
:param skip_dependencies: Do not perform the dependency check
'''
"""
if not config_settings:
config_settings = {}

Expand All @@ -91,42 +91,60 @@ def build(srcdir, outdir, distributions, config_settings=None, isolation=True, s


def main_parser(): # type: () -> argparse.ArgumentParser
'''
"""
Constructs the main parser
'''
"""
cwd = os.getcwd()
out = os.path.join(cwd, 'dist')
parser = argparse.ArgumentParser()
parser.add_argument('srcdir',
type=str, nargs='?', metavar='sourcedir', default=cwd,
help='source directory (defaults to current directory)')
parser.add_argument('--sdist', '-s',
action='store_true',
help='build a source package')
parser.add_argument('--wheel', '-w',
action='store_true',
help='build a wheel')
parser.add_argument('--outdir', '-o', metavar='dist',
type=str, default=out,
help='output directory')
parser.add_argument('--skip-dependencies', '-x',
action='store_true',
help='does not check for the dependencies')
parser.add_argument('--no-isolation', '-n',
action='store_true',
help='do not isolate the build in a virtual environment')
parser.add_argument('--config-setting', '-C',
action='append',
help='pass option to the backend')
parser.add_argument(
'srcdir',
type=str,
nargs='?',
metavar='sourcedir',
default=cwd,
help='source directory (defaults to current directory)',
)
parser.add_argument(
'--sdist',
'-s',
action='store_true',
help='build a source package',
)
parser.add_argument(
'--wheel',
'-w',
action='store_true',
help='build a wheel',
)
parser.add_argument('--outdir', '-o', metavar='dist', type=str, default=out, help='output directory')
parser.add_argument(
'--skip-dependencies',
'-x',
action='store_true',
help='does not check for the dependencies',
)
parser.add_argument(
'--no-isolation',
'-n',
action='store_true',
help='do not isolate the build in a virtual environment',
)
parser.add_argument(
'--config-setting',
'-C',
action='append',
help='pass option to the backend',
)
return parser


def main(cli_args, prog=None): # type: (List[str], Optional[str]) -> None
'''
"""
Parses the CLI arguments and invokes the build process.
:param cli_args: CLI arguments
'''
"""
parser = main_parser()
if prog:
parser.prog = prog
Expand Down
Loading

0 comments on commit fcc5106

Please sign in to comment.