def __init__(self, file_name, mode='r'): # random_number for temporary file name random_number = random.randint(1152921504606846976, 18446744073709551615) # temporary file self.temp_file_path = os.path.join(persepolis_tmp, str(random_number)) # r = read mode , w = write mode , a = append mode self.mode = mode self.file_name = file_name # Creating a lock file # lock file prevents accessing a file simoltaneously # If mode is 'w' or 'a' (write or append) , all changes is saving in a temporary file and after closing, original file replaced by temporary file # If mode is 'r' (Read) , original file is copying in temp_file_path and # then content of temporary file is reading. self.lock_file = self.file_name + ".lock" while os.path.isfile(self.lock_file) == True: sleep(0.1) osCommands.touch(self.lock_file) if self.mode == "w": self.f = open(self.temp_file_path, "w") elif self.mode == "a": shutil.copy(str(self.file_name), str(self.temp_file_path)) self.f = open(self.temp_file_path, "a") else: shutil.copy(str(self.file_name), str(self.temp_file_path)) self.f = open(self.temp_file_path, "r")
persepolis_shutdown = os.path.join(persepolis_tmp, 'shutdown') # shutil.rmtree(persepolis_shutdown, ignore_errors=True, onerror=None) # creating folders for folder in [ config_folder, download_info_folder, persepolis_tmp, category_folder, queue_info_folder, persepolis_shutdown ]: osCommands.makeDirs(folder) # creating files for file in [ queues_list, download_list_file, download_list_file_active, single_downloads_list_file ]: osCommands.touch(file) # lock files perventing to access a file simultaneously # removing lock files in starting persepolis pattern_folder_list = [ config_folder, download_info_folder, category_folder, queue_info_folder ] for folder in pattern_folder_list: pattern = os.path.join(str(folder), '*.lock') for file in glob.glob(pattern): osCommands.remove(file) # refresh logs! log_file = os.path.join(str(config_folder), 'persepolisdm.log')