| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | ## |
|---|
| 3 | ## |
|---|
| 4 | ## This file is part of CDS Indico. |
|---|
| 5 | ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN. |
|---|
| 6 | ## |
|---|
| 7 | ## CDS Indico is free software; you can redistribute it and/or |
|---|
| 8 | ## modify it under the terms of the GNU General Public License as |
|---|
| 9 | ## published by the Free Software Foundation; either version 2 of the |
|---|
| 10 | ## License, or (at your option) any later version. |
|---|
| 11 | ## |
|---|
| 12 | ## CDS Indico is distributed in the hope that it will be useful, but |
|---|
| 13 | ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 15 | ## General Public License for more details. |
|---|
| 16 | ## |
|---|
| 17 | ## You should have received a copy of the GNU General Public License |
|---|
| 18 | ## along with CDS Indico; if not, write to the Free Software Foundation, Inc., |
|---|
| 19 | ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 20 | |
|---|
| 21 | """ |
|---|
| 22 | Agent definitions for CERN Search |
|---|
| 23 | """ |
|---|
| 24 | |
|---|
| 25 | # standard library imports |
|---|
| 26 | import time, base64 |
|---|
| 27 | from urllib2 import urlopen, Request, HTTPError |
|---|
| 28 | from urllib import urlencode |
|---|
| 29 | |
|---|
| 30 | # dependency imports |
|---|
| 31 | from lxml import etree |
|---|
| 32 | |
|---|
| 33 | # plugin imports |
|---|
| 34 | from indico.ext.livesync.agent import AgentProviderComponent, RecordUploader |
|---|
| 35 | from indico.ext.livesync.bistate import BistateBatchUploaderAgent |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | class CERNSearchUploadAgent(BistateBatchUploaderAgent): |
|---|
| 39 | |
|---|
| 40 | _extraOptions = {'url': 'Server URL', |
|---|
| 41 | 'username': 'Username', |
|---|
| 42 | 'password': 'Password'} |
|---|
| 43 | |
|---|
| 44 | def __init__(self, aid, name, description, updateTime, |
|---|
| 45 | access=None, url=None, username=None, password=None): |
|---|
| 46 | super(CERNSearchUploadAgent, self).__init__( |
|---|
| 47 | aid, name, description, updateTime, access) |
|---|
| 48 | self._url = url |
|---|
| 49 | self._username = username |
|---|
| 50 | self._password = password |
|---|
| 51 | |
|---|
| 52 | def _run(self, records, logger=None, monitor=None): |
|---|
| 53 | |
|---|
| 54 | self._v_logger = logger |
|---|
| 55 | |
|---|
| 56 | # the uploader will manage everything for us... |
|---|
| 57 | |
|---|
| 58 | uploader = CERNSearchRecordUploader(logger, self, self._url, |
|---|
| 59 | self._username, self._password) |
|---|
| 60 | |
|---|
| 61 | if self._v_logger: |
|---|
| 62 | self._v_logger.info('Starting metadata/upload cycle') |
|---|
| 63 | |
|---|
| 64 | # iterate over the returned records and upload them |
|---|
| 65 | return uploader.iterateOver(records) |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | class CERNSearchRecordUploader(RecordUploader): |
|---|
| 69 | """ |
|---|
| 70 | A worker that uploads data using HTTP |
|---|
| 71 | """ |
|---|
| 72 | |
|---|
| 73 | def __init__(self, logger, agent, url, username, password): |
|---|
| 74 | super(CERNSearchRecordUploader, self).__init__(logger, agent) |
|---|
| 75 | self._url = url |
|---|
| 76 | self._username = username |
|---|
| 77 | self._password = password |
|---|
| 78 | |
|---|
| 79 | def _postRequest(self, batch): |
|---|
| 80 | |
|---|
| 81 | pass |
|---|
| 82 | |
|---|
| 83 | def _uploadBatch(self, batch): |
|---|
| 84 | """ |
|---|
| 85 | Uploads a batch to the server |
|---|
| 86 | """ |
|---|
| 87 | |
|---|
| 88 | url = "%s/ImportXML" % self._url |
|---|
| 89 | |
|---|
| 90 | self._logger.debug('getting a batch') |
|---|
| 91 | |
|---|
| 92 | tstart = time.time() |
|---|
| 93 | # get a batch |
|---|
| 94 | |
|---|
| 95 | self._logger.info('Generating metadata') |
|---|
| 96 | data = self._agent._getMetadata(batch) |
|---|
| 97 | self._logger.info('Metadata ready ') |
|---|
| 98 | |
|---|
| 99 | postData = { |
|---|
| 100 | 'xml': data |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | tgen = time.time() - tstart |
|---|
| 104 | |
|---|
| 105 | req = Request(url) |
|---|
| 106 | # remove line break |
|---|
| 107 | cred = base64.encodestring( |
|---|
| 108 | '%s:%s' % (self._username, self._password)).strip() |
|---|
| 109 | |
|---|
| 110 | req.add_header("Authorization", "Basic %s" % cred) |
|---|
| 111 | |
|---|
| 112 | try: |
|---|
| 113 | result = urlopen(req, data=urlencode(postData)) |
|---|
| 114 | except HTTPError, e: |
|---|
| 115 | self._logger.exception("Status %s: \n %s" % (e.code, e.read())) |
|---|
| 116 | raise Exception('upload failed') |
|---|
| 117 | |
|---|
| 118 | result_data = result.read() |
|---|
| 119 | |
|---|
| 120 | tupload = time.time() - (tstart + tgen) |
|---|
| 121 | |
|---|
| 122 | self._logger.debug('rec %s result: %s' % (batch, result_data)) |
|---|
| 123 | |
|---|
| 124 | xmlDoc = etree.fromstring(result_data) |
|---|
| 125 | |
|---|
| 126 | # right now there is nothing else to pay attention to |
|---|
| 127 | booleanResult = etree.tostring(xmlDoc, method="text") |
|---|
| 128 | |
|---|
| 129 | if result.code == 200 and booleanResult == 'true': |
|---|
| 130 | self._logger.info('Batch of %d records stored in server' |
|---|
| 131 | ' [%f s %f s]' % \ |
|---|
| 132 | (len(batch), tgen, tupload)) |
|---|
| 133 | else: |
|---|
| 134 | self._logger.error('Records: %s output: %s ' |
|---|
| 135 | '(HTTP code %s)' % (batch, result_data, result.code)) |
|---|
| 136 | raise Exception('upload failed') |
|---|
| 137 | |
|---|
| 138 | return True |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | class CERNSearchAgentProviderComponent(AgentProviderComponent): |
|---|
| 142 | _agentType = CERNSearchUploadAgent |
|---|