def removeMatchFileRecursively(cls, search_dir_path, re_remove_list): logger.debug("remove matched file: search-root=%s, re=%s" % ( search_dir_path, str(re_remove_list))) re_compile_list = [ re.compile(re_pattern) for re_pattern in re_remove_list if dataproperty.is_not_empty_string(re_pattern) ] dict_result_pathlist = {} for dir_path, _dir_name_list, filename_list in os.walk(search_dir_path): for filename in filename_list: for re_pattern in re_compile_list: if re_pattern.search(filename): break else: continue remove_path = os.path.join(dir_path, filename) result = cls.remove_object(remove_path) dict_result_pathlist.setdefault(result, []).append(remove_path) return dict_result_pathlist
def touch(cls, touch_path): logger.debug("touch file: " + touch_path) if cls.__dry_run: return path_obj = path.Path(touch_path) cls.make_directory(path_obj.dirname()) return path_obj.touch()
def chmod(cls, path, permission_text): """ :param str permission_text: "ls -l" style permission string. e.g. -rw-r--r-- """ try: check_file_existence(path) except FileNotFoundError: _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.debug(e) return False logger.debug("chmod %s %s" % (path, permission_text)) os.chmod(path, parseLsPermissionText(permission_text))
def make_directory(cls, dir_path, force=False): try: check_file_existence(dir_path) except FileNotFoundError: pass else: logger.debug("directory already exists: " + dir_path) return path.Path(dir_path) logger.debug("make directory: " + dir_path) path_obj = path.Path(dir_path) if any([not cls.__dry_run, force]): return path_obj.makedirs_p() return path_obj
def moveFile(cls, src_path, dst_path): import shutil try: check_file_existence(src_path) except FileNotFoundError: _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.debug(e) return False if os.path.realpath(src_path) == os.path.realpath(dst_path): logger.debug("%s and %s are the same file" % ( src_path, dst_path)) return True logger.info("move: %s -> %s" % (src_path, dst_path)) if not cls.__dry_run: shutil.move(src_path, dst_path) return True
def check_file_existence(path): """ :return: FileType :rtype: int :raises InvalidFilePathError: :raises FileNotFoundError: :raises RuntimeError: """ pathvalidate.validate_file_path(path) if not os.path.lexists(path): raise FileNotFoundError(path) if os.path.isfile(path): logger.debug("file found: " + path) return FileType.FILE if os.path.isdir(path): logger.debug("directory found: " + path) return FileType.DIRECTORY if os.path.islink(path): logger.debug("link found: " + path) return FileType.LINK raise RuntimeError()
def removeMatchDirectory(cls, search_dir_path, re_target_list): import shutil logger.debug("search-root=%s, target=%s" % ( search_dir_path, str(re_target_list))) dict_result_pathlist = {} for dir_path, dir_name_list, _filename_list in os.walk(search_dir_path): for dir_name in dir_name_list: for re_text in re_target_list: if re_text is None: logger.debug("null regular expression") continue if re.search(re_text, dir_name): break else: continue remove_path = os.path.join(dir_path, dir_name) result = False logger.debug("remove directory: " + remove_path) if not cls.__dry_run: try: shutil.rmtree(remove_path, False) result = True except (OSError, os.error): # for python 2.5 compatibility _, e, _ = sys.exc_info() logger.exception(e) dict_result_pathlist.setdefault(result, []).append(remove_path) return dict_result_pathlist
def __call__(self, *args): import thutils.gfile as gfile return_value = -1 try: return_value = self.function() return return_value except (gfile.InvalidFilePathError, gfile.FileNotFoundError): _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.exception(e) return_value = EX_NOINPUT return return_value except IOError: _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.exception(e) return_value = EX_IOERR return return_value except ValueError: _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.exception(e) return_value = EX_SOFTWARE return return_value except ImportError: _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.exception(e) return_value = EX_OSFILE return return_value except KeyboardInterrupt: logger.info( self.KEYBOARD_INTERRUPT_FORMAT % (os.path.basename(__file__))) return_value = 1 return return_value except Exception: _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.exception(e) return_value = 1 return return_value finally: logger.debug("exit code: " + str(return_value))
def remove_directory(cls, path): try: file_type = check_file_existence(path) except (InvalidFilePathError, FileNotFoundError): return True if file_type not in [FileType.DIRECTORY]: logger.error("not a directory: '%s'" % (path)) return False logger.debug("remove directory: " + path) if cls.__dry_run: return True try: import shutil shutil.rmtree(path, False) except (ImportError, IOError): _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.exception(e) return False return True
def findFileAll( search_root_dir_path, check_func, re_pattern_text, find_count=six.MAXSIZE): re_compile = re.compile(re_pattern_text) path_list = [] for dir_path, dir_name_list, filename_list in os.walk(search_root_dir_path): for file_name in dir_name_list + filename_list: path = os.path.join(dir_path, file_name) if not check_func(path): continue if re_compile.search(file_name) is None: continue path_list.append(path) if len(path_list) >= find_count: return path_list logger.debug("find file result: count=%d, files=(%s)" % ( len(path_list), ", ".join(path_list))) return path_list
def copy_file(cls, src_path, dst_path): import shutil try: check_file_existence(src_path) except FileNotFoundError: _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.debug(e) return False logger.debug("copy: %s -> %s" % (src_path, dst_path)) if cls.__dry_run: return True try: shutil.copyfile(src_path, dst_path) except (shutil.Error, IOError): _, e, _ = sys.exc_info() # for python 2.5 compatibility logger.debug("skip copy: " + str(e)) return True
def execute(cls, command, suffix=""): import thutils.common as common cache_dir_path = cls.__get_command_cache_store_dir() output_cache_path = os.path.join( cache_dir_path, common.command_to_filename(command, suffix) + ".txt") if os.path.exists(output_cache_path): if cls.__is_cache_expire(output_cache_path): logger.debug( "cache miss: cache lifetime expired: %s" % (output_cache_path)) else: logger.debug("cache hit: " + output_cache_path) return output_cache_path else: logger.debug("cache miss: " + output_cache_path) collect_command = "%s > %s 2>&1" % (command, output_cache_path) CommandCache(collect_command).run() return output_cache_path
def test_smoke(self, message, caller): logger.debug(message, caller)