Directories must be created first

merge-requests/8/head
Bob Mottram 2020-08-29 12:14:19 +01:00
parent 77c5f810f9
commit 7635652ac5
14 changed files with 53 additions and 42 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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):

View File

@ -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)

4
git.py
View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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',

View File

@ -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()

View File

@ -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:

View File

@ -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:

View File

@ -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

View File

@ -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"
}

View File

@ -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