diff mbox series

[v2] python/semanage: Use ipaddress module instead of IPy

Message ID 20200427153438.17061-1-plautrba@redhat.com (mailing list archive)
State Accepted
Headers show
Series [v2] python/semanage: Use ipaddress module instead of IPy | expand

Commit Message

Petr Lautrbach April 27, 2020, 3:34 p.m. UTC
ipaddress python module was added to standard library in Python 3.3 -
https://docs.python.org/3/library/ipaddress.html

seobject.py was the only consumer of IPy module so this dependency is not needed
anymore.

Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
---

Based on Nicolas input:

- improved the check comment
- dropped the unnecessary check

 python/semanage/seobject.py | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

Comments

Nicolas Iooss April 28, 2020, 9:28 p.m. UTC | #1
On Mon, Apr 27, 2020 at 5:35 PM Petr Lautrbach <plautrba@redhat.com> wrote:
>
> ipaddress python module was added to standard library in Python 3.3 -
> https://docs.python.org/3/library/ipaddress.html
>
> seobject.py was the only consumer of IPy module so this dependency is not needed
> anymore.
>
> Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
> ---
>
> Based on Nicolas input:
>
> - improved the check comment
> - dropped the unnecessary check

Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>

I will apply this patch tomorrow if nobody else has any comment.
Thanks,
Nicolas

>  python/semanage/seobject.py | 15 ++++++---------
>  1 file changed, 6 insertions(+), 9 deletions(-)
>
> diff --git a/python/semanage/seobject.py b/python/semanage/seobject.py
> index f2a139c970bd..6e0b87f2fa3c 100644
> --- a/python/semanage/seobject.py
> +++ b/python/semanage/seobject.py
> @@ -32,7 +32,7 @@ from semanage import *
>  PROGNAME = "policycoreutils"
>  import sepolicy
>  import setools
> -from IPy import IP
> +import ipaddress
>
>  try:
>      import gettext
> @@ -1858,15 +1858,12 @@ class nodeRecords(semanageRecords):
>          if addr == "":
>              raise ValueError(_("Node Address is required"))
>
> -        # verify valid combination
> +        # verify that (addr, mask) is either a IP address (without a mask) or a valid network mask
>          if len(mask) == 0 or mask[0] == "/":
> -            i = IP(addr + mask)
> -            newaddr = i.strNormal(0)
> -            newmask = str(i.netmask())
> -            if newmask == "0.0.0.0" and i.version() == 6:
> -                newmask = "::"
> -
> -            protocol = "ipv%d" % i.version()
> +            i = ipaddress.ip_network(addr + mask)
> +            newaddr = str(i.network_address)
> +            newmask = str(i.netmask)
> +            protocol = "ipv%d" % i.version
>
>          try:
>              newprotocol = self.protocol.index(protocol)
> --
> 2.26.2
>
Nicolas Iooss May 1, 2020, 7:47 a.m. UTC | #2
On Tue, Apr 28, 2020 at 11:28 PM Nicolas Iooss <nicolas.iooss@m4x.org> wrote:
>
> On Mon, Apr 27, 2020 at 5:35 PM Petr Lautrbach <plautrba@redhat.com> wrote:
> >
> > ipaddress python module was added to standard library in Python 3.3 -
> > https://docs.python.org/3/library/ipaddress.html
> >
> > seobject.py was the only consumer of IPy module so this dependency is not needed
> > anymore.
> >
> > Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
> > ---
> >
> > Based on Nicolas input:
> >
> > - improved the check comment
> > - dropped the unnecessary check
>
> Acked-by: Nicolas Iooss <nicolas.iooss@m4x.org>
>

Applied.
Thanks,
Nicolas

> >  python/semanage/seobject.py | 15 ++++++---------
> >  1 file changed, 6 insertions(+), 9 deletions(-)
> >
> > diff --git a/python/semanage/seobject.py b/python/semanage/seobject.py
> > index f2a139c970bd..6e0b87f2fa3c 100644
> > --- a/python/semanage/seobject.py
> > +++ b/python/semanage/seobject.py
> > @@ -32,7 +32,7 @@ from semanage import *
> >  PROGNAME = "policycoreutils"
> >  import sepolicy
> >  import setools
> > -from IPy import IP
> > +import ipaddress
> >
> >  try:
> >      import gettext
> > @@ -1858,15 +1858,12 @@ class nodeRecords(semanageRecords):
> >          if addr == "":
> >              raise ValueError(_("Node Address is required"))
> >
> > -        # verify valid combination
> > +        # verify that (addr, mask) is either a IP address (without a mask) or a valid network mask
> >          if len(mask) == 0 or mask[0] == "/":
> > -            i = IP(addr + mask)
> > -            newaddr = i.strNormal(0)
> > -            newmask = str(i.netmask())
> > -            if newmask == "0.0.0.0" and i.version() == 6:
> > -                newmask = "::"
> > -
> > -            protocol = "ipv%d" % i.version()
> > +            i = ipaddress.ip_network(addr + mask)
> > +            newaddr = str(i.network_address)
> > +            newmask = str(i.netmask)
> > +            protocol = "ipv%d" % i.version
> >
> >          try:
> >              newprotocol = self.protocol.index(protocol)
> > --
> > 2.26.2
> >
diff mbox series

Patch

diff --git a/python/semanage/seobject.py b/python/semanage/seobject.py
index f2a139c970bd..6e0b87f2fa3c 100644
--- a/python/semanage/seobject.py
+++ b/python/semanage/seobject.py
@@ -32,7 +32,7 @@  from semanage import *
 PROGNAME = "policycoreutils"
 import sepolicy
 import setools
-from IPy import IP
+import ipaddress
 
 try:
     import gettext
@@ -1858,15 +1858,12 @@  class nodeRecords(semanageRecords):
         if addr == "":
             raise ValueError(_("Node Address is required"))
 
-        # verify valid combination
+        # verify that (addr, mask) is either a IP address (without a mask) or a valid network mask
         if len(mask) == 0 or mask[0] == "/":
-            i = IP(addr + mask)
-            newaddr = i.strNormal(0)
-            newmask = str(i.netmask())
-            if newmask == "0.0.0.0" and i.version() == 6:
-                newmask = "::"
-
-            protocol = "ipv%d" % i.version()
+            i = ipaddress.ip_network(addr + mask)
+            newaddr = str(i.network_address)
+            newmask = str(i.netmask)
+            protocol = "ipv%d" % i.version
 
         try:
             newprotocol = self.protocol.index(protocol)