diff --git a/announce.py b/announce.py index 3f52cb528..c7fc01d29 100644 --- a/announce.py +++ b/announce.py @@ -41,6 +41,7 @@ from session import post_json from webfinger import webfinger_handle from auth import create_basic_auth_header from data import save_string +from data import remove_file def no_of_announces(post_json_object: {}) -> int: @@ -592,13 +593,11 @@ def undo_announce_collection_entry(recent_posts_cache: {}, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - if debug: - print('EX: undo_announce_collection_entry ' + - 'unable to delete cached post ' + - str(cached_post_filename)) + ex_text = \ + 'EX: undo_announce_collection_entry ' + \ + 'unable to delete cached post ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) remove_post_from_cache(post_json_object, recent_posts_cache) if not post_json_object.get('type'): @@ -666,13 +665,11 @@ def update_announce_collection(recent_posts_cache: {}, if os.path.isfile(cached_post_filename): print('update_announce_collection: removing ' + cached_post_filename) - try: - os.remove(cached_post_filename) - except OSError: - if debug: - print('EX: update_announce_collection ' + - 'unable to delete cached post ' + - str(cached_post_filename)) + ex_text = \ + 'EX: update_announce_collection ' + \ + 'unable to delete cached post ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) remove_post_from_cache(post_json_object, recent_posts_cache) if not has_object_dict(post_json_object): diff --git a/blocking.py b/blocking.py index 221e630e2..e1c545e5d 100644 --- a/blocking.py +++ b/blocking.py @@ -58,6 +58,7 @@ from data import load_list from data import save_string from data import save_flag_file from data import append_string +from data import remove_file def get_global_block_reason(search_text: str, @@ -216,17 +217,13 @@ def add_account_blocks(base_dir: str, if not blocking_file_text: if os.path.isfile(blocking_filename): - try: - os.remove(blocking_filename) - except OSError: - print('EX: _profile_edit unable to delete blocking ' + - blocking_filename) + remove_file(blocking_filename, + 'EX: _profile_edit unable to delete blocking ' + + blocking_filename) if os.path.isfile(blocking_reasons_filename): - try: - os.remove(blocking_reasons_filename) - except OSError: - print('EX: _profile_edit unable to delete blocking reasons' + - blocking_reasons_filename) + remove_file(blocking_reasons_filename, + 'EX: _profile_edit unable to delete blocking reasons' + + blocking_reasons_filename) return True save_string(blocking_file_text, blocking_filename, @@ -1244,12 +1241,10 @@ def mute_post(base_dir: str, nickname: str, domain: str, port: int, get_cached_post_filename(base_dir, nickname, domain, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) + if remove_file(cached_post_filename, + 'EX: MUTE cached post not removed ' + + cached_post_filename): print('MUTE: cached post removed ' + cached_post_filename) - except OSError: - print('EX: MUTE cached post not removed ' + - cached_post_filename) else: print('MUTE: cached post not found ' + cached_post_filename) @@ -1285,14 +1280,13 @@ def mute_post(base_dir: str, nickname: str, domain: str, port: int, post_json_obj) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) + ex_text = \ + 'EX: ' + \ + 'MUTE cached referenced post not removed ' + \ + cached_post_filename + if remove_file(cached_post_filename, ex_text): print('MUTE: cached referenced post removed ' + cached_post_filename) - except OSError: - print('EX: ' + - 'MUTE cached referenced post not removed ' + - cached_post_filename) if recent_posts_cache.get('json'): if recent_posts_cache['json'].get(also_update_post_id): @@ -1320,12 +1314,10 @@ def unmute_post(base_dir: str, nickname: str, domain: str, port: int, mute_filename = post_filename + '.muted' if os.path.isfile(mute_filename): - try: - os.remove(mute_filename) - except OSError: - if debug: - print('EX: unmute_post mute filename not deleted ' + - str(mute_filename)) + ex_text = \ + 'EX: unmute_post mute filename not deleted ' + \ + str(mute_filename) + remove_file(mute_filename, ex_text) print('UNMUTE: ' + mute_filename + ' file removed') post_json_obj = post_json_object @@ -1381,12 +1373,10 @@ def unmute_post(base_dir: str, nickname: str, domain: str, port: int, get_cached_post_filename(base_dir, nickname, domain, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - if debug: - print('EX: unmute_post cached post not deleted ' + - str(cached_post_filename)) + ex_text = \ + 'EX: unmute_post cached post not deleted ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) # if the post is in the recent posts cache then mark it as unmuted if recent_posts_cache.get('index'): @@ -1412,15 +1402,13 @@ def unmute_post(base_dir: str, nickname: str, domain: str, port: int, post_json_obj) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) + ex_text = \ + 'EX: ' + \ + 'unmute_post cached ref post not removed ' + \ + str(cached_post_filename) + if remove_file(cached_post_filename, ex_text): print('MUTE: cached referenced post removed ' + cached_post_filename) - except OSError: - if debug: - print('EX: ' + - 'unmute_post cached ref post not removed ' + - str(cached_post_filename)) if recent_posts_cache.get('json'): if recent_posts_cache['json'].get(also_update_post_id): @@ -1581,11 +1569,10 @@ def set_broch_mode(base_dir: str, domain_full: str, enabled: bool) -> None: if not enabled: # remove instance allow list if os.path.isfile(allow_filename): - try: - os.remove(allow_filename) - except OSError: - print('EX: set_broch_mode allow file not deleted ' + - str(allow_filename)) + ex_text = \ + 'EX: set_broch_mode allow file not deleted ' + \ + str(allow_filename) + remove_file(allow_filename, ex_text) print('Broch mode turned off') else: if os.path.isfile(allow_filename): @@ -1650,12 +1637,11 @@ def broch_modeLapses(base_dir: str, lapse_days: int) -> bool: days_since_broch = (curr_time - modified_date).days if days_since_broch >= lapse_days: removed: bool = False - try: - os.remove(allow_filename) + ex_text = \ + 'EX: broch_modeLapses allow file not deleted ' + \ + str(allow_filename) + if remove_file(allow_filename, ex_text): removed = True - except OSError: - print('EX: broch_modeLapses allow file not deleted ' + - str(allow_filename)) if removed: set_config_param(base_dir, "brochMode", False) print('Broch mode has elapsed') @@ -2178,10 +2164,9 @@ def _update_federated_blocks(session, base_dir: str, if not new_block_api_str: print('DEBUG: federated blocklist not loaded: ' + block_api_filename) if os.path.isfile(block_api_filename): - try: - os.remove(block_api_filename) - except OSError: - print('EX: unable to remove block api: ' + block_api_filename) + remove_file(block_api_filename, + 'EX: unable to remove block api: ' + + block_api_filename) else: print('DEBUG: federated blocklist loaded: ' + str(block_federated)) save_string(new_block_api_str, block_api_filename, @@ -2212,17 +2197,13 @@ def save_block_federated_endpoints(base_dir: str, result.append(endpoint) if not block_federated_endpoints_str: if os.path.isfile(block_api_endpoints_filename): - try: - os.remove(block_api_endpoints_filename) - except OSError: - print('EX: unable to delete block_api_endpoints.txt') + remove_file(block_api_endpoints_filename, + 'EX: unable to delete block_api_endpoints.txt') block_api_filename = \ data_dir(base_dir) + '/block_api.txt' if os.path.isfile(block_api_filename): - try: - os.remove(block_api_filename) - except OSError: - print('EX: unable to delete block_api.txt') + remove_file(block_api_filename, + 'EX: unable to delete block_api.txt') else: save_string(block_federated_endpoints_str, block_api_endpoints_filename, diff --git a/bookmarks.py b/bookmarks.py index 43d4e7d54..c903f85ad 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -38,6 +38,7 @@ from session import post_json from data import load_string from data import save_string from data import prepend_string +from data import remove_file def undo_bookmarks_collection_entry(recent_posts_cache: {}, @@ -60,13 +61,11 @@ def undo_bookmarks_collection_entry(recent_posts_cache: {}, domain, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - if debug: - print('EX: undo_bookmarks_collection_entry ' + - 'unable to delete cached post file ' + - str(cached_post_filename)) + ex_text = \ + 'EX: undo_bookmarks_collection_entry ' + \ + 'unable to delete cached post file ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) remove_post_from_cache(post_json_object, recent_posts_cache) # remove from the index @@ -182,13 +181,11 @@ def update_bookmarks_collection(recent_posts_cache: {}, domain, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - if debug: - print('EX: update_bookmarks_collection ' + - 'unable to delete cached post ' + - str(cached_post_filename)) + ex_text = \ + 'EX: update_bookmarks_collection ' + \ + 'unable to delete cached post ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) remove_post_from_cache(post_json_object, recent_posts_cache) if not post_json_object.get('object'): diff --git a/cache.py b/cache.py index 61d055283..0aa57fed6 100644 --- a/cache.py +++ b/cache.py @@ -33,6 +33,7 @@ from timeFunctions import date_utcnow from content import remove_script from data import save_binary from data import load_binary +from data import remove_file def remove_person_from_cache(base_dir: str, person_url: str, @@ -42,10 +43,9 @@ def remove_person_from_cache(base_dir: str, person_url: str, cache_filename = base_dir + '/cache/actors/' + \ person_url.replace('/', '#') + '.json' if os.path.isfile(cache_filename): - try: - os.remove(cache_filename) - except OSError: - print('EX: unable to delete cached actor ' + str(cache_filename)) + ex_text = \ + 'EX: unable to delete cached actor ' + str(cache_filename) + remove_file(cache_filename, ex_text) if person_cache.get(person_url): del person_cache[person_url] @@ -404,12 +404,11 @@ def remove_avatar_from_cache(base_dir: str, actor_str: str) -> None: base_dir + '/cache/avatars/' + actor_str + '.' + extension if not os.path.isfile(avatar_filename): continue - try: - os.remove(avatar_filename) - except OSError: - print('EX: remove_avatar_from_cache ' + - 'unable to delete cached avatar ' + - str(avatar_filename)) + ex_text = \ + 'EX: remove_avatar_from_cache ' + \ + 'unable to delete cached avatar ' + \ + str(avatar_filename) + remove_file(avatar_filename, ex_text) def clear_from_post_caches(base_dir: str, recent_posts_cache: {}, @@ -428,11 +427,10 @@ def clear_from_post_caches(base_dir: str, recent_posts_cache: {}, cache_dir = os.path.join(dir_str, acct) post_filename = cache_dir + filename if os.path.isfile(post_filename): - try: - os.remove(post_filename) - except OSError: - print('EX: clear_from_post_caches file not removed ' + - str(post_filename)) + ex_text = \ + 'EX: clear_from_post_caches file not removed ' + \ + str(post_filename) + remove_file(post_filename, ex_text) # if the post is in the recent posts cache then remove it if recent_posts_cache.get('index'): if post_id in recent_posts_cache['index']: diff --git a/categories.py b/categories.py index e2212ef35..cf36f3d36 100644 --- a/categories.py +++ b/categories.py @@ -16,6 +16,7 @@ from utils import replace_strings from utils import get_invalid_characters from data import load_string from data import save_string +from data import remove_file MAX_TAG_LENGTH = 42 @@ -174,12 +175,10 @@ def update_hashtag_categories(base_dir: str) -> None: hashtag_categories = get_hashtag_categories(base_dir, False, None) if not hashtag_categories: if os.path.isfile(category_list_filename): - try: - os.remove(category_list_filename) - except OSError: - print('EX: update_hashtag_categories ' + - 'unable to delete cached category list ' + - category_list_filename) + remove_file(category_list_filename, + 'EX: update_hashtag_categories ' + + 'unable to delete cached category list ' + + category_list_filename) return category_list: list[str] = [] diff --git a/content.py b/content.py index cb6cbab37..703c6e074 100644 --- a/content.py +++ b/content.py @@ -55,6 +55,7 @@ from data import load_string from data import save_string from data import save_binary from data import append_string +from data import remove_file MUSIC_SITES = ('soundcloud.com', 'bandcamp.com', 'resonate.coop') @@ -1685,21 +1686,16 @@ def save_media_in_form_post(media_bytes, debug: bool, for ex in extension_types: possible_other_format = filename_base + '.' + ex if os.path.isfile(possible_other_format): - try: - os.remove(possible_other_format) - except OSError: - if debug: - print('EX: save_media_in_form_post ' + - 'unable to delete other ' + - str(possible_other_format)) + ex_text = \ + 'EX: save_media_in_form_post ' + \ + 'unable to delete other ' + \ + str(possible_other_format) + remove_file(possible_other_format, ex_text) if os.path.isfile(filename_base): - try: - os.remove(filename_base) - except OSError: - if debug: - print('EX: save_media_in_form_post ' + - 'unable to delete ' + - str(filename_base)) + ex_text = \ + 'EX: save_media_in_form_post ' + \ + 'unable to delete ' + str(filename_base) + remove_file(filename_base, ex_text) if debug: print('DEBUG: No media found within POST') @@ -1788,13 +1784,11 @@ def save_media_in_form_post(media_bytes, debug: bool, detected_extension, '.' + ex) if os.path.isfile(possible_other_format): - try: - os.remove(possible_other_format) - except OSError: - if debug: - print('EX: save_media_in_form_post ' + - 'unable to delete other 2 ' + - str(possible_other_format)) + ex_text = \ + 'EX: save_media_in_form_post ' + \ + 'unable to delete other 2 ' + \ + str(possible_other_format) + remove_file(possible_other_format, ex_text) # don't allow scripts within svg files if detected_extension == 'svg': diff --git a/conversation.py b/conversation.py index 757600778..c76c711fa 100644 --- a/conversation.py +++ b/conversation.py @@ -24,6 +24,7 @@ from session import get_json_valid from data import save_string from data import save_flag_file from data import append_string +from data import remove_file def _get_conversation_filename(base_dir: str, nickname: str, domain: str, @@ -112,11 +113,9 @@ def unmute_conversation(base_dir: str, nickname: str, domain: str, return if not os.path.isfile(conversation_filename + '.muted'): return - try: - os.remove(conversation_filename + '.muted') - except OSError: - print('EX: unmute_conversation unable to delete ' + - conversation_filename + '.muted') + remove_file(conversation_filename + '.muted', + 'EX: unmute_conversation unable to delete ' + + conversation_filename + '.muted') def _get_replies_to_post(post_json_object: {}, diff --git a/daemon_post_hashtags.py b/daemon_post_hashtags.py index 0866b8b43..f2a4c075f 100644 --- a/daemon_post_hashtags.py +++ b/daemon_post_hashtags.py @@ -20,6 +20,7 @@ from content import extract_text_fields_in_post from blocking import is_blocked_hashtag from filters import is_filtered from categories import set_hashtag_category +from data import remove_file def set_hashtag_category2(self, calling_domain: str, cookie: str, @@ -131,11 +132,9 @@ def set_hashtag_category2(self, calling_domain: str, cookie: str, else: category_filename = base_dir + '/tags/' + hashtag + '.category' if os.path.isfile(category_filename): - try: - os.remove(category_filename) - except OSError: - print('EX: _set_hashtag_category unable to delete ' + - category_filename) + remove_file(category_filename, + 'EX: _set_hashtag_category unable to delete ' + + category_filename) # redirect back to the default timeline redirect_headers(self, tag_screen_str, diff --git a/daemon_post_links.py b/daemon_post_links.py index f79c89970..5e48882ae 100644 --- a/daemon_post_links.py +++ b/daemon_post_links.py @@ -19,6 +19,7 @@ from utils import get_config_param from httpheaders import redirect_headers from content import extract_text_fields_in_post from data import save_string +from data import remove_file def _links_update_edited(fields: {}, links_filename: str) -> None: @@ -43,11 +44,9 @@ def _links_update_edited(fields: {}, links_filename: str) -> None: links_filename) else: if os.path.isfile(links_filename): - try: - os.remove(links_filename) - except OSError: - print('EX: _links_update unable to delete ' + - links_filename) + remove_file(links_filename, + 'EX: _links_update unable to delete ' + + links_filename) def _links_update_about(fields: {}, allow_local_network_access: bool, @@ -63,11 +62,9 @@ def _links_update_about(fields: {}, allow_local_network_access: bool, about_filename) else: if os.path.isfile(about_filename): - try: - os.remove(about_filename) - except OSError: - print('EX: _links_update unable to delete ' + - about_filename) + remove_file(about_filename, + 'EX: _links_update unable to delete ' + + about_filename) def _links_update_tos(fields: {}, allow_local_network_access: bool, @@ -82,11 +79,9 @@ def _links_update_tos(fields: {}, allow_local_network_access: bool, 'EX: unable to write TOS ' + tos_filename) else: if os.path.isfile(tos_filename): - try: - os.remove(tos_filename) - except OSError: - print('EX: _links_update unable to delete ' + - tos_filename) + remove_file(tos_filename, + 'EX: _links_update unable to delete ' + + tos_filename) def _links_update_sepcification(fields: {}, @@ -100,11 +95,9 @@ def _links_update_sepcification(fields: {}, specification_filename) else: if os.path.isfile(specification_filename): - try: - os.remove(specification_filename) - except OSError: - print('EX: _links_update_specification unable to delete ' + - specification_filename) + remove_file(specification_filename, + 'EX: _links_update_specification unable to delete ' + + specification_filename) def links_update(self, calling_domain: str, cookie: str, diff --git a/daemon_post_newswire.py b/daemon_post_newswire.py index 186588456..1266751ec 100644 --- a/daemon_post_newswire.py +++ b/daemon_post_newswire.py @@ -27,6 +27,7 @@ from httpheaders import redirect_headers from content import extract_text_fields_in_post from content import load_dogwhistles from data import save_string +from data import remove_file def newswire_update(self, calling_domain: str, cookie: str, @@ -128,11 +129,9 @@ def newswire_update(self, calling_domain: str, cookie: str, else: # text area has been cleared and there is no new feed if os.path.isfile(newswire_filename): - try: - os.remove(newswire_filename) - except OSError: - print('EX: _newswire_update unable to delete ' + - newswire_filename) + remove_file(newswire_filename, + 'EX: _newswire_update unable to delete ' + + newswire_filename) # save filtered words list for the newswire filter_newswire_filename = \ @@ -144,11 +143,9 @@ def newswire_update(self, calling_domain: str, cookie: str, filter_newswire_filename) else: if os.path.isfile(filter_newswire_filename): - try: - os.remove(filter_newswire_filename) - except OSError: - print('EX: _newswire_update unable to delete ' + - filter_newswire_filename) + remove_file(filter_newswire_filename, + 'EX: _newswire_update unable to delete ' + + filter_newswire_filename) # save dogwhistle words list dogwhistles_filename = data_dir(base_dir) + '/dogwhistles.txt' @@ -175,11 +172,9 @@ def newswire_update(self, calling_domain: str, cookie: str, hashtag_rules_filename) else: if os.path.isfile(hashtag_rules_filename): - try: - os.remove(hashtag_rules_filename) - except OSError: - print('EX: _newswire_update unable to delete ' + - hashtag_rules_filename) + remove_file(hashtag_rules_filename, + 'EX: _newswire_update unable to delete ' + + hashtag_rules_filename) newswire_tusted_filename = data_dir(base_dir) + '/newswiretrusted.txt' if fields.get('trustedNewswire'): @@ -191,11 +186,9 @@ def newswire_update(self, calling_domain: str, cookie: str, newswire_tusted_filename) else: if os.path.isfile(newswire_tusted_filename): - try: - os.remove(newswire_tusted_filename) - except OSError: - print('EX: _newswire_update unable to delete ' + - newswire_tusted_filename) + remove_file(newswire_tusted_filename, + 'EX: _newswire_update unable to delete ' + + newswire_tusted_filename) # redirect back to the default timeline redirect_headers(self, actor_str + '/' + default_timeline, @@ -230,11 +223,9 @@ def citations_update(self, calling_domain: str, cookie: str, acct_dir(base_dir, nickname, domain) + '/.citations.txt' # remove any existing citations file if os.path.isfile(citations_filename): - try: - os.remove(citations_filename) - except OSError: - print('EX: _citations_update unable to delete ' + - citations_filename) + remove_file(citations_filename, + 'EX: _citations_update unable to delete ' + + citations_filename) if newswire and \ ' boundary=' in self.headers['Content-type']: diff --git a/daemon_post_person_options.py b/daemon_post_person_options.py index 7a6936a13..31bcc8bb1 100644 --- a/daemon_post_person_options.py +++ b/daemon_post_person_options.py @@ -50,6 +50,7 @@ from notifyOnPost import add_notify_on_post from notifyOnPost import remove_notify_on_post from flags import is_moderator from data import save_flag_file +from data import remove_file def _person_options_page_number(options_confirm_params: str) -> int: @@ -603,11 +604,9 @@ def _person_options_post_to_news(self, options_confirm_params: str, newswire_blocked_filename = account_dir + '/.nonewswire' if posts_to_news == 'on': if os.path.isfile(newswire_blocked_filename): - try: - os.remove(newswire_blocked_filename) - except OSError: - print('EX: _person_options unable to delete ' + - newswire_blocked_filename) + remove_file(newswire_blocked_filename, + 'EX: _person_options unable to delete ' + + newswire_blocked_filename) refresh_newswire(base_dir) else: if os.path.isdir(account_dir): @@ -657,11 +656,9 @@ def _person_options_post_to_features(self, options_confirm_params: str, features_blocked_filename = account_dir + '/.nofeatures' if posts_to_features == 'on': if os.path.isfile(features_blocked_filename): - try: - os.remove(features_blocked_filename) - except OSError: - print('EX: _person_options unable to delete ' + - features_blocked_filename) + remove_file(features_blocked_filename, + 'EX: _person_options unable to delete ' + + features_blocked_filename) refresh_newswire(base_dir) else: if os.path.isdir(account_dir): @@ -711,11 +708,9 @@ def _person_options_mod_news(self, options_confirm_params: str, newswire_mod_filename = account_dir + '/.newswiremoderated' if mod_posts_to_news != 'on': if os.path.isfile(newswire_mod_filename): - try: - os.remove(newswire_mod_filename) - except OSError: - print('EX: _person_options unable to delete ' + - newswire_mod_filename) + remove_file(newswire_mod_filename, + 'EX: _person_options unable to delete ' + + newswire_mod_filename) else: if os.path.isdir(account_dir): nw_filename = newswire_mod_filename diff --git a/daemon_post_profile.py b/daemon_post_profile.py index 49d7c8da3..fe3b973ba 100644 --- a/daemon_post_profile.py +++ b/daemon_post_profile.py @@ -150,6 +150,7 @@ from cache import store_person_in_cache from daemon_utils import post_to_outbox from data import save_string from data import save_flag_file +from data import remove_file def _profile_post_deactivate_account(base_dir: str, nickname: str, domain: str, @@ -257,11 +258,9 @@ def _profile_post_git_projects(base_dir: str, nickname: str, domain: str, 'EX: unable to write git ' + git_projects_filename) else: if os.path.isfile(git_projects_filename): - try: - os.remove(git_projects_filename) - except OSError: - print('EX: _profile_edit unable to delete ' + - git_projects_filename) + remove_file(git_projects_filename, + 'EX: _profile_edit unable to delete ' + + git_projects_filename) def _profile_post_peertube_instances(base_dir: str, fields: {}, self, @@ -285,11 +284,9 @@ def _profile_post_peertube_instances(base_dir: str, fields: {}, self, peertube_instances.append(url) else: if os.path.isfile(peertube_instances_file): - try: - os.remove(peertube_instances_file) - except OSError: - print('EX: _profile_edit unable to delete ' + - peertube_instances_file) + remove_file(peertube_instances_file, + 'EX: _profile_edit unable to delete ' + + peertube_instances_file) peertube_instances.clear() @@ -319,12 +316,10 @@ def _profile_post_robots_txt(base_dir: str, fields: {}, self) -> None: if not new_robots_txt: self.server.robots_txt = '' if os.path.isfile(robots_txt_filename): - try: - os.remove(robots_txt_filename) - except OSError: - print('EX: _profile_post_robots_txt' + - ' unable to delete ' + - robots_txt_filename) + remove_file(robots_txt_filename, + 'EX: _profile_post_robots_txt' + + ' unable to delete ' + + robots_txt_filename) else: save_string(new_robots_txt, robots_txt_filename, 'EX: _profile_post_robots_txt unable to save ' + @@ -362,11 +357,8 @@ def _profile_post_buy_domains(base_dir: str, fields: {}, self) -> None: save_json(buy_sites, buy_sites_filename) else: if os.path.isfile(buy_sites_filename): - try: - os.remove(buy_sites_filename) - except OSError: - print('EX: unable to delete ' + - buy_sites_filename) + remove_file(buy_sites_filename, + 'EX: unable to delete ' + buy_sites_filename) def _profile_post_crawlers_allowed(base_dir: str, fields: {}, self) -> None: @@ -445,12 +437,9 @@ def _profile_post_allowed_instances(base_dir: str, nickname: str, domain: str, allowed_instances_filename) else: if os.path.isfile(allowed_instances_filename): - try: - os.remove(allowed_instances_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - allowed_instances_filename) + remove_file(allowed_instances_filename, + 'EX: _profile_edit unable to delete ' + + allowed_instances_filename) def _profile_post_dm_instances(base_dir: str, nickname: str, domain: str, @@ -468,12 +457,9 @@ def _profile_post_dm_instances(base_dir: str, nickname: str, domain: str, dm_allowed_instances_filename) else: if os.path.isfile(dm_allowed_instances_filename): - try: - os.remove(dm_allowed_instances_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - dm_allowed_instances_filename) + remove_file(dm_allowed_instances_filename, + 'EX: _profile_edit unable to delete ' + + dm_allowed_instances_filename) def _profile_post_import_theme(base_dir: str, nickname: str, @@ -485,11 +471,9 @@ def _profile_post_import_theme(base_dir: str, nickname: str, os.mkdir(base_dir + '/imports') filename_base = base_dir + '/imports/newtheme.zip' if os.path.isfile(filename_base): - try: - os.remove(filename_base) - except OSError: - print('EX: _profile_edit unable to delete ' + - filename_base) + remove_file(filename_base, + 'EX: _profile_edit unable to delete ' + + filename_base) if nickname == admin_nickname or is_artist(base_dir, nickname): if import_theme(base_dir, filename_base): print(nickname + ' uploaded a theme') @@ -541,12 +525,9 @@ def _profile_post_auto_cw(base_dir: str, nickname: str, domain: str, self.server.auto_cw_cache[nickname] = fields['autoCW'].split('\n') else: if os.path.isfile(auto_cw_filename): - try: - os.remove(auto_cw_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - auto_cw_filename) + remove_file(auto_cw_filename, + 'EX: _profile_edit unable to delete ' + + auto_cw_filename) self.server.auto_cw_cache[nickname]: list[str] = [] @@ -563,11 +544,9 @@ def _profile_post_autogenerated_tags(base_dir: str, auto_tags_filename) else: if os.path.isfile(auto_tags_filename): - try: - os.remove(auto_tags_filename) - except OSError: - print('EX: _profile_edit unable to delete ' + - auto_tags_filename) + remove_file(auto_tags_filename, + 'EX: _profile_edit unable to delete ' + + auto_tags_filename) def _profile_post_word_replacements(base_dir: str, @@ -583,12 +562,9 @@ def _profile_post_word_replacements(base_dir: str, switch_filename) else: if os.path.isfile(switch_filename): - try: - os.remove(switch_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - switch_filename) + remove_file(switch_filename, + 'EX: _profile_edit unable to delete ' + + switch_filename) def _profile_post_filtered_words_within_bio(base_dir: str, @@ -604,12 +580,10 @@ def _profile_post_filtered_words_within_bio(base_dir: str, filter_bio_filename) else: if os.path.isfile(filter_bio_filename): - try: - os.remove(filter_bio_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete bio filter ' + - filter_bio_filename) + remove_file(filter_bio_filename, + 'EX: _profile_edit ' + + 'unable to delete bio filter ' + + filter_bio_filename) def _profile_post_filtered_words(base_dir: str, nickname: str, domain: str, @@ -623,12 +597,9 @@ def _profile_post_filtered_words(base_dir: str, nickname: str, domain: str, filter_filename) else: if os.path.isfile(filter_filename): - try: - os.remove(filter_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete filter ' + - filter_filename) + remove_file(filter_filename, + 'EX: _profile_edit unable to delete filter ' + + filter_filename) def _profile_post_low_bandwidth(base_dir: str, path: str, @@ -749,12 +720,9 @@ def _profile_post_notify_reactions(base_dir: str, notify_reactions_filename) if not notify_reactions_active: if os.path.isfile(notify_reactions_filename): - try: - os.remove(notify_reactions_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - notify_reactions_filename) + remove_file(notify_reactions_filename, + 'EX: _profile_edit unable to delete ' + + notify_reactions_filename) return actor_changed @@ -782,12 +750,9 @@ def _profile_post_notify_likes(on_final_welcome_screen: bool, notify_likes_filename) if not notify_likes_active: if os.path.isfile(notify_likes_filename): - try: - os.remove(notify_likes_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - notify_likes_filename) + remove_file(notify_likes_filename, + 'EX: _profile_edit unable to delete ' + + notify_likes_filename) return actor_changed @@ -884,12 +849,9 @@ def _profile_post_no_reply_boosts(base_dir: str, nickname: str, domain: str, no_reply_boosts_filename) if not no_reply_boosts: if os.path.isfile(no_reply_boosts_filename): - try: - os.remove(no_reply_boosts_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - no_reply_boosts_filename) + remove_file(no_reply_boosts_filename, + 'EX: _profile_edit unable to delete ' + + no_reply_boosts_filename) def _profile_post_no_seen_posts(base_dir: str, nickname: str, domain: str, @@ -909,12 +871,9 @@ def _profile_post_no_seen_posts(base_dir: str, nickname: str, domain: str, no_seen_posts_filename) if not no_seen_posts: if os.path.isfile(no_seen_posts_filename): - try: - os.remove(no_seen_posts_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - no_seen_posts_filename) + remove_file(no_seen_posts_filename, + 'EX: _profile_edit unable to delete ' + + no_seen_posts_filename) def _profile_post_watermark_enabled(base_dir: str, @@ -935,12 +894,10 @@ def _profile_post_watermark_enabled(base_dir: str, watermark_enabled_filename) if not watermark_enabled: if os.path.isfile(watermark_enabled_filename): - try: - os.remove(watermark_enabled_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - watermark_enabled_filename) + remove_file(watermark_enabled_filename, + 'EX: _profile_edit ' + + 'unable to delete ' + + watermark_enabled_filename) def _profile_post_hide_follows(base_dir: str, nickname: str, domain: str, @@ -970,12 +927,10 @@ def _profile_post_hide_follows(base_dir: str, nickname: str, domain: str, del self.server.hide_follows[nickname] actor_changed = True if os.path.isfile(hide_follows_filename): - try: - os.remove(hide_follows_filename) - except OSError: - print('EX: _profile_post_hide_follows ' + - 'unable to delete ' + - hide_follows_filename) + remove_file(hide_follows_filename, + 'EX: _profile_post_hide_follows ' + + 'unable to delete ' + + hide_follows_filename) return actor_changed @@ -1006,12 +961,10 @@ def _profile_post_hide_recent_posts(base_dir: str, nickname: str, domain: str, del self.server.hide_recent_posts[nickname] actor_changed = True if os.path.isfile(hide_recent_posts_filename): - try: - os.remove(hide_recent_posts_filename) - except OSError: - print('EX: _profile_post_hide_recent_posts ' + - 'unable to delete ' + - hide_recent_posts_filename) + remove_file(hide_recent_posts_filename, + 'EX: _profile_post_hide_recent_posts ' + + 'unable to delete ' + + hide_recent_posts_filename) return actor_changed @@ -1025,11 +978,9 @@ def _profile_post_mutuals_replies(account_dir: str, fields: {}) -> None: show_replies_mutuals_file = account_dir + '/.repliesFromMutualsOnly' if os.path.isfile(show_replies_mutuals_file): if not show_replies_mutuals: - try: - os.remove(show_replies_mutuals_file) - except OSError: - print('EX: unable to remove repliesFromMutualsOnly file ' + - show_replies_mutuals_file) + remove_file(show_replies_mutuals_file, + 'EX: unable to remove repliesFromMutualsOnly file ' + + show_replies_mutuals_file) else: if show_replies_mutuals: save_flag_file(show_replies_mutuals_file, @@ -1048,12 +999,10 @@ def _profile_post_only_follower_replies(fields: {}, show_replies_followers_file = account_dir + '/.repliesFromFollowersOnly' if os.path.isfile(show_replies_followers_file): if not show_replies_followers: - try: - os.remove(show_replies_followers_file) - except OSError: - print('EX: unable to remove ' + - 'repliesFromFollowersOnly file ' + - show_replies_followers_file) + remove_file(show_replies_followers_file, + 'EX: unable to remove ' + + 'repliesFromFollowersOnly file ' + + show_replies_followers_file) else: if show_replies_followers: save_flag_file(show_replies_followers_file, @@ -1072,11 +1021,9 @@ def _profile_post_show_quote_toots(fields: {}, account_dir: str) -> None: show_quote_toots_file = account_dir + '/.allowQuotes' if os.path.isfile(show_quote_toots_file): if not show_quote_toots: - try: - os.remove(show_quote_toots_file) - except OSError: - print('EX: unable to remove allowQuotes file ' + - show_quote_toots_file) + remove_file(show_quote_toots_file, + 'EX: unable to remove allowQuotes file ' + + show_quote_toots_file) else: if show_quote_toots: save_flag_file(show_quote_toots_file, @@ -1094,11 +1041,9 @@ def _profile_post_show_questions(fields: {}, account_dir: str) -> None: show_vote_file = account_dir + '/.noVotes' if os.path.isfile(show_vote_file): if show_vote_posts: - try: - os.remove(show_vote_file) - except OSError: - print('EX: unable to remove noVotes file ' + - show_vote_file) + remove_file(show_vote_file, + 'EX: unable to remove noVotes file ' + + show_vote_file) else: if not show_vote_posts: save_flag_file(show_vote_file, @@ -1144,11 +1089,9 @@ def _profile_post_bold_reading(base_dir: str, if self.server.bold_reading.get(nickname): del self.server.bold_reading[nickname] if os.path.isfile(bold_reading_filename): - try: - os.remove(bold_reading_filename) - except OSError: - print('EX: _profile_edit unable to delete ' + - bold_reading_filename) + remove_file(bold_reading_filename, + 'EX: _profile_edit unable to delete ' + + bold_reading_filename) def _profile_post_hide_reaction_button2(base_dir: str, @@ -1169,18 +1112,14 @@ def _profile_post_hide_reaction_button2(base_dir: str, hide_reaction_button_file) # remove notify Reaction selection if os.path.isfile(notify_reactions_filename): - try: - os.remove(notify_reactions_filename) - except OSError: - print('EX: _profile_edit unable to delete ' + - notify_reactions_filename) + remove_file(notify_reactions_filename, + 'EX: _profile_edit unable to delete ' + + notify_reactions_filename) if not hide_reaction_button_active: if os.path.isfile(hide_reaction_button_file): - try: - os.remove(hide_reaction_button_file) - except OSError: - print('EX: _profile_edit unable to delete ' + - hide_reaction_button_file) + remove_file(hide_reaction_button_file, + 'EX: _profile_edit unable to delete ' + + hide_reaction_button_file) def _profile_post_minimize_images(base_dir: str, nickname: str, domain: str, @@ -1224,18 +1163,14 @@ def _profile_post_hide_like_button2(base_dir: str, nickname: str, domain: str, hide_like_button_file) # remove notify likes selection if os.path.isfile(notify_likes_filename): - try: - os.remove(notify_likes_filename) - except OSError: - print('EX: _profile_edit unable to delete ' + - notify_likes_filename) + remove_file(notify_likes_filename, + 'EX: _profile_edit unable to delete ' + + notify_likes_filename) if not hide_like_button_active: if os.path.isfile(hide_like_button_file): - try: - os.remove(hide_like_button_file) - except OSError: - print('EX: _profile_edit unable to delete ' + - hide_like_button_file) + remove_file(hide_like_button_file, + 'EX: _profile_edit unable to delete ' + + hide_like_button_file) def _profile_post_remove_retweets(base_dir: str, nickname: str, domain: str, @@ -1253,11 +1188,9 @@ def _profile_post_remove_retweets(base_dir: str, nickname: str, domain: str, remove_twitter_filename) if not remove_twitter_active: if os.path.isfile(remove_twitter_filename): - try: - os.remove(remove_twitter_filename) - except OSError: - print('EX: _profile_edit unable to delete ' + - remove_twitter_filename) + remove_file(remove_twitter_filename, + 'EX: _profile_edit unable to delete ' + + remove_twitter_filename) def _profile_post_dms_from_followers(base_dir: str, nickname: str, domain: str, @@ -1284,11 +1217,9 @@ def _profile_post_dms_from_followers(base_dir: str, nickname: str, domain: str, follow_dms_filename) if not follow_dms_active: if os.path.isfile(follow_dms_filename): - try: - os.remove(follow_dms_filename) - except OSError: - print('EX: _profile_edit unable to delete ' + - follow_dms_filename) + remove_file(follow_dms_filename, + 'EX: _profile_edit unable to delete ' + + follow_dms_filename) return actor_changed @@ -1306,21 +1237,17 @@ def _profile_post_remove_custom_font(base_dir: str, nickname: str, domain: str, font_ext = ('woff', 'woff2', 'otf', 'ttf') for ext in font_ext: if os.path.isfile(base_dir + '/fonts/custom.' + ext): - try: - os.remove(base_dir + '/fonts/custom.' + ext) - except OSError: - print('EX: _profile_edit unable to delete ' + - base_dir + '/fonts/custom.' + ext) + remove_file(base_dir + '/fonts/custom.' + ext, + 'EX: _profile_edit unable to delete ' + + base_dir + '/fonts/custom.' + ext) if os.path.isfile(base_dir + '/fonts/custom.' + ext + '.etag'): - try: - os.remove(base_dir + - '/fonts/custom.' + ext + '.etag') - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - base_dir + '/fonts/custom.' + - ext + '.etag') + remove_file(base_dir + + '/fonts/custom.' + ext + '.etag', + 'EX: _profile_edit ' + + 'unable to delete ' + + base_dir + '/fonts/custom.' + + ext + '.etag') curr_theme = get_theme(base_dir) if curr_theme: self.server.theme_name = curr_theme @@ -1379,10 +1306,8 @@ def _profile_post_reject_spam_actors(base_dir: str, save_flag_file(actor_spam_filter_filename, 'EX: unable to write reject spam actors') else: - try: - os.remove(actor_spam_filter_filename) - except OSError: - print('EX: unable to remove reject spam actors') + remove_file(actor_spam_filter_filename, + 'EX: unable to remove reject spam actors') def _profile_post_approve_followers(on_final_welcome_screen: bool, @@ -2805,11 +2730,9 @@ def profile_edit(self, calling_domain: str, cookie: str, os.mkdir(base_dir + '/imports') filename_base = base_dir + '/imports/newtheme.zip' if os.path.isfile(filename_base): - try: - os.remove(filename_base) - except OSError: - print('EX: _profile_edit unable to delete ' + - filename_base) + remove_file(filename_base, + 'EX: _profile_edit unable to delete ' + + filename_base) elif m_type == 'importFollows': filename_base = \ acct_dir(base_dir, nickname, domain) + \ @@ -2853,11 +2776,9 @@ def profile_edit(self, calling_domain: str, cookie: str, ' media removing metadata') # remove existing etag if os.path.isfile(post_image_filename + '.etag'): - try: - os.remove(post_image_filename + '.etag') - except OSError: - print('EX: _profile_edit unable to delete ' + - post_image_filename + '.etag') + remove_file(post_image_filename + '.etag', + 'EX: _profile_edit unable to delete ' + + post_image_filename + '.etag') city = get_spoofed_city(self.server.city, base_dir, nickname, domain) diff --git a/daemon_post_question.py b/daemon_post_question.py index 74ded1fcb..bf663ca36 100644 --- a/daemon_post_question.py +++ b/daemon_post_question.py @@ -29,6 +29,7 @@ from posts import create_direct_message_post from daemon_utils import post_to_outbox from inbox import populate_replies from data import append_string +from data import remove_file def receive_vote(self, calling_domain: str, cookie: str, @@ -302,12 +303,10 @@ def _send_reply_to_question(self, base_dir: str, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: _send_reply_to_question ' + - 'unable to delete ' + - cached_post_filename) + remove_file(cached_post_filename, + 'EX: _send_reply_to_question ' + + 'unable to delete ' + + cached_post_filename) # remove from memory cache remove_post_from_cache(post_json_object, recent_posts_cache) diff --git a/daemon_post_receive.py b/daemon_post_receive.py index ff070316c..e886a365d 100644 --- a/daemon_post_receive.py +++ b/daemon_post_receive.py @@ -66,6 +66,7 @@ from shares import add_shares_to_actor from person import get_actor_update_json from maps import geocoords_to_osm_link from data import save_string +from data import remove_file NEW_POST_SUCCESS = 1 NEW_POST_FAILED = -1 @@ -423,11 +424,9 @@ def _receive_new_post_process_editblog(self, fields: {}, fields['postUrl'].replace('/', '#') + '.html' if os.path.isfile(cached_filename): print('Edited blog post, removing cached html') - try: - os.remove(cached_filename) - except OSError: - print('EX: _receive_new_post_process ' + - 'unable to delete ' + cached_filename) + remove_file(cached_filename, + 'EX: _receive_new_post_process ' + + 'unable to delete ' + cached_filename) # remove from memory cache remove_post_from_cache(post_json_object, recent_posts_cache) @@ -1746,11 +1745,9 @@ def _receive_new_post_process_newshare(self, fields: {}, if filename: if os.path.isfile(filename): - try: - os.remove(filename) - except OSError: - print('EX: _receive_new_post_process ' + - 'unable to delete ' + filename) + remove_file(filename, + 'EX: _receive_new_post_process ' + + 'unable to delete ' + filename) self.post_to_nickname = nickname return NEW_POST_SUCCESS diff --git a/data.py b/data.py index ad9f7a115..67a166b40 100644 --- a/data.py +++ b/data.py @@ -7,6 +7,8 @@ __email__ = "bob@libreserver.org" __status__ = "Production" __module_group__ = "Core" +import os + def _store_base(text: str, filename: str, exception_text: str, mode: str) -> bool: @@ -148,3 +150,16 @@ def prepend_string(text: str, filename: str, exception_text: str) -> bool: exception_text = exception_text.replace('[ex]', str(exc)) print(exception_text) return False + + +def remove_file(filename: str, exception_text: str) -> bool: + """Deletes a file + """ + try: + os.remove(filename) + return True + except OSError as exc: + if '[ex]' in exception_text: + exception_text = exception_text.replace('[ex]', str(exc)) + print(exception_text) + return False diff --git a/delete.py b/delete.py index 0b8472b19..cfaf211a4 100644 --- a/delete.py +++ b/delete.py @@ -28,6 +28,7 @@ from session import post_json from webfinger import webfinger_handle from auth import create_basic_auth_header from posts import get_person_box +from data import remove_file def send_delete_via_server(base_dir: str, session, @@ -214,8 +215,6 @@ def remove_old_hashtags(base_dir: str, max_months: int) -> str: break for remove_filename in remove_hashtags: - try: - os.remove(remove_filename) - except OSError: - print('EX: remove_old_hashtags unable to delete ' + - remove_filename) + remove_file(remove_filename, + 'EX: remove_old_hashtags unable to delete ' + + remove_filename) diff --git a/follow.py b/follow.py index 66d7fd653..8e1ca5a73 100644 --- a/follow.py +++ b/follow.py @@ -49,6 +49,7 @@ from data import load_string from data import append_string from data import load_list from data import save_string +from data import remove_file def create_initial_last_seen(base_dir: str, http_prefix: str) -> None: @@ -388,10 +389,8 @@ def clear_follows(base_dir: str, nickname: str, domain: str, os.mkdir(accounts_dir) filename = accounts_dir + '/' + follow_file if os.path.isfile(filename): - try: - os.remove(filename) - except OSError: - print('EX: clear_follows unable to delete ' + filename) + remove_file(filename, + 'EX: clear_follows unable to delete ' + filename) def clear_followers(base_dir: str, nickname: str, domain: str) -> None: @@ -781,12 +780,10 @@ def followed_account_accepts(session, base_dir: str, http_prefix: str, acct_dir(base_dir, nickname_to_follow, domain_to_follow) + \ '/requests/' + nickname + '@' + domain + '.follow' if os.path.isfile(follow_activity_filename): - try: - os.remove(follow_activity_filename) - except OSError: - print('EX: follow Accept ' + - 'followed_account_accepts unable to delete ' + - follow_activity_filename) + remove_file(follow_activity_filename, + 'EX: follow Accept ' + + 'followed_account_accepts unable to delete ' + + follow_activity_filename) group_account: bool = False if follow_json: @@ -873,11 +870,9 @@ def followed_account_rejects(session, session_onion, session_i2p, remove_from_follow_requests(base_dir, nickname_to_follow, domain_to_follow, deny_handle, debug) # remove the follow request json - try: - os.remove(follow_activity_filename) - except OSError: - print('EX: followed_account_rejects unable to delete ' + - follow_activity_filename) + remove_file(follow_activity_filename, + 'EX: followed_account_rejects unable to delete ' + + follow_activity_filename) curr_session = session if domain.endswith('.onion') and session_onion: curr_session = session_onion diff --git a/happening.py b/happening.py index 8cc8e7b89..33e851e01 100644 --- a/happening.py +++ b/happening.py @@ -41,6 +41,7 @@ from data import load_string from data import save_string from data import append_string from data import prepend_string +from data import remove_file def _strings_are_digits(strings_list: []) -> bool: @@ -101,10 +102,8 @@ def _remove_event_from_timeline(event_id: str, save_string(events_timeline, tl_events_filename, 'EX: ERROR: unable to save events timeline') elif os.path.isfile(tl_events_filename): - try: - os.remove(tl_events_filename) - except OSError: - print('EX: ERROR: unable to remove events timeline') + remove_file(tl_events_filename, + 'EX: ERROR: unable to remove events timeline') def save_event_post(base_dir: str, handle: str, post_id: str, diff --git a/importFollowing.py b/importFollowing.py index 07e19b28c..486cf0da8 100644 --- a/importFollowing.py +++ b/importFollowing.py @@ -24,6 +24,7 @@ from threads import begin_thread from person import set_person_notes from data import load_string from data import save_string +from data import remove_file def _establish_import_session(httpd, @@ -220,11 +221,9 @@ def run_import_following(base_dir: str, httpd): continue if not _update_import_following(base_dir, account, httpd, import_filename): - try: - os.remove(import_filename) - except OSError: - print('EX: unable to remove import file ' + - import_filename) + remove_file(import_filename, + 'EX: unable to remove import file ' + + import_filename) else: break diff --git a/inbox.py b/inbox.py index 24ec81ca8..e6bac5be4 100644 --- a/inbox.py +++ b/inbox.py @@ -143,6 +143,7 @@ from data import save_flag_file from data import load_string from data import append_string from data import prepend_string +from data import remove_file def _store_last_post_id(base_dir: str, nickname: str, domain: str, @@ -2810,12 +2811,11 @@ def clear_queue_items(base_dir: str, queue: []) -> None: continue for _, _, queuefiles in os.walk(queue_dir): for qfile in queuefiles: - try: - os.remove(os.path.join(queue_dir, qfile)) + filename = os.path.join(queue_dir, qfile) + if remove_file(filename, + 'EX: clear_queue_items unable to delete ' + + qfile): ctr += 1 - except OSError: - print('EX: clear_queue_items unable to delete ' + - qfile) break break if ctr > 0: @@ -2924,11 +2924,10 @@ def _inbox_quota_exceeded(queue: {}, queue_filename: str, post_domain + ' reached (' + str(domain_max_posts_per_day) + ')') if queue: - try: - os.remove(queue_filename) - except OSError: - print('EX: _inbox_quota_exceeded unable to delete 1 ' + - str(queue_filename)) + ex_text = \ + 'EX: _inbox_quota_exceeded unable to delete 1 ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) return True @@ -2947,11 +2946,10 @@ def _inbox_quota_exceeded(queue: {}, queue_filename: str, post_domain + ' reached (' + str(domain_max_posts_per_min) + ')') if queue: - try: - os.remove(queue_filename) - except OSError: - print('EX: _inbox_quota_exceeded unable to delete 2 ' + - str(queue_filename)) + ex_text = \ + 'EX: _inbox_quota_exceeded unable to delete 2 ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) return True @@ -2969,11 +2967,10 @@ def _inbox_quota_exceeded(queue: {}, queue_filename: str, post_handle + ' reached (' + str(account_max_posts_per_day) + ')') if queue: - try: - os.remove(queue_filename) - except OSError: - print('EX: _inbox_quota_exceeded unable to delete 3 ' + - str(queue_filename)) + ex_text = \ + 'EX: _inbox_quota_exceeded unable to delete 3 ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) return True @@ -2992,11 +2989,10 @@ def _inbox_quota_exceeded(queue: {}, queue_filename: str, post_handle + ' reached (' + str(account_max_posts_per_min) + ')') if queue: - try: - os.remove(queue_filename) - except OSError: - print('EX: _inbox_quota_exceeded unable to delete 4 ' + - str(queue_filename)) + ex_text = \ + 'EX: _inbox_quota_exceeded unable to delete 4 ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) return True @@ -3574,11 +3570,10 @@ def run_inbox_queue(server, queue.pop(0) # delete the queue file if os.path.isfile(queue_filename): - try: - os.remove(queue_filename) - except OSError: - print('EX: run_inbox_queue 1 unable to delete ' + - str(queue_filename)) + ex_text = \ + 'EX: run_inbox_queue 1 unable to delete ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) continue curr_time = get_current_time_int() @@ -3666,11 +3661,10 @@ def run_inbox_queue(server, sender_nickname = get_nickname_from_actor(queue_json['actor']) if evil_nickname(sender_nickname): if os.path.isfile(queue_filename): - try: - os.remove(queue_filename) - except OSError: - print('EX: run_inbox_queue 11 unable to delete ' + - str(queue_filename)) + ex_text = \ + 'EX: run_inbox_queue 11 unable to delete ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) continue @@ -3743,11 +3737,10 @@ def run_inbox_queue(server, if debug: print('Queue: public key could not be obtained from ' + key_id) if os.path.isfile(queue_filename): - try: - os.remove(queue_filename) - except OSError: - print('EX: run_inbox_queue 2 unable to delete ' + - str(queue_filename)) + ex_text = \ + 'EX: run_inbox_queue 2 unable to delete ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) continue @@ -3803,11 +3796,10 @@ def run_inbox_queue(server, if http_signature_failed or verify_all_signatures: if os.path.isfile(queue_filename): - try: - os.remove(queue_filename) - except OSError: - print('EX: run_inbox_queue 3 unable to delete ' + - str(queue_filename)) + ex_text = \ + 'EX: run_inbox_queue 3 unable to delete ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) continue @@ -3825,11 +3817,10 @@ def run_inbox_queue(server, print('WARN: jsonld inbox signature check failed ' + key_id) if os.path.isfile(queue_filename): - try: - os.remove(queue_filename) - except OSError: - print('EX: run_inbox_queue 4 unable to delete ' + - str(queue_filename)) + ex_text = \ + 'EX: run_inbox_queue 4 unable to delete ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) fitness_performance(inbox_start_time, server.fitness, @@ -4138,10 +4129,9 @@ def run_inbox_queue(server, # should the current queue item be removed? if remove_queue_item: if os.path.isfile(queue_filename): - try: - os.remove(queue_filename) - except OSError: - print('EX: run_inbox_queue 10 unable to delete ' + - str(queue_filename)) + ex_text = \ + 'EX: run_inbox_queue 10 unable to delete ' + \ + str(queue_filename) + remove_file(queue_filename, ex_text) if queue: queue.pop(0) diff --git a/inbox_receive.py b/inbox_receive.py index 1c5b5cf72..d7ecaba5c 100644 --- a/inbox_receive.py +++ b/inbox_receive.py @@ -91,6 +91,7 @@ from data import save_flag_file from data import append_string from data import prepend_string from data import load_string +from data import remove_file def inbox_update_index(boxname: str, base_dir: str, handle: str, @@ -352,11 +353,9 @@ def _receive_update_to_question(recent_posts_cache: {}, message_json: {}, get_cached_post_filename(base_dir, nickname, domain, message_json) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: _receive_update_to_question unable to delete ' + - cached_post_filename) + remove_file(cached_post_filename, + 'EX: _receive_update_to_question unable to delete ' + + cached_post_filename) # remove from memory cache remove_post_from_cache(message_json, recent_posts_cache) return True @@ -484,11 +483,9 @@ def receive_edit_to_post(recent_posts_cache: {}, message_json: {}, get_cached_post_filename(base_dir, nickname, domain, message_json) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: _receive_edit_to_post unable to delete ' + - cached_post_filename) + remove_file(cached_post_filename, + 'EX: _receive_edit_to_post unable to delete ' + + cached_post_filename) # remove any cached html for the post which was edited delete_cached_html(base_dir, nickname, domain, post_json_object) # remove from memory cache @@ -2088,11 +2085,10 @@ def receive_announce(recent_posts_cache: {}, if domain not in announce_url and not_in_onion: if os.path.isfile(post_filename): # if the announce can't be downloaded then remove it - try: - os.remove(post_filename) - except OSError: - print('EX: _receive_announce unable to delete ' + - str(post_filename)) + ex_text = \ + 'EX: _receive_announce unable to delete ' + \ + str(post_filename) + remove_file(post_filename, ex_text) else: if debug: actor_url = get_actor_from_post(message_json) @@ -2213,11 +2209,9 @@ def receive_question_vote(server, base_dir: str, nickname: str, domain: str, get_cached_post_filename(base_dir, nickname, domain, question_json) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: replytoQuestion unable to delete ' + - cached_post_filename) + remove_file(cached_post_filename, + 'EX: replytoQuestion unable to delete ' + + cached_post_filename) page_number: int = 1 show_published_date_only: bool = False diff --git a/inbox_receive_undo.py b/inbox_receive_undo.py index 8a59bdb87..78d80d27d 100644 --- a/inbox_receive_undo.py +++ b/inbox_receive_undo.py @@ -36,6 +36,7 @@ from follow import follower_approval_active from bookmarks import undo_bookmarks_collection_entry from webapp_post import individual_post_as_html from reaction import undo_reaction_collection_entry +from data import remove_file def _receive_undo_follow(base_dir: str, message_json: {}, @@ -652,9 +653,8 @@ def receive_undo_announce(recent_posts_cache: {}, undo_announce_collection_entry(recent_posts_cache, base_dir, post_filename, actor_url, domain, debug) if os.path.isfile(post_filename): - try: - os.remove(post_filename) - except OSError: - print('EX: _receive_undo_announce unable to delete ' + - str(post_filename)) + ex_text = \ + 'EX: _receive_undo_announce unable to delete ' + \ + str(post_filename) + remove_file(post_filename, ex_text) return True diff --git a/like.py b/like.py index 56c116261..f0aff4c09 100644 --- a/like.py +++ b/like.py @@ -33,6 +33,7 @@ from session import post_json from webfinger import webfinger_handle from auth import create_basic_auth_header from posts import get_person_box +from data import remove_file def no_of_likes(post_json_object: {}) -> int: @@ -471,11 +472,9 @@ def update_likes_collection(recent_posts_cache: {}, domain, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: update_likes_collection unable to delete ' + - cached_post_filename) + remove_file(cached_post_filename, + 'EX: update_likes_collection unable to delete ' + + cached_post_filename) obj = post_json_object if has_object_dict(post_json_object): @@ -551,12 +550,11 @@ def undo_likes_collection_entry(recent_posts_cache: {}, domain, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: undo_likes_collection_entry ' + - 'unable to delete cached post ' + - str(cached_post_filename)) + ex_text = \ + 'EX: undo_likes_collection_entry ' + \ + 'unable to delete cached post ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) remove_post_from_cache(post_json_object, recent_posts_cache) if not post_json_object.get('type'): diff --git a/lxmf.py b/lxmf.py index 01e3be0e8..b23147af5 100644 --- a/lxmf.py +++ b/lxmf.py @@ -14,6 +14,7 @@ from utils import get_attachment_property_value from utils import acct_dir from utils import load_json from utils import string_contains +from data import remove_file VALID_LXMF_CHARS = set('0123456789abcdefghijklmnopqrstuvwxyz') @@ -111,10 +112,8 @@ def set_lxmf_address(base_dir: str, nickname: str, domain: str, qrcode_filename = \ acct_dir(base_dir, nickname, domain) + '/qrcode_lxmf.png' if os.path.isfile(qrcode_filename): - try: - os.remove(qrcode_filename) - except OSError: - print('EX: cannot remove lxmf qrcode ' + qrcode_filename) + remove_file(qrcode_filename, + 'EX: cannot remove lxmf qrcode ' + qrcode_filename) lxmf_address = lxmf_address.strip() diff --git a/manualapprove.py b/manualapprove.py index 5cd305728..5e4b22fc4 100644 --- a/manualapprove.py +++ b/manualapprove.py @@ -29,6 +29,7 @@ from data import load_string from data import append_string from data import prepend_string from data import load_list +from data import remove_file def manual_deny_follow_request2(session, session_onion, session_i2p, @@ -377,17 +378,13 @@ def manual_approve_follow_request(session, session_onion, session_i2p, # remove the .follow file if follow_activity_filename: if os.path.isfile(follow_activity_filename): - try: - os.remove(follow_activity_filename) - except OSError: - print('EX: manual_approve_follow_request ' + - 'unable to delete ' + follow_activity_filename) + remove_file(follow_activity_filename, + 'EX: manual_approve_follow_request ' + + 'unable to delete ' + follow_activity_filename) else: - try: - os.remove(approve_follows_filename + '.new') - except OSError: - print('EX: manual_approve_follow_request unable to delete ' + - approve_follows_filename + '.new') + remove_file(approve_follows_filename + '.new', + 'EX: manual_approve_follow_request unable to delete ' + + approve_follows_filename + '.new') def manual_approve_follow_request_thread(session, session_onion, session_i2p, diff --git a/media.py b/media.py index 6a21c2009..89aef017a 100644 --- a/media.py +++ b/media.py @@ -35,6 +35,7 @@ from data import load_binary from data import save_string from data import load_string from data import append_string +from data import remove_file # music file ID3 v1 genres @@ -475,11 +476,9 @@ def convert_image_to_low_bandwidth(image_filename: str) -> None: """ low_bandwidth_filename = image_filename + '.low' if os.path.isfile(low_bandwidth_filename): - try: - os.remove(low_bandwidth_filename) - except OSError: - print('EX: convert_image_to_low_bandwidth unable to delete ' + - low_bandwidth_filename) + remove_file(low_bandwidth_filename, + 'EX: convert_image_to_low_bandwidth unable to delete ' + + low_bandwidth_filename) cmd = \ '/usr/bin/convert +noise Multiplicative ' + \ @@ -498,11 +497,9 @@ def convert_image_to_low_bandwidth(image_filename: str) -> None: print('WARN: timed out waiting for low bandwidth image conversion') break if os.path.isfile(low_bandwidth_filename): - try: - os.remove(image_filename) - except OSError: - print('EX: convert_image_to_low_bandwidth unable to delete ' + - image_filename) + remove_file(image_filename, + 'EX: convert_image_to_low_bandwidth unable to delete ' + + image_filename) try: os.rename(low_bandwidth_filename, image_filename) except OSError: @@ -927,11 +924,9 @@ def apply_watermark_to_image(base_dir: str, nickname: str, domain: str, if not os.path.isfile(post_image_filename + '.watermarked'): return False - try: - os.remove(post_image_filename) - except OSError: - print('EX: _apply_watermark_to_image unable to remove ' + - post_image_filename) + if not remove_file(post_image_filename, + 'EX: _apply_watermark_to_image unable to remove ' + + post_image_filename): return False try: diff --git a/newsdaemon.py b/newsdaemon.py index 98080c9cf..f60fecf85 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -47,6 +47,7 @@ from data import load_string from data import save_string from data import append_string from data import prepend_string +from data import remove_file def _update_feeds_outbox_index(base_dir: str, domain: str, @@ -753,11 +754,10 @@ def _convert_rss_to_activitypub(base_dir: str, http_prefix: str, blog['object']['arrived']) else: if os.path.isfile(filename + '.arrived'): - try: - os.remove(filename + '.arrived') - except OSError: - print('EX: _convert_rss_to_activitypub ' + - 'unable to delete ' + filename + '.arrived') + remove_file(filename + '.arrived', + 'EX: _convert_rss_to_activitypub ' + + 'unable to delete ' + + filename + '.arrived') # setting the url here links to the activitypub object # stored locally @@ -866,11 +866,10 @@ def run_newswire_daemon(base_dir: str, httpd, # waiting and recalculate the newswire if not os.path.isfile(refresh_filename): continue - try: - os.remove(refresh_filename) - except OSError: - print('EX: run_newswire_daemon unable to delete ' + - str(refresh_filename)) + ex_text = \ + 'EX: run_newswire_daemon unable to delete ' + \ + str(refresh_filename) + remove_file(refresh_filename, ex_text) break diff --git a/newswire.py b/newswire.py index db0a90356..f4f3b283c 100644 --- a/newswire.py +++ b/newswire.py @@ -57,6 +57,7 @@ from content import remove_script from data import load_list from data import load_string from data import save_binary +from data import remove_file def _remove_cdata(text: str) -> str: @@ -1799,11 +1800,10 @@ def _add_blogs_to_newswire(base_dir: str, domain: str, newswire: {}, else: # remove the file if there is nothing to moderate if os.path.isfile(newswire_moderation_filename): - try: - os.remove(newswire_moderation_filename) - except OSError: - print('EX: _add_blogs_to_newswire unable to delete ' + - str(newswire_moderation_filename)) + ex_text = \ + 'EX: _add_blogs_to_newswire unable to delete ' + \ + str(newswire_moderation_filename) + remove_file(newswire_moderation_filename, ex_text) def get_dict_from_newswire(session, base_dir: str, domain: str, diff --git a/outbox.py b/outbox.py index 4524d0522..a9226b20d 100644 --- a/outbox.py +++ b/outbox.py @@ -73,6 +73,7 @@ from inbox_receive import inbox_update_index from gemini import blog_to_gemini from markdown import blog_to_markdown from markdown import blog_to_micron +from data import remove_file def _localonly_not_local(message_json: {}, domain_full: str) -> bool: @@ -614,11 +615,9 @@ def post_message_to_outbox(session, translate: {}, data_dir(base_dir) + '/' + \ post_to_nickname + '@' + domain + '/.citations.txt' if os.path.isfile(citations_filename): - try: - os.remove(citations_filename) - except OSError: - print('EX: post_message_to_outbox unable to delete ' + - citations_filename) + remove_file(citations_filename, + 'EX: post_message_to_outbox unable to delete ' + + citations_filename) # The following activity types get added to the index files indexed_activities = ( diff --git a/person.py b/person.py index 89a544fd1..1fadfe3a3 100644 --- a/person.py +++ b/person.py @@ -96,6 +96,7 @@ from data import save_string from data import save_flag_file from data import load_string from data import append_string +from data import remove_file def generate_rsa_key() -> (str, str): @@ -704,15 +705,13 @@ def clear_person_qrcodes(base_dir: str) -> None: qrcode_filename = \ acct_dir(base_dir, nickname, domain) + '/qrcode.png' if os.path.isfile(qrcode_filename): - try: - os.remove(qrcode_filename) - except OSError: - pass + remove_file(qrcode_filename, + 'EX: clear_person_qrcodes 1 ' + + qrcode_filename) if os.path.isfile(qrcode_filename + '.etag'): - try: - os.remove(qrcode_filename + '.etag') - except OSError: - pass + remove_file(qrcode_filename + '.etag', + 'EX: clear_person_qrcodes 2 ' + + qrcode_filename + '.etag') break @@ -1388,16 +1387,12 @@ def suspend_account(base_dir: str, nickname: str, domain: str) -> None: account_dir = acct_dir(base_dir, nickname, domain) salt_filename = account_dir + '/.salt' if os.path.isfile(salt_filename): - try: - os.remove(salt_filename) - except OSError: - print('EX: suspend_account unable to delete ' + salt_filename) + remove_file(salt_filename, + 'EX: suspend_account unable to delete ' + salt_filename) token_filename = acct_dir(base_dir, nickname, domain) + '/.token' if os.path.isfile(token_filename): - try: - os.remove(token_filename) - except OSError: - print('EX: suspend_account unable to delete 2 ' + token_filename) + remove_file(token_filename, + 'EX: suspend_account unable to delete 2 ' + token_filename) suspended_filename = data_dir(base_dir) + '/suspended.txt' if os.path.isfile(suspended_filename): @@ -1509,10 +1504,8 @@ def _remove_account_media(base_dir: str, nickname: str, domain: str) -> None: media_filename = base_dir + filename if not os.path.isfile(media_filename): continue - try: - os.remove(media_filename) - except OSError: - print('EX: unable to remove media ' + media_filename) + remove_file(media_filename, + 'EX: unable to remove media ' + media_filename) def remove_account(base_dir: str, nickname: str, @@ -1550,38 +1543,28 @@ def remove_account(base_dir: str, nickname: str, if os.path.isdir(handle_dir): shutil.rmtree(handle_dir, ignore_errors=False) if os.path.isfile(handle_dir + '.json'): - try: - os.remove(handle_dir + '.json') - except OSError: - print('EX: remove_account unable to delete ' + - handle_dir + '.json') + remove_file(handle_dir + '.json', + 'EX: remove_account unable to delete ' + + handle_dir + '.json') if os.path.isfile(base_dir + '/wfendpoints/' + handle + '.json'): - try: - os.remove(base_dir + '/wfendpoints/' + handle + '.json') - except OSError: - print('EX: remove_account unable to delete ' + - base_dir + '/wfendpoints/' + handle + '.json') + remove_file(base_dir + '/wfendpoints/' + handle + '.json', + 'EX: remove_account unable to delete ' + + base_dir + '/wfendpoints/' + handle + '.json') if os.path.isfile(base_dir + '/keys/private/' + handle + '.key'): - try: - os.remove(base_dir + '/keys/private/' + handle + '.key') - except OSError: - print('EX: remove_account unable to delete ' + - base_dir + '/keys/private/' + handle + '.key') + remove_file(base_dir + '/keys/private/' + handle + '.key', + 'EX: remove_account unable to delete ' + + base_dir + '/keys/private/' + handle + '.key') if os.path.isfile(base_dir + '/keys/public/' + handle + '.pem'): - try: - os.remove(base_dir + '/keys/public/' + handle + '.pem') - except OSError: - print('EX: remove_account unable to delete ' + - base_dir + '/keys/public/' + handle + '.pem') + remove_file(base_dir + '/keys/public/' + handle + '.pem', + 'EX: remove_account unable to delete ' + + base_dir + '/keys/public/' + handle + '.pem') if os.path.isdir(base_dir + '/sharefiles/' + nickname): shutil.rmtree(base_dir + '/sharefiles/' + nickname, ignore_errors=False) if os.path.isfile(base_dir + '/wfdeactivated/' + handle + '.json'): - try: - os.remove(base_dir + '/wfdeactivated/' + handle + '.json') - except OSError: - print('EX: remove_account unable to delete ' + - base_dir + '/wfdeactivated/' + handle + '.json') + remove_file(base_dir + '/wfdeactivated/' + handle + '.json', + 'EX: remove_account unable to delete ' + + base_dir + '/wfdeactivated/' + handle + '.json') if os.path.isdir(base_dir + '/sharefilesdeactivated/' + nickname): shutil.rmtree(base_dir + '/sharefilesdeactivated/' + nickname, ignore_errors=False) diff --git a/posts.py b/posts.py index 38f015335..dedc943e5 100644 --- a/posts.py +++ b/posts.py @@ -150,6 +150,7 @@ from data import save_string from data import save_flag_file from data import append_string from data import prepend_string +from data import remove_file def convert_post_content_to_html(message_json: {}) -> None: @@ -1053,18 +1054,16 @@ def save_post_to_box(base_dir: str, http_prefix: str, post_id: str, '/postcache/').replace('.json', '') ssml_filename = base_filename + '.ssml' if os.path.isfile(ssml_filename): - try: - os.remove(ssml_filename) - except OSError: - print('EX: save_post_to_box unable to delete ssml file ' + - ssml_filename) + remove_file(ssml_filename, + 'EX: ' + + 'save_post_to_box unable to delete ssml file ' + + ssml_filename) html_filename = base_filename + '.html' if os.path.isfile(html_filename): - try: - os.remove(html_filename) - except OSError: - print('EX: save_post_to_box unable to delete html file ' + - html_filename) + remove_file(html_filename, + 'EX: ' + + 'save_post_to_box unable to delete html file ' + + html_filename) return filename @@ -2168,10 +2167,8 @@ def undo_pinned_post(base_dir: str, nickname: str, domain: str) -> None: pinned_filename = account_dir + '/pinToProfile.txt' if not os.path.isfile(pinned_filename): return - try: - os.remove(pinned_filename) - except OSError: - print('EX: undo_pinned_post unable to delete ' + pinned_filename) + remove_file(pinned_filename, + 'EX: undo_pinned_post unable to delete ' + pinned_filename) def get_pinned_post_as_json(base_dir: str, http_prefix: str, @@ -5114,11 +5111,9 @@ def _expire_announce_cache_for_person(base_dir: str, last_modified = file_last_modified(full_filename) # get time difference if not valid_post_date(last_modified, max_age_days, False): - try: - os.remove(full_filename) - except OSError: - print('EX: unable to delete from announce cache ' + - full_filename) + remove_file(full_filename, + 'EX: unable to delete from announce cache ' + + full_filename) expired_post_count += 1 return expired_post_count @@ -5146,11 +5141,9 @@ def _expire_conversations_for_person(base_dir: str, last_modified = file_last_modified(full_filename) # get time difference if not valid_post_date(last_modified, max_age_days, False): - try: - os.remove(full_filename) - except OSError: - print('EX: unable to delete from conversations ' + - full_filename) + remove_file(full_filename, + 'EX: unable to delete from conversations ' + + full_filename) expired_post_count += 1 return expired_post_count @@ -5175,11 +5168,9 @@ def _expire_posts_cache_for_person(base_dir: str, last_modified = file_last_modified(full_filename) # get time difference if not valid_post_date(last_modified, max_age_days, False): - try: - os.remove(full_filename) - except OSError: - print('EX: unable to delete from post cache ' + - full_filename) + remove_file(full_filename, + 'EX: unable to delete from post cache ' + + full_filename) expired_post_count += 1 return expired_post_count @@ -5478,11 +5469,9 @@ def set_post_expiry_keep_dms(base_dir: str, nickname: str, domain: str, acct_handle_dir(base_dir, handle) + '/.expire_posts_dms' if keep_dms: if os.path.isfile(expire_dms_filename): - try: - os.remove(expire_dms_filename) - except OSError: - print('EX: unable to write set_post_expiry_keep_dms False ' + - expire_dms_filename) + remove_file(expire_dms_filename, + 'EX: unable to write set_post_expiry_keep_dms False ' + + expire_dms_filename) return save_flag_file(expire_dms_filename, 'EX: unable to write set_post_expiry_keep_dms True ' + @@ -5626,12 +5615,11 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str, if not os.path.isfile(full_original_filename): # if the original file doesn't exist (was remotely deleted by # its author) then remove the corresponding edits - try: - os.remove(full_filename) + if remove_file(full_filename, + 'EX: unable to remove ' + ext_name + ' file ' + + full_filename): edits_removed_ctr += 1 - except OSError: - print('EX: unable to remove ' + ext_name + ' file ' + - full_filename) + else: continue edit_files_ctr += 1 if os.path.isfile(full_filename): @@ -5681,13 +5669,11 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str, ext_name + ' ' + file_path + ' -> ' + archive_path) else: - try: - os.remove(edit_filename) + if remove_file(edit_filename, + 'EX: archive_posts_for_person ' + + 'unable to delete ' + + ext_name + ' ' + edit_filename): remove_edits_ctr += 1 - except OSError: - print('EX: ' + - 'archive_posts_for_person unable to delete ' + - ext_name + ' ' + edit_filename) if archive_dir: print('Archived ' + str(remove_edits_ctr) + ' ' + boxname + ' ' + ext_name + ' for ' + nickname + '@' + domain) @@ -5780,11 +5766,9 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str, os.path.join(post_cache_dir, post_filename) post_cache_filename = post_cache_filename.replace('.json', '.html') if os.path.isfile(post_cache_filename): - try: - os.remove(post_cache_filename) - except OSError: - print('EX: archive_posts_for_person unable to delete ' + - post_cache_filename) + remove_file(post_cache_filename, + 'EX: archive_posts_for_person unable to delete ' + + post_cache_filename) no_of_posts -= 1 remove_ctr += 1 diff --git a/reaction.py b/reaction.py index 67ac924c8..c7090a48e 100644 --- a/reaction.py +++ b/reaction.py @@ -40,6 +40,7 @@ from auth import create_basic_auth_header from posts import get_person_box from data import load_list from data import save_string +from data import remove_file # the maximum number of reactions from individual actors which can be # added to a post. Hence an adversary can't bombard you with sockpuppet @@ -546,11 +547,9 @@ def update_reaction_collection(recent_posts_cache: {}, domain, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: update_reaction_collection unable to delete ' + - cached_post_filename) + remove_file(cached_post_filename, + 'EX: update_reaction_collection unable to delete ' + + cached_post_filename) obj = post_json_object if has_object_dict(post_json_object): @@ -716,12 +715,11 @@ def undo_reaction_collection_entry(recent_posts_cache: {}, domain, post_json_object) if cached_post_filename: if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: undo_reaction_collection_entry ' + - 'unable to delete cached post ' + - str(cached_post_filename)) + ex_text = \ + 'EX: undo_reaction_collection_entry ' + \ + 'unable to delete cached post ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) remove_post_from_cache(post_json_object, recent_posts_cache) if not post_json_object.get('type'): diff --git a/relationships.py b/relationships.py index 7190ced38..d3aefc5e9 100644 --- a/relationships.py +++ b/relationships.py @@ -24,6 +24,7 @@ from utils import get_domain_from_actor from utils import load_json from data import load_string from data import save_string +from data import remove_file def get_moved_accounts(base_dir: str, nickname: str, domain: str, @@ -306,11 +307,9 @@ def update_moved_actors(base_dir: str, debug: bool) -> None: moved_accounts_filename = data_dir(base_dir) + '/actors_moved.txt' if not moved_str: if os.path.isfile(moved_accounts_filename): - try: - os.remove(moved_accounts_filename) - except OSError: - print('EX: update_moved_actors unable to remove ' + - moved_accounts_filename) + remove_file(moved_accounts_filename, + 'EX: update_moved_actors unable to remove ' + + moved_accounts_filename) return save_string(moved_str, moved_accounts_filename, diff --git a/roles.py b/roles.py index ca009a8e3..183e4b46e 100644 --- a/roles.py +++ b/roles.py @@ -18,6 +18,7 @@ from utils import get_config_param from status import get_status_number from data import load_list from data import save_string +from data import remove_file def _clear_role_status(base_dir: str, role: str) -> None: @@ -315,10 +316,8 @@ def set_roles_from_list(base_dir: str, domain: str, admin_nickname: str, if not fields.get(list_name): if os.path.isfile(roles_filename): _clear_role_status(base_dir, role_name) - try: - os.remove(roles_filename) - except OSError: - print('EX: failed to remove roles file ' + roles_filename) + remove_file(roles_filename, + 'EX: failed to remove roles file ' + roles_filename) return _clear_role_status(base_dir, role_name) if ',' in fields[list_name]: diff --git a/schedule.py b/schedule.py index 291bc2164..62e5d52d1 100644 --- a/schedule.py +++ b/schedule.py @@ -26,6 +26,7 @@ from threads import begin_thread from siteactive import save_unavailable_sites from data import save_string from data import load_list +from data import remove_file def _update_post_schedule(base_dir: str, handle: str, httpd, @@ -64,12 +65,11 @@ def _update_post_schedule(base_dir: str, handle: str, httpd, if delete_schedule_post: # delete extraneous scheduled posts if os.path.isfile(post_filename): - try: - os.remove(post_filename) - except OSError: - print('EX: ' + - '_update_post_schedule unable to delete ' + - str(post_filename)) + ex_text = \ + 'EX: ' + \ + '_update_post_schedule unable to delete ' + \ + str(post_filename) + remove_file(post_filename, ex_text) continue # create the new index file index_lines.append(line) @@ -173,11 +173,10 @@ def _update_post_schedule(base_dir: str, handle: str, httpd, httpd.mitm_servers, httpd.instance_software): index_lines.remove(line) - try: - os.remove(post_filename) - except OSError: - print('EX: _update_post_schedule unable to delete ' + - str(post_filename)) + ex_text = \ + 'EX: _update_post_schedule unable to delete ' + \ + str(post_filename) + remove_file(post_filename, ex_text) continue # move to the outbox @@ -262,11 +261,9 @@ def remove_scheduled_posts(base_dir: str, nickname: str, domain: str) -> None: schedule_index_filename = \ acct_dir(base_dir, nickname, domain) + '/schedule.index' if os.path.isfile(schedule_index_filename): - try: - os.remove(schedule_index_filename) - except OSError: - print('EX: remove_scheduled_posts unable to delete ' + - schedule_index_filename) + remove_file(schedule_index_filename, + 'EX: remove_scheduled_posts unable to delete ' + + schedule_index_filename) # remove the scheduled posts scheduled_dir = acct_dir(base_dir, nickname, domain) + '/scheduled' if not os.path.isdir(scheduled_dir): @@ -275,8 +272,6 @@ def remove_scheduled_posts(base_dir: str, nickname: str, domain: str) -> None: file_path = os.path.join(scheduled_dir, scheduled_post_filename) if not os.path.isfile(file_path): continue - try: - os.remove(file_path) - except OSError: - print('EX: remove_scheduled_posts unable to delete ' + - file_path) + remove_file(file_path, + 'EX: remove_scheduled_posts unable to delete ' + + file_path) diff --git a/session.py b/session.py index 326215ccd..cc43cca17 100644 --- a/session.py +++ b/session.py @@ -30,6 +30,7 @@ from data import append_string from data import save_string from data import save_binary from data import load_binary +from data import remove_file def create_session(proxy_type: str): @@ -846,11 +847,9 @@ def download_image(session, url: str, image_filename: str, debug: bool, str(result.status_code)) # remove partial download if os.path.isfile(image_filename): - try: - os.remove(image_filename) - except OSError: - print('EX: download_image unable to delete ' + - image_filename) + remove_file(image_filename, + 'EX: download_image unable to delete ' + + image_filename) else: media_binary = result.content if binary_is_image(image_filename, media_binary): diff --git a/shares.py b/shares.py index df44911c4..aa66d18fd 100644 --- a/shares.py +++ b/shares.py @@ -62,6 +62,7 @@ from cache import remove_person_from_cache from cache import store_person_in_cache from data import save_string from data import load_string +from data import remove_file def _load_dfc_ids(base_dir: str, system_language: str, @@ -171,11 +172,9 @@ def remove_shared_item2(base_dir: str, nickname: str, domain: str, continue if not os.path.isfile(item_idfile + '.' + ext): continue - try: - os.remove(item_idfile + '.' + ext) - except OSError: - print('EX: remove_shared_item unable to delete ' + - item_idfile + '.' + ext) + remove_file(item_idfile + '.' + ext, + 'EX: remove_shared_item unable to delete ' + + item_idfile + '.' + ext) # remove the item itself del shares_json[item_id] save_json(shares_json, shares_filename) @@ -401,11 +400,10 @@ def add_share(base_dir: str, image_filename, item_idfile + '.' + ext, city, content_license_url, exif_json) if move_image: - try: - os.remove(image_filename) - except OSError: - print('EX: add_share unable to delete ' + - str(image_filename)) + ex_text = \ + 'EX: add_share unable to delete ' + \ + str(image_filename) + remove_file(image_filename, ex_text) image_url = \ http_prefix + '://' + domain_full + \ '/sharefiles/' + nickname + '/' + item_id + '.' + ext @@ -507,11 +505,9 @@ def _expire_shares_for_account(base_dir: str, nickname: str, domain: str, for ext in formats: if not os.path.isfile(item_idfile + '.' + ext): continue - try: - os.remove(item_idfile + '.' + ext) - except OSError: - print('EX: _expire_shares_for_account unable to delete ' + - item_idfile + '.' + ext) + remove_file(item_idfile + '.' + ext, + 'EX: _expire_shares_for_account unable to delete ' + + item_idfile + '.' + ext) save_json(shares_json, shares_filename) return removed_ctr diff --git a/tests.py b/tests.py index e98209ba6..6997ee99e 100644 --- a/tests.py +++ b/tests.py @@ -247,6 +247,7 @@ from blog import html_blog_post_gemini_links from data import load_list from data import load_string from data import save_string +from data import remove_file TEST_SERVER_GROUP_RUNNING = False @@ -4352,10 +4353,7 @@ def _test_json_string() -> None: assert received_json['content'] == message_str encoded_str = json.dumps(test_json, ensure_ascii=False) assert message_str in encoded_str - try: - os.remove(filename) - except OSError: - pass + remove_file(filename, 'EX: _test_json_string') def _test_save_load_json(): @@ -4366,10 +4364,7 @@ def _test_save_load_json(): } test_filename = '.epicyon_tests_test_save_load_json.json' if os.path.isfile(test_filename): - try: - os.remove(test_filename) - except OSError: - pass + remove_file(test_filename, 'EX: _test_save_load_json 1') assert save_json(test_json, test_filename) assert os.path.isfile(test_filename) test_load_json = load_json(test_filename) @@ -4378,10 +4373,7 @@ def _test_save_load_json(): assert test_load_json.get('param2') assert test_load_json['param1'] == 3 assert test_load_json['param2'] == '"Crème brûlée यह एक परीक्षण ह"' - try: - os.remove(test_filename) - except OSError: - pass + remove_file(test_filename, 'EX: _test_save_load_json 2') def _test_theme(): @@ -4666,7 +4658,7 @@ def _test_danger_svg(base_dir: str) -> None: with open(svg_image_filename, 'rb') as fp_svg: cached_content = fp_svg.read().decode() - os.remove(svg_image_filename) + remove_file(svg_image_filename, 'EX: _test_danger_svg') assert cached_content == svg_clean assert not scan_themes_for_scripts(base_dir) diff --git a/textmode.py b/textmode.py index 7366327c8..45d71f676 100644 --- a/textmode.py +++ b/textmode.py @@ -11,6 +11,7 @@ import os from shutil import copyfile from utils import data_dir from data import load_string +from data import remove_file def text_mode_browser(ua_str: str) -> bool: @@ -92,11 +93,9 @@ def set_text_mode_theme(base_dir: str, name: str) -> None: text_mode_banner_filename = \ base_dir + '/theme/' + name + '/banner.txt' if os.path.isfile(dir_str + '/banner.txt'): - try: - os.remove(dir_str + '/banner.txt') - except OSError: - print('EX: set_text_mode_theme unable to delete ' + - dir_str + '/banner.txt') + remove_file(dir_str + '/banner.txt', + 'EX: set_text_mode_theme unable to delete ' + + dir_str + '/banner.txt') if os.path.isfile(text_mode_banner_filename): try: copyfile(text_mode_banner_filename, dir_str + '/banner.txt') diff --git a/theme.py b/theme.py index 97071fe8c..5f4d3c817 100644 --- a/theme.py +++ b/theme.py @@ -31,6 +31,7 @@ from textmode import set_text_mode_theme from data import load_string from data import save_string from data import save_flag_file +from data import remove_file def import_theme(base_dir: str, filename: str) -> bool: @@ -105,10 +106,9 @@ def export_theme(base_dir: str, theme: str) -> bool: os.mkdir(base_dir + '/exports') export_filename = base_dir + '/exports/' + theme + '.zip' if os.path.isfile(export_filename): - try: - os.remove(export_filename) - except OSError: - print('EX: export_theme unable to delete ' + str(export_filename)) + ex_text = \ + 'EX: export_theme unable to delete ' + str(export_filename) + remove_file(export_filename, ex_text) try: make_archive(base_dir + '/exports/' + theme, 'zip', theme_dir) except BaseException: @@ -265,11 +265,9 @@ def _remove_theme(base_dir: str): for filename in theme_files: if not os.path.isfile(base_dir + '/' + filename): continue - try: - os.remove(base_dir + '/' + filename) - except OSError: - print('EX: _remove_theme unable to delete ' + - base_dir + '/' + filename) + remove_file(base_dir + '/' + filename, + 'EX: _remove_theme unable to delete ' + + base_dir + '/' + filename) def set_css_param(css: str, param: str, value: str) -> str: @@ -471,11 +469,9 @@ def disable_grayscale(base_dir: str) -> None: filename + ' [ex]') grayscale_filename = data_dir(base_dir) + '/.grayscale' if os.path.isfile(grayscale_filename): - try: - os.remove(grayscale_filename) - except OSError: - print('EX: disable_grayscale unable to delete ' + - grayscale_filename) + remove_file(grayscale_filename, + 'EX: disable_grayscale unable to delete ' + + grayscale_filename) def _set_dyslexic_font(base_dir: str) -> bool: @@ -564,11 +560,10 @@ def reset_theme_designer_settings(base_dir: str) -> None: """ custom_variables_file = data_dir(base_dir) + '/theme.json' if os.path.isfile(custom_variables_file): - try: - os.remove(custom_variables_file) + if remove_file(custom_variables_file, + 'EX: ' + + 'unable to remove theme designer settings on reset'): print('Theme designer settings were reset') - except OSError: - print('EX: unable to remove theme designer settings on reset') def _read_variables_file(base_dir: str, theme_name: str, @@ -708,13 +703,11 @@ def _set_theme_images(base_dir: str, name: str) -> None: # so remove any existing file if os.path.isfile(dir_str + '/' + background_type + '-background.' + ext): - try: - os.remove(dir_str + '/' + - background_type + '-background.' + ext) - except OSError: - print('EX: _set_theme_images unable to delete ' + - dir_str + '/' + - background_type + '-background.' + ext) + remove_file(dir_str + '/' + + background_type + '-background.' + ext, + 'EX: _set_theme_images unable to delete ' + + dir_str + '/' + + background_type + '-background.' + ext) if os.path.isfile(profile_image_filename) and \ os.path.isfile(banner_filename): @@ -746,11 +739,9 @@ def _set_theme_images(base_dir: str, name: str) -> None: account_dir + '/left_col_image.png') elif os.path.isfile(account_dir + '/left_col_image.png'): - try: - os.remove(account_dir + '/left_col_image.png') - except OSError: - print('EX: _set_theme_images unable to delete ' + - account_dir + '/left_col_image.png') + remove_file(account_dir + '/left_col_image.png', + 'EX: _set_theme_images unable to delete ' + + account_dir + '/left_col_image.png') except OSError: print('EX: _set_theme_images unable to copy ' + left_col_image_filename) @@ -762,12 +753,10 @@ def _set_theme_images(base_dir: str, name: str) -> None: else: if os.path.isfile(account_dir + '/right_col_image.png'): - try: - os.remove(account_dir + '/right_col_image.png') - except OSError: - print('EX: _set_theme_images ' + - 'unable to delete ' + - account_dir + '/right_col_image.png') + remove_file(account_dir + '/right_col_image.png', + 'EX: _set_theme_images ' + + 'unable to delete ' + + account_dir + '/right_col_image.png') except OSError: print('EX: _set_theme_images unable to copy ' + right_col_image_filename) @@ -791,10 +780,8 @@ def set_news_avatar(base_dir: str, name: str, filename = base_dir + '/cache/avatars/' + avatar_filename if os.path.isfile(filename): - try: - os.remove(filename) - except OSError: - print('EX: set_news_avatar unable to delete ' + filename) + remove_file(filename, + 'EX: set_news_avatar unable to delete ' + filename) if os.path.isdir(base_dir + '/cache/avatars'): copyfile(new_filename, filename) account_dir = acct_dir(base_dir, nickname, domain) diff --git a/utils.py b/utils.py index b3f14089e..e71dae560 100644 --- a/utils.py +++ b/utils.py @@ -27,6 +27,7 @@ from data import save_string from data import save_flag_file from data import load_string from data import append_string +from data import remove_file VALID_HASHTAG_CHARS = \ set('_0123456789' + @@ -1795,11 +1796,10 @@ def _remove_attachment(base_dir: str, http_prefix: str, media_filename: str = base_dir + '/' + \ attachment_url.replace(http_prefix + '://' + domain + '/', '') if os.path.isfile(media_filename): - try: - os.remove(media_filename) - except OSError: - print('EX: _remove_attachment unable to delete media file ' + - str(media_filename)) + ex_text = \ + 'EX: _remove_attachment unable to delete media file ' + \ + str(media_filename) + remove_file(media_filename, ex_text) # remove from the log file account_dir: str = acct_dir(base_dir, nickname, domain) @@ -1819,20 +1819,18 @@ def _remove_attachment(base_dir: str, http_prefix: str, # remove the transcript if os.path.isfile(media_filename + '.vtt'): - try: - os.remove(media_filename + '.vtt') - except OSError: - print('EX: _remove_attachment unable to delete media transcript ' + - str(media_filename) + '.vtt') + ex_text = \ + 'EX: _remove_attachment unable to delete media transcript ' + \ + str(media_filename) + '.vtt' + remove_file(media_filename + '.vtt', ex_text) # remove the etag etag_filename: str = media_filename + '.etag' if os.path.isfile(etag_filename): - try: - os.remove(etag_filename) - except OSError: - print('EX: _remove_attachment unable to delete etag file ' + - str(etag_filename)) + ex_text = \ + 'EX: _remove_attachment unable to delete etag file ' + \ + str(etag_filename) + remove_file(etag_filename, ex_text) post_json['attachment']: list[dict] = [] @@ -1927,11 +1925,10 @@ def _delete_post_remove_replies(base_dir: str, nickname: str, domain: str, nickname, domain, reply_file, debug, recent_posts_cache, manual) # remove the replies file - try: - os.remove(replies_filename) - except OSError: - print('EX: _delete_post_remove_replies ' + - 'unable to delete replies file ' + str(replies_filename)) + ex_text = \ + 'EX: _delete_post_remove_replies ' + \ + 'unable to delete replies file ' + str(replies_filename) + remove_file(replies_filename, ex_text) def _is_bookmarked(base_dir: str, nickname: str, domain: str, @@ -1989,31 +1986,27 @@ def delete_cached_html(base_dir: str, nickname: str, domain: str, if not cached_post_filename: return if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: delete_cached_html ' + - 'unable to delete cached post file ' + - str(cached_post_filename)) + ex_text = \ + 'EX: delete_cached_html unable to delete cached post file ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) cached_post_filename = cached_post_filename.replace('.html', '.ssml') if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: delete_cached_html ' + - 'unable to delete cached ssml post file ' + - str(cached_post_filename)) + ex_text = \ + 'EX: ' + \ + 'delete_cached_html unable to delete cached ssml post file ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) cached_post_filename = \ cached_post_filename.replace('/postcache/', '/outbox/') if os.path.isfile(cached_post_filename): - try: - os.remove(cached_post_filename) - except OSError: - print('EX: delete_cached_html ' + - 'unable to delete cached outbox ssml post file ' + - str(cached_post_filename)) + ex_text = \ + 'EX: delete_cached_html ' + \ + 'unable to delete cached outbox ssml post file ' + \ + str(cached_post_filename) + remove_file(cached_post_filename, ex_text) def _remove_post_id_from_tag_index(tag_index_filename: str, @@ -2034,11 +2027,9 @@ def _remove_post_id_from_tag_index(tag_index_filename: str, newlines += file_line if not newlines.strip(): # if there are no lines then remove the hashtag file - try: - os.remove(tag_index_filename) - except OSError: - print('EX: _delete_hashtags_on_post ' + - 'unable to delete tag index ' + str(tag_index_filename)) + ex_text = 'EX: _delete_hashtags_on_post ' + \ + 'unable to delete tag index ' + str(tag_index_filename) + remove_file(tag_index_filename, ex_text) else: # write the new hashtag index without the given post in it save_string(newlines, tag_index_filename, @@ -2127,18 +2118,14 @@ def _delete_conversation_post(base_dir: str, nickname: str, domain: str, conversation_filename) else: if os.path.isfile(conversation_filename + '.muted'): - try: - os.remove(conversation_filename + '.muted') - except OSError: - print('EX: _delete_conversation_post ' + - 'unable to remove conversation ' + - str(conversation_filename) + '.muted') - try: - os.remove(conversation_filename) - except OSError: - print('EX: _delete_conversation_post ' + - 'unable to remove conversation ' + - str(conversation_filename)) + ex_text = 'EX: _delete_conversation_post ' + \ + 'unable to remove conversation ' + \ + str(conversation_filename) + '.muted' + remove_file(conversation_filename + '.muted', ex_text) + ex_text = 'EX: _delete_conversation_post ' + \ + 'unable to remove conversation ' + \ + str(conversation_filename) + remove_file(conversation_filename, ex_text) def is_dm(post_json_object: {}) -> bool: @@ -2315,13 +2302,10 @@ def delete_post(base_dir: str, http_prefix: str, http_prefix, post_filename, recent_posts_cache, debug, manual) # finally, remove the post itself - try: - os.remove(post_filename) + ex_text = 'EX: delete_post unable to delete post ' + \ + str(post_filename) + if remove_file(post_filename, ex_text): return True - except OSError: - if debug: - print('EX: delete_post unable to delete post ' + - str(post_filename)) return False # don't allow DMs to be deleted if they came from a different instance @@ -2353,13 +2337,10 @@ def delete_post(base_dir: str, http_prefix: str, debug, False) if gemini_blog_filename: if os.path.isfile(gemini_blog_filename): - try: - os.remove(gemini_blog_filename) + ex_text = 'EX: delete_post unable to delete gemini post ' + \ + str(gemini_blog_filename) + if remove_file(gemini_blog_filename, ex_text): return True - except OSError: - if debug: - print('EX: delete_post unable to delete gemini post ' + - str(gemini_blog_filename)) # delete markdown blog post markdown_blog_filename: str = \ @@ -2368,13 +2349,10 @@ def delete_post(base_dir: str, http_prefix: str, debug, False) if markdown_blog_filename: if os.path.isfile(markdown_blog_filename): - try: - os.remove(markdown_blog_filename) + ex_text = 'EX: delete_post unable to delete markdown post ' + \ + str(markdown_blog_filename) + if remove_file(markdown_blog_filename, ex_text): return True - except OSError: - if debug: - print('EX: delete_post unable to delete markdown post ' + - str(markdown_blog_filename)) # delete micron blog post micron_blog_filename: str = \ @@ -2383,13 +2361,10 @@ def delete_post(base_dir: str, http_prefix: str, debug, False) if micron_blog_filename: if os.path.isfile(micron_blog_filename): - try: - os.remove(micron_blog_filename) + ex_text = 'EX: delete_post unable to delete micron post ' + \ + str(micron_blog_filename) + if remove_file(micron_blog_filename, ex_text): return True - except OSError: - if debug: - print('EX: delete_post unable to delete micron post ' + - str(micron_blog_filename)) # remove from recent posts cache in memory remove_post_from_cache(post_json_object, recent_posts_cache) @@ -2407,19 +2382,15 @@ def delete_post(base_dir: str, http_prefix: str, for ext in extensions: ext_filename: str = post_filename + '.' + ext if os.path.isfile(ext_filename): - try: - os.remove(ext_filename) - except OSError: - print('EX: delete_post unable to remove ext ' + - str(ext_filename)) + ex_text = 'EX: delete_post unable to remove ext ' + \ + str(ext_filename) + remove_file(ext_filename, ex_text) elif post_filename.endswith('.json'): ext_filename = post_filename.replace('.json', '') + '.' + ext if os.path.isfile(ext_filename): - try: - os.remove(ext_filename) - except OSError: - print('EX: delete_post unable to remove ext ' + - str(ext_filename)) + ex_text = 'EX: delete_post unable to remove ext ' + \ + str(ext_filename) + remove_file(ext_filename, ex_text) # remove cached html version of the post delete_cached_html(base_dir, nickname, domain, post_json_object) @@ -2445,13 +2416,10 @@ def delete_post(base_dir: str, http_prefix: str, http_prefix, post_filename, recent_posts_cache, debug, manual) # finally, remove the post itself - try: - os.remove(post_filename) + ex_text = 'EX: delete_post unable to delete post ' + \ + str(post_filename) + if remove_file(post_filename, ex_text): return True - except OSError: - if debug: - print('EX: delete_post unable to delete post ' + - str(post_filename)) return False @@ -3513,10 +3481,8 @@ def set_minimize_all_images(base_dir: str, if nickname in min_images_for_accounts: min_images_for_accounts.remove(nickname) if os.path.isfile(filename): - try: - os.remove(filename) - except OSError: - print('EX: unable to delete ' + filename) + remove_file(filename, + 'EX: unable to delete ' + filename) def load_reverse_timeline(base_dir: str) -> []: @@ -3560,11 +3526,9 @@ def save_reverse_timeline(base_dir: str, reverse_sequence: []) -> None: reverse_filename) else: if os.path.isfile(reverse_filename): - try: - os.remove(reverse_filename) - except OSError: - print('EX: failed to delete reverse ' + - reverse_filename) + remove_file(reverse_filename, + 'EX: failed to delete reverse ' + + reverse_filename) break @@ -4115,10 +4079,9 @@ def set_premium_account(base_dir: str, nickname: str, domain: str, premium_filename: str = acct_dir(base_dir, nickname, domain) + '/.premium' if os.path.isfile(premium_filename): if not flag_state: - try: - os.remove(premium_filename) - except OSError: - print('EX: unable to remove premium flag ' + premium_filename) + if not remove_file(premium_filename, + 'EX: unable to remove premium flag ' + + premium_filename): return False else: if flag_state: diff --git a/webapp_calendar.py b/webapp_calendar.py index a5931e1c8..70ac6b991 100644 --- a/webapp_calendar.py +++ b/webapp_calendar.py @@ -39,6 +39,7 @@ from webapp_utils import html_footer from webapp_utils import html_hide_from_screen_reader from webapp_utils import html_keyboard_navigation from maps import html_open_street_map +from data import remove_file def html_calendar_delete_confirm(translate: {}, base_dir: str, @@ -131,10 +132,8 @@ def _html_calendar_day(person_cache: {}, translate: {}, account_dir = acct_dir(base_dir, nickname, domain) calendar_file = account_dir + '/.newCalendar' if os.path.isfile(calendar_file): - try: - os.remove(calendar_file) - except OSError: - print('EX: _html_calendar_day unable to delete ' + calendar_file) + remove_file(calendar_file, + 'EX: _html_calendar_day unable to delete ' + calendar_file) css_filename = base_dir + '/epicyon-calendar.css' if os.path.isfile(base_dir + '/calendar.css'): diff --git a/webapp_minimalbutton.py b/webapp_minimalbutton.py index 507fb0360..2e7bd8927 100644 --- a/webapp_minimalbutton.py +++ b/webapp_minimalbutton.py @@ -9,6 +9,7 @@ __module_group__ = "Timeline" import os from utils import acct_dir +from data import remove_file from data import save_flag_file @@ -35,10 +36,8 @@ def set_minimal(base_dir: str, domain: str, nickname: str, minimal_filename = account_dir + '/.notminimal' minimal_file_exists = os.path.isfile(minimal_filename) if minimal and minimal_file_exists: - try: - os.remove(minimal_filename) - except OSError: - print('EX: set_minimal unable to delete ' + minimal_filename) + remove_file(minimal_filename, + 'EX: set_minimal unable to delete ' + minimal_filename) elif not minimal and not minimal_file_exists: save_flag_file(minimal_filename, 'EX: unable to write minimal ' + minimal_filename) diff --git a/webapp_timeline.py b/webapp_timeline.py index 86dc4e47a..91679104a 100644 --- a/webapp_timeline.py +++ b/webapp_timeline.py @@ -53,6 +53,7 @@ from question import is_html_question from question import is_question from data import load_string from data import load_list +from data import remove_file def _log_timeline_timing(enable_timing_log: bool, timeline_start_time, @@ -520,10 +521,8 @@ def html_timeline(default_timeline: str, if os.path.isfile(dm_file): new_dm = True if box_name == 'dm': - try: - os.remove(dm_file) - except OSError: - print('EX: html_timeline unable to delete ' + dm_file) + remove_file(dm_file, + 'EX: html_timeline unable to delete ' + dm_file) # should the Replies button be highlighted? new_reply: bool = False @@ -531,10 +530,8 @@ def html_timeline(default_timeline: str, if os.path.isfile(reply_file): new_reply = True if box_name == 'tlreplies': - try: - os.remove(reply_file) - except OSError: - print('EX: html_timeline unable to delete ' + reply_file) + remove_file(reply_file, + 'EX: html_timeline unable to delete ' + reply_file) # should the Shares button be highlighted? new_share: bool = False @@ -542,10 +539,9 @@ def html_timeline(default_timeline: str, if os.path.isfile(new_share_file): new_share = True if box_name == 'tlshares': - try: - os.remove(new_share_file) - except OSError: - print('EX: html_timeline unable to delete ' + new_share_file) + remove_file(new_share_file, + 'EX: html_timeline unable to delete ' + + new_share_file) # should the Wanted button be highlighted? new_wanted: bool = False @@ -553,10 +549,9 @@ def html_timeline(default_timeline: str, if os.path.isfile(new_wanted_file): new_wanted = True if box_name == 'tlwanted': - try: - os.remove(new_wanted_file) - except OSError: - print('EX: html_timeline unable to delete ' + new_wanted_file) + remove_file(new_wanted_file, + 'EX: html_timeline unable to delete ' + + new_wanted_file) # should the Moderation/reports button be highlighted? new_report: bool = False @@ -564,10 +559,9 @@ def html_timeline(default_timeline: str, if os.path.isfile(new_report_file): new_report = True if box_name == 'moderation': - try: - os.remove(new_report_file) - except OSError: - print('EX: html_timeline unable to delete ' + new_report_file) + remove_file(new_report_file, + 'EX: html_timeline unable to delete ' + + new_report_file) # show polls/votes? show_vote_posts: bool = True diff --git a/webapp_utils.py b/webapp_utils.py index 1da40e44b..bba371ffa 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -63,6 +63,7 @@ from data import load_list from data import save_flag_file from data import save_binary from data import load_string +from data import remove_file def minimizing_attached_images(base_dir: str, nickname: str, domain: str, @@ -424,12 +425,10 @@ def update_avatar_image_cache(signing_priv_key_pem: str, str(result.status_code)) # remove partial download if os.path.isfile(avatar_image_filename): - try: - os.remove(avatar_image_filename) - except OSError: - print('EX: ' + - 'update_avatar_image_cache unable to delete ' + - avatar_image_filename) + remove_file(avatar_image_filename, + 'EX: ' + + 'update_avatar_image_cache unable to delete ' + + avatar_image_filename) else: media_binary = result.content if binary_is_image(avatar_image_filename, media_binary):