os_utils.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import shutil
  18. import string
  19. import os
  20. from os_check import *
  21. if OSCheck.is_windows_family():
  22. from os_windows import *
  23. else:
  24. # MacOS not supported
  25. from os_linux import *
  26. from logging_utils import *
  27. from exceptions import FatalException
  28. def is_valid_filepath(filepath):
  29. if not filepath or not os.path.exists(filepath) or os.path.isdir(filepath):
  30. print 'Invalid path, please provide the absolute file path.'
  31. return False
  32. else:
  33. return True
  34. def quote_path(filepath):
  35. if(filepath.find(' ') != -1):
  36. filepath_ret = '"' + filepath + '"'
  37. else:
  38. filepath_ret = filepath
  39. return filepath_ret
  40. def search_file(filename, search_path, pathsep=os.pathsep):
  41. """ Given a search path, find file with requested name """
  42. for path in string.split(search_path, pathsep):
  43. candidate = os.path.join(path, filename)
  44. if os.path.exists(candidate):
  45. return os.path.abspath(candidate)
  46. return None
  47. def copy_file(src, dest_file):
  48. try:
  49. shutil.copyfile(src, dest_file)
  50. except Exception, e:
  51. err = "Can not copy file {0} to {1} due to: {2} . Please check file " \
  52. "permissions and free disk space.".format(src, dest_file, e.message)
  53. raise FatalException(1, err)
  54. def copy_files(files, dest_dir):
  55. if os.path.isdir(dest_dir):
  56. for filepath in files:
  57. shutil.copy(filepath, dest_dir)
  58. return 0
  59. else:
  60. return -1
  61. def remove_file(filePath):
  62. if os.path.exists(filePath):
  63. try:
  64. os.remove(filePath)
  65. except Exception, e:
  66. print_warning_msg('Unable to remove file: ' + str(e))
  67. return 1
  68. pass
  69. return 0
  70. def set_file_permissions(file, mod, user, recursive):
  71. if os.path.exists(file):
  72. os_set_file_permissions(file, mod, recursive, user)
  73. else:
  74. print_info_msg("File %s does not exist" % file)
  75. def is_root():
  76. return os_is_root()
  77. # Proxy to the os implementation
  78. def change_owner(filePath, user):
  79. os_change_owner(filePath, user)
  80. # Proxy to the os implementation
  81. def set_open_files_limit(maxOpenFiles):
  82. os_set_open_files_limit(maxOpenFiles)
  83. def get_password(prompt):
  84. return os_getpass(prompt)
  85. def find_in_path(file):
  86. dirs = os.environ["PATH"].split(os.pathsep)
  87. for dir in dirs:
  88. full_path = os.path.join(dir, file)
  89. if os.path.exists(full_path):
  90. return full_path
  91. raise Exception("File {} not found in PATH".format(file))