#!/usr/bin/python # -*- coding: utf-8 -*- ## ## @author Edouard DUPIN ## ## @copyright 2012, Edouard DUPIN, all right reserved ## ## @license MPL v2.0 (see license file) ## import os import shutil import errno import fnmatch import stat # Local import from realog import debug """ """ def get_run_path(): return os.getcwd() """ """ def get_current_path(file): return os.path.dirname(os.path.realpath(file)) def create_directory_of_file(file): debug.info("Create directory of path: '" + file + "'") path = os.path.dirname(file) debug.info("Create directory: '" + path + "'") try: os.stat(path) except: os.makedirs(path) def get_list_sub_path(path): # TODO : os.listdir(path) for dirname, dirnames, filenames in os.walk(path): return dirnames return [] def remove_path_and_sub_path(path): if os.path.isdir(path): debug.verbose("remove path : '" + path + "'") shutil.rmtree(path) def remove_file(path): if os.path.isfile(path): os.remove(path) elif os.path.islink(path): os.remove(path) def exist(path): if os.path.isdir(path): return True if os.path.isfile(path): return True if os.path.islink(path): return True return False def file_size(path): if not os.path.isfile(path): return 0 statinfo = os.stat(path) return statinfo.st_size def file_read_data(path, binary=False): debug.verbose("path= " + path) if not os.path.isfile(path): return "" if binary == True: file = open(path, "rb") else: file = open(path, "r") data_file = file.read() file.close() return data_file def version_to_string(version): version_ID = "" for id in version: if len(version_ID) != 0: if type(id) == str: version_ID += "-" else: version_ID += "." version_ID += str(id) return version_ID ## ## @brief Write data in a specific path. ## @param[in] path Path of the data might be written. ## @param[in] data Data To write in the file. ## @param[in] only_if_new (default: False) Write data only if data is different. ## @return True Something has been copied ## @return False Nothing has been copied ## def file_write_data(path, data, only_if_new=False): if only_if_new == True: if os.path.exists(path) == True: old_data = file_read_data(path) if old_data == data: return False #real write of data: create_directory_of_file(path) file = open(path, "w") file.write(data) file.close() return True def file_write_data_safe(path, data): #real write of data: create_directory_of_file(path) file = open(path + ".tmp", "w") file.write(data) file.close() shutil.move(path + ".tmp", path) return True def file_move(path_src, path_dst): #real write of data: create_directory_of_file(path_dst) shutil.move(path_src, path_dst) return True def list_to_str(list): if type(list) == type(str()): return list + " " else: result = "" # mulyiple imput in the list ... for elem in list: result += list_to_str(elem) return result