Commit ac73aaaa authored by Julien Muchembled's avatar Julien Muchembled

Fix ingestion script wrt latest changes in Contribution Tool

parent dfa56d05
#!/usr/bin/python #!/usr/bin/python
import os, subprocess, sys, textwrap, traceback, urllib, urlparse import os, sys, traceback, urllib, urlparse
# Example of configuration of postfix to deliver to ERP5: # Example of configuration of postfix to deliver to ERP5:
# - Add the following lines to master.cf: # - Add the following lines to master.cf:
...@@ -9,28 +9,26 @@ import os, subprocess, sys, textwrap, traceback, urllib, urlparse ...@@ -9,28 +9,26 @@ import os, subprocess, sys, textwrap, traceback, urllib, urlparse
# - Tell smtpd service to use the new filter, by adding: # - Tell smtpd service to use the new filter, by adding:
# -o content_filter=erp5: # -o content_filter=erp5:
class HTTPError(IOError):
def __init__(self, errcode, errmsg, result):
self.__dict__.update(errcode=errcode, errmsg=errmsg, result=result)
def __str__(self):
return '%s %s' % (self.errcode, self.errmsg)
class urlopen(urllib.FancyURLopener, object): class urlopen(urllib.FancyURLopener, object):
"""Open a network object denoted by a URL for reading """Open a network object denoted by a URL for reading
Raise a HTTPError exception if HTTP error code is not 200. HTTP error handling:
- ContributionTool returns a 302 to itself if successfully ingested.
- ERP5 returns a 302 to login_form if authentication is required.
- Raise otherwise, even for 307 since Python does not support POST redirect.
""" """
def __new__(cls, *args, **kw): def __new__(cls, *args, **kw):
self = object.__new__(cls) self = object.__new__(cls)
self.__init__() self.__init__()
return self.open(*args, **kw) return self.open(*args, **kw)
http_error_default = urllib.URLopener.http_error_default
def http_error(self, url, fp, errcode, errmsg, headers, data=None): def http_error(self, url, fp, errcode, errmsg, headers, data=None):
raise HTTPError(errcode, errmsg, if errcode != 302 or urlparse.urlsplit(
self.http_error_default(url, fp, errcode, errmsg, headers)) headers['Location'])[2].endswith('/login_form'):
return self.http_error_default(url, fp, errcode, errmsg, headers)
return fp
class Message(object): class Message(object):
...@@ -75,20 +73,12 @@ class Message(object): ...@@ -75,20 +73,12 @@ class Message(object):
else: else:
user, password = urllib.splitpasswd(user) user, password = urllib.splitpasswd(user)
user = kw.pop('user', user) user = kw.pop('user', user)
if user is not None: if user:
password = kw.pop('password', password) host = '%s:%s@%s' % (user, kw.pop('password', password) or '', host)
if password is not None:
user = '%s:%s' % (user, password)
host = '%s@%s' % (user, host)
url = urlparse.urlunsplit((scheme, host, path.rstrip('/'), '', '')) + \ url = urlparse.urlunsplit((scheme, host, path.rstrip('/'), '', '')) + \
'/portal_contributions/newContent' '/portal_contributions/newContent'
kw['data'] = sys.stdin.read() kw['data'] = sys.stdin.read()
try:
result = urlopen(url, urllib.urlencode(kw)) result = urlopen(url, urllib.urlencode(kw))
except HTTPError, e:
if e.errcode >= 300:
raise
result = e.result
result.read() # ERP5 does not return useful information result.read() # ERP5 does not return useful information
print 'Message ingested' print 'Message ingested'
else: else:
...@@ -101,12 +91,9 @@ class SimpleIngestionMap(object): ...@@ -101,12 +91,9 @@ class SimpleIngestionMap(object):
This class maps recipients to parameters for portal_contributions/newContent This class maps recipients to parameters for portal_contributions/newContent
""" """
def __init__(self, ingestion_map_filename): def __init__(self, ingestion_map_filename):
fd = file(ingestion_map_filename)
g = {} g = {}
try: with open(ingestion_map_filename) as f:
exec fd in g exec f in g
finally:
fd.close()
self._map = g['ingestion_map'] self._map = g['ingestion_map']
def __call__(self, message, **kw): def __call__(self, message, **kw):
...@@ -137,7 +124,7 @@ by using it as a filter (cf documentation of /etc/postfix/master.cf).""") ...@@ -137,7 +124,7 @@ by using it as a filter (cf documentation of /etc/postfix/master.cf).""")
" to the sender; 'SENDMAIL' injects it back into MTA") " to the sender; 'SENDMAIL' injects it back into MTA")
_("--user", help="use this user to connect to ERP5") _("--user", help="use this user to connect to ERP5")
_("--password", help="use this password to connect to ERP5") _("--password", help="use this password to connect to ERP5")
_("--file_name", help="ERP5 requires a file name to guess content type") _("--filename", help="ERP5 requires a file name to guess content type")
_("--container_path", help="define where to contribute the content" _("--container_path", help="define where to contribute the content"
" (by default, it is guessed by ERP5)") " (by default, it is guessed by ERP5)")
#_("--portal_type", default="Mail Message") #_("--portal_type", default="Mail Message")
...@@ -147,14 +134,14 @@ database. The module must define an 'ingestion_map' variable implementing \ ...@@ -147,14 +134,14 @@ database. The module must define an 'ingestion_map' variable implementing \
'get(recipient) -> option_dict'. Example: 'get(recipient) -> option_dict'. Example:
ingestion_map = { ingestion_map = {
'foo@bar.com': dict(user='foo', password='12345'), 'foo@bar.com': dict(user='foo', password='12345'),
'patches@prj1.org': dict(file_name='prj1.patch'), 'patches@prj1.org': dict(filename='prj1.patch'),
'spam@example.invalid': dict(portal=None), # drop 'spam@example.invalid': dict(portal=None), # drop
}""") }""")
group.add_option("--ingestion_map", help="get options from this file," group.add_option("--ingestion_map", help="get options from this file,"
" according to recipients") " according to recipients")
parser.add_option_group(group) parser.add_option_group(group)
_ = group.add_option _ = group.add_option
parser.set_defaults(file_name="unnamed.eml") parser.set_defaults(filename="unnamed.eml")
return parser return parser
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment