diff options
author | Daniel Watkins <oddbloke@ubuntu.com> | 2020-03-14 08:31:33 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-14 06:31:33 -0600 |
commit | 309e9a3cb33a16721d87650f9c1b50709e507356 (patch) | |
tree | c3d1d4d3eaae35bde65d74bd302e94aedfd17872 | |
parent | 727f38ed16e1efbfafbce99ca5a6ff6d62628302 (diff) | |
download | vyos-cloud-init-309e9a3cb33a16721d87650f9c1b50709e507356.tar.gz vyos-cloud-init-309e9a3cb33a16721d87650f9c1b50709e507356.zip |
util/netbsd: drop six usage (#252)
Drop remaining python six usage
-rw-r--r-- | cloudinit/distros/netbsd.py | 4 | ||||
-rw-r--r-- | cloudinit/util.py | 61 |
2 files changed, 29 insertions, 36 deletions
diff --git a/cloudinit/distros/netbsd.py b/cloudinit/distros/netbsd.py index 353eb671..96794d76 100644 --- a/cloudinit/distros/netbsd.py +++ b/cloudinit/distros/netbsd.py @@ -5,7 +5,6 @@ import crypt import os import platform -import six import cloudinit.distros.bsd from cloudinit import log as logging @@ -46,8 +45,7 @@ class Distro(cloudinit.distros.bsd.BSD): } for key, val in kwargs.items(): - if (key in adduser_opts and val and - isinstance(val, six.string_types)): + if key in adduser_opts and val and isinstance(val, str): adduser_cmd.extend([adduser_opts[key], val]) elif key in adduser_flags and val: diff --git a/cloudinit/util.py b/cloudinit/util.py index 718c6959..6c13f3f4 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -15,6 +15,7 @@ import glob import grp import gzip import hashlib +import io import json import os import os.path @@ -30,13 +31,11 @@ import string import subprocess import sys import time +from urllib import parse from errno import ENOENT, ENOEXEC from base64 import b64decode, b64encode -from six.moves.urllib import parse as urlparse - -import six from cloudinit import importer from cloudinit import log as logging @@ -118,7 +117,7 @@ def target_path(target, path=None): # return 'path' inside target, accepting target as None if target in (None, ""): target = "/" - elif not isinstance(target, six.string_types): + elif not isinstance(target, str): raise ValueError("Unexpected input for target: %s" % target) else: target = os.path.abspath(target) @@ -138,14 +137,14 @@ def target_path(target, path=None): def decode_binary(blob, encoding='utf-8'): # Converts a binary type into a text type using given encoding. - if isinstance(blob, six.string_types): + if isinstance(blob, str): return blob return blob.decode(encoding) def encode_text(text, encoding='utf-8'): # Converts a text string into a binary type using given encoding. - if isinstance(text, six.binary_type): + if isinstance(text, bytes): return text return text.encode(encoding) @@ -175,8 +174,7 @@ def fully_decoded_payload(part): # bytes, first try to decode to str via CT charset, and failing that, try # utf-8 using surrogate escapes. cte_payload = part.get_payload(decode=True) - if (six.PY3 and - part.get_content_maintype() == 'text' and + if (part.get_content_maintype() == 'text' and isinstance(cte_payload, bytes)): charset = part.get_charset() if charset and charset.input_codec: @@ -240,7 +238,7 @@ class ProcessExecutionError(IOError): else: self.description = description - if not isinstance(exit_code, six.integer_types): + if not isinstance(exit_code, int): self.exit_code = self.empty_attr else: self.exit_code = exit_code @@ -281,7 +279,7 @@ class ProcessExecutionError(IOError): """ if data is bytes object, decode """ - return text.decode() if isinstance(text, six.binary_type) else text + return text.decode() if isinstance(text, bytes) else text def _indent_text(self, text, indent_level=8): """ @@ -290,7 +288,7 @@ class ProcessExecutionError(IOError): cr = '\n' indent = ' ' * indent_level # if input is bytes, return bytes - if isinstance(text, six.binary_type): + if isinstance(text, bytes): cr = cr.encode() indent = indent.encode() # remove any newlines at end of text first to prevent unneeded blank @@ -322,9 +320,6 @@ class SeLinuxGuard(object): return path = os.path.realpath(self.path) - # path should be a string, not unicode - if six.PY2: - path = str(path) try: stats = os.lstat(path) self.selinux.matchpathcon(path, stats[stat.ST_MODE]) @@ -369,7 +364,7 @@ def is_true(val, addons=None): check_set = TRUE_STRINGS if addons: check_set = list(check_set) + addons - if six.text_type(val).lower().strip() in check_set: + if str(val).lower().strip() in check_set: return True return False @@ -380,7 +375,7 @@ def is_false(val, addons=None): check_set = FALSE_STRINGS if addons: check_set = list(check_set) + addons - if six.text_type(val).lower().strip() in check_set: + if str(val).lower().strip() in check_set: return True return False @@ -441,7 +436,7 @@ def uniq_merge_sorted(*lists): def uniq_merge(*lists): combined_list = [] for a_list in lists: - if isinstance(a_list, six.string_types): + if isinstance(a_list, str): a_list = a_list.strip().split(",") # Kickout the empty ones a_list = [a for a in a_list if len(a)] @@ -464,7 +459,7 @@ def clean_filename(fn): def decomp_gzip(data, quiet=True, decode=True): try: - buf = six.BytesIO(encode_text(data)) + buf = io.BytesIO(encode_text(data)) with contextlib.closing(gzip.GzipFile(None, "rb", 1, buf)) as gh: # E1101 is https://github.com/PyCQA/pylint/issues/1444 if decode: @@ -475,7 +470,7 @@ def decomp_gzip(data, quiet=True, decode=True): if quiet: return data else: - raise DecompressionError(six.text_type(e)) + raise DecompressionError(str(e)) def extract_usergroup(ug_pair): @@ -567,7 +562,7 @@ def get_cfg_option_str(yobj, key, default=None): if key not in yobj: return default val = yobj[key] - if not isinstance(val, six.string_types): + if not isinstance(val, str): val = str(val) return val @@ -707,7 +702,7 @@ def get_cfg_option_list(yobj, key, default=None): if isinstance(val, (list)): cval = [v for v in val] return cval - if not isinstance(val, six.string_types): + if not isinstance(val, str): val = str(val) return [val] @@ -728,7 +723,7 @@ def get_cfg_by_path(yobj, keyp, default=None): @return: The value of the item at keyp." is not found.""" - if isinstance(keyp, six.string_types): + if isinstance(keyp, str): keyp = keyp.split("/") cur = yobj for tok in keyp: @@ -826,7 +821,7 @@ def make_url(scheme, host, port=None, pieces.append(query or '') pieces.append(fragment or '') - return urlparse.urlunparse(pieces) + return parse.urlunparse(pieces) def mergemanydict(srcs, reverse=False): @@ -1035,7 +1030,7 @@ def read_conf_with_confd(cfgfile): if "conf_d" in cfg: confd = cfg['conf_d'] if confd: - if not isinstance(confd, six.string_types): + if not isinstance(confd, str): raise TypeError(("Config file %s contains 'conf_d' " "with non-string type %s") % (cfgfile, type_utils.obj_name(confd))) @@ -1227,7 +1222,7 @@ def is_resolvable_url(url): """determine if this url is resolvable (existing or ip).""" return log_time(logfunc=LOG.debug, msg="Resolving URL: " + url, func=is_resolvable, - args=(urlparse.urlparse(url).hostname,)) + args=(parse.urlparse(url).hostname,)) def search_for_mirror(candidates): @@ -1378,7 +1373,7 @@ def uniq_list(in_list): def load_file(fname, read_cb=None, quiet=False, decode=True): LOG.debug("Reading from %s (quiet=%s)", fname, quiet) - ofh = six.BytesIO() + ofh = io.BytesIO() try: with open(fname, 'rb') as ifh: pipe_in_out(ifh, ofh, chunk_cb=read_cb) @@ -2074,13 +2069,13 @@ def subp(args, data=None, rcs=None, env=None, capture=True, # Popen converts entries in the arguments array from non-bytes to bytes. # When locale is unset it may use ascii for that encoding which can # cause UnicodeDecodeErrors. (LP: #1751051) - if isinstance(args, six.binary_type): + if isinstance(args, bytes): bytes_args = args - elif isinstance(args, six.string_types): + elif isinstance(args, str): bytes_args = args.encode("utf-8") else: bytes_args = [ - x if isinstance(x, six.binary_type) else x.encode("utf-8") + x if isinstance(x, bytes) else x.encode("utf-8") for x in args] try: sp = subprocess.Popen(bytes_args, stdout=stdout, @@ -2159,10 +2154,10 @@ def shellify(cmdlist, add_header=True): if isinstance(args, (list, tuple)): fixed = [] for f in args: - fixed.append("'%s'" % (six.text_type(f).replace("'", escaped))) + fixed.append("'%s'" % (str(f).replace("'", escaped))) content = "%s%s\n" % (content, ' '.join(fixed)) cmds_made += 1 - elif isinstance(args, six.string_types): + elif isinstance(args, str): content = "%s%s\n" % (content, args) cmds_made += 1 else: @@ -2288,7 +2283,7 @@ def expand_package_list(version_fmt, pkgs): pkglist = [] for pkg in pkgs: - if isinstance(pkg, six.string_types): + if isinstance(pkg, str): pkglist.append(pkg) continue @@ -2788,7 +2783,7 @@ def read_dmi_data(key): def message_from_string(string): if sys.version_info[:2] < (2, 7): - return email.message_from_file(six.StringIO(string)) + return email.message_from_file(io.StringIO(string)) return email.message_from_string(string) |