epicyon/git.py

127 lines
4.0 KiB
Python
Raw Normal View History

2020-05-02 10:07:50 +00:00
__filename__ = "git.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import os
2020-05-02 17:16:24 +00:00
def gitFormatContent(content: str) -> str:
2020-05-02 17:06:13 +00:00
""" replace html formatting, so that it's more
like the original patch file
"""
2020-05-02 17:16:24 +00:00
contentStr = content.replace('<br>', '\n').replace('<br \\>', '\n')
2020-05-02 17:06:13 +00:00
contentStr = contentStr.replace('<p>', '').replace('</p>', '\n')
2020-05-02 17:24:00 +00:00
contentStr = contentStr.replace('&lt;', '<').replace('&gt;', '>')
2020-05-02 17:06:13 +00:00
if 'From ' in contentStr:
2020-05-02 17:16:24 +00:00
contentStr = 'From ' + contentStr.split('From ', 1)[1]
2020-05-02 17:06:13 +00:00
return contentStr
2020-05-02 11:08:38 +00:00
def getGitProjectName(baseDir: str, nickname: str, domain: str,
2020-05-02 14:47:30 +00:00
subject: str) -> str:
2020-05-02 11:08:38 +00:00
"""Returns the project name for a git patch
The project name should be contained within the subject line
and should match against a list of projects which the account
holder wants to receive
"""
2020-05-02 16:24:17 +00:00
print('Debug git subject ' + subject)
2020-05-02 11:08:38 +00:00
gitProjectsFilename = \
baseDir + '/accounts/' + nickname + '@' + domain + '/gitprojects.txt'
if not os.path.isfile(gitProjectsFilename):
return None
2020-05-02 16:05:28 +00:00
subjectLineWords = subject.lower().split(' ')
for word in subjectLineWords:
2020-05-02 16:29:43 +00:00
if word in open(gitProjectsFilename).read():
2020-05-02 11:08:38 +00:00
return word
2020-05-02 16:24:17 +00:00
return None
2020-05-02 11:08:38 +00:00
def isGitPatch(baseDir: str, nickname: str, domain: str,
subject: str, content: str) -> bool:
2020-05-02 10:07:50 +00:00
"""Is the given post content a git patch?
"""
# must have a subject line
2020-05-02 16:24:17 +00:00
print('Debug git 11: ' + content)
2020-05-02 10:19:24 +00:00
if not subject:
2020-05-02 10:07:50 +00:00
return False
2020-05-02 16:24:17 +00:00
print('Debug git 12')
2020-05-02 10:07:50 +00:00
if '[PATCH]' not in content:
return False
2020-05-02 16:24:17 +00:00
print('Debug git 13')
2020-05-02 10:07:50 +00:00
if '---' not in content:
return False
2020-05-02 16:24:17 +00:00
print('Debug git 14')
2020-05-02 10:07:50 +00:00
if 'diff ' not in content:
return False
2020-05-02 16:24:17 +00:00
print('Debug git 15')
2020-05-02 10:07:50 +00:00
if 'From:' not in content:
return False
2020-05-02 16:24:17 +00:00
print('Debug git 16')
2020-05-02 10:07:50 +00:00
if 'Date:' not in content:
return False
2020-05-02 16:24:17 +00:00
print('Debug git 17')
2020-05-02 10:07:50 +00:00
if 'Subject:' not in content:
return False
2020-05-02 16:24:17 +00:00
print('Debug git 18')
2020-05-02 14:47:30 +00:00
if '<br>' not in content:
if '<br />' not in content:
return False
2020-05-02 16:24:17 +00:00
print('Debug git 19')
2020-05-02 11:08:38 +00:00
projectName = \
getGitProjectName(baseDir, nickname, domain,
2020-05-02 14:47:30 +00:00
subject)
2020-05-02 10:07:50 +00:00
if not projectName:
return False
2020-05-02 16:24:17 +00:00
print('Debug git 20')
2020-05-02 11:08:38 +00:00
return True
def receiveGitPatch(baseDir: str, nickname: str, domain: str,
subject: str, content: str) -> bool:
"""Receive a git patch
"""
if not isGitPatch(baseDir, nickname, domain,
subject, content):
return False
2020-05-02 16:05:28 +00:00
2020-05-02 17:06:13 +00:00
contentStr = gitFormatContent(content)
2020-05-02 16:05:28 +00:00
2020-05-02 14:47:30 +00:00
patchLines = contentStr.split('\n')
2020-05-02 10:07:50 +00:00
patchFilename = None
2020-05-02 16:53:20 +00:00
projectDir = None
patchesDir = \
baseDir + '/accounts/' + nickname + '@' + domain + \
'/patches'
2020-05-02 10:07:50 +00:00
# get the subject line and turn it into a filename
for line in patchLines:
if line.startswith('Subject:'):
patchSubject = \
2020-05-02 16:44:46 +00:00
line.replace('Subject:', '').replace('/', '|')
patchSubject = patchSubject.replace('[PATCH]', '').strip()
patchSubject = patchSubject.replace(' ', '_')
2020-05-02 11:08:38 +00:00
projectName = \
2020-05-02 16:05:28 +00:00
getGitProjectName(baseDir, nickname, domain, subject)
2020-05-02 16:53:20 +00:00
if not os.path.isdir(patchesDir):
os.mkdir(patchesDir)
projectDir = patchesDir + '/' + projectName
if not os.path.isdir(projectDir):
os.mkdir(projectDir)
2020-05-02 10:07:50 +00:00
patchFilename = \
2020-05-02 16:53:20 +00:00
projectDir + '/' + patchSubject + '.patch'
2020-05-02 10:07:50 +00:00
break
if not patchFilename:
return False
2020-05-02 16:44:46 +00:00
with open(patchFilename, "w") as patchFile:
patchFile.write(contentStr)
patchNotifyFilename = \
baseDir + '/accounts/' + \
nickname + '@' + domain + '/.newPatchContent'
with open(patchNotifyFilename, "w") as patchFile:
patchFile.write(contentStr)
2020-05-02 16:44:46 +00:00
return True
return False