spelling
[plstackapi.git] / plstackapi / importer / plclassic / importer.py
1 import os
2 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "plstackapi.planetstack.settings")
3 import sys
4 from optparse import OptionParser
5 from getpass import getpass
6 import xmlrpclib
7 from plstackapi.importer.plclassic.site_importer import SiteImporter
8 from plstackapi.importer.plclassic.user_importer import UserImporter
9 from plstackapi.importer.plclassic.slice_importer import SliceImporter
10 from plstackapi.importer.plclassic.sliver_importer import SliverImporter
11
12
13 class Call:
14     def __init__(self, callable, auth):
15         self.callable = callable
16         self.auth = auth
17
18     def __call__(self, *args, **kwds):
19         a = [self.auth] + list(args)
20         return self.callable(*a)
21
22 class API():
23     def __init__(self, username, password, url):
24         self.auth = {'AuthMethod': 'password',
25                      'Username': username,
26                      'AuthString': password}
27         self.server = xmlrpclib.ServerProxy(url, allow_none=True)
28
29     def __getattr__(self, name):         
30         return Call(getattr(self.server, name), self.auth) 
31
32 class Importer: 
33
34     def __init__(self, username, password, url):
35         api = API(username, password, url)
36         self.sites = SiteImporter(api)
37         self.slices = SliceImporter(api)
38         self.users = UserImporter(api)
39         self.slivers = SliverImporter(api)
40
41     def run(self):
42         self.sites.run()
43         self.users.run()
44         self.slices.run(remote_sites=self.sites.remote_sites, 
45                         local_sites=self.sites.local_sites)
46         self.slivers.run()           
47
48
49
50 if __name__ == '__main__':
51     parser = OptionParser()
52         
53     parser.add_option("-u", "--username", dest="username",
54                         help="PLC username with which to authenticate")
55     parser.add_option("", "--url", dest="url",
56                         help="PLC url to contact")
57
58     (config, args) = parser.parse_args()
59     if len(sys.argv) == 1:
60         parser.print_help()
61         sys.exit(1)
62
63     password = None
64     try:
65         password = getpass()
66     except (EOFError, KeyboardInterrupt):
67         print
68         sys.exit(0)
69
70     Importer(config.username, password, config.url).run()