|
@@ -27,9 +27,7 @@ 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
|
|
|
|
|
|
|
|
@@ -144,6 +142,8 @@ if os.geteuid() == 0:
|
|
|
def kill(pid, signal):
|
|
|
os.kill(pid, signal)
|
|
|
|
|
|
+ def listdir(path):
|
|
|
+ return os.listdir(path)
|
|
|
|
|
|
else:
|
|
|
# os.chown replacement
|
|
@@ -197,7 +197,19 @@ else:
|
|
|
|
|
|
# fp.write replacement
|
|
|
def create_file(filename, content, encoding=None):
|
|
|
- return _create_file(filename, content, True, encoding)
|
|
|
+ """
|
|
|
+ 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=True)
|
|
|
+ finally:
|
|
|
+ os.unlink(tmpf_name)
|
|
|
|
|
|
# fp.read replacement
|
|
|
def read_file(filename, encoding=None):
|
|
@@ -255,7 +267,17 @@ else:
|
|
|
# shutil.copy replacement
|
|
|
def copy(src, dst):
|
|
|
shell.checked_call(["sudo", "cp", "-r", src, dst], sudo=True)
|
|
|
+
|
|
|
+ # os.listdir replacement
|
|
|
+ def listdir(path):
|
|
|
+ if not path_isdir(path):
|
|
|
+ raise Fail("{0} is not a directory. Cannot list files of it.".format(path))
|
|
|
|
|
|
+ code, out, err = shell.checked_call(["ls", path], sudo=True, stderr=subprocess.PIPE)
|
|
|
+ files = out.splitlines()
|
|
|
+ return files
|
|
|
+
|
|
|
+
|
|
|
def chmod_recursive(path, recursive_mode_flags, recursion_follow_links):
|
|
|
find_flags = []
|
|
|
if recursion_follow_links:
|
|
@@ -263,19 +285,3 @@ def chmod_recursive(path, recursive_mode_flags, recursion_follow_links):
|
|
|
|
|
|
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)
|