1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/copy_store.py Thu Apr 21 23:20:57 2016 +0200
1.3 @@ -0,0 +1,169 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Copy store information into another store.
1.8 +
1.9 +Copyright (C) 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from os.path import abspath, split
1.26 +import sys
1.27 +
1.28 +# Find the modules.
1.29 +
1.30 +try:
1.31 + import imiptools
1.32 +except ImportError:
1.33 + parent = abspath(split(split(__file__)[0])[0])
1.34 + if split(parent)[1] == "imip-agent":
1.35 + sys.path.append(parent)
1.36 +
1.37 +from imiptools import config
1.38 +from imiptools.data import Object
1.39 +from imiptools.stores import get_store, get_publisher, get_journal
1.40 +
1.41 +def copy_store(from_store, from_journal, to_store, to_journal):
1.42 +
1.43 + """
1.44 + Copy stored information from the specified 'from_store' and 'from_journal'
1.45 + to the specified 'to_store' and 'to_journal' respectively.
1.46 + """
1.47 +
1.48 + # For each user...
1.49 +
1.50 + for user in from_store.get_users():
1.51 +
1.52 + # Copy requests.
1.53 +
1.54 + to_store.set_requests(user, from_store.get_requests(user))
1.55 +
1.56 + # Copy events, both active and cancellations.
1.57 +
1.58 + for dirname in (None, "cancellations"):
1.59 +
1.60 + # Get event, recurrence information.
1.61 +
1.62 + for uid, recurrenceid in from_store.get_all_events(user, dirname=dirname):
1.63 + d = from_store.get_event(user, uid, recurrenceid, dirname=dirname)
1.64 + if d:
1.65 + to_store.set_event(user, uid, recurrenceid, Object(d).to_node())
1.66 + if dirname == "cancellations":
1.67 + to_store.cancel_event(user, uid, recurrenceid)
1.68 + else:
1.69 + print >>sys.stderr, "Event for %s with UID %s and RECURRENCE-ID %s not found in %s" % (
1.70 + (user, uid, recurrenceid or "null", dirname or "active events"))
1.71 +
1.72 + # Copy counter-proposals.
1.73 +
1.74 + if dirname is None:
1.75 + for other in from_store.get_counters(user, uid, recurrenceid):
1.76 + d = from_store.get_counter(user, other, uid, recurrenceid)
1.77 + if d:
1.78 + to_store.set_counter(user, other, Object(d).to_node(), uid, recurrenceid)
1.79 + else:
1.80 + print >>sys.stderr, "Counter-proposal for %s with UID %s and RECURRENCE-ID %s not found in %s" % (
1.81 + (user, uid, recurrenceid or "null", dirname or "active events"))
1.82 +
1.83 + # Copy free/busy information for the user.
1.84 +
1.85 + to_store.set_freebusy(user, from_store.get_freebusy(user))
1.86 +
1.87 + # Copy free/busy information for other users.
1.88 +
1.89 + for other in from_store.get_freebusy_others(user):
1.90 + to_store.set_freebusy_for_other(user, from_store.get_freebusy_for_other(user, other), other)
1.91 +
1.92 + # Copy free/busy offers.
1.93 +
1.94 + to_store.set_freebusy_offers(user, from_store.get_freebusy_offers(user))
1.95 +
1.96 + # For each quota group...
1.97 +
1.98 + for quota in from_journal.get_quotas():
1.99 +
1.100 + # Copy quota limits.
1.101 +
1.102 + for user_group, limit in from_journal.get_limits(quota).items():
1.103 + to_journal.set_limit(quota, user_group, limit)
1.104 +
1.105 + # Copy group mappings.
1.106 +
1.107 + for store_user, user_group in from_journal.get_groups(quota).items():
1.108 + to_journal.set_group(quota, store_user, user_group)
1.109 +
1.110 + # Copy journal details.
1.111 +
1.112 + for group in from_journal.get_quota_users(quota):
1.113 + to_journal.set_entries(quota, group, from_journal.get_entries(quota, group))
1.114 +
1.115 + # Copy individual free/busy details.
1.116 +
1.117 + for store_user in from_journal.get_freebusy_users(quota):
1.118 + to_journal.set_freebusy(store_user, from_journal.get_freebusy(store_user))
1.119 +
1.120 +# Main program.
1.121 +
1.122 +if __name__ == "__main__":
1.123 +
1.124 + # Interpret the command line arguments.
1.125 +
1.126 + from_store_args = []
1.127 + to_store_args = []
1.128 + l = ignored = []
1.129 +
1.130 + for arg in sys.argv[1:]:
1.131 + if arg in ("-t", "--to"):
1.132 + l = to_store_args
1.133 + elif arg in ("-f", "--from"):
1.134 + l = from_store_args
1.135 + else:
1.136 + l.append(arg)
1.137 +
1.138 + if len(from_store_args) not in (0, 3) or len(to_store_args) != 3:
1.139 + print >>sys.stderr, """\
1.140 +Usage: %s \\
1.141 + [ ( -f | --from ) <store type> <store directory> <journal directory> ] \\
1.142 + ( -t | --to ) <store type> <store directory> <journal directory>
1.143 +
1.144 +Need details of a destination store indicated by the -t or --to option.
1.145 +In addition, details of a source store may be indicated by the -f or --from
1.146 +option; otherwise, the currently-configured store is used.
1.147 +""" % split(sys.argv[0])[1]
1.148 + sys.exit(1)
1.149 +
1.150 + # Override defaults if indicated.
1.151 +
1.152 + getvalue = lambda value, pos=0, default=None: value and value[pos] or default
1.153 +
1.154 + from_store_type = getvalue(from_store_args, 0, config.STORE_TYPE)
1.155 + from_store_dir = getvalue(from_store_args, 1)
1.156 + from_journal_dir = getvalue(from_store_args, 2)
1.157 +
1.158 + to_store_type, to_store_dir, to_journal_dir = to_store_args
1.159 +
1.160 + # Obtain store-related objects.
1.161 +
1.162 + from_store = get_store(from_store_type, from_store_dir)
1.163 + from_journal = get_journal(from_store_type, from_journal_dir)
1.164 +
1.165 + to_store = get_store(to_store_type, to_store_dir)
1.166 + to_journal = get_journal(to_store_type, to_journal_dir)
1.167 +
1.168 + # Process the store.
1.169 +
1.170 + copy_store(from_store, from_journal, to_store, to_journal)
1.171 +
1.172 +# vim: tabstop=4 expandtab shiftwidth=4