Repository: ambari Updated Branches: refs/heads/branch-2.2 e540e5c60 -> 35f2302ad
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/35f2302a Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/35f2302a Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/35f2302a Branch: refs/heads/branch-2.2 Commit: 35f2302adf86691d8ef98b07f7ad7e0ff5e8fe20 Parents: e540e5c Author: Sandor Magyari <[email protected]> Authored: Mon May 2 17:44:02 2016 +0200 Committer: Sandor Magyari <[email protected]> Committed: Tue May 3 15:29:50 2016 +0200 ---------------------------------------------------------------------- .../python/resource_management/core/sudo.py | 46 ++++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/35f2302a/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 46fa9d0..9fb06d0 100644 --- a/ambari-common/src/main/python/resource_management/core/sudo.py +++ b/ambari-common/src/main/python/resource_management/core/sudo.py @@ -24,12 +24,12 @@ import os import tempfile import shutil import stat +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 @@ -155,21 +155,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): @@ -226,4 +212,28 @@ else: # shutil.copy replacement def copy(src, dst): - shell.checked_call(["sudo", "cp", "-r", src, dst], sudo=True) \ No newline at end of file + shell.checked_call(["sudo", "cp", "-r", src, dst], sudo=True) + +def chmod_recursive(path, recursive_mode_flags, recursion_follow_links): + find_flags = [] + if 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 ,"{}" ,";"]) + +# 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)
