4040__metaclass__ = type
4141
4242import atexit
43+ import ctypes
4344import json
45+ import logging
4446import os
4547import re
4648import shlex
5052import traceback
5153import types
5254
55+ from ansible .module_utils .six .moves import shlex_quote
56+
5357import mitogen .core
5458import ansible_mitogen .target # TODO: circular import
55- from mitogen .core import b
56- from mitogen .core import bytes_partition
57- from mitogen .core import str_rpartition
5859from mitogen .core import to_text
5960
60- try :
61- import ctypes
62- except ImportError :
63- # Python 2.4
64- ctypes = None
65-
6661try :
6762 # Python >= 3.4, PEP 451 ModuleSpec API
6863 import importlib .machinery
7772except ImportError :
7873 from io import StringIO
7974
80- try :
81- from shlex import quote as shlex_quote
82- except ImportError :
83- from pipes import quote as shlex_quote
84-
85- # Absolute imports for <2.5.
86- logging = __import__ ('logging' )
87-
88-
8975# Prevent accidental import of an Ansible module from hanging on stdin read.
9076import ansible .module_utils .basic
9177ansible .module_utils .basic ._ANSIBLE_ARGS = '{}'
9581# explicit call to res_init() on each task invocation. BSD-alikes export it
9682# directly, Linux #defines it as "__res_init".
9783libc__res_init = None
98- if ctypes :
99- libc = ctypes .CDLL (None )
100- for symbol in 'res_init' , '__res_init' :
101- try :
102- libc__res_init = getattr (libc , symbol )
103- except AttributeError :
104- pass
84+ libc = ctypes .CDLL (None )
85+ for symbol in 'res_init' , '__res_init' :
86+ try :
87+ libc__res_init = getattr (libc , symbol )
88+ except AttributeError :
89+ pass
10590
106- iteritems = getattr (dict , 'iteritems' , dict .items )
10791LOG = logging .getLogger (__name__ )
10892
10993
@@ -217,13 +201,13 @@ def _parse(self, fp):
217201 for line in fp :
218202 # ' #export foo=some var ' -> ['#export', 'foo=some var ']
219203 bits = shlex_split_b (line )
220- if (not bits ) or bits [0 ].startswith (b ( '#' ) ):
204+ if (not bits ) or bits [0 ].startswith (b'#' ):
221205 continue
222206
223- if bits [0 ] == b ( 'export' ) :
207+ if bits [0 ] == b'export' :
224208 bits .pop (0 )
225209
226- key , sep , value = bytes_partition ( b ( ' ' ) .join (bits ), b ( '=' ) )
210+ key , sep , value = b ' ' .join (bits ). partition ( b '=' )
227211 if key and sep :
228212 yield key , value
229213
@@ -596,7 +580,7 @@ def load_module(self, fullname):
596580 mod .__path__ = []
597581 mod .__package__ = str (fullname )
598582 else :
599- mod .__package__ = str (str_rpartition ( to_text (fullname ), '.' )[0 ])
583+ mod .__package__ = str (to_text (fullname ). rpartition ( '.' )[0 ])
600584 exec (code , mod .__dict__ )
601585 self ._loaded .add (fullname )
602586 return mod
@@ -611,7 +595,7 @@ class TemporaryEnvironment(object):
611595 def __init__ (self , env = None ):
612596 self .original = dict (os .environ )
613597 self .env = env or {}
614- for key , value in iteritems (self .env ):
598+ for key , value in mitogen . core . iteritems (self .env ):
615599 key = mitogen .core .to_text (key )
616600 value = mitogen .core .to_text (value )
617601 if value is None :
@@ -819,7 +803,7 @@ def __init__(self, interpreter_fragment, is_python, **kwargs):
819803 self .interpreter_fragment = interpreter_fragment
820804 self .is_python = is_python
821805
822- b_ENCODING_STRING = b ( '# -*- coding: utf-8 -*-' )
806+ b_ENCODING_STRING = b'# -*- coding: utf-8 -*-'
823807
824808 def _get_program (self ):
825809 return self ._rewrite_source (
@@ -852,13 +836,13 @@ def _rewrite_source(self, s):
852836 # While Ansible rewrites the #! using ansible_*_interpreter, it is
853837 # never actually used to execute the script, instead it is a shell
854838 # fragment consumed by shell/__init__.py::build_module_command().
855- new = [b ( '#!' ) + utf8 (self .interpreter_fragment )]
839+ new = [b'#!' + utf8 (self .interpreter_fragment )]
856840 if self .is_python :
857841 new .append (self .b_ENCODING_STRING )
858842
859- _ , _ , rest = bytes_partition ( s , b ( '\n ' ) )
843+ _ , _ , rest = s . partition ( b '\n ' )
860844 new .append (rest )
861- return b ( '\n ' ) .join (new )
845+ return b'\n ' .join (new )
862846
863847
864848class NewStyleRunner (ScriptRunner ):
@@ -971,16 +955,15 @@ def _setup_args(self):
971955 # change the default encoding. This hack was removed from Ansible long ago,
972956 # but not before permeating into many third party modules.
973957 PREHISTORIC_HACK_RE = re .compile (
974- b (r'reload\s*\(\s*sys\s*\)\s*'
975- r'sys\s*\.\s*setdefaultencoding\([^)]+\)' )
958+ br'reload\s*\(\s*sys\s*\)\s*sys\s*\.\s*setdefaultencoding\([^)]+\)' ,
976959 )
977960
978961 def _setup_program (self ):
979962 source = ansible_mitogen .target .get_small_file (
980963 context = self .service_context ,
981964 path = self .path ,
982965 )
983- self .source = self .PREHISTORIC_HACK_RE .sub (b ( '' ) , source )
966+ self .source = self .PREHISTORIC_HACK_RE .sub (b'' , source )
984967
985968 def _get_code (self ):
986969 try :
@@ -998,7 +981,7 @@ def _get_code(self):
998981 if mitogen .core .PY3 :
999982 main_module_name = '__main__'
1000983 else :
1001- main_module_name = b ( '__main__' )
984+ main_module_name = b'__main__'
1002985
1003986 def _handle_magic_exception (self , mod , exc ):
1004987 """
@@ -1030,7 +1013,7 @@ def _get_module_package(self):
10301013 approximation of the original package hierarchy, so that relative
10311014 imports function correctly.
10321015 """
1033- pkg , sep , modname = str_rpartition ( self .py_module_name , '.' )
1016+ pkg , sep , _ = self .py_module_name . rpartition ( '.' )
10341017 if not sep :
10351018 return None
10361019 if mitogen .core .PY3 :
@@ -1073,7 +1056,7 @@ def _run(self):
10731056
10741057
10751058class JsonArgsRunner (ScriptRunner ):
1076- JSON_ARGS = b ( '<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>' )
1059+ JSON_ARGS = b'<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>'
10771060
10781061 def _get_args_contents (self ):
10791062 return json .dumps (self .args ).encode ()
0 commit comments