chiark / gitweb /
make-secnet-sites: Switch to python3
[secnet.git] / make-secnet-sites
index f32267824cbd954177416cb9bcfac3c9582e73f6..e344886782a2a7f68be67914c7798d71c37250e0 100755 (executable)
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
 #
 # This file is part of secnet.
 # See README for full list of copyright holders.
@@ -46,12 +46,12 @@ no-suppress-args
 cd ~/secnet/sites-test/
 execute ~/secnet/make-secnet-sites.py -u vpnheader groupfiles sites
 
-This program is part of secnet. It relies on the "ipaddr" library from
-Cendio Systems AB.
+This program is part of secnet.
 
 """
 
 from __future__ import print_function
+from __future__ import unicode_literals
 from builtins import int
 
 import string
@@ -61,7 +61,7 @@ import os
 import getopt
 import re
 
-import ipaddr
+import ipaddress
 
 # entry 0 is "near the executable", or maybe from PYTHONPATH=.,
 # which we don't want to preempt
@@ -71,6 +71,14 @@ import ipaddrset
 
 VERSION="0.1.18"
 
+from sys import version_info
+if version_info.major == 2:  # for python2
+    import codecs
+    sys.stdin = codecs.getreader('utf-8')(sys.stdin)
+    sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
+    import io
+    open=lambda f,m='r': io.open(f,m,encoding='utf-8')
+
 # Are we being invoked from userv?
 service=0
 # If we are, which group does the caller want to modify?
@@ -139,7 +147,7 @@ def listof(subtype):
 class single_ipaddr (basetype):
        "An IP address"
        def __init__(self,w):
-               self.addr=ipaddr.IPAddress(w[1])
+               self.addr=ipaddress.ip_address(w[1])
        def __str__(self):
                return '"%s"'%self.addr
 
@@ -148,7 +156,7 @@ class networks (basetype):
        def __init__(self,w):
                self.set=ipaddrset.IPAddressSet()
                for i in w[1:]:
-                       x=ipaddr.IPNetwork(i,strict=True)
+                       x=ipaddress.ip_network(i,strict=True)
                        self.set.append([x])
        def __str__(self):
                return ",".join(map((lambda n: '"%s"'%n), self.set.networks()))
@@ -257,6 +265,7 @@ class level:
        allow_properties={}
        require_properties={}
        def __init__(self,w):
+               self.type=w[0]
                self.name=w[1]
                self.properties={}
                self.children={}
@@ -517,7 +526,7 @@ def live(n):
        return 0
 def delempty(n):
        "Delete nodes that have no leafnode children"
-       for i in n.children.keys():
+       for i in list(n.children.keys()):
                delempty(n.children[i])
                if not live(n.children[i]):
                        del n.children[i]