OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import ftplib |
| 6 import gzip |
| 7 import hashlib |
| 8 import os |
| 9 import StringIO |
| 10 |
| 11 from autotest_lib.client.bin import factory |
| 12 from autotest_lib.client.bin import test, utils |
| 13 from autotest_lib.client.common_lib import error |
| 14 |
| 15 class factory_UploadLogs(test.test): |
| 16 version = 1 |
| 17 |
| 18 DEFAULT_DESTINATION_PARAM = { |
| 19 'method': 'ftp', |
| 20 'host': 'localhost', |
| 21 'port': ftplib.FTP.port, |
| 22 'user': 'anonymous', |
| 23 'passwd': '', |
| 24 'timeout': 3*60, |
| 25 'initdir': '', |
| 26 } |
| 27 |
| 28 USE_GZIP = True |
| 29 |
| 30 def prepare_source_object(self, text_source_filename): |
| 31 ''' prepares the source file object from logs ''' |
| 32 content = open(text_source_filename, 'r').read() |
| 33 hashval = hashlib.sha1(content).hexdigest() |
| 34 if self.USE_GZIP: |
| 35 buf = StringIO.StringIO() |
| 36 zbuf = gzip.GzipFile(fileobj=buf, mode='w') |
| 37 zbuf.write(content) |
| 38 zbuf.close() # force gzip flush to stringIO buffer |
| 39 else: |
| 40 buf.write(content) |
| 41 buf.seek(0) # prepare for further read |
| 42 return hashval, buf |
| 43 |
| 44 def do_upload_file(self, dest, fileobj): |
| 45 ''' uploads (via FTP) fileobj to dest ''' |
| 46 factory.log('upload destination: %s://%s:%s@%s:%s/%s' % |
| 47 (dest['method'], dest['user'], |
| 48 '*', |
| 49 #dest['password'], |
| 50 dest['host'], dest['port'], dest['filename'])) |
| 51 |
| 52 assert dest['method'] == 'ftp', "only FTP is supported now." |
| 53 ftp = ftplib.FTP() |
| 54 ftp.connect(host=dest['host'], port=dest['port'], |
| 55 timeout=dest['timeout']) |
| 56 ftp.login(user=dest['user'], passwd=dest['passwd']) |
| 57 ftp.storbinary('STOR %s' % dest['filename'], fileobj) |
| 58 ftp.quit() |
| 59 |
| 60 def run_once(self, destination): |
| 61 assert 'filename' not in destination, "file names must be generated" |
| 62 if destination['host'] == '*': |
| 63 factory.log('WARNING: FACTORY LOG UPLOADING IS BYPASSED.') |
| 64 return |
| 65 src_hash, src_obj = self.prepare_source_object(factory.LOG_PATH) |
| 66 dest = {} |
| 67 dest.update(DEFAULT_DESTINATION_PARAM) |
| 68 dest.update(destination) |
| 69 dest_name = src_hash + '.log' |
| 70 if self.USE_GZIP: |
| 71 dest_name = dest_name + '.gz' |
| 72 dest['filename'] = os.path.join(dest['initdir'], dest_name) |
| 73 self.do_upload_file(dest, src_obj) |
OLD | NEW |