Skip to content

Commit 3279ea1

Browse files
authored
feature_add_build_workflow (#258)
* * add workflow for building wheels * add dummy.c file * add missed return statement * fix secrets; fix over-indentated lines; * move shell scripts to scripts directory.\n add tests after build * add install dependencies to doc workflow * retry * fix directory name * Improved build workflow * Reduce the number of packages to one per platform * Move all workflow related files to a separate directory * Remove upload to pypi * Revert the previous version of the setup.py, so as not to break the other build options * downgrade wget version due to a bug * fix test * add env for the build option * fix path * fix import
1 parent d465252 commit 3279ea1

File tree

12 files changed

+374
-11
lines changed

12 files changed

+374
-11
lines changed

.github/build_scripts/dummy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "snap7.h"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
get_absolute_path () {
2+
if [[ $1 == /* ]]
3+
then
4+
echo $1
5+
else
6+
echo "$PWD/${1#./}"
7+
fi
8+
}
9+
10+
# set absolute path for directories from args. Default value is current directory
11+
work_dir=$(get_absolute_path $1)
12+
output_dir=$(get_absolute_path $2)
13+
14+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
15+
lib_src_path=python_snap7.libs/
16+
lib_ext=so
17+
ls_pattern=manylinux
18+
else
19+
lib_src_path=snap7/.dylibs/
20+
lib_ext=dylib
21+
ls_pattern=macosx
22+
fi
23+
24+
cd $work_dir
25+
26+
wheel=$(ls *$ls_pattern*.whl | head -n1)
27+
unpack_dir=$wheel-dir
28+
packagename=$(python3 -m wheel unpack $wheel -d $unpack_dir | sed -e 's/.*\/\(.*\)...OK/\1/')
29+
mv $unpack_dir/$packagename/$lib_src_path/libsnap7*.$lib_ext $output_dir/libsnap7.$lib_ext
30+
rm $wheel
31+
rm -rf $unpack_dir
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
get_absolute_path () {
2+
if [[ $1 == /* ]]
3+
then
4+
echo $1
5+
else
6+
echo "$PWD/${1#./}"
7+
fi
8+
}
9+
10+
# set absolute path for directories from args. Default value is current directory
11+
work_dir=$(get_absolute_path $1)
12+
lib_dir=$(get_absolute_path $2)
13+
output_dir=$(get_absolute_path $3)
14+
15+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
16+
libname=libsnap7.so
17+
else
18+
libname=libsnap7.dylib
19+
fi
20+
21+
cd $work_dir
22+
23+
wheel=$(ls *.whl | head -n1)
24+
unpack_dir=$wheel-dir
25+
packagename=$(python3 -m wheel unpack $wheel -d $unpack_dir | sed -e 's/.*\/\(.*\)...OK/\1/')
26+
mkdir -p $unpack_dir/$packagename/snap7/lib
27+
mkdir -p $output_dir
28+
mv $lib_dir/$libname $unpack_dir/$packagename/snap7/lib/$libname
29+
python3 -m wheel pack $unpack_dir/$packagename -d $output_dir
30+
rm -rf $unpack_dir
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
$dll_dir = $args[0]
2+
$output_dir = $args[1]
3+
New-Item -ItemType Directory $output_dir
4+
Set-Location dist
5+
6+
$wheel = (Get-ChildItem *.whl)[0]
7+
$dirname = -join($wheel, "-dir")
8+
$unpack_string = $(python3 -m wheel unpack $wheel -d $dirname)
9+
$unpack_string -match ".*: (?<pkgname>.*)...Ok"
10+
$packagename = $matches['pkgname']
11+
$libdir = -join($packagename,"\snap7\lib")
12+
New-Item -ItemType Directory -Path $libdir
13+
Copy-Item ..\snap7-full-1.4.2\release\Windows\$dll_dir\snap7.dll $libdir
14+
python3 -m wheel pack $packagename -d ..\$output_dir
15+
Remove-Item $dirname -Force -Recurse
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
name: build-and-test-wheels
2+
on:
3+
push:
4+
branches: [master]
5+
pull_request:
6+
branches: [master]
7+
env:
8+
snap7-archive-url: https://sourceforge.net/projects/snap7/files/1.4.2/snap7-full-1.4.2.7z/download
9+
jobs:
10+
windows-build:
11+
name: Build wheel for windows
12+
runs-on: windows-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
17+
- name: Install choco packages
18+
run: |
19+
choco install 7zip python wget
20+
21+
- name: Cache snap7-archive
22+
id: snap7-archive
23+
uses: actions/cache@v2
24+
with:
25+
path: snap7-full-1.4.2.7z
26+
key: ${{ env.snap7-archive-url }}
27+
28+
- name: Get snap7
29+
if: steps.snap7-archive.outputs.cache-hit != 'true'
30+
run: |
31+
wget -O snap7-full-1.4.2.7z --content-disposition -c ${{ env.snap7-archive-url }}
32+
33+
- name: Extract archive
34+
run: 7z x snap7-full-1.4.2.7z
35+
36+
- name: Update wheel
37+
run: python3 -m pip install wheel --upgrade
38+
39+
- name: Build wheel
40+
run: python3 setup.py bdist_wheel --plat-name win_amd64
41+
42+
- name: Repack wheel
43+
run: .github/build_scripts/repack_wheel_windows.ps1 Win64 wheelhouse/${{ runner.os }}
44+
45+
- uses: actions/upload-artifact@v2
46+
with:
47+
name: wheels
48+
path: wheelhouse/*/*.whl
49+
50+
linux-build:
51+
name: Build wheel for linux
52+
runs-on: ubuntu-latest
53+
steps:
54+
- name: Checkout
55+
uses: actions/checkout@v2
56+
57+
- name: Install Debian packages
58+
run: |
59+
sudo apt-get update -qq
60+
sudo apt-get install -y python3-pip python3-setuptools p7zip
61+
62+
- name: Cache snap7-archive
63+
id: snap7-archive
64+
uses: actions/cache@v2
65+
with:
66+
path: snap7-full-1.4.2.7z
67+
key: ${{ env.snap7-archive-url }}
68+
69+
- name: Get snap7
70+
if: steps.snap7-archive.outputs.cache-hit != 'true'
71+
run: |
72+
wget -O snap7-full-1.4.2.7z --content-disposition -c ${{ env.snap7-archive-url }}
73+
74+
- name: Extract archive
75+
run: 7z x snap7-full-1.4.2.7z
76+
77+
- name: Copy files
78+
run: |
79+
mkdir src
80+
cp snap7-full-1.4.2/release/Wrappers/c-cpp/snap7.h src
81+
82+
- name: Install cibuildwheel and wheel
83+
run: python3 -m pip install cibuildwheel==1.10.0 wheel --upgrade
84+
85+
- name: Build pure python wheel
86+
run: python3 setup.py bdist_wheel --plat-name=linux_x86_64
87+
88+
- name: Build platform wheel
89+
run: |
90+
cp .github/build_scripts/dummy.c ./
91+
python3 -m cibuildwheel --output-dir wheelhouse
92+
env:
93+
CIBW_BUILD: cp36-manylinux_x86_64
94+
CIBW_MANYLINUX_X86_64_IMAGE: manylinux1
95+
CIBW_ENVIRONMENT: "BUILD_WHEEL_WITH_EXTENSION=1"
96+
CIBW_BEFORE_ALL_LINUX: cd snap7-full-1.4.2/build/unix/;make -f x86_64_linux.mk install
97+
98+
- name: Repack wheel
99+
run: |
100+
bash .github/build_scripts/extract_lib.sh wheelhouse dist
101+
bash .github/build_scripts/repack_wheel.sh dist dist wheelhouse/${{ runner.os }}
102+
103+
- uses: actions/upload-artifact@v2
104+
with:
105+
name: wheels
106+
path: wheelhouse/*/*.whl
107+
108+
osx-build:
109+
name: Build wheel for osx
110+
runs-on: macos-latest
111+
steps:
112+
- name: Checkout
113+
uses: actions/checkout@v2
114+
115+
- name: Cache snap7-archive
116+
id: snap7-archive
117+
uses: actions/cache@v2
118+
with:
119+
path: snap7-full-1.4.2.7z
120+
key: ${{ env.snap7-archive-url }}
121+
122+
- name: Get snap7
123+
if: steps.snap7-archive.outputs.cache-hit != 'true'
124+
run: |
125+
wget -nv -O snap7-full-1.4.2.7z --content-disposition -c ${{ env.snap7-archive-url }}
126+
127+
- name: Extract archive
128+
run: 7z x snap7-full-1.4.2.7z
129+
130+
- name: Copy files
131+
run: |
132+
mkdir src
133+
cp snap7-full-1.4.2/release/Wrappers/c-cpp/snap7.h src
134+
pushd snap7-full-1.4.2/build/osx/
135+
make -f x86_64_osx.mk all
136+
cp ../bin/x86_64-osx/libsnap7.dylib /usr/local/lib
137+
install_name_tool -id /usr/local/lib/libsnap7.dylib /usr/local/lib/libsnap7.dylib
138+
139+
- name: Install cibuildwheel
140+
run: python3 -m pip install cibuildwheel==1.10.0 wheel --upgrade
141+
142+
- name: Build pure python wheel
143+
run: python3 setup.py bdist_wheel --plat-name=macosx_10_9_x86_64
144+
145+
- name: Build platform wheel
146+
run: |
147+
cp .github/build_scripts/dummy.c ./
148+
python3 -m cibuildwheel --output-dir wheelhouse
149+
env:
150+
CIBW_BUILD: cp36-macosx_x86_64
151+
CIBW_BUILD_VERBOSITY: 1
152+
CIBW_ENVIRONMENT: "BUILD_WHEEL_WITH_EXTENSION=1"
153+
154+
- name: Repack wheel
155+
run: |
156+
bash .github/build_scripts/extract_lib.sh wheelhouse dist
157+
bash .github/build_scripts/repack_wheel.sh dist dist wheelhouse/${{ runner.os }}
158+
159+
- uses: actions/upload-artifact@v2
160+
with:
161+
name: wheels
162+
path: wheelhouse/*/*.whl
163+
164+
test-wheels:
165+
name: testing wheels
166+
needs: [windows-build, linux-build, osx-build]
167+
runs-on: ${{ matrix.os }}
168+
strategy:
169+
matrix:
170+
os: [ubuntu-latest, macos-latest, windows-latest]
171+
python-version: [3.6, 3.7, 3.8, 3.9]
172+
steps:
173+
- name: Checkout
174+
uses: actions/checkout@v2
175+
176+
- name: Set up Python ${{ matrix.python-version }}
177+
uses: actions/setup-python@v2
178+
with:
179+
python-version: ${{ matrix.python-version }}
180+
181+
- name: Install pytest
182+
run: |
183+
python3 -m pip install --upgrade pip
184+
python3 -m pip install pytest
185+
186+
- uses: actions/download-artifact@v2
187+
with:
188+
name: wheels
189+
path: wheelhouse
190+
191+
- name: install python-snap7
192+
run: python3 -m pip install $(ls wheelhouse/${{ runner.os }}/*.whl)
193+
194+
- name: Run pytest
195+
run: |
196+
which pytest
197+
pytest test/test_server.py test/test_util.py test/test_client.py test/test_mainloop.py
198+
199+
- name: Run tests required sudo
200+
if: ${{ runner.os == 'Linux'}}
201+
run: sudo /opt/hostedtoolcache/Python/${{ matrix.python-version }}*/x64/bin/pytest test/test_partner.py
202+
203+
- name: Run tests required sudo
204+
if: ${{ runner.os == 'macOS'}}
205+
run: sudo pytest test/test_partner.py
206+
207+
- name: Run tests required sudo
208+
if: ${{ runner.os == 'Windows'}}
209+
run: pytest test/test_partner.py

.github/workflows/doc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ jobs:
2121
- name: Run doc
2222
run: |
2323
cd doc
24-
make html
24+
make html
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: test pipy packages
2+
on: [workflow_dispatch]
3+
jobs:
4+
tests:
5+
runs-on: ${{ matrix.os }}
6+
strategy:
7+
matrix:
8+
os: [ubuntu-latest, macos-latest, windows-latest]
9+
python-version: [3.6, 3.7, 3.8, 3.9]
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
14+
- name: Set up Python ${{ matrix.python-version }}
15+
uses: actions/setup-python@v2
16+
with:
17+
python-version: ${{ matrix.python-version }}
18+
19+
- name: Install pytest
20+
run: |
21+
python3 -m pip install --upgrade pip
22+
python3 -m pip install pytest
23+
24+
- name: install python-snap7
25+
run: python3 -m pip install -i https://test.pypi.org/simple/ python-snap7
26+
27+
- name: Run pytest
28+
run: |
29+
which pytest
30+
pytest test/test_server.py test/test_util.py test/test_client.py
31+
32+
- name: Run tests required sudo
33+
if: ${{ runner.os == 'Linux'}}
34+
run: sudo /opt/hostedtoolcache/Python/${{ matrix.python-version }}*/x64/bin/pytest test/test_partner.py
35+
36+
- name: Run test_partner.py on windows
37+
if: ${{ runner.os == 'Windows' }}
38+
run: pytest test/test_partner.py
39+
40+
- name: Run tests required sudo
41+
if: ${{ runner.os == 'macOS'}}
42+
run: sudo pytest test/test_partner.py

.github/workflows/windows.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ jobs:
1212
- name: Checkout
1313
uses: actions/checkout@v2
1414
- name: Install choco packages
15-
run: choco install 7zip wget python
15+
run: |
16+
choco install 7zip python
17+
choco install --allow-downgrade wget --version 1.20.3.20190531
1618
- name: Get snap7
1719
run: |
1820
wget.exe -O snap7-full-1.4.2.7z --content-disposition -c https://sourceforge.net/projects/snap7/files/1.4.2/snap7-full-1.4.2.7z/download

setup.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
from setuptools import setup, find_packages
3+
from setuptools import setup, find_packages, Extension
44

55
__version__ = "1.0"
66

@@ -11,6 +11,17 @@
1111
'doc': ['sphinx', 'sphinx_rtd_theme'],
1212
}
1313

14+
ext_modules = []
15+
if os.environ.get('BUILD_WHEEL_WITH_EXTENSION'):
16+
ext_modules = [
17+
Extension(
18+
"snap7.__dummy__",
19+
["dummy.c"],
20+
libraries=['snap7'],
21+
include_dirs=['/usr/lib', '/usr/local/lib', 'src'],
22+
),
23+
]
24+
1425

1526
def read(fname):
1627
return open(os.path.join(os.path.dirname(__file__), fname)).read()
@@ -43,4 +54,5 @@ def read(fname):
4354
extras_require=extras_require,
4455
tests_require=tests_require,
4556
test_suite="tests",
46-
)
57+
ext_modules=ext_modules,
58+
)

snap7/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def as_compress(self, time: int) -> int:
459459
return result
460460

461461
def as_copy_ram_to_rom(self, timeout: int = 1) -> int:
462-
result = self._library.Cli_AsCopyRamToRom(self._pointer)
462+
result = self._library.Cli_AsCopyRamToRom(self._pointer, timeout)
463463
check_error(result, context="client")
464464
return result
465465

0 commit comments

Comments
 (0)