From ceb4ae92d3224b88f11c75443b3dd426bc813359 Mon Sep 17 00:00:00 2001
From: Alessandro Pilotti <apilotti@cloudbasesolutions.com>
Date: Tue, 9 Sep 2014 23:40:52 +0300
Subject: [PATCH] Replaces s.unicode with six.text_type

six.text_type() provides already the feature provided by s.unicode().
---
 cloudbaseinit/osutils/windows.py              | 21 +++++------
 cloudbaseinit/tests/osutils/test_windows.py   | 12 +++----
 .../tests/utils/windows/test_x509.py          |  6 ++--
 cloudbaseinit/utils/s.py                      | 35 -------------------
 cloudbaseinit/utils/windows/x509.py           |  6 ++--
 5 files changed, 22 insertions(+), 58 deletions(-)
 delete mode 100644 cloudbaseinit/utils/s.py

diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py
index cca4114d..2c32163a 100644
--- a/cloudbaseinit/osutils/windows.py
+++ b/cloudbaseinit/osutils/windows.py
@@ -17,6 +17,7 @@
 import ctypes
 import os
 import re
+import six
 import time
 import win32process
 import win32security
@@ -29,7 +30,6 @@ from win32com import client
 
 from cloudbaseinit.openstack.common import log as logging
 from cloudbaseinit.osutils import base
-from cloudbaseinit.utils import s
 from cloudbaseinit.utils.windows import network
 
 LOG = logging.getLogger(__name__)
@@ -372,7 +372,7 @@ class WindowsUtils(base.BaseOSUtils):
         sidNameUse = wintypes.DWORD()
 
         ret_val = advapi32.LookupAccountNameW(
-            0, s.unicode(username), sid, ctypes.byref(cbSid), domainName,
+            0, six.text_type(username), sid, ctypes.byref(cbSid), domainName,
             ctypes.byref(cchReferencedDomainName), ctypes.byref(sidNameUse))
         if not ret_val:
             raise Exception("Cannot get user SID")
@@ -382,10 +382,10 @@ class WindowsUtils(base.BaseOSUtils):
     def add_user_to_local_group(self, username, groupname):
 
         lmi = Win32_LOCALGROUP_MEMBERS_INFO_3()
-        lmi.lgrmi3_domainandname = s.unicode(username)
+        lmi.lgrmi3_domainandname = six.text_type(username)
 
-        ret_val = netapi32.NetLocalGroupAddMembers(0, s.unicode(groupname), 3,
-                                                   ctypes.addressof(lmi), 1)
+        ret_val = netapi32.NetLocalGroupAddMembers(0, six.text_type(groupname),
+                                                   3, ctypes.addressof(lmi), 1)
 
         if ret_val == self.NERR_GroupNotFound:
             raise Exception('Group not found')
@@ -410,8 +410,9 @@ class WindowsUtils(base.BaseOSUtils):
     def create_user_logon_session(self, username, password, domain='.',
                                   load_profile=True):
         token = wintypes.HANDLE()
-        ret_val = advapi32.LogonUserW(s.unicode(username), s.unicode(domain),
-                                      s.unicode(password), 2, 0,
+        ret_val = advapi32.LogonUserW(six.text_type(username),
+                                      six.text_type(domain),
+                                      six.text_type(password), 2, 0,
                                       ctypes.byref(token))
         if not ret_val:
             raise Exception("User logon failed")
@@ -419,7 +420,7 @@ class WindowsUtils(base.BaseOSUtils):
         if load_profile:
             pi = Win32_PROFILEINFO()
             pi.dwSize = ctypes.sizeof(Win32_PROFILEINFO)
-            pi.lpUserName = s.unicode(username)
+            pi.lpUserName = six.text_type(username)
             ret_val = userenv.LoadUserProfileW(token, ctypes.byref(pi))
             if not ret_val:
                 kernel32.CloseHandle(token)
@@ -446,7 +447,7 @@ class WindowsUtils(base.BaseOSUtils):
     def set_host_name(self, new_host_name):
         ret_val = kernel32.SetComputerNameExW(
             self.ComputerNamePhysicalDnsHostname,
-            s.unicode(new_host_name))
+            six.text_type(new_host_name))
         if not ret_val:
             raise Exception("Cannot set host name")
 
@@ -753,7 +754,7 @@ class WindowsUtils(base.BaseOSUtils):
     def get_volume_label(self, drive):
         max_label_size = 261
         label = ctypes.create_unicode_buffer(max_label_size)
-        ret_val = kernel32.GetVolumeInformationW(s.unicode(drive), label,
+        ret_val = kernel32.GetVolumeInformationW(six.text_type(drive), label,
                                                  max_label_size, 0, 0, 0, 0, 0)
         if ret_val:
             return label.value
diff --git a/cloudbaseinit/tests/osutils/test_windows.py b/cloudbaseinit/tests/osutils/test_windows.py
index 162cc2c1..50767ce6 100644
--- a/cloudbaseinit/tests/osutils/test_windows.py
+++ b/cloudbaseinit/tests/osutils/test_windows.py
@@ -18,13 +18,11 @@ import ctypes
 import importlib
 import mock
 import os
+import six
 import unittest
 
 from oslo.config import cfg
 
-from cloudbaseinit.utils import s
-
-
 CONF = cfg.CONF
 
 
@@ -273,7 +271,7 @@ class WindowsUtilsTest(unittest.TestCase):
             response = self._winutils._get_user_sid_and_domain(self._USERNAME)
 
             advapi32.LookupAccountNameW.assert_called_with(
-                0, s.unicode(self._USERNAME), sid,
+                0, six.text_type(self._USERNAME), sid,
                 self._ctypes_mock.byref(cbSid), domainName,
                 self._ctypes_mock.byref(cchReferencedDomainName),
                 self._ctypes_mock.byref(sidNameUse))
@@ -308,12 +306,12 @@ class WindowsUtilsTest(unittest.TestCase):
                                                    group_name)
 
             netapi32.NetLocalGroupAddMembers.assert_called_with(
-                0, s.unicode(group_name), 3,
+                0, six.text_type(group_name), 3,
                 self._ctypes_mock.addressof.return_value, 1)
 
             self._ctypes_mock.addressof.assert_called_once_with(lmi)
             self.assertEqual(lmi.lgrmi3_domainandname,
-                             s.unicode(self._USERNAME))
+                             six.text_type(self._USERNAME))
 
     def test_add_user_to_local_group_no_error(self):
         self._test_add_user_to_local_group(ret_value=0)
