Fixing flake8 warnings and errors

flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude benchmark

Excluding the benchmark directory (due to protobuf generated files)
Also removing some Python2 specific code
This commit is contained in:
Jacob Alexander
2019-09-26 22:18:28 -07:00
parent 1f0200af9c
commit b3021e4f6b
37 changed files with 536 additions and 452 deletions

View File

@@ -2,6 +2,7 @@
Largely adapted from h5py
"""
# flake8: noqa F401 F403
from .msg import *
from .config import *

View File

@@ -5,26 +5,29 @@ import os
import tempfile
def build_libcapnp(bundle_dir, build_dir, verbose=False):
bundle_dir = os.path.abspath(bundle_dir)
capnp_dir = os.path.join(bundle_dir, 'capnproto-c++')
build_dir = os.path.abspath(build_dir)
'''
Build capnproto
'''
bundle_dir = os.path.abspath(bundle_dir)
capnp_dir = os.path.join(bundle_dir, 'capnproto-c++')
build_dir = os.path.abspath(build_dir)
with tempfile.TemporaryFile() as f:
stdout = f
if verbose:
stdout = None
cxxflags = os.environ.get('CXXFLAGS', None)
os.environ['CXXFLAGS'] = (cxxflags or '') + ' -fPIC -O2 -DNDEBUG'
conf = subprocess.Popen(['./configure', '--disable-shared', '--prefix', build_dir], cwd=capnp_dir, stdout=stdout)
returncode = conf.wait()
if returncode != 0:
raise RuntimeError('Configure failed')
with tempfile.TemporaryFile() as f:
stdout = f
if verbose:
stdout = None
cxxflags = os.environ.get('CXXFLAGS', None)
os.environ['CXXFLAGS'] = (cxxflags or '') + ' -fPIC -O2 -DNDEBUG'
conf = subprocess.Popen(['./configure', '--disable-shared', '--prefix', build_dir], cwd=capnp_dir, stdout=stdout)
returncode = conf.wait()
if returncode != 0:
raise RuntimeError('Configure failed')
make = subprocess.Popen(['make', '-j4', 'install'], cwd=capnp_dir, stdout=stdout)
returncode = make.wait()
if cxxflags is None:
del os.environ['CXXFLAGS']
else:
os.environ['CXXFLAGS'] = cxxflags
if returncode != 0:
raise RuntimeError('Make failed')
make = subprocess.Popen(['make', '-j4', 'install'], cwd=capnp_dir, stdout=stdout)
returncode = make.wait()
if cxxflags is None:
del os.environ['CXXFLAGS']
else:
os.environ['CXXFLAGS'] = cxxflags
if returncode != 0:
raise RuntimeError('Make failed')

View File

@@ -1,12 +1,11 @@
"""utilities for fetching build dependencies."""
#-----------------------------------------------------------------------------
#
# Copyright (C) PyZMQ Developers
# Distributed under the terms of the Modified BSD License.
#
# This bundling code is largely adapted from pyzmq-static's get.sh by
# Brandon Craig-Rhodes, which is itself BSD licensed.
#-----------------------------------------------------------------------------
#
# Adapted for use in pycapnp from pyzmq. See https://github.com/zeromq/pyzmq
# for original project.
@@ -17,7 +16,6 @@ import shutil
import stat
import sys
import tarfile
from glob import glob
from subprocess import Popen, PIPE
try:
@@ -27,27 +25,28 @@ except ImportError:
# py3
from urllib.request import urlopen
from .msg import fatal, debug, info, warn
from .msg import fatal, info, warn
pjoin = os.path.join
#-----------------------------------------------------------------------------
#
# Constants
#-----------------------------------------------------------------------------
#
bundled_version = (0,7,0)
libcapnp = "capnproto-c++-%i.%i.%i.tar.gz" % (bundled_version)
libcapnp_url = "https://capnproto.org/" + libcapnp
bundled_version = (0, 7, 4)
libcapnp_name = "capnproto-c++-%i.%i.%i.tar.gz" % (bundled_version)
libcapnp_url = "https://capnproto.org/" + libcapnp_name
HERE = os.path.dirname(__file__)
ROOT = os.path.dirname(HERE)
#-----------------------------------------------------------------------------
#
# Utilities
#-----------------------------------------------------------------------------
#
def untgz(archive):
"""Remove .tar.gz"""
return archive.replace('.tar.gz', '')
def localpath(*args):
@@ -69,9 +68,9 @@ def fetch_archive(savedir, url, fname, force=False):
f.write(req.read())
return dest
#-----------------------------------------------------------------------------
#
# libcapnp
#-----------------------------------------------------------------------------
#
def fetch_libcapnp(savedir, url=None):
"""download and extract libcapnp"""
@@ -83,7 +82,7 @@ def fetch_libcapnp(savedir, url=None):
if os.path.exists(dest):
info("already have %s" % dest)
return
fname = fetch_archive(savedir, url, libcapnp)
fname = fetch_archive(savedir, url, libcapnp_name)
tf = tarfile.open(fname)
with_version = pjoin(savedir, tf.firstmember.path)
tf.extractall(savedir)
@@ -96,7 +95,7 @@ def fetch_libcapnp(savedir, url=None):
conf = Popen(['autoreconf', '-i'], cwd=cpp_dir)
returncode = conf.wait()
if returncode != 0:
raise RuntimeError('Autoreconf failed. Make sure autotools are installed on your system.')
raise RuntimeError('Autoreconf failed. Make sure autotools are installed on your system.')
shutil.move(cpp_dir, dest)
@@ -120,7 +119,7 @@ def stage_platform_hpp(capnproot):
p = Popen('./configure', cwd=capnproot, shell=True,
stdout=PIPE, stderr=PIPE,
)
o,e = p.communicate()
_, e = p.communicate()
if p.returncode:
warn("failed to configure libcapnp:\n%s" % e)
if sys.platform == 'darwin':
@@ -146,14 +145,14 @@ def copy_and_patch_libcapnp(capnp, libcapnp):
if sys.platform.startswith('win'):
return
# copy libcapnp into capnp for bdist
local = localpath('capnp',libcapnp)
local = localpath('capnp', libcapnp)
if not capnp and not os.path.exists(local):
fatal("Please specify capnp prefix via `setup.py configure --capnp=/path/to/capnp` "
"or copy libcapnp into capnp/ manually prior to running bdist.")
try:
# resolve real file through symlinks
lib = os.path.realpath(pjoin(capnp, 'lib', libcapnp))
print ("copying %s -> %s"%(lib, local))
print ("copying %s -> %s" % (lib, local))
shutil.copy(lib, local)
except Exception:
if not os.path.exists(local):
@@ -167,11 +166,11 @@ def copy_and_patch_libcapnp(capnp, libcapnp):
mode = os.stat(local).st_mode
os.chmod(local, mode | stat.S_IWUSR)
# patch install_name on darwin, instead of using rpath
cmd = ['install_name_tool', '-id', '@loader_path/../%s'%libcapnp, local]
cmd = ['install_name_tool', '-id', '@loader_path/../%s' % libcapnp, local]
try:
p = Popen(cmd, stdout=PIPE,stderr=PIPE)
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
except OSError:
fatal("install_name_tool not found, cannot patch libcapnp for bundling.")
out,err = p.communicate()
_, err = p.communicate()
if p.returncode:
fatal("Could not patch bundled libcapnp install_name: %s"%err, p.returncode)
fatal("Could not patch bundled libcapnp install_name: %s" % err, p.returncode)

View File

@@ -1,5 +1,5 @@
"""Config functions"""
#-----------------------------------------------------------------------------
#
# Copyright (C) PyZMQ Developers
#
# This file is part of pyzmq, copied and adapted from h5py.
@@ -9,23 +9,24 @@
#
# Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------
#
import sys
import os
import json
from .msg import debug, warn
try:
from configparser import ConfigParser
except:
except Exception:
from ConfigParser import ConfigParser
pjoin = os.path.join
from .msg import debug, fatal, warn
#-----------------------------------------------------------------------------
#
# Utility functions (adapted from h5py: http://h5py.googlecode.com)
#-----------------------------------------------------------------------------
#
def load_config(name, base='conf'):
@@ -46,7 +47,7 @@ def save_config(name, data, base='conf'):
"""Save config dict to JSON"""
if not os.path.exists(base):
os.mkdir(base)
fname = pjoin(base, name+'.json')
fname = pjoin(base, name + '.json')
with open(fname, 'w') as f:
json.dump(data, f, indent=2)
@@ -69,7 +70,7 @@ def get_eargs():
def cfg2dict(cfg):
"""turn a ConfigParser into a nested dict
because ConfigParser objects are dumb.
"""
d = {}
@@ -120,7 +121,7 @@ def config_from_prefix(prefix):
def merge(into, d):
"""merge two containers
into is updated, d has priority
"""
if isinstance(into, dict):
@@ -130,10 +131,9 @@ def merge(into, d):
else:
into[key] = merge(into[key], d[key])
return into
elif isinstance(into, list):
if isinstance(into, list):
return into + d
else:
return d
return d
def discover_settings(conf_base=None):
""" Discover custom settings for ZMQ path"""
@@ -147,11 +147,11 @@ def discover_settings(conf_base=None):
}
if sys.platform.startswith('win'):
settings['have_sys_un_h'] = False
if conf_base:
# lowest priority
merge(settings, load_config('config', conf_base))
merge(settings, get_cfg_args())
merge(settings, get_eargs())
return settings

View File

@@ -23,7 +23,7 @@ pjoin = os.path.join
root = os.path.abspath(pjoin(os.path.dirname(__file__), os.path.pardir))
sys.path.insert(0, pjoin(root, 'zmq', 'utils'))
from constant_names import all_names, no_prefix
from constant_names import all_names, no_prefix # noqa: E402
ifndef_t = """#ifndef {0}
#define {0} (_PYZMQ_UNDEFINED)
@@ -38,7 +38,7 @@ def cython_enums():
lines.append('enum: ZMQ_{0} "{0}"'.format(name))
else:
lines.append('enum: ZMQ_{0}'.format(name))
return dict(ZMQ_ENUMS='\n '.join(lines))
def ifndefs():
@@ -79,5 +79,6 @@ def render_constants():
generate_file("constants.pxi", constants_pyx, pjoin(root, 'zmq', 'backend', 'cython'))
generate_file("zmq_constants.h", ifndefs, pjoin(root, 'zmq', 'utils'))
if __name__ == '__main__':
render_constants()

