From 7635652ac5487de6af6d4e9f5b4b359afe1aeeae Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 29 Aug 2020 12:14:19 +0100 Subject: [PATCH] Directories must be created first --- auth.py | 6 +++--- blog.py | 2 +- daemon.py | 18 +++++++++--------- follow.py | 2 +- git.py | 4 ++-- happening.py | 6 +++--- inbox.py | 4 ++-- person.py | 20 ++++++++++---------- posts.py | 2 +- question.py | 4 ++-- roles.py | 4 ++-- tests.py | 3 +++ translations/de.json | 2 +- utils.py | 18 +++++++++++++----- 14 files changed, 53 insertions(+), 42 deletions(-) diff --git a/auth.py b/auth.py index a9b83a938..7ac45cecf 100644 --- a/auth.py +++ b/auth.py @@ -121,7 +121,7 @@ def storeBasicCredentials(baseDir: str, nickname: str, password: str) -> bool: if os.path.isfile(passwordFile): if nickname + ':' in open(passwordFile).read(): with open(passwordFile, "r") as fin: - with open(passwordFile + '.new', "w") as fout: + with open(passwordFile + '.new', 'w+') as fout: for line in fin: if not line.startswith(nickname + ':'): fout.write(line) @@ -133,7 +133,7 @@ def storeBasicCredentials(baseDir: str, nickname: str, password: str) -> bool: with open(passwordFile, 'a+') as passfile: passfile.write(storeStr + '\n') else: - with open(passwordFile, "w") as passfile: + with open(passwordFile, 'w+') as passfile: passfile.write(storeStr + '\n') return True @@ -145,7 +145,7 @@ def removePassword(baseDir: str, nickname: str) -> None: passwordFile = baseDir + '/accounts/passwords' if os.path.isfile(passwordFile): with open(passwordFile, "r") as fin: - with open(passwordFile + '.new', "w") as fout: + with open(passwordFile + '.new', 'w+') as fout: for line in fin: if not line.startswith(nickname + ':'): fout.write(line) diff --git a/blog.py b/blog.py index e41aacdff..91db9ca66 100644 --- a/blog.py +++ b/blog.py @@ -73,7 +73,7 @@ def noOfBlogReplies(baseDir: str, httpPrefix: str, translate: {}, if lines and removals: print('Rewriting ' + postFilename + ' to remove ' + str(len(removals)) + ' entries') - with open(postFilename, "w") as f: + with open(postFilename, 'w+') as f: for replyPostId in lines: replyPostId = replyPostId.replace('\n', '').replace('\r', '') if replyPostId not in removals: diff --git a/daemon.py b/daemon.py index ab21dc404..3b322a57e 100644 --- a/daemon.py +++ b/daemon.py @@ -7502,7 +7502,7 @@ class PubServer(BaseHTTPRequestHandler): if fields.get('followDMs'): if fields['followDMs'] == 'on': followDMsActive = True - with open(followDMsFilename, "w") as fFile: + with open(followDMsFilename, 'w+') as fFile: fFile.write('\n') if not followDMsActive: if os.path.isfile(followDMsFilename): @@ -7516,7 +7516,7 @@ class PubServer(BaseHTTPRequestHandler): if fields.get('removeTwitter'): if fields['removeTwitter'] == 'on': removeTwitterActive = True - with open(removeTwitterFilename, "w") as rFile: + with open(removeTwitterFilename, 'w+') as rFile: rFile.write('\n') if not removeTwitterActive: if os.path.isfile(removeTwitterFilename): @@ -7534,7 +7534,7 @@ class PubServer(BaseHTTPRequestHandler): if fields.get('hideLikeButton'): if fields['hideLikeButton'] == 'on': hideLikeButtonActive = True - with open(hideLikeButtonFile, "w") as rFile: + with open(hideLikeButtonFile, 'w+') as rFile: rFile.write('\n') # remove notify likes selection if os.path.isfile(notifyLikesFilename): @@ -7548,7 +7548,7 @@ class PubServer(BaseHTTPRequestHandler): if fields['notifyLikes'] == 'on' and \ not hideLikeButtonActive: notifyLikesActive = True - with open(notifyLikesFilename, "w") as rFile: + with open(notifyLikesFilename, 'w+') as rFile: rFile.write('\n') if not notifyLikesActive: if os.path.isfile(notifyLikesFilename): @@ -7585,7 +7585,7 @@ class PubServer(BaseHTTPRequestHandler): nickname + '@' + self.server.domain + \ '/filters.txt' if fields.get('filteredWords'): - with open(filterFilename, "w") as filterfile: + with open(filterFilename, 'w+') as filterfile: filterfile.write(fields['filteredWords']) else: if os.path.isfile(filterFilename): @@ -7596,7 +7596,7 @@ class PubServer(BaseHTTPRequestHandler): nickname + '@' + self.server.domain + \ '/replacewords.txt' if fields.get('switchWords'): - with open(switchFilename, "w") as switchfile: + with open(switchFilename, 'w+') as switchfile: switchfile.write(fields['switchWords']) else: if os.path.isfile(switchFilename): @@ -7607,7 +7607,7 @@ class PubServer(BaseHTTPRequestHandler): nickname + '@' + self.server.domain + \ '/blocking.txt' if fields.get('blocked'): - with open(blockedFilename, "w") as blockedfile: + with open(blockedFilename, 'w+') as blockedfile: blockedfile.write(fields['blocked']) else: if os.path.isfile(blockedFilename): @@ -7618,7 +7618,7 @@ class PubServer(BaseHTTPRequestHandler): nickname + '@' + self.server.domain + \ '/allowedinstances.txt' if fields.get('allowedInstances'): - with open(allowedInstancesFilename, "w") as aFile: + with open(allowedInstancesFilename, 'w+') as aFile: aFile.write(fields['allowedInstances']) else: if os.path.isfile(allowedInstancesFilename): @@ -7629,7 +7629,7 @@ class PubServer(BaseHTTPRequestHandler): nickname + '@' + self.server.domain + \ '/gitprojects.txt' if fields.get('gitProjects'): - with open(gitProjectsFilename, "w") as aFile: + with open(gitProjectsFilename, 'w+') as aFile: aFile.write(fields['gitProjects'].lower()) else: if os.path.isfile(gitProjectsFilename): diff --git a/follow.py b/follow.py index a2da2d991..6b764df37 100644 --- a/follow.py +++ b/follow.py @@ -210,7 +210,7 @@ def unfollowPerson(baseDir: str, nickname: str, domain: str, return with open(filename, "r") as f: lines = f.readlines() - with open(filename, "w") as f: + with open(filename, 'w+') as f: for line in lines: if line.strip("\n").strip("\r").lower() != handleToUnfollowLower: f.write(line) diff --git a/git.py b/git.py index 68680e33e..dda0f31a0 100644 --- a/git.py +++ b/git.py @@ -210,12 +210,12 @@ def receiveGitPatch(baseDir: str, nickname: str, domain: str, return False patchStr = \ gitAddFromHandle(patchStr, '@' + fromNickname + '@' + fromDomain) - with open(patchFilename, "w") as patchFile: + with open(patchFilename, 'w+') as patchFile: patchFile.write(patchStr) patchNotifyFilename = \ baseDir + '/accounts/' + \ nickname + '@' + domain + '/.newPatchContent' - with open(patchNotifyFilename, "w") as patchFile: + with open(patchNotifyFilename, 'w+') as patchFile: patchFile.write(patchStr) return True return False diff --git a/happening.py b/happening.py index 1c81e0d5c..6f1d2b1e7 100644 --- a/happening.py +++ b/happening.py @@ -244,7 +244,7 @@ def getTodaysEvents(baseDir: str, nickname: str, domain: str, # if some posts have been deleted then regenerate the calendar file if recreateEventsFile: - calendarFile = open(calendarFilename, "w") + calendarFile = open(calendarFilename, 'w+') for postId in calendarPostIds: calendarFile.write(postId + '\n') calendarFile.close() @@ -412,7 +412,7 @@ def getThisWeeksEvents(baseDir: str, nickname: str, domain: str) -> {}: # if some posts have been deleted then regenerate the calendar file if recreateEventsFile: - calendarFile = open(calendarFilename, "w") + calendarFile = open(calendarFilename, 'w+') for postId in calendarPostIds: calendarFile.write(postId + '\n') calendarFile.close() @@ -494,7 +494,7 @@ def getCalendarEvents(baseDir: str, nickname: str, domain: str, # if some posts have been deleted then regenerate the calendar file if recreateEventsFile: - calendarFile = open(calendarFilename, "w") + calendarFile = open(calendarFilename, 'w+') for postId in calendarPostIds: calendarFile.write(postId + '\n') calendarFile.close() diff --git a/inbox.py b/inbox.py index 3fe84dc5e..107865bda 100644 --- a/inbox.py +++ b/inbox.py @@ -1642,7 +1642,7 @@ def populateReplies(baseDir: str, httpPrefix: str, domain: str, repliesFile.write(messageId + '\n') repliesFile.close() else: - repliesFile = open(postRepliesFilename, "w") + repliesFile = open(postRepliesFilename, 'w+') repliesFile.write(messageId + '\n') repliesFile.close() return True @@ -2399,7 +2399,7 @@ def inboxAfterCapabilities(recentPostsCache: {}, maxRecentPosts: int, # This enables you to ignore a threat that's getting boring if isReplyToMutedPost: print('MUTE REPLY: ' + destinationFilename) - muteFile = open(destinationFilename + '.muted', "w") + muteFile = open(destinationFilename + '.muted', 'w+') if muteFile: muteFile.write('\n') muteFile.close() diff --git a/person.py b/person.py index 0afdc7b14..af7020df6 100644 --- a/person.py +++ b/person.py @@ -350,7 +350,7 @@ 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: + with open(filename, 'w+') as text_file: print(privateKeyPem, file=text_file) # save the public key @@ -358,7 +358,7 @@ def createPersonBase(baseDir: str, nickname: str, domain: str, port: int, if not os.path.isdir(baseDir + publicKeysSubdir): os.mkdir(baseDir + publicKeysSubdir) filename = baseDir + publicKeysSubdir + '/' + handle + '.pem' - with open(filename, "w") as text_file: + with open(filename, 'w+') as text_file: print(publicKeyPem, file=text_file) if password: @@ -454,22 +454,22 @@ def createPerson(baseDir: str, nickname: str, domain: str, port: int, setRole(baseDir, nickname, domain, 'instance', 'delegator') setConfigParam(baseDir, 'admin', nickname) + if not os.path.isdir(baseDir + '/accounts'): + os.mkdir(baseDir + '/accounts') + if not os.path.isdir(baseDir + '/accounts/' + nickname + '@' + domain): + os.mkdir(baseDir + '/accounts/' + nickname + '@' + domain) + if manualFollowerApproval: followDMsFilename = baseDir + '/accounts/' + \ nickname + '@' + domain + '/.followDMs' - with open(followDMsFilename, "w") as fFile: + with open(followDMsFilename, 'w+') as fFile: fFile.write('\n') # notify when posts are liked notifyLikesFilename = baseDir + '/accounts/' + \ nickname + '@' + domain + '/.notifyLikes' - with open(notifyLikesFilename, "w") as fFile: - fFile.write('\n') - - if not os.path.isdir(baseDir + '/accounts'): - os.mkdir(baseDir + '/accounts') - if not os.path.isdir(baseDir + '/accounts/' + nickname + '@' + domain): - os.mkdir(baseDir + '/accounts/' + nickname + '@' + domain) + with open(notifyLikesFilename, 'w+') as nFile: + nFile.write('\n') if os.path.isfile(baseDir + '/img/default-avatar.png'): copyfile(baseDir + '/img/default-avatar.png', diff --git a/posts.py b/posts.py index e9503e122..95026c113 100644 --- a/posts.py +++ b/posts.py @@ -3536,7 +3536,7 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str, return print('MUTE: ' + postFilename) - muteFile = open(postFilename + '.muted', "w") + muteFile = open(postFilename + '.muted', 'w+') if muteFile: muteFile.write('\n') muteFile.close() diff --git a/question.py b/question.py index bf328293a..9618154ae 100644 --- a/question.py +++ b/question.py @@ -66,7 +66,7 @@ def questionUpdateVotes(baseDir: str, nickname: str, domain: str, votersFilename = questionPostFilename.replace('.json', '.voters') if not os.path.isfile(votersFilename): # create a new voters file - votersFile = open(votersFilename, "w") + votersFile = open(votersFilename, 'w+') if votersFile: votersFile.write(replyJson['actor'] + votersFileSeparator + @@ -99,7 +99,7 @@ def questionUpdateVotes(baseDir: str, nickname: str, domain: str, else: newlines.append(voteLine) if saveVotersFile: - with open(votersFilename, "w") as votersFile: + with open(votersFilename, 'w+') as votersFile: for voteLine in newlines: votersFile.write(voteLine) else: diff --git a/roles.py b/roles.py index 967997b07..f2bc57374 100644 --- a/roles.py +++ b/roles.py @@ -52,7 +52,7 @@ def addModerator(baseDir: str, nickname: str, domain: str) -> None: if moderator == nickname: return lines.append(nickname) - with open(moderatorsFile, "w") as f: + with open(moderatorsFile, 'w+') as f: for moderator in lines: moderator = moderator.strip('\n').strip('\r') if len(moderator) > 1: @@ -74,7 +74,7 @@ def removeModerator(baseDir: str, nickname: str): return with open(moderatorsFile, "r") as f: lines = f.readlines() - with open(moderatorsFile, "w") as f: + with open(moderatorsFile, 'w+') as f: for moderator in lines: moderator = moderator.strip('\n').strip('\r') if len(moderator) > 1 and moderator != nickname: diff --git a/tests.py b/tests.py index 539349b9b..13e3a2892 100644 --- a/tests.py +++ b/tests.py @@ -2064,6 +2064,9 @@ def testTranslations(): langDict = {} for lang in languagesStr: langJson = loadJson('translations/' + lang + '.json') + if not langJson: + print('Missing language file ' + + 'translations/' + lang + '.json') assert langJson langDict[lang] = langJson diff --git a/translations/de.json b/translations/de.json index f3ef51ba8..957c54f6b 100644 --- a/translations/de.json +++ b/translations/de.json @@ -283,5 +283,5 @@ "Moderation policy or code of conduct": "Moderationsrichtlinie oder Verhaltenskodex", "Edit event": "Ereignis bearbeiten", "Notify when posts are liked": "Benachrichtigen, wenn Beiträge gefallen", - "Don't show the Like button": "Zeigen Sie nicht die Schaltfläche "Gefällt mir" an" + "Don't show the Like button": "Zeigen Sie nicht die Schaltfläche \"Gefällt mir\" an" } diff --git a/utils.py b/utils.py index e74d407d1..f143bebbd 100644 --- a/utils.py +++ b/utils.py @@ -351,7 +351,7 @@ def followPerson(baseDir: str, nickname: str, domain: str, for line in lines: if handleToFollow not in line: newLines += line - with open(unfollowedFilename, "w") as f: + with open(unfollowedFilename, 'w+') as f: f.write(newLines) if not os.path.isdir(baseDir + '/accounts'): @@ -383,7 +383,7 @@ def followPerson(baseDir: str, nickname: str, domain: str, followNickname, followDomain) if debug: print('DEBUG: creating new following file to follow ' + handleToFollow) - with open(filename, "w") as followfile: + with open(filename, 'w+') as followfile: followfile.write(handleToFollow + '\n') return True @@ -907,12 +907,20 @@ def searchBoxPosts(baseDir: str, nickname: str, domain: str, def getFileCaseInsensitive(path: str) -> str: """Returns a case specific filename given a case insensitive version of it """ + # does the given file exist? If so then we don't need + # to do a directory search + if os.path.isfile(path): + return path + if path != path.lower(): + if os.path.isfile(path.lower()): + return path.lower() directory, filename = os.path.split(path) directory, filename = (directory or '.'), filename.lower() for f in os.listdir(directory): - newpath = os.path.join(directory, f) - if os.path.isfile(newpath) and f.lower() == filename: - return newpath + if f.lower() == filename: + newpath = os.path.join(directory, f) + if os.path.isfile(newpath): + return newpath return path