mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Micro-optimization: replace s.find(x)!=-1 with x in s
timeit shows a speedup of ~3x on Python 2.7.5 x86_64. It also makes the code a bit shorter.
This commit is contained in:
committed by
Michael DeHaan
parent
a7da5d8702
commit
0749112286
@@ -415,7 +415,7 @@ class Runner(object):
|
||||
|
||||
environment_string = self._compute_environment_string(inject)
|
||||
|
||||
if tmp.find("tmp") != -1 and ((self.sudo and self.sudo_user != 'root') or (self.su and self.su_user != 'root')):
|
||||
if "tmp" in tmp and ((self.sudo and self.sudo_user != 'root') or (self.su and self.su_user != 'root')):
|
||||
# deal with possible umask issues once sudo'ed to other user
|
||||
cmd_chmod = "chmod a+r %s" % remote_module_path
|
||||
self._low_level_exec_command(conn, cmd_chmod, tmp, sudoable=False)
|
||||
@@ -469,7 +469,7 @@ class Runner(object):
|
||||
cmd = " ".join([environment_string.strip(), shebang.replace("#!","").strip(), cmd])
|
||||
cmd = cmd.strip()
|
||||
|
||||
if tmp.find("tmp") != -1 and not C.DEFAULT_KEEP_REMOTE_FILES and not persist_files and delete_remote_tmp:
|
||||
if "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES and not persist_files and delete_remote_tmp:
|
||||
if not self.sudo or self.su or self.sudo_user == 'root' or self.su_user == 'root':
|
||||
# not sudoing or sudoing to root, so can cleanup files in the same step
|
||||
cmd = cmd + "; rm -rf %s >/dev/null 2>&1" % tmp
|
||||
@@ -485,7 +485,7 @@ class Runner(object):
|
||||
else:
|
||||
res = self._low_level_exec_command(conn, cmd, tmp, sudoable=sudoable, in_data=in_data)
|
||||
|
||||
if tmp.find("tmp") != -1 and not C.DEFAULT_KEEP_REMOTE_FILES and not persist_files and delete_remote_tmp:
|
||||
if "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES and not persist_files and delete_remote_tmp:
|
||||
if (self.sudo and self.sudo_user != 'root') or (self.su and self.su_user != 'root'):
|
||||
# not sudoing to root, so maybe can't delete files as that other user
|
||||
# have to clean up temp files as original user in a second step
|
||||
@@ -883,7 +883,7 @@ class Runner(object):
|
||||
return False
|
||||
|
||||
def _late_needs_tmp_path(self, conn, tmp, module_style):
|
||||
if tmp.find("tmp") != -1:
|
||||
if "tmp" in tmp:
|
||||
# tmp has already been created
|
||||
return False
|
||||
if not conn.has_pipelining or not C.ANSIBLE_SSH_PIPELINING or C.DEFAULT_KEEP_REMOTE_FILES or self.su:
|
||||
|
||||
@@ -33,7 +33,7 @@ class ActionModule(object):
|
||||
module_name = 'command'
|
||||
module_args += " #USE_SHELL"
|
||||
|
||||
if tmp.find("tmp") == -1:
|
||||
if "tmp" not in tmp:
|
||||
tmp = self.runner._make_tmp_path(conn)
|
||||
|
||||
(module_path, is_new_style, shebang) = self.runner._copy_module(conn, tmp, module_name, module_args, inject, complex_args=complex_args)
|
||||
|
||||
@@ -331,7 +331,7 @@ class ActionModule(object):
|
||||
src = open(source)
|
||||
src_contents = src.read(8192)
|
||||
st = os.stat(source)
|
||||
if src_contents.find("\x00") != -1:
|
||||
if "\x00" in src_contents:
|
||||
diff['src_binary'] = 1
|
||||
elif st[stat.ST_SIZE] > utils.MAX_FILE_SIZE_FOR_DIFF:
|
||||
diff['src_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF
|
||||
|
||||
@@ -128,7 +128,7 @@ class ActionModule(object):
|
||||
result = handler.run(conn, tmp, 'raw', module_args, inject)
|
||||
|
||||
# clean up after
|
||||
if tmp.find("tmp") != -1 and not C.DEFAULT_KEEP_REMOTE_FILES:
|
||||
if "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES:
|
||||
self.runner._low_level_exec_command(conn, 'rm -rf %s >/dev/null 2>&1' % tmp, tmp)
|
||||
|
||||
result.result['changed'] = True
|
||||
|
||||
@@ -68,9 +68,9 @@ class Connection(object):
|
||||
cp_in_use = False
|
||||
cp_path_set = False
|
||||
for arg in self.common_args:
|
||||
if arg.find("ControlPersist") != -1:
|
||||
if "ControlPersist" in arg:
|
||||
cp_in_use = True
|
||||
if arg.find("ControlPath") != -1:
|
||||
if "ControlPath" in arg:
|
||||
cp_path_set = True
|
||||
|
||||
if cp_in_use and not cp_path_set:
|
||||
@@ -137,7 +137,7 @@ class Connection(object):
|
||||
data = host_fh.read()
|
||||
host_fh.close()
|
||||
for line in data.split("\n"):
|
||||
if line is None or line.find(" ") == -1:
|
||||
if line is None or " " not in line:
|
||||
continue
|
||||
tokens = line.split()
|
||||
if tokens[0].find(self.HASHED_KEY_MAGIC) == 0:
|
||||
@@ -324,7 +324,8 @@ class Connection(object):
|
||||
# the host to known hosts is not intermingled with multiprocess output.
|
||||
fcntl.lockf(self.runner.output_lockfile, fcntl.LOCK_UN)
|
||||
fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_UN)
|
||||
controlpersisterror = stderr.find('Bad configuration option: ControlPersist') != -1 or stderr.find('unknown configuration option: ControlPersist') != -1
|
||||
controlpersisterror = 'Bad configuration option: ControlPersist' in stderr or \
|
||||
'unknown configuration option: ControlPersist' in stderr
|
||||
|
||||
if C.HOST_KEY_CHECKING:
|
||||
if ssh_cmd[0] == "sshpass" and p.returncode == 6:
|
||||
|
||||
Reference in New Issue
Block a user