View File

@@ -1,5 +1,5 @@
"""Detect zmq version"""
#-----------------------------------------------------------------------------
#
# Copyright (C) PyZMQ Developers
#
# This file is part of pyzmq, copied and adapted from h5py.
@@ -9,7 +9,7 @@
#
# Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------
#
#
# Adapted for use in pycapnp from pyzmq. See https://github.com/zeromq/pyzmq
# for original project.
@@ -29,20 +29,20 @@ from .patch import patch_lib_paths
pjoin = os.path.join
#-----------------------------------------------------------------------------
#
# Utility functions (adapted from h5py: http://h5py.googlecode.com)
#-----------------------------------------------------------------------------
#
def test_compilation(cfile, compiler=None, **compiler_attrs):
"""Test simple compilation with given settings"""
cc = get_compiler(compiler, **compiler_attrs)
efile, ext = os.path.splitext(cfile)
efile, _ = os.path.splitext(cfile)
cpreargs = lpreargs = []
if sys.platform == 'darwin':
# use appropriate arch for compiler
if platform.architecture()[0]=='32bit':
if platform.architecture()[0] == '32bit':
if platform.processor() == 'powerpc':
cpu = 'ppc'
else:
@@ -53,7 +53,7 @@ def test_compilation(cfile, compiler=None, **compiler_attrs):
# allow for missing UB arch, since it will still work:
lpreargs = ['-undefined', 'dynamic_lookup']
if sys.platform == 'sunos5':
if platform.architecture()[0]=='32bit':
if platform.architecture()[0] == '32bit':
lpreargs = ['-m32']
else:
lpreargs = ['-m64']
@@ -65,6 +65,7 @@ def test_compilation(cfile, compiler=None, **compiler_attrs):
return efile
def compile_and_run(basedir, src, compiler=None, **compiler_attrs):
"""Compile and run"""
if not os.path.exists(basedir):
os.makedirs(basedir)
cfile = pjoin(basedir, os.path.basename(src))
@@ -130,11 +131,11 @@ def detect_version(basedir, compiler=None, **compiler_attrs):
rc, so, se = get_output_error([efile])
if rc:
msg = "Error running version detection script:\n%s\n%s" % (so,se)
msg = "Error running version detection script:\n%s\n%s" % (so, se)
logging.error(msg)
raise IOError(msg)
handlers = {'vers': lambda val: tuple(int(v) for v in val.split('.'))}
handlers = {'vers': lambda val: tuple(int(v) for v in val.split('.'))}
props = {}
for line in (x for x in so.split('\n') if x):
@@ -161,8 +162,9 @@ def test_build():
return detected
def erase_dir(dir):
def erase_dir(path):
"""Erase directory"""
try:
shutil.rmtree(dir)
shutil.rmtree(path)
except Exception:
pass