@@ -461,7 +459,7 @@ class WindowsUtilsTest(unittest.TestCase):
 
         mock_SetComputerNameExW.assert_called_with(
             self._winutils.ComputerNamePhysicalDnsHostname,
-            s.unicode('fake name'))
+            six.text_type('fake name'))
 
     def test_set_host_name(self):
         self._test_set_host_name(ret_value='fake response')
diff --git a/cloudbaseinit/tests/utils/windows/test_x509.py b/cloudbaseinit/tests/utils/windows/test_x509.py
index 0b442368..e2eba390 100644
--- a/cloudbaseinit/tests/utils/windows/test_x509.py
+++ b/cloudbaseinit/tests/utils/windows/test_x509.py
@@ -16,9 +16,9 @@
 
 import importlib
 import mock
+import six
 import unittest
 
-from cloudbaseinit.utils import s
 from cloudbaseinit.utils import x509constants
 
 
@@ -220,7 +220,7 @@ class CryptoAPICertManagerTests(unittest.TestCase):
             mock_CertOpenStore.assert_called_with(
                 self.x509.cryptoapi.CERT_STORE_PROV_SYSTEM, 0, 0,
                 self.x509.cryptoapi.CERT_SYSTEM_STORE_LOCAL_MACHINE,
-                s.unicode(self.x509.STORE_NAME_MY))
+                six.text_type(self.x509.STORE_NAME_MY))
             mock_get_cert_thumprint.assert_called_once_with(
                 mock_CertCreateSelfSignCertificate())
 
@@ -353,7 +353,7 @@ class CryptoAPICertManagerTests(unittest.TestCase):
             mock_CertOpenStore.assert_called_with(
                 self.x509.cryptoapi.CERT_STORE_PROV_SYSTEM, 0, 0,
                 self.x509.cryptoapi.CERT_SYSTEM_STORE_LOCAL_MACHINE,
-                s.unicode(self.x509.STORE_NAME_MY))
+                six.text_type(self.x509.STORE_NAME_MY))
 
             mock_CertAddEncodedCertificateToStore.assert_called_with(
                 mock_CertOpenStore(),
diff --git a/cloudbaseinit/utils/s.py b/cloudbaseinit/utils/s.py
deleted file mode 100644
index 98d09fe6..00000000
--- a/cloudbaseinit/utils/s.py
+++ /dev/null
@@ -1,35 +0,0 @@
-# Copyright 2014 Cloudbase Solutions Srl
-#
-#    Licensed under the Apache License, Version 2.0 (the "License"); you may
-#    not use this file except in compliance with the License. You may obtain
-#    a copy of the License at
-#
-#         http://www.apache.org/licenses/LICENSE-2.0
-#
-#    Unless required by applicable law or agreed to in writing, software
-#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-#    License for the specific language governing permissions and limitations
-#    under the License.
-
-import sys
-
-_unicode = None
-
-
-def unicode(obj):
-    def unicode_2(obj):
-        import __builtin__
-        return __builtin__.unicode(obj)
-
-    def unicode_3(obj):
-        return str(obj)
-
-    global _unicode
-    if not _unicode:
-        if sys.version_info.major >= 3:
-            _unicode = unicode_3
-        else:
-            _unicode = unicode_2
-
-    return _unicode(obj)
diff --git a/cloudbaseinit/utils/windows/x509.py b/cloudbaseinit/utils/windows/x509.py
index 4a7231b1..7e2de462 100644
--- a/cloudbaseinit/utils/windows/x509.py
+++ b/cloudbaseinit/utils/windows/x509.py
@@ -16,11 +16,11 @@
 
 import copy
 import ctypes
+import six
 import uuid
 
 from ctypes import wintypes
 
-from cloudbaseinit.utils import s
 from cloudbaseinit.utils.windows import cryptoapi
 from cloudbaseinit.utils import x509constants
 
@@ -182,7 +182,7 @@ class CryptoAPICertManager(object):
 
             store_handle = cryptoapi.CertOpenStore(
                 cryptoapi.CERT_STORE_PROV_SYSTEM, 0, 0, flags,
-                s.unicode(store_name))
+                six.text_type(store_name))
             if not store_handle:
                 raise cryptoapi.CryptoAPIException()
 
@@ -246,7 +246,7 @@ class CryptoAPICertManager(object):
 
             store_handle = cryptoapi.CertOpenStore(
                 cryptoapi.CERT_STORE_PROV_SYSTEM, 0, 0, flags,
-                s.unicode(store_name))
+                six.text_type(store_name))
             if not store_handle:
                 raise cryptoapi.CryptoAPIException()