Skip to content

Commit ececff1

Browse files
authored
Merge pull request #234 from vintozver/master
Proper error import & lib autodetection
2 parents b722987 + 971c44d commit ececff1

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

iptc/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
.. moduleauthor:: Vilmos Nebehaj
88
"""
99

10-
from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target,
11-
Policy, IPTCError)
10+
from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target, Policy, IPTCError)
1211
from iptc.ip6tc import is_table6_available, Table6, Rule6
13-
from iptc.xtables import XTablesError
12+
from iptc.errors import *
13+
1414

1515
__all__ = []

iptc/errors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
class XTablesError(Exception):
5+
"""Raised when an xtables call fails for some reason."""
6+
7+
8+
__all__ = ['XTablesError']

iptc/xtables.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from . import version
99
from .util import find_library
10+
from .errors import *
1011

1112
XT_INV_PROTO = 0x40 # invert the sense of PROTO
1213

@@ -790,9 +791,6 @@ class xtables_target(ct.Union):
790791
("v11", _xtables_target_v11),
791792
("v12", _xtables_target_v12)]
792793

793-
class XTablesError(Exception):
794-
"""Raised when an xtables call fails for some reason."""
795-
796794

797795
_libc, _ = find_library("c")
798796
_optind = ct.c_long.in_dll(_libc, "optind")
@@ -806,13 +804,23 @@ class XTablesError(Exception):
806804
_lib_xtables, xtables_version = find_library(_searchlib)
807805
_xtables_libdir = os.getenv("XTABLES_LIBDIR")
808806
if _xtables_libdir is None:
809-
for xtdir in ["/lib/xtables", "/lib64/xtables", "/usr/lib/xtables",
810-
"/usr/lib/iptables", "/usr/lib64/xtables",
811-
"/usr/lib64/iptables", "/usr/local/lib/xtables",
812-
"/usr/lib/x86_64-linux-gnu/xtables"]:
813-
if os.path.isdir(xtdir):
814-
_xtables_libdir = xtdir
815-
break
807+
import re
808+
ldconfig_path_regex = re.compile('^(/.*):$')
809+
import subprocess
810+
ldconfig = subprocess.Popen(
811+
('ldconfig', '-N', '-v'),
812+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
813+
)
814+
ldconfig_out, ldconfig_err = ldconfig.communicate()
815+
if ldconfig.returncode != 0:
816+
raise XTablesError("ldconfig failed, please set XTABLES_LIBDIR")
817+
for ldconfig_out_line in ldconfig_out.splitlines():
818+
ldconfig_path_regex_match = ldconfig_path_regex.match(ldconfig_out_line)
819+
if ldconfig_path_regex_match is not None:
820+
ldconfig_path = os.path.join(ldconfig_path_regex_match.group(1), 'xtables')
821+
if os.path.isdir(ldconfig_path):
822+
_xtables_libdir = ldconfig_path
823+
break
816824
if _xtables_libdir is None:
817825
raise XTablesError("can't find directory with extensions; "
818826
"please set XTABLES_LIBDIR")

0 commit comments

Comments
 (0)