Fix bad indents
[platform/upstream/mic.git] / mic / pluginbase.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright (c) 2011 Intel, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 import os
19 import shutil
20 from mic import msger
21 from mic.utils import errors
22
23 class _Plugin(object):
24     class __metaclass__(type):
25         def __init__(cls, name, bases, attrs):
26             if not hasattr(cls, 'plugins'):
27                 cls.plugins = {}
28
29             elif 'mic_plugin_type' in attrs:
30                 if attrs['mic_plugin_type'] not in cls.plugins:
31                     cls.plugins[attrs['mic_plugin_type']] = {}
32
33             elif hasattr(cls, 'mic_plugin_type') and 'name' in attrs:
34                 cls.plugins[cls.mic_plugin_type][attrs['name']] = cls
35
36         def show_plugins(cls):
37             for cls in cls.plugins[cls.mic_plugin_type]:
38                 print cls
39
40         def get_plugins(cls):
41             return cls.plugins
42
43 class ImagerPlugin(_Plugin):
44     mic_plugin_type = "imager"
45
46     @classmethod
47     def check_image_exists(self, destdir, apacking=None,
48                                           images=[],
49                                           release=None):
50
51         # if it's a packing file, reset images
52         if apacking:
53             images = [apacking]
54
55         # release option will override images
56         if release is not None:
57             images = [os.path.basename(destdir.rstrip('/'))]
58             destdir = os.path.dirname(destdir.rstrip('/'))
59
60         for name in images:
61             if not name:
62                 continue
63
64             image = os.path.join(destdir, name)
65             if not os.path.exists(image):
66                 continue
67
68             if msger.ask("Target image/dir: %s already exists, "
69                          "clean up and continue?" % image):
70                 if os.path.isdir(image):
71                     shutil.rmtree(image)
72                 else:
73                     os.unlink(image)
74             else:
75                 raise errors.Abort("Cancled")
76
77     def do_create(self):
78         pass
79
80     def do_chroot(self):
81         pass
82
83 class BackendPlugin(_Plugin):
84     mic_plugin_type="backend"
85
86     # suppress the verbose rpm warnings
87     if msger.get_loglevel() != 'debug':
88         import rpm
89         rpm.setVerbosity(rpm.RPMLOG_ERR)
90
91     def addRepository(self):
92         pass
93
94 def get_plugins(typen):
95     ps = ImagerPlugin.get_plugins()
96     if typen in ps:
97         return ps[typen]
98     else:
99         return None
100
101 __all__ = ['ImagerPlugin', 'BackendPlugin', 'get_plugins']