Repository: ambari Updated Branches: refs/heads/trunk 3d3f06ad8 -> 61a3ac44d
AMBARI-16136. Make temp filenames unique when creating files. (magyari_sandor) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/61a3ac44 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/61a3ac44 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/61a3ac44 Branch: refs/heads/trunk Commit: 61a3ac44dddd330dd4cf1b60ca1f40d8b7613dcc Parents: 3d3f06a Author: Sandor Magyari <[email protected]> Authored: Mon May 2 17:44:02 2016 +0200 Committer: Sandor Magyari <[email protected]> Committed: Tue May 3 15:15:35 2016 +0200 ---------------------------------------------------------------------- .../python/resource_management/core/sudo.py | 36 +++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/61a3ac44/ambari-common/src/main/python/resource_management/core/sudo.py ---------------------------------------------------------------------- diff --git a/ambari-common/src/main/python/resource_management/core/sudo.py b/ambari-common/src/main/python/resource_management/core/sudo.py index 5dbcddd..4ea44bc 100644 --- a/ambari-common/src/main/python/resource_management/core/sudo.py +++ b/ambari-common/src/main/python/resource_management/core/sudo.py @@ -25,12 +25,14 @@ import tempfile import shutil import stat import errno +import random from resource_management.core import shell from resource_management.core.logger import Logger from resource_management.core.exceptions import Fail from ambari_commons.os_check import OSCheck import subprocess + if os.geteuid() == 0: def chown(path, owner, group): uid = owner.pw_uid if owner else -1 @@ -195,21 +197,7 @@ else: # fp.write replacement def create_file(filename, content, encoding=None): - """ - if content is None, create empty file - """ - content = content if content else "" - content = content.encode(encoding) if encoding else content - - tmpf_name = tempfile.gettempdir() + os.sep + tempfile.template + str(time.time()) - - try: - with open(tmpf_name, "wb") as fp: - fp.write(content) - - shell.checked_call(["cp", "-f", tmpf_name, filename], sudo=True) - finally: - os.unlink(tmpf_name) + return _create_file(filename, content, True, encoding) # fp.read replacement def read_file(filename, encoding=None): @@ -274,4 +262,20 @@ def chmod_recursive(path, recursive_mode_flags, recursion_follow_links): find_flags.append('-L') for key, flags in recursive_mode_flags.iteritems(): - shell.checked_call(["find"] + find_flags + [path, "-type", key, "-exec" , "chmod", flags ,"{}" ,";"]) \ No newline at end of file + shell.checked_call(["find"] + find_flags + [path, "-type", key, "-exec" , "chmod", flags ,"{}" ,";"]) + +# fp.write replacement +def _create_file(filename, content, withSudo, encoding=None): + """ + if content is None, create empty file + """ + content = content if content else "" + content = content.encode(encoding) if encoding else content + + tmpf_name = tempfile.gettempdir() + os.sep + tempfile.template + str(time.time()) + "_" + str(random.randint(0, 1000)) + try: + with open(tmpf_name, "wb") as fp: + fp.write(content) + shell.checked_call(["cp", "-f", tmpf_name, filename], sudo=withSudo) + finally: + os.unlink(tmpf_name) \ No newline at end of file
