Skip to content

Commit 39ab3d8

Browse files
committed
fix: restore Python 2.7 compatibility for EL7
- Replace `from builtins import chr` with `six.unichr()` (Country.py) - Remove `exist_ok=True` from os.makedirs (Python 3.2+) (WebClient.py) - Replace `FileNotFoundError` with `OSError` + errno check (FirewallWrapper.py) - Replace `text=True` with `universal_newlines=True` (test_unblock_region.py) - Add Python 2.7 import tests (test_py2_compat.py) - Enable CI workflow with Python 2.7 check using CentOS 7 container
1 parent cabae7a commit 39ab3d8

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

.github/workflows/test.yml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,32 @@ name: Super-Linter
44
on:
55
push:
66
branches:
7-
- none
7+
- master
88

99
jobs:
10+
# Python 2.7 syntax/import check using CentOS 7 container
11+
test-py27:
12+
name: Python 2.7 compatibility check
13+
runs-on: ubuntu-latest
14+
container:
15+
image: centos:7
16+
steps:
17+
- name: Install git
18+
run: yum -y install git
19+
20+
- name: Checkout code
21+
uses: actions/checkout@v3
22+
23+
- name: Install dependencies
24+
run: |
25+
yum -y install python2 python2-pip python-six
26+
pip2 install netaddr
27+
28+
- name: Verify Python 2.7 imports
29+
run: |
30+
echo '__version__ = "0.0.0"' > fds/__about__.py
31+
python2 -c "import fds; import fds.Country; import fds.Countries; import fds.WebClient; import fds.FirewallWrapper"
32+
1033
# Set the job key. The key is displayed as the job name
1134
# when a job name is not provided
1235
test:
@@ -26,7 +49,7 @@ jobs:
2649
uses: actions/checkout@v2
2750

2851
- name: Fake version
29-
run: |
52+
run: |
3053
echo "ref-names: HEAD -> master, tag: v0" > ".git_archival.txt"
3154
3255
- name: Ensure venv

fds/Country.py

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

3-
from builtins import chr
3+
import six
44

55

66
class Country:
@@ -9,7 +9,7 @@ class Country:
99
def getFlag(self):
1010
code = self.code
1111
if code:
12-
return chr(ord(code[0]) + Country.OFFSET) + chr(ord(code[1]) + Country.OFFSET)
12+
return six.unichr(ord(code[0]) + Country.OFFSET) + six.unichr(ord(code[1]) + Country.OFFSET)
1313
return False
1414

1515
def __init__(self, name, data={}):

fds/FirewallWrapper.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,16 @@ def block_ip(self, ip, ipset_name=None, reload=True):
183183
log.info('Reloading FirewallD to apply permanent configuration')
184184
self.fw.reload()
185185
log.info('Breaking connection with {}'.format(ip))
186+
import errno
186187
from subprocess import CalledProcessError, check_output, STDOUT
187188
try:
188189
check_output(["/sbin/conntrack", "-D", "-s", str(ip)], stderr=STDOUT)
189-
except FileNotFoundError:
190-
log.warning('conntrack not found, skipping connection drop')
191-
except CalledProcessError as e:
190+
except OSError as e:
191+
if e.errno == errno.ENOENT:
192+
log.warning('conntrack not found, skipping connection drop')
193+
else:
194+
raise
195+
except CalledProcessError:
192196
pass
193197

194198
def get_blocked_ips4(self, name=None):

fds/WebClient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def download_file(self, url, local_filename=None, display_name=None,
8888
# Ensure destination directory exists
8989
dest_dir = os.path.dirname(local_filename)
9090
if dest_dir and not os.path.isdir(dest_dir):
91-
os.makedirs(dest_dir, exist_ok=True)
91+
os.makedirs(dest_dir)
9292
with open(local_filename, 'wb') as f:
9393
for chunk in r.iter_content(chunk_size=chunk_size):
9494
if chunk: # filter out keep-alive new chunks

tests/test_py2_compat.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Verify all fds modules can be imported (syntax/compatibility check)."""
2+
from __future__ import unicode_literals
3+
4+
5+
def test_all_modules_import():
6+
import fds
7+
import fds.Country
8+
import fds.Countries
9+
import fds.Config
10+
import fds.fds
11+
import fds.FirewallWrapper
12+
import fds.utils
13+
import fds.WebClient
14+
15+
16+
def test_country_flag_unicode():
17+
from fds.Country import Country
18+
country = Country('Germany', {'cca2': 'DE', 'demonym': 'German', 'tld': '.de'})
19+
flag = country.getFlag()
20+
assert flag is not False
21+
assert len(flag) == 2

tests/test_unblock_region.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
def exec_in_container(cmd):
77
return subprocess.check_output([
88
'docker', 'exec', 'firewalld-container', '/bin/bash', '-lc', cmd
9-
], stderr=subprocess.STDOUT, text=True)
9+
], stderr=subprocess.STDOUT, universal_newlines=True)
1010

1111

1212
def test_unblock_region_smoke():
1313
# Ensure container is up
14-
out = subprocess.check_output(['docker', 'ps', '--filter', 'name=firewalld-container', '--format', '{{.Names}}'], text=True)
14+
out = subprocess.check_output(['docker', 'ps', '--filter', 'name=firewalld-container', '--format', '{{.Names}}'], universal_newlines=True)
1515
assert 'firewalld-container' in out.strip()
1616

1717
# Start firewalld if needed

0 commit comments

Comments
 (0)