summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2016-05-24 13:48:50 -0700
committerJoshua Harlow <harlowja@gmail.com>2016-05-24 13:48:50 -0700
commit85a53d66ad0241b2d6453d902487bb2edc1512b8 (patch)
treef52cff059de9fdb0aab88ba3ecba3dc2f0badced /cloudinit
parentef47b4f4c14ffe1337508f85f365b338a048a5a1 (diff)
downloadvyos-cloud-init-85a53d66ad0241b2d6453d902487bb2edc1512b8.tar.gz
vyos-cloud-init-85a53d66ad0241b2d6453d902487bb2edc1512b8.zip
Fix up some of the net usage and restore imports and add a mini compat module
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/net/__init__.py5
-rw-r--r--cloudinit/net/cmdline.py15
-rw-r--r--cloudinit/net/compat.py51
-rw-r--r--cloudinit/net/eni.py15
-rw-r--r--cloudinit/net/network_state.py23
5 files changed, 79 insertions, 30 deletions
diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py
index 776af0d3..f8df58f0 100644
--- a/cloudinit/net/__init__.py
+++ b/cloudinit/net/__init__.py
@@ -20,7 +20,8 @@ import errno
import logging
import os
-import six
+from .import compat
+
import yaml
LOG = logging.getLogger(__name__)
@@ -55,7 +56,7 @@ def write_file(path, content):
if not os.path.isdir(base_path):
os.makedirs(base_path)
with open(path, "wb+") as fh:
- if isinstance(content, six.text_type):
+ if isinstance(content, compat.text_type):
content = content.encode("utf8")
fh.write(content)
diff --git a/cloudinit/net/cmdline.py b/cloudinit/net/cmdline.py
index b85d4b0a..41cba893 100644
--- a/cloudinit/net/cmdline.py
+++ b/cloudinit/net/cmdline.py
@@ -21,20 +21,17 @@ import glob
import gzip
import io
import shlex
-import sys
-import six
-
-from cloudinit.net import get_devicelist
-from cloudinit.net import sys_netdev_info
+from . import compat
+from . import get_devicelist
+from . import read_file
+from . import sys_netdev_info
from cloudinit import util
-PY26 = sys.version_info[0:2] == (2, 6)
-
def _shlex_split(blob):
- if PY26 and isinstance(blob, six.text_type):
+ if compat.PY26 and isinstance(blob, compat.text_type):
# Older versions don't support unicode input
blob = blob.encode("utf8")
return shlex.split(blob)
@@ -143,7 +140,7 @@ def config_from_klibc_net_cfg(files=None, mac_addrs=None):
entries = []
names = {}
for cfg_file in files:
- name, entry = _klibc_to_config_entry(util.load_file(cfg_file),
+ name, entry = _klibc_to_config_entry(read_file(cfg_file),
mac_addrs=mac_addrs)
if name in names:
raise ValueError(
diff --git a/cloudinit/net/compat.py b/cloudinit/net/compat.py
new file mode 100644
index 00000000..8bf92ef5
--- /dev/null
+++ b/cloudinit/net/compat.py
@@ -0,0 +1,51 @@
+# Curtin is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the
+# Free Software Foundation, either version 3 of the License, or (at your
+# option) any later version.
+#
+# Curtin is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
+# more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Curtin. If not, see <http://www.gnu.org/licenses/>.
+
+# Mini six like module (so that this code can be more easily extracted).
+
+import sys
+
+PY26 = sys.version_info[0:2] == (2, 6)
+PY27 = sys.version_info[0:2] == (2, 7)
+PY2 = PY26 or PY27
+PY3 = sys.version_info[0:2] >= (3, 0)
+
+if PY3:
+ text_type = str
+ binary_type = bytes
+ string_types = (text_type, text_type)
+ import io
+ StringIO = io.StringIO
+else:
+ text_type = unicode
+ binary_type = bytes
+ string_types = (binary_type, text_type)
+ from StringIO import StringIO # noqa
+
+
+# Taken from six (remove when we can actually directly use six)
+
+def add_metaclass(metaclass):
+ """Class decorator for creating a class with a metaclass."""
+ def wrapper(cls):
+ orig_vars = cls.__dict__.copy()
+ slots = orig_vars.get('__slots__')
+ if slots is not None:
+ if isinstance(slots, str):
+ slots = [slots]
+ for slots_var in slots:
+ orig_vars.pop(slots_var)
+ orig_vars.pop('__dict__', None)
+ orig_vars.pop('__weakref__', None)
+ return metaclass(cls.__name__, cls.__bases__, orig_vars)
+ return wrapper
diff --git a/cloudinit/net/eni.py b/cloudinit/net/eni.py
index 18bae97a..f82c7f54 100644
--- a/cloudinit/net/eni.py
+++ b/cloudinit/net/eni.py
@@ -16,11 +16,11 @@ import glob
import os
import re
-from cloudinit import net
+from . import LINKS_FNAME_PREFIX
+from . import ParserError
+from . import write_file
-from cloudinit.net import LINKS_FNAME_PREFIX
-from cloudinit.net import ParserError
-from cloudinit.net.udev import generate_udev_rule
+from .udev import generate_udev_rule
NET_CONFIG_COMMANDS = [
@@ -365,12 +365,11 @@ class Renderer(object):
netrules='etc/udev/rules.d/70-persistent-net.rules'):
fpeni = os.path.join(target, eni)
- net.write_file(fpeni, self._render_interfaces(network_state))
+ write_file(fpeni, self._render_interfaces(network_state))
if netrules:
netrules = os.path.join(target, netrules)
- net.write_file(netrules,
- self._render_persistent_net(network_state))
+ write_file(netrules, self._render_persistent_net(network_state))
if links_prefix:
self._render_systemd_links(target, network_state, links_prefix)
@@ -393,4 +392,4 @@ class Renderer(object):
"Name=" + iface['name'],
""
])
- net.write_file(fname, content)
+ write_file(fname, content)
diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py
index c5aeadb5..32c48229 100644
--- a/cloudinit/net/network_state.py
+++ b/cloudinit/net/network_state.py
@@ -16,11 +16,12 @@
# along with Curtin. If not, see <http://www.gnu.org/licenses/>.
import copy
+import functools
import logging
-import six
-
-from cloudinit import net
+from . import compat
+from . import dump_yaml
+from . import read_yaml_file
LOG = logging.getLogger(__name__)
@@ -48,7 +49,7 @@ def parse_net_config(path, skip_broken=True):
"""Parses a curtin network configuration file and
return network state"""
ns = None
- net_config = net.read_yaml_file(path)
+ net_config = read_yaml_file(path)
if 'network' in net_config:
ns = parse_net_config_data(net_config.get('network'),
skip_broken=skip_broken)
@@ -57,7 +58,7 @@ def parse_net_config(path, skip_broken=True):
def from_state_file(state_file):
network_state = None
- state = net.read_yaml_file(state_file)
+ state = read_yaml_file(state_file)
network_state = NetworkState()
network_state.load(state)
return network_state
@@ -78,7 +79,7 @@ def ensure_command_keys(required_keys):
def wrapper(func):
- @six.wraps(func)
+ @functools.wraps(func)
def decorator(self, command, *args, **kwargs):
if required_keys:
missing_keys = diff_keys(required_keys, command)
@@ -102,8 +103,8 @@ class CommandHandlerMeta(type):
"""
def __new__(cls, name, parents, dct):
command_handlers = {}
- for attr_name, attr in six.iteritems(dct):
- if six.callable(attr) and attr_name.startswith('handle_'):
+ for attr_name, attr in dct.items():
+ if callable(attr) and attr_name.startswith('handle_'):
handles_what = attr_name[len('handle_'):]
if handles_what:
command_handlers[handles_what] = attr
@@ -112,7 +113,7 @@ class CommandHandlerMeta(type):
parents, dct)
-@six.add_metaclass(CommandHandlerMeta)
+@compat.add_metaclass(CommandHandlerMeta)
class NetworkState(object):
initial_network_state = {
@@ -135,7 +136,7 @@ class NetworkState(object):
'config': self.config,
'network_state': self.network_state,
}
- return net.dump_yaml(state)
+ return dump_yaml(state)
def load(self, state):
if 'version' not in state:
@@ -154,7 +155,7 @@ class NetworkState(object):
setattr(self, key, state[key])
def dump_network_state(self):
- return net.dump_yaml(self.network_state)
+ return dump_yaml(self.network_state)
def parse_config(self, skip_broken=True):
# rebuild network state