From a2b4efcaaf0beef9a368ac8749161db9baa2e865 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 25 Nov 2021 21:18:53 +0000 Subject: [PATCH] File write exceptions --- auth.py | 9 ++-- blocking.py | 17 +++---- blog.py | 6 +-- categories.py | 5 +- daemon.py | 10 ++-- follow.py | 43 +++++++++++------ followingCalendar.py | 28 +++++++---- git.py | 15 +++--- happening.py | 74 +++++++++++++++++----------- inbox.py | 112 ++++++++++++++++++++++++++++--------------- manualapprove.py | 28 +++++++---- media.py | 6 +-- newsdaemon.py | 35 ++++++++++---- person.py | 103 +++++++++++++++++++++++++++------------ petnames.py | 24 +++++++--- posts.py | 7 ++- 16 files changed, 346 insertions(+), 176 deletions(-) diff --git a/auth.py b/auth.py index 42dac82b9..5a496cf4a 100644 --- a/auth.py +++ b/auth.py @@ -171,8 +171,9 @@ def storeBasicCredentials(baseDir: str, nickname: str, password: str) -> bool: fout.write(line) else: fout.write(storeStr + '\n') - except OSError: - print('WARN: unable to save password ' + passwordFile) + except OSError as e: + print('WARN: unable to save password ' + passwordFile + + ' ' + str(e)) return False try: @@ -210,8 +211,8 @@ def removePassword(baseDir: str, nickname: str) -> None: for line in fin: if not line.startswith(nickname + ':'): fout.write(line) - except OSError: - print('WARN: unable to remove password from file') + except OSError as e: + print('WARN: unable to remove password from file ' + str(e)) return try: diff --git a/blocking.py b/blocking.py index 8c48aa1d7..73ef66c77 100644 --- a/blocking.py +++ b/blocking.py @@ -153,9 +153,9 @@ def removeGlobalBlock(baseDir: str, line.replace('\n', '').replace('\r', '') if unblockHandle not in line: fpnew.write(handle + '\n') - except OSError: + except OSError as e: print('WARN: failed to remove global block ' + - unblockingFilename) + unblockingFilename + ' ' + str(e)) return False if os.path.isfile(unblockingFilename + '.new'): os.rename(unblockingFilename + '.new', unblockingFilename) @@ -172,9 +172,9 @@ def removeGlobalBlock(baseDir: str, line.replace('\n', '').replace('\r', '') if unblockHashtag not in line: fpnew.write(blockLine + '\n') - except OSError: + except OSError as e: print('WARN: failed to remove global hashtag block ' + - unblockingFilename) + unblockingFilename + ' ' + str(e)) return False if os.path.isfile(unblockingFilename + '.new'): os.rename(unblockingFilename + '.new', unblockingFilename) @@ -198,8 +198,9 @@ def removeBlock(baseDir: str, nickname: str, domain: str, handle = line.replace('\n', '').replace('\r', '') if unblockHandle not in line: fpnew.write(handle + '\n') - except OSError: - print('WARN: failed to remove block ' + unblockingFilename) + except OSError as e: + print('WARN: failed to remove block ' + + unblockingFilename + ' ' + str(e)) return False if os.path.isfile(unblockingFilename + '.new'): os.rename(unblockingFilename + '.new', unblockingFilename) @@ -907,8 +908,8 @@ def setBrochMode(baseDir: str, domainFull: str, enabled: bool) -> None: for d in allowedDomains: allowFile.write(d + '\n') print('Broch mode enabled') - except OSError: - print('WARN: Broch mode not enabled due to file write') + except OSError as e: + print('WARN: Broch mode not enabled due to file write ' + str(e)) return setConfigParam(baseDir, "brochMode", enabled) diff --git a/blog.py b/blog.py index 2adca3da0..8ef86ee3d 100644 --- a/blog.py +++ b/blog.py @@ -98,9 +98,9 @@ def _noOfBlogReplies(baseDir: str, httpPrefix: str, translate: {}, replyPostId.replace('\n', '').replace('\r', '') if replyPostId not in removals: f.write(replyPostId + '\n') - except OSError: - print('WARN: unable to remove replies from post ' + postFilename) - pass + except OSError as e: + print('WARN: unable to remove replies from post ' + + postFilename + ' ' + str(e)) return replies diff --git a/categories.py b/categories.py index 868fcfe4f..1b2cd2337 100644 --- a/categories.py +++ b/categories.py @@ -167,8 +167,9 @@ def setHashtagCategory(baseDir: str, hashtag: str, category: str, if update: updateHashtagCategories(baseDir) return True - except OSError: - print('WARN: unable to write category ' + categoryFilename) + except OSError as e: + print('WARN: unable to write category ' + categoryFilename + + ' ' + str(e)) pass return False diff --git a/daemon.py b/daemon.py index 9eecf5fc7..fbb9727be 100644 --- a/daemon.py +++ b/daemon.py @@ -2362,8 +2362,9 @@ class PubServer(BaseHTTPRequestHandler): with open(nwFilename, 'w+') as noNewswireFile: noNewswireFile.write('\n') refreshNewswire(self.server.baseDir) - except OSError: - print('WARN: unable to write ' + nwFilename) + except OSError as e: + print('WARN: unable to write ' + nwFilename + + ' ' + str(e)) usersPathStr = \ usersPath + '/' + self.server.defaultTimeline + \ '?page=' + str(pageNumber) @@ -2404,8 +2405,9 @@ class PubServer(BaseHTTPRequestHandler): with open(featFilename, 'w+') as noFeaturesFile: noFeaturesFile.write('\n') refreshNewswire(self.server.baseDir) - except OSError: - print('WARN: unable to write ' + featFilename) + except OSError as e: + print('WARN: unable to write ' + featFilename + + ' ' + str(e)) usersPathStr = \ usersPath + '/' + self.server.defaultTimeline + \ '?page=' + str(pageNumber) diff --git a/follow.py b/follow.py index 704dd90dd..39fb3211d 100644 --- a/follow.py +++ b/follow.py @@ -296,12 +296,15 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str, return with open(filename, 'r') as f: lines = f.readlines() - with open(filename, 'w+') as f: - for line in lines: - checkHandle = line.strip("\n").strip("\r").lower() - if checkHandle != handleToUnfollowLower and \ - checkHandle != '!' + handleToUnfollowLower: - f.write(line) + try: + with open(filename, 'w+') as f: + for line in lines: + checkHandle = line.strip("\n").strip("\r").lower() + if checkHandle != handleToUnfollowLower and \ + checkHandle != '!' + handleToUnfollowLower: + f.write(line) + except OSError as e: + print('WARN: unable to write ' + filename + ' ' + str(e)) # write to an unfollowed file so that if a follow accept # later arrives then it can be ignored @@ -312,8 +315,11 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str, with open(unfollowedFilename, 'a+') as f: f.write(handleToUnfollow + '\n') else: - with open(unfollowedFilename, 'w+') as f: - f.write(handleToUnfollow + '\n') + try: + with open(unfollowedFilename, 'w+') as f: + f.write(handleToUnfollow + '\n') + except OSError: + print('WARN: unable to write ' + unfollowedFilename) return True @@ -650,8 +656,11 @@ def _storeFollowRequest(baseDir: str, print('DEBUG: ' + approveHandleStored + ' is already awaiting approval') else: - with open(approveFollowsFilename, 'w+') as fp: - fp.write(approveHandleStored + '\n') + try: + with open(approveFollowsFilename, 'w+') as fp: + fp.write(approveHandleStored + '\n') + except OSError: + print('WARN: unable to write ' + approveFollowsFilename) # store the follow request in its own directory # We don't rely upon the inbox because items in there could expire @@ -851,8 +860,11 @@ def receiveFollowRequest(session, baseDir: str, httpPrefix: str, 'Failed to write entry to followers file ' + str(e)) else: - with open(followersFilename, 'w+') as followersFile: - followersFile.write(approveHandle + '\n') + try: + with open(followersFilename, 'w+') as followersFile: + followersFile.write(approveHandle + '\n') + except OSError: + print('WARN: unable to write ' + followersFilename) print('Beginning follow accept') return followedAccountAccepts(session, baseDir, httpPrefix, @@ -1046,8 +1058,11 @@ def sendFollowRequest(session, baseDir: str, unfollowedFile = \ unfollowedFile.replace(followHandle + '\n', '') if unfollowedFile: - with open(unfollowedFilename, 'w+') as fp: - fp.write(unfollowedFile) + try: + with open(unfollowedFilename, 'w+') as fp: + fp.write(unfollowedFile) + except OSError: + print('WARN: unable to write ' + unfollowedFilename) newFollowJson = { '@context': 'https://www.w3.org/ns/activitystreams', diff --git a/followingCalendar.py b/followingCalendar.py index 5782f801e..680101a97 100644 --- a/followingCalendar.py +++ b/followingCalendar.py @@ -46,8 +46,11 @@ def receivingCalendarEvents(baseDir: str, nickname: str, domain: str, # create a new calendar file from the following file with open(followingFilename, 'r') as followingFile: followingHandles = followingFile.read() - with open(calendarFilename, 'w+') as fp: - fp.write(followingHandles) + try: + with open(calendarFilename, 'w+') as fp: + fp.write(followingHandles) + except OSError: + print('WARN: unable to write ' + calendarFilename) return handle + '\n' in open(calendarFilename).read() @@ -89,8 +92,11 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str, with open(followingFilename, 'r') as followingFile: followingHandles = followingFile.read() if add: - with open(calendarFilename, 'w+') as fp: - fp.write(followingHandles + handle + '\n') + try: + with open(calendarFilename, 'w+') as fp: + fp.write(followingHandles + handle + '\n') + except OSError: + print('WARN: unable to write ' + calendarFilename) # already in the calendar file? if handle + '\n' in followingHandles: @@ -100,16 +106,22 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str, return # remove from calendar file followingHandles = followingHandles.replace(handle + '\n', '') - with open(calendarFilename, 'w+') as fp: - fp.write(followingHandles) + try: + with open(calendarFilename, 'w+') as fp: + fp.write(followingHandles) + except OSError: + print('WARN: unable to write ' + calendarFilename) else: print(handle + ' not in followingCalendar.txt') # not already in the calendar file if add: # append to the list of handles followingHandles += handle + '\n' - with open(calendarFilename, 'w+') as fp: - fp.write(followingHandles) + try: + with open(calendarFilename, 'w+') as fp: + fp.write(followingHandles) + except OSError: + print('WARN: unable to write ' + calendarFilename) def addPersonToCalendar(baseDir: str, nickname: str, domain: str, diff --git a/git.py b/git.py index 2cab9bf32..6e9eba4c9 100644 --- a/git.py +++ b/git.py @@ -208,11 +208,14 @@ def receiveGitPatch(baseDir: str, nickname: str, domain: str, return False patchStr = \ _gitAddFromHandle(patchStr, '@' + fromNickname + '@' + fromDomain) - with open(patchFilename, 'w+') as patchFile: - patchFile.write(patchStr) - patchNotifyFilename = \ - acctDir(baseDir, nickname, domain) + '/.newPatchContent' - with open(patchNotifyFilename, 'w+') as patchFile: + try: + with open(patchFilename, 'w+') as patchFile: patchFile.write(patchStr) - return True + patchNotifyFilename = \ + acctDir(baseDir, nickname, domain) + '/.newPatchContent' + with open(patchNotifyFilename, 'w+') as patchFile: + patchFile.write(patchStr) + return True + except OSError as e: + print('WARN unable to write patch ' + patchFilename + ' ' + str(e)) return False diff --git a/happening.py b/happening.py index 3151dee74..df79e4428 100644 --- a/happening.py +++ b/happening.py @@ -41,9 +41,8 @@ def _removeEventFromTimeline(eventId: str, tlEventsFilename: str) -> None: try: with open(tlEventsFilename, 'w+') as fp2: fp2.write(eventsTimeline) - except BaseException: + except OSError: print('EX: ERROR: unable to save events timeline') - pass def saveEventPost(baseDir: str, handle: str, postId: str, @@ -105,13 +104,16 @@ def saveEventPost(baseDir: str, handle: str, postId: str, if eventId + '\n' not in content: tlEventsFile.seek(0, 0) tlEventsFile.write(eventId + '\n' + content) - except Exception as e: + except OSError as e: print('WARN: Failed to write entry to events file ' + tlEventsFilename + ' ' + str(e)) return False else: - with open(tlEventsFilename, 'w+') as tlEventsFile: - tlEventsFile.write(eventId + '\n') + try: + with open(tlEventsFilename, 'w+') as tlEventsFile: + tlEventsFile.write(eventId + '\n') + except OSError: + print('WARN: unable to write ' + tlEventsFilename) # create a directory for the calendar year if not os.path.isdir(calendarPath + '/' + str(eventYear)): @@ -128,18 +130,24 @@ def saveEventPost(baseDir: str, handle: str, postId: str, return False # append the post Id to the file for the calendar month - with open(calendarFilename, 'a+') as calendarFile: - calendarFile.write(postId + '\n') + try: + with open(calendarFilename, 'a+') as calendarFile: + calendarFile.write(postId + '\n') + except OSError: + print('WARN: unable to append ' + calendarFilename) # create a file which will trigger a notification that # a new event has been added - calendarNotificationFilename = \ - baseDir + '/accounts/' + handle + '/.newCalendar' - with open(calendarNotificationFilename, 'w+') as calendarNotificationFile: - notifyStr = \ - '/calendar?year=' + str(eventYear) + '?month=' + \ - str(eventMonthNumber) + '?day=' + str(eventDayOfMonth) - calendarNotificationFile.write(notifyStr) + calNotifyFilename = baseDir + '/accounts/' + handle + '/.newCalendar' + notifyStr = \ + '/calendar?year=' + str(eventYear) + '?month=' + \ + str(eventMonthNumber) + '?day=' + str(eventDayOfMonth) + try: + with open(calNotifyFilename, 'w+') as calendarNotificationFile: + calendarNotificationFile.write(notifyStr) + except OSError: + print('WARN: unable to write ' + calNotifyFilename) + return False return True @@ -244,9 +252,12 @@ def getTodaysEvents(baseDir: str, nickname: str, domain: str, # if some posts have been deleted then regenerate the calendar file if recreateEventsFile: - with open(calendarFilename, 'w+') as calendarFile: - for postId in calendarPostIds: - calendarFile.write(postId + '\n') + try: + with open(calendarFilename, 'w+') as calendarFile: + for postId in calendarPostIds: + calendarFile.write(postId + '\n') + except OSError: + print('WARN: unable to write ' + calendarFilename) return events @@ -360,9 +371,12 @@ def getThisWeeksEvents(baseDir: str, nickname: str, domain: str) -> {}: # if some posts have been deleted then regenerate the calendar file if recreateEventsFile: - with open(calendarFilename, 'w+') as calendarFile: - for postId in calendarPostIds: - calendarFile.write(postId + '\n') + try: + with open(calendarFilename, 'w+') as calendarFile: + for postId in calendarPostIds: + calendarFile.write(postId + '\n') + except OSError: + print('WARN: unable to write ' + calendarFilename) return events @@ -424,9 +438,12 @@ def getCalendarEvents(baseDir: str, nickname: str, domain: str, # if some posts have been deleted then regenerate the calendar file if recreateEventsFile: - with open(calendarFilename, 'w+') as calendarFile: - for postId in calendarPostIds: - calendarFile.write(postId + '\n') + try: + with open(calendarFilename, 'w+') as calendarFile: + for postId in calendarPostIds: + calendarFile.write(postId + '\n') + except OSError: + print('WARN: unable to write ' + calendarFilename) return events @@ -449,7 +466,10 @@ def removeCalendarEvent(baseDir: str, nickname: str, domain: str, lines = f.readlines() if not lines: return - with open(calendarFilename, 'w+') as f: - for line in lines: - if messageId not in line: - f.write(line) + try: + with open(calendarFilename, 'w+') as f: + for line in lines: + if messageId not in line: + f.write(line) + except OSError: + print('WARN: unable to write ' + calendarFilename) diff --git a/inbox.py b/inbox.py index 84c1e0f11..109ad7610 100644 --- a/inbox.py +++ b/inbox.py @@ -141,9 +141,8 @@ def _storeLastPostId(baseDir: str, nickname: str, domain: str, try: with open(actorFilename, 'w+') as fp: fp.write(postId) - except BaseException: + except OSError: print('EX: Unable to write last post id to ' + actorFilename) - pass def _updateCachedHashtagSwarm(baseDir: str, nickname: str, domain: str, @@ -185,10 +184,9 @@ def _updateCachedHashtagSwarm(baseDir: str, nickname: str, domain: str, with open(cachedHashtagSwarmFilename, 'w+') as fp: fp.write(newSwarmStr) return True - except BaseException: + except OSError: print('EX: unable to write cached hashtag swarm ' + cachedHashtagSwarmFilename) - pass return False @@ -238,8 +236,11 @@ def storeHashTags(baseDir: str, nickname: str, domain: str, tagline = str(daysSinceEpoch) + ' ' + nickname + ' ' + postUrl + '\n' hashtagsCtr += 1 if not os.path.isfile(tagsFilename): - with open(tagsFilename, 'w+') as tagsFile: - tagsFile.write(tagline) + try: + with open(tagsFilename, 'w+') as tagsFile: + tagsFile.write(tagline) + except OSError: + print('WARN: unable to write ' + tagsFilename) else: if postUrl not in open(tagsFilename).read(): try: @@ -248,7 +249,7 @@ def storeHashTags(baseDir: str, nickname: str, domain: str, if tagline not in content: tagsFile.seek(0, 0) tagsFile.write(tagline + content) - except Exception as e: + except OSError as e: print('WARN: Failed to write entry to tags file ' + tagsFilename + ' ' + str(e)) removeOldHashtags(baseDir, 3) @@ -1980,8 +1981,12 @@ def _receiveAnnounce(recentPostsCache: {}, postJsonObject, personCache, translate, lookupActor, themeName) - with open(postFilename + '.tts', 'w+') as ttsFile: - ttsFile.write('\n') + try: + with open(postFilename + '.tts', 'w+') as ttsFile: + ttsFile.write('\n') + except OSError: + print('WARN: unable to write recent post ' + + postFilename) if debug: print('DEBUG: Obtaining actor for announce post ' + @@ -2145,11 +2150,17 @@ def populateReplies(baseDir: str, httpPrefix: str, domain: str, if numLines > maxReplies: return False if messageId not in open(postRepliesFilename).read(): - with open(postRepliesFilename, 'a+') as repliesFile: - repliesFile.write(messageId + '\n') + try: + with open(postRepliesFilename, 'a+') as repliesFile: + repliesFile.write(messageId + '\n') + except OSError: + print('WARN: unable to append ' + postRepliesFilename) else: - with open(postRepliesFilename, 'w+') as repliesFile: - repliesFile.write(messageId + '\n') + try: + with open(postRepliesFilename, 'w+') as repliesFile: + repliesFile.write(messageId + '\n') + except OSError: + print('WARN: unable to write ' + postRepliesFilename) return True @@ -2322,8 +2333,11 @@ def _dmNotify(baseDir: str, handle: str, url: str) -> None: return dmFile = accountDir + '/.newDM' if not os.path.isfile(dmFile): - with open(dmFile, 'w+') as fp: - fp.write(url) + try: + with open(dmFile, 'w+') as fp: + fp.write(url) + except OSError: + print('WARN: unable to write ' + dmFile) def _alreadyLiked(baseDir: str, nickname: str, domain: str, @@ -2438,17 +2452,16 @@ def _likeNotify(baseDir: str, domain: str, onionDomain: str, try: with open(prevLikeFile, 'w+') as fp: fp.write(likeStr) - except BaseException: + except OSError: print('EX: ERROR: unable to save previous like notification ' + prevLikeFile) - pass + try: with open(likeFile, 'w+') as fp: fp.write(likeStr) - except BaseException: + except OSError: print('EX: ERROR: unable to write like notification file ' + likeFile) - pass def _reactionNotify(baseDir: str, domain: str, onionDomain: str, @@ -2503,17 +2516,16 @@ def _reactionNotify(baseDir: str, domain: str, onionDomain: str, try: with open(prevReactionFile, 'w+') as fp: fp.write(reactionStr) - except BaseException: + except OSError: print('EX: ERROR: unable to save previous reaction notification ' + prevReactionFile) - pass + try: with open(reactionFile, 'w+') as fp: fp.write(reactionStr) - except BaseException: + except OSError: print('EX: ERROR: unable to write reaction notification file ' + reactionFile) - pass def _notifyPostArrival(baseDir: str, handle: str, url: str) -> None: @@ -2531,8 +2543,11 @@ def _notifyPostArrival(baseDir: str, handle: str, url: str) -> None: existingNotificationMessage = fp.read() if url in existingNotificationMessage: return - with open(notifyFile, 'w+') as fp: - fp.write(url) + try: + with open(notifyFile, 'w+') as fp: + fp.write(url) + except OSError: + print('WARN: unable to write ' + notifyFile) def _replyNotify(baseDir: str, handle: str, url: str) -> None: @@ -2543,8 +2558,11 @@ def _replyNotify(baseDir: str, handle: str, url: str) -> None: return replyFile = accountDir + '/.newReply' if not os.path.isfile(replyFile): - with open(replyFile, 'w+') as fp: - fp.write(url) + try: + with open(replyFile, 'w+') as fp: + fp.write(url) + except OSError: + print('WARN: unable to write ' + replyFile) def _gitPatchNotify(baseDir: str, handle: str, @@ -2558,8 +2576,11 @@ def _gitPatchNotify(baseDir: str, handle: str, patchFile = accountDir + '/.newPatch' subject = subject.replace('[PATCH]', '').strip() handle = '@' + fromNickname + '@' + fromDomain - with open(patchFile, 'w+') as fp: - fp.write('git ' + handle + ' ' + subject) + try: + with open(patchFile, 'w+') as fp: + fp.write('git ' + handle + ' ' + subject) + except OSError: + print('WARN: unable to write ' + patchFile) def _groupHandle(baseDir: str, handle: str) -> bool: @@ -2709,14 +2730,14 @@ def inboxUpdateIndex(boxname: str, baseDir: str, handle: str, indexFile.write(destinationFilename + '\n' + content) written = True return True - except Exception as e: + except OSError as e: print('WARN: Failed to write entry to index ' + str(e)) else: try: with open(indexFilename, 'w+') as indexFile: indexFile.write(destinationFilename + '\n') written = True - except Exception as e: + except OSError as e: print('WARN: Failed to write initial entry to index ' + str(e)) return written @@ -2749,8 +2770,11 @@ def _updateLastSeen(baseDir: str, handle: str, actor: str) -> None: if int(daysSinceEpochFile) == daysSinceEpoch: # value hasn't changed, so we can save writing anything to file return - with open(lastSeenFilename, 'w+') as lastSeenFile: - lastSeenFile.write(str(daysSinceEpoch)) + try: + with open(lastSeenFilename, 'w+') as lastSeenFile: + lastSeenFile.write(str(daysSinceEpoch)) + except OSError: + print('WARN: unable to write ' + lastSeenFilename) def _bounceDM(senderPostId: str, session, httpPrefix: str, @@ -3485,8 +3509,12 @@ def _inboxAfterInitial(recentPostsCache: {}, maxRecentPosts: int, # This enables you to ignore a threat that's getting boring if isReplyToMutedPost: print('MUTE REPLY: ' + destinationFilename) - with open(destinationFilename + '.muted', 'w+') as muteFile: - muteFile.write('\n') + destinationFilenameMuted = destinationFilename + '.muted' + try: + with open(destinationFilenameMuted, 'w+') as muteFile: + muteFile.write('\n') + except OSError: + print('WARN: unable to write ' + destinationFilenameMuted) # update the indexes for different timelines for boxname in updateIndexList: @@ -3773,8 +3801,11 @@ def _checkJsonSignature(baseDir: str, queueJson: {}) -> (bool, bool): alreadyUnknown = True if not alreadyUnknown: - with open(unknownContextsFile, 'a+') as unknownFile: - unknownFile.write(unknownContext + '\n') + try: + with open(unknownContextsFile, 'a+') as unknownFile: + unknownFile.write(unknownContext + '\n') + except OSError: + print('WARN: unable to append ' + unknownContextsFile) else: print('Unrecognized jsonld signature type: ' + jwebsigType) @@ -3788,8 +3819,11 @@ def _checkJsonSignature(baseDir: str, queueJson: {}) -> (bool, bool): alreadyUnknown = True if not alreadyUnknown: - with open(unknownSignaturesFile, 'a+') as unknownFile: - unknownFile.write(jwebsigType + '\n') + try: + with open(unknownSignaturesFile, 'a+') as unknownFile: + unknownFile.write(jwebsigType + '\n') + except OSError: + print('WARN: unable to append ' + unknownSignaturesFile) return hasJsonSignature, jwebsigType diff --git a/manualapprove.py b/manualapprove.py index 1a8edd2f9..f1ce5c110 100644 --- a/manualapprove.py +++ b/manualapprove.py @@ -46,8 +46,11 @@ def manualDenyFollowRequest(session, baseDir: str, removeFromFollowRequests(baseDir, nickname, domain, denyHandle, debug) # Store rejected follows - with open(rejectedFollowsFilename, 'a+') as rejectsFile: - rejectsFile.write(denyHandle + '\n') + try: + with open(rejectedFollowsFilename, 'a+') as rejectsFile: + rejectsFile.write(denyHandle + '\n') + except OSError: + print('WARN: unable to append ' + rejectedFollowsFilename) denyNickname = denyHandle.split('@')[0] denyDomain = \ @@ -104,11 +107,17 @@ def _approveFollowerHandle(accountDir: str, approveHandle: str) -> None: approvedFilename = accountDir + '/approved.txt' if os.path.isfile(approvedFilename): if approveHandle not in open(approvedFilename).read(): - with open(approvedFilename, 'a+') as approvedFile: - approvedFile.write(approveHandle + '\n') + try: + with open(approvedFilename, 'a+') as approvedFile: + approvedFile.write(approveHandle + '\n') + except OSError: + print('WARN: unable to append ' + approvedFilename) else: - with open(approvedFilename, 'w+') as approvedFile: - approvedFile.write(approveHandle + '\n') + try: + with open(approvedFilename, 'w+') as approvedFile: + approvedFile.write(approveHandle + '\n') + except OSError: + print('WARN: unable to write ' + approvedFilename) def manualApproveFollowRequest(session, baseDir: str, @@ -239,8 +248,11 @@ def manualApproveFollowRequest(session, baseDir: str, else: print('Manual follow accept: first follower accepted for ' + handle + ' is ' + approveHandleFull) - with open(followersFilename, 'w+') as followersFile: - followersFile.write(approveHandleFull + '\n') + try: + with open(followersFilename, 'w+') as followersFile: + followersFile.write(approveHandleFull + '\n') + except OSError: + print('WARN: unable to write ' + followersFilename) # only update the follow requests file if the follow is confirmed to be # in followers.txt diff --git a/media.py b/media.py index 8448ce101..2976a0de8 100644 --- a/media.py +++ b/media.py @@ -126,9 +126,8 @@ def _spoofMetaData(baseDir: str, nickname: str, domain: str, try: with open(decoySeedFilename, 'w+') as fp: fp.write(str(decoySeed)) - except BaseException: + except OSError: print('EX: unable to write ' + decoySeedFilename) - pass if os.path.isfile('/usr/bin/exiftool'): print('Spoofing metadata in ' + outputFilename + ' using exiftool') @@ -290,10 +289,9 @@ def _updateEtag(mediaFilename: str) -> None: try: with open(mediaFilename + '.etag', 'w+') as etagFile: etagFile.write(etag) - except BaseException: + except OSError: print('EX: _updateEtag unable to write ' + str(mediaFilename) + '.etag') - pass def attachMedia(baseDir: str, httpPrefix: str, diff --git a/newsdaemon.py b/newsdaemon.py index 87ab01938..55e6d35f6 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -57,15 +57,21 @@ def _updateFeedsOutboxIndex(baseDir: str, domain: str, postId: str) -> None: print('WARN: Failed to write entry to feeds posts index ' + indexFilename + ' ' + str(e)) else: - with open(indexFilename, 'w+') as feedsFile: - feedsFile.write(postId + '\n') + try: + with open(indexFilename, 'w+') as feedsFile: + feedsFile.write(postId + '\n') + except OSError: + print('WARN: unable to write ' + indexFilename) def _saveArrivedTime(baseDir: str, postFilename: str, arrived: str) -> None: """Saves the time when an rss post arrived to a file """ - with open(postFilename + '.arrived', 'w+') as arrivedFile: - arrivedFile.write(arrived) + try: + with open(postFilename + '.arrived', 'w+') as arrivedFile: + arrivedFile.write(arrived) + except OSError: + print('WARN: unable to write ' + postFilename + '.arrived') def _removeControlCharacters(content: str) -> str: @@ -483,8 +489,11 @@ def _createNewsMirror(baseDir: str, domain: str, for removePostId in removals: indexContent = \ indexContent.replace(removePostId + '\n', '') - with open(mirrorIndexFilename, 'w+') as indexFile: - indexFile.write(indexContent) + try: + with open(mirrorIndexFilename, 'w+') as indexFile: + indexFile.write(indexContent) + except OSError: + print('WARN: unable to write ' + mirrorIndexFilename) mirrorArticleDir = mirrorDir + '/' + postIdNumber if os.path.isdir(mirrorArticleDir): @@ -509,11 +518,17 @@ def _createNewsMirror(baseDir: str, domain: str, # append the post Id number to the index file if os.path.isfile(mirrorIndexFilename): - with open(mirrorIndexFilename, 'a+') as indexFile: - indexFile.write(postIdNumber + '\n') + try: + with open(mirrorIndexFilename, 'a+') as indexFile: + indexFile.write(postIdNumber + '\n') + except OSError: + print('WARN: unable to append ' + mirrorIndexFilename) else: - with open(mirrorIndexFilename, 'w+') as indexFile: - indexFile.write(postIdNumber + '\n') + try: + with open(mirrorIndexFilename, 'w+') as indexFile: + indexFile.write(postIdNumber + '\n') + except OSError: + print('WARN: unable to write ' + mirrorIndexFilename) return True diff --git a/person.py b/person.py index 64d66a847..adff421cc 100644 --- a/person.py +++ b/person.py @@ -507,16 +507,22 @@ def _createPersonBase(baseDir: str, nickname: str, domain: str, port: int, if not os.path.isdir(baseDir + privateKeysSubdir): os.mkdir(baseDir + privateKeysSubdir) filename = baseDir + privateKeysSubdir + '/' + handle + '.key' - with open(filename, 'w+') as text_file: - print(privateKeyPem, file=text_file) + try: + with open(filename, 'w+') as text_file: + print(privateKeyPem, file=text_file) + except OSError: + print('WARN: unable to save ' + filename) # save the public key publicKeysSubdir = '/keys/public' if not os.path.isdir(baseDir + publicKeysSubdir): os.mkdir(baseDir + publicKeysSubdir) filename = baseDir + publicKeysSubdir + '/' + handle + '.pem' - with open(filename, 'w+') as text_file: - print(publicKeyPem, file=text_file) + try: + with open(filename, 'w+') as text_file: + print(publicKeyPem, file=text_file) + except OSError: + print('WARN: unable to save 2 ' + filename) if password: password = removeLineEndings(password) @@ -625,22 +631,31 @@ def createPerson(baseDir: str, nickname: str, domain: str, port: int, if manualFollowerApproval: followDMsFilename = acctDir(baseDir, nickname, domain) + '/.followDMs' - with open(followDMsFilename, 'w+') as fFile: - fFile.write('\n') + try: + with open(followDMsFilename, 'w+') as fFile: + fFile.write('\n') + except OSError: + print('WARN: unable to write ' + followDMsFilename) # notify when posts are liked if nickname != 'news': notifyLikesFilename = \ acctDir(baseDir, nickname, domain) + '/.notifyLikes' - with open(notifyLikesFilename, 'w+') as nFile: - nFile.write('\n') + try: + with open(notifyLikesFilename, 'w+') as nFile: + nFile.write('\n') + except OSError: + print('WARN: unable to write ' + notifyLikesFilename) # notify when posts have emoji reactions if nickname != 'news': notifyReactionsFilename = \ acctDir(baseDir, nickname, domain) + '/.notifyReactions' - with open(notifyReactionsFilename, 'w+') as nFile: - nFile.write('\n') + try: + with open(notifyReactionsFilename, 'w+') as nFile: + nFile.write('\n') + except OSError: + print('WARN: unable to write ' + notifyReactionsFilename) theme = getConfigParam(baseDir, 'theme') if not theme: @@ -1016,10 +1031,14 @@ def reenableAccount(baseDir: str, nickname: str) -> None: lines = [] with open(suspendedFilename, 'r') as f: lines = f.readlines() - with open(suspendedFilename, 'w+') as suspendedFile: - for suspended in lines: - if suspended.strip('\n').strip('\r') != nickname: - suspendedFile.write(suspended) + try: + with open(suspendedFilename, 'w+') as suspendedFile: + for suspended in lines: + if suspended.strip('\n').strip('\r') != nickname: + suspendedFile.write(suspended) + except OSError as e: + print('WARN: unable to save ' + suspendedFilename + + ' ' + str(e)) def suspendAccount(baseDir: str, nickname: str, domain: str) -> None: @@ -1061,11 +1080,17 @@ def suspendAccount(baseDir: str, nickname: str, domain: str) -> None: for suspended in lines: if suspended.strip('\n').strip('\r') == nickname: return - with open(suspendedFilename, 'a+') as suspendedFile: - suspendedFile.write(nickname + '\n') + try: + with open(suspendedFilename, 'a+') as suspendedFile: + suspendedFile.write(nickname + '\n') + except OSError: + print('WARN: unable to append ' + suspendedFilename) else: - with open(suspendedFilename, 'w+') as suspendedFile: - suspendedFile.write(nickname + '\n') + try: + with open(suspendedFilename, 'w+') as suspendedFile: + suspendedFile.write(nickname + '\n') + except OSError: + print('WARN: unable to write ' + suspendedFilename) def canRemovePost(baseDir: str, nickname: str, @@ -1122,10 +1147,13 @@ def _removeTagsForNickname(baseDir: str, nickname: str, lines = [] with open(tagFilename, 'r') as f: lines = f.readlines() - with open(tagFilename, 'w+') as tagFile: - for tagline in lines: - if matchStr not in tagline: - tagFile.write(tagline) + try: + with open(tagFilename, 'w+') as tagFile: + for tagline in lines: + if matchStr not in tagline: + tagFile.write(tagline) + except OSError: + print('WARN: unable to write ' + tagFilename) def removeAccount(baseDir: str, nickname: str, @@ -1290,8 +1318,11 @@ def isPersonSnoozed(baseDir: str, nickname: str, domain: str, with open(snoozedFilename, 'r') as snoozedFile: content = snoozedFile.read().replace(replaceStr, '') if content: - with open(snoozedFilename, 'w+') as writeSnoozedFile: - writeSnoozedFile.write(content) + try: + with open(snoozedFilename, 'w+') as writeSnoozedFile: + writeSnoozedFile.write(content) + except OSError: + print('WARN: unable to write ' + snoozedFilename) if snoozeActor + ' ' in open(snoozedFilename).read(): return True @@ -1310,9 +1341,12 @@ def personSnooze(baseDir: str, nickname: str, domain: str, if os.path.isfile(snoozedFilename): if snoozeActor + ' ' in open(snoozedFilename).read(): return - with open(snoozedFilename, 'a+') as snoozedFile: - snoozedFile.write(snoozeActor + ' ' + - str(int(time.time())) + '\n') + try: + with open(snoozedFilename, 'a+') as snoozedFile: + snoozedFile.write(snoozeActor + ' ' + + str(int(time.time())) + '\n') + except OSError: + print('WARN: unable to append ' + snoozedFilename) def personUnsnooze(baseDir: str, nickname: str, domain: str, @@ -1339,8 +1373,11 @@ def personUnsnooze(baseDir: str, nickname: str, domain: str, with open(snoozedFilename, 'r') as snoozedFile: content = snoozedFile.read().replace(replaceStr, '') if content: - with open(snoozedFilename, 'w+') as writeSnoozedFile: - writeSnoozedFile.write(content) + try: + with open(snoozedFilename, 'w+') as writeSnoozedFile: + writeSnoozedFile.write(content) + except OSError: + print('WARN: unable to write ' + snoozedFilename) def setPersonNotes(baseDir: str, nickname: str, domain: str, @@ -1355,8 +1392,12 @@ def setPersonNotes(baseDir: str, nickname: str, domain: str, if not os.path.isdir(notesDir): os.mkdir(notesDir) notesFilename = notesDir + '/' + handle + '.txt' - with open(notesFilename, 'w+') as notesFile: - notesFile.write(notes) + try: + with open(notesFilename, 'w+') as notesFile: + notesFile.write(notes) + except OSError: + print('WARN: unable to write ' + notesFilename) + return False return True diff --git a/petnames.py b/petnames.py index e0230c1be..e74037989 100644 --- a/petnames.py +++ b/petnames.py @@ -41,17 +41,29 @@ def setPetName(baseDir: str, nickname: str, domain: str, else: newPetnamesStr += entry # save the updated petnames file - with open(petnamesFilename, 'w+') as petnamesFile: - petnamesFile.write(newPetnamesStr) + try: + with open(petnamesFilename, 'w+') as petnamesFile: + petnamesFile.write(newPetnamesStr) + except OSError: + print('WARN: unable to save ' + petnamesFilename) + return False return True # entry does not exist in the petnames file - with open(petnamesFilename, 'a+') as petnamesFile: - petnamesFile.write(entry) + try: + with open(petnamesFilename, 'a+') as petnamesFile: + petnamesFile.write(entry) + except OSError: + print('WARN: unable to append ' + petnamesFilename) + return False return True # first entry - with open(petnamesFilename, 'w+') as petnamesFile: - petnamesFile.write(entry) + try: + with open(petnamesFilename, 'w+') as petnamesFile: + petnamesFile.write(entry) + except OSError: + print('WARN: unable to write ' + petnamesFilename) + return False return True diff --git a/posts.py b/posts.py index da4ba8d32..d4678915a 100644 --- a/posts.py +++ b/posts.py @@ -1573,8 +1573,11 @@ def pinPost(baseDir: str, nickname: str, domain: str, """ accountDir = acctDir(baseDir, nickname, domain) pinnedFilename = accountDir + '/pinToProfile.txt' - with open(pinnedFilename, 'w+') as pinFile: - pinFile.write(pinnedContent) + try: + with open(pinnedFilename, 'w+') as pinFile: + pinFile.write(pinnedContent) + except OSError: + print('WARN: unable to write ' + pinnedFilename) def undoPinnedPost(baseDir: str, nickname: str, domain: str) -> None: