Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
include: ${{ fromJson(needs.generate-wheels-matrix.outputs.include) }}

env:
PYXMLSEC_LIBXML2_VERSION: 2.14.5
PYXMLSEC_LIBXML2_VERSION: 2.14.6
PYXMLSEC_LIBXSLT_VERSION: 1.1.43

steps:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ repos:
rev: v1.18.2
hooks:
- id: mypy
exclude: (setup.py|tests/.*.py|doc/.*)
exclude: (setup.py|tests|build_support/.*.py|doc/.*)
types: []
files: ^.*.pyi?$
additional_dependencies: [lxml-stubs, types-docutils]
Expand Down
Empty file added build_support/__init__.py
Empty file.
86 changes: 86 additions & 0 deletions build_support/build_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import sys
from distutils import log
from distutils.errors import DistutilsError

from setuptools.command.build_ext import build_ext as build_ext_orig

from .static_build import CrossCompileInfo, StaticBuildHelper


class build_ext(build_ext_orig):
def info(self, message):
self.announce(message, level=log.INFO)

def run(self):
ext = self.ext_map['xmlsec']
self.debug = os.environ.get('PYXMLSEC_ENABLE_DEBUG', False)
self.static = os.environ.get('PYXMLSEC_STATIC_DEPS', False)
self.size_opt = os.environ.get('PYXMLSEC_OPTIMIZE_SIZE', True)

if self.static or sys.platform == 'win32':
helper = StaticBuildHelper(self)
helper.prepare(sys.platform)
else:
import pkgconfig

try:
config = pkgconfig.parse('xmlsec1')
except OSError as error:
raise DistutilsError('Unable to invoke pkg-config.') from error
except pkgconfig.PackageNotFoundError as error:
raise DistutilsError('xmlsec1 is not installed or not in path.') from error

if config is None or not config.get('libraries'):
raise DistutilsError('Bad or incomplete result returned from pkg-config.')

ext.define_macros.extend(config['define_macros'])
ext.include_dirs.extend(config['include_dirs'])
ext.library_dirs.extend(config['library_dirs'])
ext.libraries.extend(config['libraries'])

import lxml

ext.include_dirs.extend(lxml.get_include())

ext.define_macros.extend(
[('MODULE_NAME', self.distribution.metadata.name), ('MODULE_VERSION', self.distribution.metadata.version)]
)
for key, value in ext.define_macros:
if key == 'XMLSEC_CRYPTO' and not (value.startswith('"') and value.endswith('"')):
ext.define_macros.remove((key, value))
ext.define_macros.append((key, f'"{value}"'))
break

if sys.platform == 'win32':
ext.extra_compile_args.append('/Zi')
else:
ext.extra_compile_args.extend(
[
'-g',
'-std=c99',
'-fPIC',
'-fno-strict-aliasing',
'-Wno-error=declaration-after-statement',
'-Werror=implicit-function-declaration',
]
)

if self.debug:
ext.define_macros.append(('PYXMLSEC_ENABLE_DEBUG', '1'))
if sys.platform == 'win32':
ext.extra_compile_args.append('/Od')
else:
ext.extra_compile_args.append('-Wall')
ext.extra_compile_args.append('-O0')
else:
if self.size_opt:
if sys.platform == 'win32':
ext.extra_compile_args.append('/Os')
else:
ext.extra_compile_args.append('-Os')

super().run()


__all__ = ('CrossCompileInfo', 'build_ext')
29 changes: 29 additions & 0 deletions build_support/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import contextlib
import json
from urllib.request import Request, urlopen

DEFAULT_USER_AGENT = 'https://github.com/xmlsec/python-xmlsec'
DOWNLOAD_USER_AGENT = 'python-xmlsec build'


def make_request(url, github_token=None, json_response=False):
headers = {'User-Agent': DEFAULT_USER_AGENT}
if github_token:
headers['authorization'] = 'Bearer ' + github_token
request = Request(url, headers=headers)
with contextlib.closing(urlopen(request)) as response:
charset = response.headers.get_content_charset() or 'utf-8'
content = response.read().decode(charset)
if json_response:
return json.loads(content)
return content


def download_lib(url, filename):
request = Request(url, headers={'User-Agent': DOWNLOAD_USER_AGENT})
with urlopen(request) as response, open(filename, 'wb') as target:
while True:
chunk = response.read(8192)
if not chunk:
break
target.write(chunk)
77 changes: 77 additions & 0 deletions build_support/releases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import html.parser
import os
import re
from distutils import log
from distutils.version import StrictVersion as Version

from .network import make_request


class HrefCollector(html.parser.HTMLParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.hrefs = []

def handle_starttag(self, tag, attrs):
if tag == 'a':
for name, value in attrs:
if name == 'href':
self.hrefs.append(value)


def latest_release_from_html(url, matcher):
content = make_request(url)
collector = HrefCollector()
collector.feed(content)
hrefs = collector.hrefs

def comp(text):
try:
return Version(matcher.match(text).groupdict()['version'])
except (AttributeError, ValueError):
return Version('0.0')

latest = max(hrefs, key=comp)
return f'{url}/{latest}'


def latest_release_from_gnome_org_cache(url, lib_name):
cache_url = f'{url}/cache.json'
cache = make_request(cache_url, json_response=True)
latest_version = cache[2][lib_name][-1]
latest_source = cache[1][lib_name][latest_version]['tar.xz']
return f'{url}/{latest_source}'


def latest_release_json_from_github_api(repo):
api_url = f'https://api.github.com/repos/{repo}/releases/latest'
token = os.environ.get('GH_TOKEN')
if token:
log.info('Using GitHub token to avoid rate limiting')
return make_request(api_url, token, json_response=True)


def latest_openssl_release():
return latest_release_json_from_github_api('openssl/openssl')['tarball_url']


def latest_zlib_release():
return latest_release_from_html('https://zlib.net/fossils', re.compile('zlib-(?P<version>.*).tar.gz'))


def latest_libiconv_release():
return latest_release_from_html('https://ftpmirror.gnu.org/libiconv', re.compile('libiconv-(?P<version>.*).tar.gz'))


def latest_libxml2_release():
return latest_release_from_gnome_org_cache('https://download.gnome.org/sources/libxml2', 'libxml2')


def latest_libxslt_release():
return latest_release_from_gnome_org_cache('https://download.gnome.org/sources/libxslt', 'libxslt')


def latest_xmlsec_release():
assets = latest_release_json_from_github_api('lsh123/xmlsec')['assets']
(tar_gz,) = [asset for asset in assets if asset['name'].endswith('.tar.gz')]
return tar_gz['browser_download_url']
Loading
Loading