Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github Actions pipeline for building and publishsing python wheels #27

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions .github/workflows/build-wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Mercury wheels Build and Publish to PyPI

on:
workflow_dispatch:
push:
branches:
- main

jobs:
build-mercury-wheels:
name: Build wheels on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
environment:
name: pypi
url: https://pypi.org/p/mercury-python
permissions:
id-token: write
strategy:
matrix:
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v4
with:
detch-depth: 1

- name: Install packages
run: sudo apt-get update && sudo apt-get install -y zlib1g-dev libssl-dev make

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'

- name: Set up QEMU
if: runner.os == 'Linux'
uses: docker/setup-qemu-action@v3
with:
platforms: all

- name: Install pip packages
run: python -m pip install cython cibuildwheel==2.20.0

- name: Build Mercury
run: ./configure && make && cp -r src/cython/* ./

- name: Build wheels
uses: pypa/cibuildwheel@v2.20.0
env:
CIBW_ARCHS_LINUX: x86_64 aarch64
CIBW_ENVIRONMENT: CC='g++' CXX='g++' MERCURY_DIR='./'
CIBW_SKIP: "*-musllinux_* pp*"
CIBW_BEFORE_ALL: yum install -y openssl-devel make zlib-devel || apt-get install -y zlib1g-dev libssl-dev make || apk add zlib1g-dev libssl-dev make

- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: wheelhouse/
skip-existing: true
8 changes: 8 additions & 0 deletions src/cython/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build-system]
requires = [
"setuptools>=42",
"Cython",
]

[tool.cibuildwheel]
environment = "CC='g++' CXX='g++' ENV_CFLAGS='-DSSLNEW'"
120 changes: 70 additions & 50 deletions src/cython/setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from setuptools import Extension, setup
#from distutils.core import setup, Extension

# from distutils.core import setup, Extension
from Cython.Distutils import build_ext
#from distutils.extension import Extension

# from distutils.extension import Extension
import os
import re
import shlex
Expand All @@ -17,76 +19,94 @@
# "../parser.c" is needed to include parser functions
# "-std=c++17" is needed due to c++17 dependency


def readme():
with open('README.md') as f:
with open("README.md") as f:
return f.read()


###
## get version string
#
VERSIONFILE = "_version.py"
verstrline = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
verstrline = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
if mo:
version_str = mo.group(1)
else:
raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))

sources = ['mercury.pyx',
'../libmerc/asn1/oid.cc',
'../libmerc/dns.cc',
'../libmerc/utils.cc',
'../libmerc/analysis.cc',
'../libmerc/libmerc.cc',
'../libmerc/addr.cc',
'../libmerc/wireguard.cc',
'../libmerc/ssh.cc',
'../libmerc/match.cc',
'../libmerc/http.cc',
'../libmerc/pkt_proc.cc',
'../libmerc/tls.cc',
'../libmerc/asn1.cc',
'../libmerc/smb2.cc',
'../libmerc/config_generator.cc',
'../libmerc/bencode.cc',
mercury_dir = os.getenv("MERCURY_DIR")
if mercury_dir == '':
mercury_dir = '../../'

sources = [
"mercury.pyx",
f"{mercury_dir}/src/libmerc/asn1/oid.cc",
f"{mercury_dir}/src/libmerc/dns.cc",
f"{mercury_dir}/src/libmerc/utils.cc",
f"{mercury_dir}/src/libmerc/analysis.cc",
f"{mercury_dir}/src/libmerc/libmerc.cc",
f"{mercury_dir}/src/libmerc/addr.cc",
f"{mercury_dir}/src/libmerc/wireguard.cc",
f"{mercury_dir}/src/libmerc/ssh.cc",
f"{mercury_dir}/src/libmerc/match.cc",
f"{mercury_dir}/src/libmerc/http.cc",
f"{mercury_dir}/src/libmerc/pkt_proc.cc",
f"{mercury_dir}/src/libmerc/tls.cc",
f"{mercury_dir}/src/libmerc/asn1.cc",
f"{mercury_dir}/src/libmerc/smb2.cc",
f"{mercury_dir}/src/libmerc/config_generator.cc",
f"{mercury_dir}/src/libmerc/bencode.cc",
]


def get_additional_flags():
env_cflags = os.getenv('ENV_CFLAGS')
env_cflags = os.getenv("ENV_CFLAGS")
if env_cflags is None:
return []
else:
return shlex.split(env_cflags)


additional_flags = get_additional_flags()
print("additional_flags =", repr(additional_flags))


setup(name='mercury-python',
version=version_str,
description="Python interface into mercury's network protocol fingerprinting and analysis functionality",
long_description=readme(),
long_description_content_type="text/markdown",
classifiers=[
'Development Status :: 3 - Alpha',
'Programming Language :: Python :: 3 :: Only',
'Topic :: System :: Networking :: Monitoring',
'Topic :: Security',
],
python_requires='>=3.6.0',
keywords='tls fingerprinting network traffic analysis',
url='https://github.com/cisco/mercury/src/cython/',
author='Blake Anderson',
author_email='blake.anderson@cisco.com',
ext_modules=[Extension("mercury",
sources=sources,
include_dirs=['../libmerc'],
language="c++",
extra_compile_args=["-std=c++17","-Wno-narrowing","-Wno-deprecated-declarations"] + additional_flags,
extra_link_args=["-std=c++17","-lz"] + additional_flags,
libraries = ['crypto'],
runtime_library_dirs=['../'])
],
cmdclass={'build_ext':build_ext})
setup(
name="mercury-python",
version=version_str,
description="Python interface into mercury's network protocol fingerprinting and analysis functionality",
long_description=readme(),
long_description_content_type="text/markdown",
classifiers=[
"Development Status :: 3 - Alpha",
"Programming Language :: Python :: 3 :: Only",
"Topic :: System :: Networking :: Monitoring",
"Topic :: Security",
],
python_requires=">=3.6.0",
keywords="tls fingerprinting network traffic analysis",
url="https://github.com/cisco/mercury-python/",
author="Blake Anderson",
author_email="blake.anderson@cisco.com",
ext_modules=[
Extension(
"mercury",
sources=sources,
include_dirs=[f"{mercury_dir}/src/libmerc"],
language="c++",
extra_compile_args=[
"-std=c++17",
"-Wno-narrowing",
"-Wno-deprecated-declarations",
]
+ additional_flags,
extra_link_args=["-std=c++17", "-lz"] + additional_flags,
libraries=["crypto"],
runtime_library_dirs=[f"{mercury_dir}/src/"],
)
],
cmdclass={"build_ext": build_ext},
)