Python bindings setup: Near PEP8 compliance

* lang/python/version.py.in: Fixed most things, but there's still an
  issue near the build portion with the existing Python bugs referenced.
* lang/python/setup.py.in: Now PEP8 compliant.
This commit is contained in:
Ben McGinnes 2018-08-18 18:19:16 +10:00
parent 8a6a73b9c4
commit fc55caccfc
2 changed files with 118 additions and 88 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# Copyright (C) 2016-2017 g10 Code GmbH # Copyright (C) 2016-2018 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org> # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
# #
@ -19,11 +19,14 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from distutils.core import setup, Extension from distutils.core import setup, Extension
import os, os.path, sys
import glob import glob
import os
import os.path
import re import re
import shutil import shutil
import subprocess import subprocess
import sys
# Out-of-tree build of the gpg bindings. # Out-of-tree build of the gpg bindings.
gpg_error_config = ["gpg-error-config"] gpg_error_config = ["gpg-error-config"]
@ -40,9 +43,11 @@ top_builddir = os.environ.get("top_builddir")
if top_builddir: if top_builddir:
# In-tree build. # In-tree build.
in_tree = True in_tree = True
gpgme_config = [os.path.join(top_builddir, "src/gpgme-config")] + gpgme_config_flags gpgme_config = [os.path.join(top_builddir, "src/gpgme-config")
] + gpgme_config_flags
gpgme_h = os.path.join(top_builddir, "src/gpgme.h") gpgme_h = os.path.join(top_builddir, "src/gpgme.h")
library_dirs = [os.path.join(top_builddir, "src/.libs")] # XXX uses libtool internals library_dirs = [os.path.join(top_builddir,
"src/.libs")] # XXX uses libtool internals
extra_macros.update( extra_macros.update(
HAVE_CONFIG_H=1, HAVE_CONFIG_H=1,
HAVE_DATA_H=1, HAVE_DATA_H=1,
@ -55,17 +60,18 @@ else:
devnull = open(os.devnull, "w") devnull = open(os.devnull, "w")
try: try:
subprocess.check_call(gpgme_config + ['--version'], subprocess.check_call(gpgme_config + ['--version'], stdout=devnull)
stdout=devnull)
except: except:
sys.exit("Could not find gpgme-config. " + sys.exit("Could not find gpgme-config. " +
"Please install the libgpgme development package.") "Please install the libgpgme development package.")
def getconfig(what, config=gpgme_config): def getconfig(what, config=gpgme_config):
confdata = subprocess.Popen(config + ["--%s" % what], confdata = subprocess.Popen(
stdout=subprocess.PIPE).communicate()[0] config + ["--%s" % what], stdout=subprocess.PIPE).communicate()[0]
return [x for x in confdata.decode('utf-8').split() if x != ''] return [x for x in confdata.decode('utf-8').split() if x != '']
version = version_raw = getconfig("version")[0] version = version_raw = getconfig("version")[0]
if '-' in version: if '-' in version:
version = version.split('-')[0] version = version.split('-')[0]
@ -90,7 +96,7 @@ for item in getconfig('cflags'):
include_dirs.append(item[2:]) include_dirs.append(item[2:])
elif item.startswith("-D"): elif item.startswith("-D"):
defitem = item[2:].split("=", 1) defitem = item[2:].split("=", 1)
if len(defitem)==2: if len(defitem) == 2:
define_macros.append((defitem[0], defitem[1])) define_macros.append((defitem[0], defitem[1]))
else: else:
define_macros.append((defitem[0], None)) define_macros.append((defitem[0], None))
@ -98,49 +104,59 @@ for item in getconfig('cflags'):
# Adjust include and library locations in case of win32 # Adjust include and library locations in case of win32
uname_s = os.popen("uname -s").read() uname_s = os.popen("uname -s").read()
if uname_s.startswith("MINGW32"): if uname_s.startswith("MINGW32"):
mnts = [x.split()[0:3:2] for x in os.popen("mount").read().split("\n") if x] mnts = [
tmplist = sorted([(len(x[1]), x[1], x[0]) for x in mnts]) x.split()[0:3:2] for x in os.popen("mount").read().split("\n") if x
tmplist.reverse() ]
extra_dirs = [] tmplist = sorted([(len(x[1]), x[1], x[0]) for x in mnts])
for item in include_dirs: tmplist.reverse()
for ln, mnt, tgt in tmplist: extra_dirs = []
if item.startswith(mnt): for item in include_dirs:
item = os.path.normpath(item[ln:]) for ln, mnt, tgt in tmplist:
while item[0] == os.path.sep: if item.startswith(mnt):
item = item[1:] item = os.path.normpath(item[ln:])
extra_dirs.append(os.path.join(tgt, item)) while item[0] == os.path.sep:
break item = item[1:]
include_dirs += extra_dirs extra_dirs.append(os.path.join(tgt, item))
for item in [x[2:] for x in libs if x.startswith("-L")]: break
for ln, mnt, tgt in tmplist: include_dirs += extra_dirs
if item.startswith(mnt): for item in [x[2:] for x in libs if x.startswith("-L")]:
item = os.path.normpath(item[ln:]) for ln, mnt, tgt in tmplist:
while item[0] == os.path.sep: if item.startswith(mnt):
item = item[1:] item = os.path.normpath(item[ln:])
library_dirs.append(os.path.join(tgt, item)) while item[0] == os.path.sep:
break item = item[1:]
library_dirs.append(os.path.join(tgt, item))
break
def in_srcdir(name): def in_srcdir(name):
return os.path.join(os.environ.get("srcdir", ""), name) return os.path.join(os.environ.get("srcdir", ""), name)
def up_to_date(source, target): def up_to_date(source, target):
return (os.path.exists(target) return (os.path.exists(target) and
and os.path.getmtime(source) <= os.path.getmtime(target)) os.path.getmtime(source) <= os.path.getmtime(target))
# We build an Extension using SWIG, which generates a Python module. # We build an Extension using SWIG, which generates a Python module.
# By default, the 'build_py' step is run before 'build_ext', and # By default, the 'build_py' step is run before 'build_ext', and
# therefore the generated Python module is not copied into the build # therefore the generated Python module is not copied into the build
# directory. # directory.
# Bug: http://bugs.python.org/issue1016626 # Bugs: https://bugs.python.org/issue1016626
# https://bugs.python.org/issue2624
# Workaround: # Workaround:
# http://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module # https://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module
from distutils.command.build import build from distutils.command.build import build
class BuildExtFirstHack(build):
class BuildExtFirstHack(build):
def _read_header(self, header, cflags): def _read_header(self, header, cflags):
tmp_include = self._in_build_base("include1.h") tmp_include = self._in_build_base("include1.h")
with open(tmp_include, 'w') as f: with open(tmp_include, 'w') as f:
f.write("#include <%s>" % header) f.write("#include <%s>" % header)
return subprocess.check_output(os.environ.get('CPP', 'cc -E').split() + cflags + [tmp_include]).decode('utf-8') return subprocess.check_output(
os.environ.get('CPP', 'cc -E').split() + cflags +
[tmp_include]).decode('utf-8')
def _write_if_unchanged(self, target, content): def _write_if_unchanged(self, target, content):
if os.path.exists(target): if os.path.exists(target):
@ -158,13 +174,14 @@ class BuildExtFirstHack(build):
def _generate_errors_i(self): def _generate_errors_i(self):
try: try:
subprocess.check_call(gpg_error_config + ['--version'], subprocess.check_call(
stdout=devnull) gpg_error_config + ['--version'], stdout=devnull)
except: except:
sys.exit("Could not find gpg-error-config. " + sys.exit("Could not find gpg-error-config. " +
"Please install the libgpg-error development package.") "Please install the libgpg-error development package.")
gpg_error_content = self._read_header("gpg-error.h", getconfig("cflags", config=gpg_error_config)) gpg_error_content = self._read_header(
"gpg-error.h", getconfig("cflags", config=gpg_error_config))
filter_re = re.compile(r'GPG_ERR_[^ ]* =') filter_re = re.compile(r'GPG_ERR_[^ ]* =')
rewrite_re = re.compile(r' *(.*) = .*') rewrite_re = re.compile(r' *(.*) = .*')
@ -173,9 +190,11 @@ class BuildExtFirstHack(build):
for line in gpg_error_content.splitlines(): for line in gpg_error_content.splitlines():
if not filter_re.search(line): if not filter_re.search(line):
continue continue
errors_i_content += rewrite_re.sub(r'%constant long \1 = \1;'+'\n', line.strip()) errors_i_content += rewrite_re.sub(
r'%constant long \1 = \1;' + '\n', line.strip())
self._write_if_unchanged(self._in_build_base("errors.i"), errors_i_content) self._write_if_unchanged(
self._in_build_base("errors.i"), errors_i_content)
def _in_build_base(self, name): def _in_build_base(self, name):
return os.path.join(self.build_base, name) return os.path.join(self.build_base, name)
@ -191,7 +210,8 @@ class BuildExtFirstHack(build):
# Copy due to http://bugs.python.org/issue2624 # Copy due to http://bugs.python.org/issue2624
# Avoid creating in srcdir # Avoid creating in srcdir
for source, target in ((in_srcdir(n), self._in_build_base(n)) for source, target in ((in_srcdir(n), self._in_build_base(n))
for n in ('gpgme.i', 'helpers.c', 'private.h', 'helpers.h')): for n in ('gpgme.i', 'helpers.c', 'private.h',
'helpers.h')):
if not up_to_date(source, target): if not up_to_date(source, target):
shutil.copy2(source, target) shutil.copy2(source, target)
@ -203,53 +223,60 @@ class BuildExtFirstHack(build):
def run(self): def run(self):
self._generate() self._generate()
swig_sources.extend((self._in_build_base('gpgme.i'), self._in_build_base('helpers.c'))) swig_sources.extend((self._in_build_base('gpgme.i'),
swig_opts.extend(['-I' + self.build_base, self._in_build_base('helpers.c')))
'-outdir', os.path.join(self.build_lib, 'gpg')]) swig_opts.extend([
'-I' + self.build_base, '-outdir',
os.path.join(self.build_lib, 'gpg')
])
include_dirs.insert(0, self.build_base) include_dirs.insert(0, self.build_base)
self.run_command('build_ext') self.run_command('build_ext')
build.run(self) build.run(self)
py3 = [] if sys.version_info.major < 3 else ['-py3'] py3 = [] if sys.version_info.major < 3 else ['-py3']
swig_sources = [] swig_sources = []
swig_opts = ['-threads'] + py3 + extra_swig_opts swig_opts = ['-threads'] + py3 + extra_swig_opts
swige = Extension("gpg._gpgme", swige = Extension(
sources = swig_sources, "gpg._gpgme",
swig_opts = swig_opts, sources=swig_sources,
include_dirs = include_dirs, swig_opts=swig_opts,
define_macros = define_macros, include_dirs=include_dirs,
library_dirs = library_dirs, define_macros=define_macros,
extra_link_args = libs) library_dirs=library_dirs,
extra_link_args=libs)
setup(name="gpg", setup(
cmdclass={'build': BuildExtFirstHack}, name="gpg",
version="@VERSION@", cmdclass={'build': BuildExtFirstHack},
description='Python bindings for GPGME GnuPG cryptography library', version="@VERSION@",
# XXX add a long description description='Python bindings for GPGME GnuPG cryptography library',
#long_description=long_description, # TODO: add a long description
author='The GnuPG hackers', # long_description=long_description,
author_email='gnupg-devel@gnupg.org', author='The GnuPG hackers',
url='https://www.gnupg.org', author_email='gnupg-devel@gnupg.org',
ext_modules=[swige], url='https://www.gnupg.org',
packages = ['gpg', 'gpg.constants', 'gpg.constants.data', ext_modules=[swige],
'gpg.constants.keylist', 'gpg.constants.sig', packages=[
'gpg.constants.tofu'], 'gpg', 'gpg.constants', 'gpg.constants.data', 'gpg.constants.keylist',
license="LGPL2.1+ (the library), GPL2+ (tests and examples)", 'gpg.constants.sig', 'gpg.constants.tofu'
classifiers=[ ],
'Development Status :: 4 - Beta', license="LGPL2.1+ (the library), GPL2+ (tests and examples)",
'Intended Audience :: Developers', classifiers=[
'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)', 'Development Status :: 4 - Beta',
'Programming Language :: Python :: 2', 'Intended Audience :: Developers',
'Programming Language :: Python :: 2.7', 'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
'Programming Language :: Python :: 3', 'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.5',
'Operating System :: POSIX', 'Programming Language :: Python :: 3.6',
'Operating System :: Microsoft :: Windows', 'Programming Language :: Python :: 3.7',
'Topic :: Communications :: Email', 'Operating System :: POSIX',
'Topic :: Security :: Cryptography', 'Operating System :: Microsoft :: Windows',
], 'Topic :: Communications :: Email',
'Topic :: Security :: Cryptography',
],
) )

View File

@ -1,4 +1,6 @@
# Copyright (C) 2016 g10 Code GmbH # -*- coding: utf-8 -*-
# Copyright (C) 2016-2018 g10 Code GmbH
# Copyright (C) 2015 Ben McGinnes <ben@adversary.org> # Copyright (C) 2015 Ben McGinnes <ben@adversary.org>
# Copyright (C) 2004 Igor Belyi <belyi@users.sourceforge.net> # Copyright (C) 2004 Igor Belyi <belyi@users.sourceforge.net>
# #
@ -17,10 +19,11 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
del absolute_import, print_function
from . import gpgme from . import gpgme
del absolute_import, print_function
productname = 'gpg' productname = 'gpg'
versionstr = "@VERSION@" versionstr = "@VERSION@"
gpgme_versionstr = gpgme.GPGME_VERSION gpgme_versionstr = gpgme.GPGME_VERSION
@ -32,8 +35,8 @@ minor = versionlist[1]
patch = versionlist[2] patch = versionlist[2]
copyright = """\ copyright = """\
Copyright (C) 2016 g10 Code GmbH Copyright (C) 2016-2018 g10 Code GmbH
Copyright (C) 2015 Ben McGinnes Copyright (C) 2015 Benjamin D. McGinnes
Copyright (C) 2014-2015 Martin Albrecht Copyright (C) 2014-2015 Martin Albrecht
Copyright (C) 2004-2008 Igor Belyi Copyright (C) 2004-2008 Igor Belyi
Copyright (C) 2002 John Goerzen""" Copyright (C) 2002 John Goerzen"""
@ -44,8 +47,8 @@ author_email = "gnupg-devel@gnupg.org"
description = "Python support for GPGME GnuPG cryptography library" description = "Python support for GPGME GnuPG cryptography library"
homepage = "https://gnupg.org" homepage = "https://gnupg.org"
license = """Copyright (C) 2016 g10 Code GmbH license = """Copyright (C) 2016-2018 g10 Code GmbH
Copyright (C) 2015 Ben McGinnes <ben@adversary.org> Copyright (C) 2015 Benjamin D. McGinnes <ben@adversary.org>
Copyright (C) 2014, 2015 Martin Albrecht <martinralbrecht@googlemail.com> Copyright (C) 2014, 2015 Martin Albrecht <martinralbrecht@googlemail.com>
Copyright (C) 2004, 2008 Igor Belyi <belyi@users.sourceforge.net> Copyright (C) 2004, 2008 Igor Belyi <belyi@users.sourceforge.net>
Copyright (C) 2002 John Goerzen <jgoerzen@complete.org> Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>