def close(self): if self.opened: lock = FileLock(self.dbfile) lock.acquire() # Update database. if self.root != None: self.db[self.key] = self.root self.db.close() # Release lock. lock.release() # Set object to closed state. self.opened = False
def update_mbox (): dprint ("update_mbox()") global real_mbox, tmp_mbox global real_mbox_in_use # - lock so that touch_real_mbox() doesn't screw this up. See # def touch_real_mbox(): to see why it's needed. real_mbox_in_use = TRUE FileLock.lockf_lock_entire_file (real_mbox) global need_to_delete_messages_in_real_mbox if need_to_delete_messages_in_real_mbox == FALSE: # - just need to expand the file lock on real_mbox to include # new messages dprint ("simply expanding real_mbox lock to include new msgs") else: new_messages_file = create_tmp_copy_of_new_messages () remove_deleted_messages_from_real_mbox () need_to_delete_messages_in_real_mbox = FALSE if new_messages_file != None: readd_new_messages_to_real_mbox (new_messages_file) new_messages_file.close () copy_file (real_mbox, tmp_mbox) global bytes_locked_in_real_mbox bytes_locked_in_real_mbox = os.stat(real_mbox.name)[stat.ST_SIZE] global min_real_mbox_size min_real_mbox_size = bytes_locked_in_real_mbox FileLock.fcntl_lock_file_up_to_present_bytes (real_mbox) FileLock.lockf_remove_lock (real_mbox) real_mbox_in_use = FALSE # - I guess some xbiff-type programs use /var/spool/mail/mbox size to # indicate new messages. This is a problem if you delete read # messages and then add the new ones, mbox is shorter. So here's # a kludge. timeout_add (2000, touch_real_mbox)
def update_mbox(): dprint("update_mbox()") global real_mbox, tmp_mbox global real_mbox_in_use # - lock so that touch_real_mbox() doesn't screw this up. See # def touch_real_mbox(): to see why it's needed. real_mbox_in_use = TRUE FileLock.lockf_lock_entire_file(real_mbox) global need_to_delete_messages_in_real_mbox if need_to_delete_messages_in_real_mbox == FALSE: # - just need to expand the file lock on real_mbox to include # new messages dprint("simply expanding real_mbox lock to include new msgs") else: new_messages_file = create_tmp_copy_of_new_messages() remove_deleted_messages_from_real_mbox() need_to_delete_messages_in_real_mbox = FALSE if new_messages_file != None: readd_new_messages_to_real_mbox(new_messages_file) new_messages_file.close() copy_file(real_mbox, tmp_mbox) global bytes_locked_in_real_mbox bytes_locked_in_real_mbox = os.stat(real_mbox.name)[stat.ST_SIZE] global min_real_mbox_size min_real_mbox_size = bytes_locked_in_real_mbox FileLock.fcntl_lock_file_up_to_present_bytes(real_mbox) FileLock.lockf_remove_lock(real_mbox) real_mbox_in_use = FALSE # - I guess some xbiff-type programs use /var/spool/mail/mbox size to # indicate new messages. This is a problem if you delete read # messages and then add the new ones, mbox is shorter. So here's # a kludge. timeout_add(2000, touch_real_mbox)
def remove_file_locks (): global real_mbox, tmp_mbox if real_mbox != None: FileLock.fcntl_remove_lock (real_mbox) if tmp_mbox != None: FileLock.fcntl_remove_lock (tmp_mbox)
"program_name" : os.path.basename (sys.argv[0]) \ } remove_file_locks () sys.exit (1) #FIXME remember to remove try/except statements, they are just put in while # I write the program so I don't have to keep deleteing 'tempdir' try: os.mkdir (tempdir, 0700) # drwx------ except: print "remember to take this try/except out." pass real_mbox_in_use = FALSE # make sure real_mbox is opened for writing so that file-locks can work real_mbox = open (os.environ["MAIL"], "r+") FileLock.fcntl_lock_file_up_to_present_bytes (real_mbox) # these are used to check if another program messes with /var/spool/mail/mbox bytes_locked_in_real_mbox = os.stat (real_mbox.name)[stat.ST_SIZE] min_real_mbox_size = bytes_locked_in_real_mbox # - Create a work copy (tmp_mbox) of the mbox, in case other programs screw # with the actual mbox (real_mbox). tmp_mbox_filename = os.path.join (tempdir, "mbox") tmp_mbox = open (tmp_mbox_filename, "w+") FileLock.lockf_lock_entire_file (tmp_mbox) copy_file (real_mbox, tmp_mbox) #timeout_add (1000, check_mbox_size) timeout_add (1000, check_if_real_mbox_has_been_altered_by_another_program) get_list_of_messages ()
def remove_file_locks(): global real_mbox, tmp_mbox if real_mbox != None: FileLock.fcntl_remove_lock(real_mbox) if tmp_mbox != None: FileLock.fcntl_remove_lock(tmp_mbox)
"program_name" : os.path.basename (sys.argv[0]) \ } remove_file_locks() sys.exit(1) #FIXME remember to remove try/except statements, they are just put in while # I write the program so I don't have to keep deleteing 'tempdir' try: os.mkdir(tempdir, 0700) # drwx------ except: print "remember to take this try/except out." pass real_mbox_in_use = FALSE # make sure real_mbox is opened for writing so that file-locks can work real_mbox = open(os.environ["MAIL"], "r+") FileLock.fcntl_lock_file_up_to_present_bytes(real_mbox) # these are used to check if another program messes with /var/spool/mail/mbox bytes_locked_in_real_mbox = os.stat(real_mbox.name)[stat.ST_SIZE] min_real_mbox_size = bytes_locked_in_real_mbox # - Create a work copy (tmp_mbox) of the mbox, in case other programs screw # with the actual mbox (real_mbox). tmp_mbox_filename = os.path.join(tempdir, "mbox") tmp_mbox = open(tmp_mbox_filename, "w+") FileLock.lockf_lock_entire_file(tmp_mbox) copy_file(real_mbox, tmp_mbox) #timeout_add (1000, check_mbox_size) timeout_add(1000, check_if_real_mbox_has_been_altered_by_another_program) get_list_of_messages()
def __init__(self, filename, lock=0, cache=0, min_recordsize=MINRECORDSIZE, readonly=0, recover=0): """ Create an instance using filename as data file. If lock is true, a filelock will be enabled until the file is closed. cache controls whether to enable a cache or not (should only be used where OS level caches are not available). min_recordsize can be set to have all records be padded to at least this size (might reduce overhead due to reallocation of records that have become too small for updates). readonly can be set to true to have existing files opened in read-only mode. Writes will then cause an IOError, opening a non-existing file in read-only mode will too. Opening the storage file in recover mode will disable some of the checks normally done. This flag should only be used if a previous normal opening failed with a hint to run recovery. """#' self.readonly = readonly if _debug: log.call(SYSTEM_DEBUG) # Adjust min_recordsize if min_recordsize < MINRECORDSIZE: min_recordsize = MINRECORDSIZE if min_recordsize % BLOCKSIZE != 0: min_recordsize = (min_recordsize / BLOCKSIZE + 1) * BLOCKSIZE self.min_recordsize = min_recordsize # Lock the file if lock: self.filelock = filelock = FileLock.FileLock(filename) # This may raise a FileLock.Error if recover: try: filelock.lock() except FileLock.Error: filelock.remove_lock() filelock.lock() else: filelock.lock() # Open it: # first try for an existing file, then revert to creating a new one if readonly: mode = 'rb' else: mode = 'r+b' self.filename = filename try: # Existing file self.file = file = open(filename, mode) file.seek(0, 2) self.EOF = EOF = file.tell() except IOError, why: if readonly: raise IOError, why # New file: write header and state marker if _debug: log(SYSTEM_INFO, 'Creating a new storage file %s' % filename) self.file = file = open(filename, 'w+b') self.write_fileheader(file) self.mark(COLD) EOF = file.tell() if EOF % BLOCKSIZE != 0: # pad to block boundary file.write(' ' * (BLOCKSIZE - EOF % BLOCKSIZE)) EOF = file.tell() self.EOF = EOF self.is_new = 1
def make_style_transfer(message): project_dir_path = "/tmp/TeleBotProject/files_folder/deerace/" user_dir_path = project_dir_path + str(message.from_user.id) + "/" try: if os.path.isdir(project_dir_path) == False: mode = 0o755 os.makedirs(project_dir_path, mode) if os.path.isdir(user_dir_path) == False: mode = 0o755 os.makedirs(user_dir_path, mode) # dicrectory for current user except OSError: print("Creation of the directory %s failed" % user_dir_path) else: print("Successfully created the directory %s" % user_dir_path) print(message.photo[-1]) try: cid = message.chat.id file_info = bot.get_file(message.photo[-1].file_id) downloaded_file = bot.download_file(file_info.file_path) is_content_photo = False downloaded_file_path = '' if os.path.isfile(user_dir_path + "style_path.txt") == False: downloaded_file_path = user_dir_path + "style_" + message.photo[ -1].file_id + ".jfif" else: downloaded_file_path = user_dir_path + "content_" + message.photo[ -1].file_id + ".jfif" is_content_photo = True print("downloaded_file_path = ", downloaded_file_path) with open(downloaded_file_path, 'wb') as new_file: new_file.write(downloaded_file) if is_content_photo == True: style_image_file_path = "" # path to style photo style_path_file_handler = open( user_dir_path + "style_path.txt", "r") style_image_file_path = style_path_file_handler.readline( ).strip() ############################################################## #### Better create with exception throw, but i'm so lazy :(#### if style_image_file_path == None or len( style_image_file_path) == 0: bot.reply_to( message, ":ERROR: Problem with STYLE photo.\n" + "Please use command /deleteStyleImage and upload STYLE photo again" ) return elif os.path.isfile(style_image_file_path) == False: bot.reply_to( message, ":ERROR: Can't find file with style image path.\n" + "Please use command /deleteStyleImage and upload STYLE photo again" ) return #### Better create with exception throw, but i'm so lazy :(#### ############################################################## bot.reply_to( message, "*CONTENT PHOTO GETTED*\n\n" + "I get CONTENT PHOTO, now wait, i make image and send it to you\n\n" + "*MAKE STEP->* Wait i make stylize and send photo to you ", parse_mode='Markdown') #bot.reply_to(message, "I get CONTENT PHOTO, now wait, i make image and send it to you") # for some reason the 'upload_photo' status isn't quite working (doesn't show at all) bot.send_chat_action(message.chat.id, 'typing') protected_file_path = os.path.join(project_dir_path, "lock_file.tmp") print("Protecting file: {}".format(protected_file_path)) file_lock_handlers = FileLock(protected_file_path) with file_lock_handlers: with open(protected_file_path, "a") as file_handler: #object_style_transfer_facade = StyleTransferFacade(style_image_path=user_dir_path + "style.jfif", # content_image_path=downloaded_file_path) object_style_transfer_facade = StyleTransferFacade( style_image_path=style_image_file_path, content_image_path=downloaded_file_path) object_style_transfer_facade.transfer_style( out_image_path=user_dir_path + "out_image.jpg") del object_style_transfer_facade bot.send_photo( message.chat.id, open(user_dir_path + "out_image.jpg", 'rb'), reply_markup=hideBoard, reply_to_message_id=message.message_id ) # send file and hide keyboard, after image is sent print("bot.send_photo") file_handler.flush() else: with open(user_dir_path + "style_path.txt", 'w') as style_path_file_handler: style_path_file_handler.write(downloaded_file_path) bot.reply_to( message, "*STYLE PHOTO GETTED*\n\n" + "I get STYLE photo, now i need CONTENT PHOTO\n\n" + "*MAKE STEP->*Please send me one more photo, witch i will stylize", parse_mode='Markdown') #bot.reply_to(message, "WTF is this?\nhttps://www.youtube.com/watch?v=78_W7-3oJD8") print("File uploaded path = ", downloaded_file_path) except Exception as e: bot.reply_to(message, e)
def make_photo_deerace(message): project_dir_path = "/tmp/TeleBotProject/files_folder/deerace/" user_dir_path = project_dir_path + str(message.from_user.id) + "/" try: if os.path.isdir(project_dir_path) == False: mode = 0o755 os.makedirs(project_dir_path, mode) if os.path.isdir(user_dir_path) == False: mode = 0o755 os.makedirs(user_dir_path, mode) # dicrectory for current user os.makedirs(user_dir_path + "deerace/datasets/", mode) # dicrectory for current user os.makedirs(user_dir_path + "deerace/results/", mode) # dicrectory for current user except OSError: print("Creation of the directory %s failed" % user_dir_path) else: print("Successfully created the directory %s" % user_dir_path) print(message.photo[-1]) try: cid = message.chat.id file_info = bot.get_file(message.photo[-1].file_id) downloaded_file = bot.download_file(file_info.file_path) photo_name = "" photo_name = "deerace_" + message.photo[-1].file_id + ".jfif" downloaded_file_path = '' downloaded_file_path = user_dir_path + photo_name print("downloaded_file_path = ", downloaded_file_path) with open(downloaded_file_path, 'wb') as new_file: new_file.write(downloaded_file) bot.reply_to( message, "I get photo for DeERace, wait a minute i processing them...") # for some reason the 'upload_photo' status isn't quite working (doesn't show at all) bot.send_chat_action(message.chat.id, 'typing') protected_file_path = os.path.join(project_dir_path, "lock_file.tmp") print("Protecting file: {}".format(protected_file_path)) file_lock_handlers = FileLock(protected_file_path) with file_lock_handlers: with open(protected_file_path, "a") as file_handler: deerace_object = DeERace( images_with_photo_path=downloaded_file_path, photo_name=photo_name, user_dir_path=user_dir_path) face_find_count, list_of_file_paths_with_faces = deerace_object.find_face_and_processing( ) if face_find_count == 0: bot.reply_to( message, "Wait wait wait. I don't see faces in this photo! \n" + "Please send me photo with faces") return else: bot.reply_to( message, "Ok i see " + str(face_find_count) + " faces.\n" + "When i prosecced all faces, i send result to you") for dict_from_list in list_of_file_paths_with_faces: for keys_from_dict in dict_from_list.keys(): bot.send_photo( message.chat.id, open( dict_from_list[keys_from_dict]['real'], 'rb'), caption="Real face", reply_markup=hideBoard, reply_to_message_id=message.message_id ) # send file and hide keyboard, after image is sent bot.send_photo( message.chat.id, open( dict_from_list[keys_from_dict]['fake'], 'rb'), caption="Fake face", reply_markup=hideBoard, reply_to_message_id=message.message_id ) # send file and hide keyboard, after image is sent print(dict_from_list[keys_from_dict]['real']) print(dict_from_list[keys_from_dict]['fake']) print() file_handler.flush() except Exception as e: bot.reply_to(message, e)