View File

@@ -4,7 +4,6 @@
# Distributed under the terms of the Modified BSD License.
import os
import sys
import logging
from distutils import ccompiler
from distutils.sysconfig import customize_compiler
@@ -14,13 +13,8 @@ from subprocess import Popen, PIPE
pjoin = os.path.join
if sys.version_info[0] >= 3:
u = lambda x: x
else:
u = lambda x: x.decode('utf8', 'replace')
def customize_mingw(cc):
"""customize mingw"""
# strip -mno-cygwin from mingw32 (Python Issue #12641)
for cmd in [cc.compiler, cc.compiler_cxx, cc.compiler_so, cc.linker_exe, cc.linker_so]:
if '-mno-cygwin' in cmd:
@@ -55,11 +49,10 @@ def get_output_error(cmd):
try:
result = Popen(cmd, stdout=PIPE, stderr=PIPE)
except IOError as e:
return -1, u(''), u('Failed to run %r: %r' % (cmd, e))
return -1, '', 'Failed to run %r: %r' % (cmd, e)
so, se = result.communicate()
# unicode:
so = so.decode('utf8', 'replace')
se = se.decode('utf8', 'replace')
return result.returncode, so, se

View File

@@ -9,9 +9,9 @@ import os
import sys
import logging
#-----------------------------------------------------------------------------
#
# Logging (adapted from h5py: http://h5py.googlecode.com)
#-----------------------------------------------------------------------------
#
logger = logging.getLogger()
@@ -22,18 +22,22 @@ else:
logger.addHandler(logging.StreamHandler(sys.stderr))
def debug(msg):
"""Debug"""
logger.debug(msg)
def info(msg):
"""Info"""
logger.info(msg)
def fatal(msg, code=1):
logger.error("Fatal: " + msg)
"""Fatal"""
logger.error("Fatal: %s", msg)
exit(code)
def warn(msg):
logger.error("Warning: " + msg)
"""Warning"""
logger.error("Warning: %s", msg)
def line(c='*', width=48):
"""Horizontal rule"""
print(c * (width // len(c)))

View File

@@ -20,7 +20,7 @@ LIB_PAT = re.compile(r"\s*(.*) \(compatibility version (\d+\.\d+\.\d+), "
def _get_libs(fname):
rc, so, se = get_output_error(['otool', '-L', fname])
if rc:
logging.error("otool -L %s failed: %r" % (fname, se))
logging.error("otool -L %s failed: %r", fname, se)
return
for line in so.splitlines()[1:]:
m = LIB_PAT.match(line)
@@ -33,6 +33,7 @@ def _find_library(lib, path):
real_lib = os.path.join(d, lib)
if os.path.exists(real_lib):
return real_lib
return None
def _install_name_change(fname, lib, real_lib):
rc, so, se = get_output_error(['install_name_tool', '-change', lib, real_lib, fname])
@@ -41,15 +42,15 @@ def _install_name_change(fname, lib, real_lib):
def patch_lib_paths(fname, library_dirs):
"""Load any weakly-defined libraries from their real location
(only on OS X)
- Find libraries with `otool -L`
- Update with `install_name_tool -change`
"""
if sys.platform != 'darwin':
return
libs = _get_libs(fname)
for lib in libs:
if not lib.startswith(('@', '/')):
@@ -58,4 +59,4 @@ def patch_lib_paths(fname, library_dirs):
_install_name_change(fname, lib, real_lib)
__all__ = ['patch_lib_paths']
__all__ = ['patch_lib_paths']