summaryrefslogtreecommitdiffstats
path: root/cobbler/config.py
blob: 3444dede90e836c9f17ad5dd907cc935276e7a8c (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Config.py is a repository of the Cobbler object model

Copyright 2006, Red Hat, Inc
Michael DeHaan <mdehaan@redhat.com>

This software may be freely redistributed under the terms of the GNU
general public license.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""

import os
import weakref

import item_distro as distro
import item_profile as profile
import item_system as system
import item_repo as repo

import collection_distros as distros
import collection_profiles as profiles
import collection_systems as systems
import collection_repos as repos

import settings
import serializer

class Config:

   def __init__(self,api):
       """
       Constructor.  Manages a definitive copy of all data collections with weakrefs
       poiting back into the class so they can understand each other's contents
       """
       self.api           = api
       self._distros      = distros.Distros(weakref.proxy(self))
       self._profiles     = profiles.Profiles(weakref.proxy(self))
       self._systems      = systems.Systems(weakref.proxy(self))
       self._settings     = settings.Settings() # not a true collection
       self._repos        = repos.Repos(weakref.proxy(self))
       self._classes = [
          self._distros,
          self._profiles,
          self._systems,
          self._settings,
          self._repos
       ]
       self.file_check()

   def __cmp(self,a,b):
       return cmp(a.name,b.name)

   def distros(self):
       """
       Return the definitive copy of the Distros collection
       """
       return self._distros

   def profiles(self):
       """
       Return the definitive copy of the Profiles collection
       """
       return self._profiles

   def systems(self):
       """
       Return the definitive copy of the Systems collection
       """
       return self._systems

   def settings(self):
       """
       Return the definitive copy of the application settings
       """
       return self._settings

   def repos(self):
       """
       Return the definitive copy of the Repos collection
       """
       return self._repos

   def new_distro(self):
       """
       Create a new distro object with a backreference to this object
       """
       return distro.Distro(weakref.proxy(self))

   def new_system(self):
       """
       Create a new system with a backreference to this object
       """
       return system.System(weakref.proxy(self))

   def new_profile(self):
       """
       Create a new profile with a backreference to this object
       """
       return profile.Profile(weakref.proxy(self))

   def new_repo(self):
       """
       Create a new mirror to keep track of...
       """
       return repo.Repo(weakref.proxy(self))

   def clear(self):
       """
       Forget about all loaded configuration data
       """
       for x in self._classes:
          x.clear()
       return True

   def file_check(self):
       """
       Serialize any files that do not yet exist.  This is useful for bringing the
       app up to a working state on first run or if files are deleted.  See api.py
       """
       for x in self._classes:
          if not os.path.exists(x.filename()):
              if not serializer.serialize(x):
                  return False
       return True


   def serialize(self):
       """
       Save the object hierarchy to disk, using the filenames referenced in each object.
       """
       for x in self._classes:
          if not serializer.serialize(x):
              return False
       return True

   def deserialize(self):
       """
       Load the object hierachy from disk, using the filenames referenced in each object.
       """
       for x in self._classes:
          if not serializer.deserialize(x):
              return False
       return True