File: storagecopy.py

package info (click to toggle)
wfrog 0.8.2%2Bsvn973-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,176 kB
  • ctags: 1,354
  • sloc: python: 8,245; sh: 235; sql: 185; xml: 133; makefile: 37
file content (89 lines) | stat: -rw-r--r-- 2,894 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python

## Copyright 2009 Laurent Bovet <laurent.bovet@windmaster.ch>
##                Jordi Puigsegur <jordi.puigsegur@gmail.com>
##
##  This file is part of wfrog
##
##  wfrog is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Before loading other modules add wfrog directory to sys.path to be able to use wfcommon
import os.path
import sys
if __name__ == "__main__": sys.path.append(os.path.abspath(sys.path[0] + '/..'))

import wfcommon.storage
import optparse
import logging
import wfcommon.config

class StorageCopy(object):

    logger = logging.getLogger('storagecopy')

    def __init__(self, config_file=None):

        # Prepare the configurer
        module_map = (
            ( "Storages", wfcommon.storage)
        )

        if config_file is None:
            config_file = "config/storagecopy.yaml"

        configurer = wfcommon.config.Configurer(module_map)

        # Initialize the option parser
        opt_parser = optparse.OptionParser()
        configurer.add_options(opt_parser)

        # Parse the options and create object trees from configuration
        (options, args) = opt_parser.parse_args()

        (config, context) = configurer.configure(options, self, config_file)

        self.from_storage = config['from']
        self.to_storage = config['to']
        try:
            self.from_storage.init(context=context)
        except AttributeError:
            pass # In case the element has not init method

        try:
            self.to_storage.init(context=context)
        except AttributeError:
            pass # In case the element has not init method

    def run(self):

        keys = self.from_storage.keys()
        n = 0

        for sample in self.from_storage.samples():
            if n % 500 == 0: 
                self.logger.info("Processed %d samples" % n)
            sample_to_write = {}
            for i in range(len(keys)):
                sample_to_write[keys[i]] = sample[i]
            self.to_storage.write_sample(sample_to_write)
            n += 1

if __name__ == "__main__":
    driver = StorageCopy()
    driver.logger.debug("Started main()")
    try:
        driver.run()
    except:
        driver.logger.exception("An unexpected error has ocurred while copying storages:")
    driver.logger.debug("Finished main()")