diff --git a/blocking.py b/blocking.py index fed8af9a5..836f85ca2 100644 --- a/blocking.py +++ b/blocking.py @@ -67,7 +67,7 @@ def removeGlobalBlock(baseDir: str, if os.path.isfile(unblockingFilename): if unblockHandle in open(unblockingFilename).read(): with open(unblockingFilename, 'r') as fp: - with open(unblockingFilename + '.new', 'w') as fpnew: + with open(unblockingFilename + '.new', 'w+') as fpnew: for line in fp: handle = line.replace('\n', '').replace('\r', '') if unblockHandle not in line: @@ -80,7 +80,7 @@ def removeGlobalBlock(baseDir: str, if os.path.isfile(unblockingFilename): if unblockHashtag + '\n' in open(unblockingFilename).read(): with open(unblockingFilename, 'r') as fp: - with open(unblockingFilename + '.new', 'w') as fpnew: + with open(unblockingFilename + '.new', 'w+') as fpnew: for line in fp: blockLine = \ line.replace('\n', '').replace('\r', '') @@ -104,7 +104,7 @@ def removeBlock(baseDir: str, nickname: str, domain: str, if os.path.isfile(unblockingFilename): if unblockHandle in open(unblockingFilename).read(): with open(unblockingFilename, 'r') as fp: - with open(unblockingFilename + '.new', 'w') as fpnew: + with open(unblockingFilename + '.new', 'w+') as fpnew: for line in fp: handle = line.replace('\n', '').replace('\r', '') if unblockHandle not in line: diff --git a/bookmarks.py b/bookmarks.py index 0039a6d89..22387d395 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -57,7 +57,7 @@ def undoBookmarksCollectionEntry(recentPostsCache: {}, indexStr = '' with open(bookmarksIndexFilename, 'r') as indexFile: indexStr = indexFile.read().replace(bookmarkIndex + '\n', '') - bookmarksIndexFile = open(bookmarksIndexFilename, 'w') + bookmarksIndexFile = open(bookmarksIndexFilename, 'w+') if bookmarksIndexFile: bookmarksIndexFile.write(indexStr) bookmarksIndexFile.close() @@ -213,7 +213,7 @@ def updateBookmarksCollection(recentPostsCache: {}, print('WARN: Failed to write entry to bookmarks index ' + bookmarksIndexFilename + ' ' + str(e)) else: - bookmarksIndexFile = open(bookmarksIndexFilename, 'w') + bookmarksIndexFile = open(bookmarksIndexFilename, 'w+') if bookmarksIndexFile: bookmarksIndexFile.write(bookmarkIndex + '\n') bookmarksIndexFile.close() diff --git a/daemon.py b/daemon.py index 679aadeb3..6795a1504 100644 --- a/daemon.py +++ b/daemon.py @@ -292,7 +292,7 @@ class PubServer(BaseHTTPRequestHandler): if not minimal and minimalFileExists: os.remove(minimalFilename) elif minimal and not minimalFileExists: - with open(minimalFilename, 'w') as fp: + with open(minimalFilename, 'w+') as fp: fp.write('\n') def _sendReplyToQuestion(self, nickname: str, messageId: str, @@ -539,7 +539,7 @@ class PubServer(BaseHTTPRequestHandler): if not etag: etag = sha1(data).hexdigest() # nosec try: - with open(mediaFilename + '.etag', 'w') as etagFile: + with open(mediaFilename + '.etag', 'w+') as etagFile: etagFile.write(etag) except BaseException: pass @@ -5108,7 +5108,7 @@ class PubServer(BaseHTTPRequestHandler): mediaBinary = avFile.read() etag = sha1(mediaBinary).hexdigest() # nosec try: - with open(mediaTagFilename, 'w') as etagFile: + with open(mediaTagFilename, 'w+') as etagFile: etagFile.write(etag) except BaseException: pass @@ -5255,7 +5255,7 @@ class PubServer(BaseHTTPRequestHandler): self.server.baseDir + '/accounts/' + \ nickname + '@' + self.server.domain + '/.lastUsed' try: - lastUsedFile = open(lastUsedFilename, 'w') + lastUsedFile = open(lastUsedFilename, 'w+') if lastUsedFile: lastUsedFile.write(str(int(time.time()))) lastUsedFile.close() @@ -5903,7 +5903,7 @@ class PubServer(BaseHTTPRequestHandler): loginNickname + ' ' + str(e)) else: try: - with open(saltFilename, 'w') as fp: + with open(saltFilename, 'w+') as fp: fp.write(salt) except Exception as e: print('WARN: Unable to save salt for ' + @@ -5917,7 +5917,7 @@ class PubServer(BaseHTTPRequestHandler): self.server.baseDir+'/accounts/' + \ loginHandle + '/.token' try: - with open(tokenFilename, 'w') as fp: + with open(tokenFilename, 'w+') as fp: fp.write(token) except Exception as e: print('WARN: Unable to save token for ' + diff --git a/epicyon.py b/epicyon.py index c8271e636..a6a8bd900 100644 --- a/epicyon.py +++ b/epicyon.py @@ -479,7 +479,7 @@ if args.socnet: httpPrefix, debug, __version__) try: - with open('socnet.dot', 'w') as fp: + with open('socnet.dot', 'w+') as fp: fp.write(dotGraph) print('Saved to socnet.dot') except BaseException: diff --git a/filters.py b/filters.py index 81335234b..744e28775 100644 --- a/filters.py +++ b/filters.py @@ -32,7 +32,7 @@ def removeFilter(baseDir: str, nickname: str, domain: str, if os.path.isfile(filtersFilename): if words in open(filtersFilename).read(): with open(filtersFilename, 'r') as fp: - with open(filtersFilename + '.new', 'w') as fpnew: + with open(filtersFilename + '.new', 'w+') as fpnew: for line in fp: line = line.replace('\n', '') if line != words: diff --git a/followingCalendar.py b/followingCalendar.py index f414634b7..10c5ae39e 100644 --- a/followingCalendar.py +++ b/followingCalendar.py @@ -29,7 +29,7 @@ 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: + with open(calendarFilename, 'w+') as fp: fp.write(followingHandles) return handle + '\n' in open(calendarFilename).read() @@ -65,7 +65,7 @@ def receiveCalendarEvents(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: + with open(calendarFilename, 'w+') as fp: fp.write(followingHandles) # already in the calendar file? @@ -75,14 +75,14 @@ def receiveCalendarEvents(baseDir: str, nickname: str, domain: str, return # remove from calendar file followingHandles = followingHandles.replace(handle + '\n', '') - with open(calendarFilename, 'w') as fp: + with open(calendarFilename, 'w+') as fp: fp.write(followingHandles) else: # not already in the calendar file if add: # append to the list of handles followingHandles += handle + '\n' - with open(calendarFilename, 'w') as fp: + with open(calendarFilename, 'w+') as fp: fp.write(followingHandles) diff --git a/inbox.py b/inbox.py index c8545e49a..ffeaed505 100644 --- a/inbox.py +++ b/inbox.py @@ -1702,7 +1702,7 @@ 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: + with open(dmFile, 'w+') as fp: fp.write(url) @@ -1748,10 +1748,16 @@ def likeNotify(baseDir: str, domain: str, onionDomain: str, prevLikeStr = likeFile.read() if prevLikeStr == likeStr: return - with open(prevLikeFile, 'w') as fp: - fp.write(likeStr) - with open(likeFile, 'w') as fp: - fp.write(likeStr) + try: + with open(prevLikeFile, 'w+') as fp: + fp.write(likeStr) + except BaseException: + pass + try: + with open(likeFile, 'w+') as fp: + fp.write(likeStr) + except BaseException: + pass def replyNotify(baseDir: str, handle: str, url: str) -> None: @@ -1762,7 +1768,7 @@ 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: + with open(replyFile, 'w+') as fp: fp.write(url) @@ -1777,7 +1783,7 @@ def gitPatchNotify(baseDir: str, handle: str, patchFile = accountDir + '/.newPatch' subject = subject.replace('[PATCH]', '').strip() handle = '@' + fromNickname + '@' + fromDomain - with open(patchFile, 'w') as fp: + with open(patchFile, 'w+') as fp: fp.write('git ' + handle + ' ' + subject) @@ -1949,7 +1955,7 @@ def inboxUpdateCalendar(baseDir: str, handle: str, postJsonObject: {}) -> None: calendarNotificationFilename = \ baseDir + '/accounts/' + handle + '/.newCalendar' calendarNotificationFile = \ - open(calendarNotificationFilename, 'w') + open(calendarNotificationFilename, 'w+') if calendarNotificationFile: calendarNotificationFile.write('/calendar?year=' + str(eventYear) + diff --git a/media.py b/media.py index 4afa96571..7c6b87306 100644 --- a/media.py +++ b/media.py @@ -122,7 +122,7 @@ def updateEtag(mediaFilename: str) -> None: etag = sha1(data).hexdigest() # nosec # save the hash try: - with open(mediaFilename + '.etag', 'w') as etagFile: + with open(mediaFilename + '.etag', 'w+') as etagFile: etagFile.write(etag) except BaseException: pass diff --git a/migrate.py b/migrate.py index ac9502e03..6fb8bee28 100644 --- a/migrate.py +++ b/migrate.py @@ -25,7 +25,7 @@ def migrateFollows(followFilename: str, oldHandle: str, newFollowData = followData.replace(oldHandle, newHandle) if followData == newFollowData: return - with open(followFilename, 'w') as followFile: + with open(followFilename, 'w+') as followFile: followFile.write(newFollowData) diff --git a/person.py b/person.py index 0e6252b49..78b20a01e 100644 --- a/person.py +++ b/person.py @@ -1004,7 +1004,7 @@ def isPersonSnoozed(baseDir: str, nickname: str, domain: str, with open(snoozedFilename, 'r') as snoozedFile: content = snoozedFile.read().replace(replaceStr, '') if content: - writeSnoozedFile = open(snoozedFilename, 'w') + writeSnoozedFile = open(snoozedFilename, 'w+') if writeSnoozedFile: writeSnoozedFile.write(content) writeSnoozedFile.close() @@ -1057,7 +1057,7 @@ def personUnsnooze(baseDir: str, nickname: str, domain: str, with open(snoozedFilename, 'r') as snoozedFile: content = snoozedFile.read().replace(replaceStr, '') if content: - writeSnoozedFile = open(snoozedFilename, 'w') + writeSnoozedFile = open(snoozedFilename, 'w+') if writeSnoozedFile: writeSnoozedFile.write(content) writeSnoozedFile.close() diff --git a/petnames.py b/petnames.py index e28048250..7046a5d94 100644 --- a/petnames.py +++ b/petnames.py @@ -40,7 +40,7 @@ def setPetName(baseDir: str, nickname: str, domain: str, else: newPetnamesStr += entry # save the updated petnames file - with open(petnamesFilename, 'w') as petnamesFile: + with open(petnamesFilename, 'w+') as petnamesFile: petnamesFile.write(newPetnamesStr) return True # entry does not exist in the petnames file @@ -49,7 +49,7 @@ def setPetName(baseDir: str, nickname: str, domain: str, return True # first entry - with open(petnamesFilename, 'w') as petnamesFile: + with open(petnamesFilename, 'w+') as petnamesFile: petnamesFile.write(entry) return True diff --git a/posts.py b/posts.py index dfcfc100f..fdca227b5 100644 --- a/posts.py +++ b/posts.py @@ -599,7 +599,7 @@ def addSchedulePost(baseDir: str, nickname: str, domain: str, print('WARN: Failed to write entry to scheduled posts index ' + scheduleIndexFilename + ' ' + str(e)) else: - scheduleFile = open(scheduleIndexFilename, 'w') + scheduleFile = open(scheduleIndexFilename, 'w+') if scheduleFile: scheduleFile.write(indexStr + '\n') scheduleFile.close() @@ -1337,7 +1337,7 @@ def createReportPost(baseDir: str, if os.path.isfile(newReportFile): continue try: - with open(newReportFile, 'w') as fp: + with open(newReportFile, 'w+') as fp: fp.write(toUrl + '/moderation') except BaseException: pass diff --git a/shares.py b/shares.py index a20fb8055..af300d223 100644 --- a/shares.py +++ b/shares.py @@ -181,7 +181,7 @@ def addShare(baseDir: str, if not os.path.isfile(newShareFile): nickname = handle.split('@')[0] try: - with open(newShareFile, 'w') as fp: + with open(newShareFile, 'w+') as fp: fp.write(httpPrefix + '://' + domainFull + '/users/' + nickname + '/tlshares') except BaseException: diff --git a/theme.py b/theme.py index 786f49584..c1a323c17 100644 --- a/theme.py +++ b/theme.py @@ -105,7 +105,7 @@ def setThemeFromDict(baseDir: str, name: str, themeParams: {}) -> None: for paramName, paramValue in themeParams.items(): css = setCSSparam(css, paramName, paramValue) filename = baseDir + '/' + filename - with open(filename, 'w') as cssfile: + with open(filename, 'w+') as cssfile: cssfile.write(css) @@ -124,11 +124,11 @@ def enableGrayscale(baseDir: str) -> None: css.replace('body, html {', 'body, html {\n filter: grayscale(100%);') filename = baseDir + '/' + filename - with open(filename, 'w') as cssfile: + with open(filename, 'w+') as cssfile: cssfile.write(css) grayscaleFilename = baseDir + '/accounts/.grayscale' if not os.path.isfile(grayscaleFilename): - with open(grayscaleFilename, 'w') as grayfile: + with open(grayscaleFilename, 'w+') as grayfile: grayfile.write(' ') @@ -146,7 +146,7 @@ def disableGrayscale(baseDir: str) -> None: css = \ css.replace('\n filter: grayscale(100%);', '') filename = baseDir + '/' + filename - with open(filename, 'w') as cssfile: + with open(filename, 'w+') as cssfile: cssfile.write(css) grayscaleFilename = baseDir + '/accounts/.grayscale' if os.path.isfile(grayscaleFilename): @@ -187,7 +187,7 @@ def setCustomFont(baseDir: str): customFontType + "')") css = setCSSparam(css, "*font-family", "'CustomFont'") filename = baseDir + '/' + filename - with open(filename, 'w') as cssfile: + with open(filename, 'w+') as cssfile: cssfile.write(css) diff --git a/utils.py b/utils.py index c53811f26..abdb67be7 100644 --- a/utils.py +++ b/utils.py @@ -51,7 +51,7 @@ def saveJson(jsonObject: {}, filename: str) -> bool: tries = 0 while tries < 5: try: - with open(filename, 'w') as fp: + with open(filename, 'w+') as fp: fp.write(json.dumps(jsonObject)) return True except BaseException: diff --git a/webinterface.py b/webinterface.py index 079632c27..4907b1d95 100644 --- a/webinterface.py +++ b/webinterface.py @@ -3337,7 +3337,7 @@ def saveIndividualPostAsHtmlToCache(baseDir: str, os.mkdir(htmlPostCacheDir) try: - with open(cachedPostFilename, 'w') as fp: + with open(cachedPostFilename, 'w+') as fp: fp.write(postHtml) return True except Exception as e: