cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
config.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # @cond LICENSE
4 #
5 # Copyright [2010-2013] Institute of Nuclear Physics PAN, Krakow, Poland
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # @endcond LICENSE
20 ##
21 # @package src.common.tools.config
22 # @author Maciej Nabozny <mn@mnabozny.pl>
23 #
24 import shutil
25 import os
26 
27 
28 ##
29 #
30 # Updates specific key-value pair in config file. It reads config.py file
31 # and searches for that key-value pair. If found, it's replaced with new
32 # value.
33 #
34 # @parameter{module,string}
35 # @parameter{key,string}
36 # @parameter{value,string}
37 #
38 def update(module, key, value):
39  try:
40  config = open('/etc/cc1/%s/config.py' % module).readlines()
41  except:
42  raise Exception('config_open')
43 
44  new_config = []
45 
46  for line in config:
47  if line.startswith(key):
48  if type(value) == type(""):
49  new_config.append('%s="%s"\n' % (key, value))
50  else:
51  new_config.append('%s=%d\n' % (key, value))
52  else:
53  new_config.append(line)
54 
55  config = open('/etc/cc1/%s/config.py' % module, 'w')
56  config.write(''.join(new_config))
57  config.close()
58 
59 
60 ##
61 #
62 # @parameter{config,module} Python module, where configuration constants are stored
63 #
64 def options(config):
65  return filter(lambda option: option[0:2] != "__", dir(config))
66 
67 
68 ##
69 #
70 # @parameter{module,string} module name
71 #
72 def check(module):
73  try:
74  config = __import__('config')
75  except:
76  config = __import__('%s.config' % module, fromlist=[module.lower()])
77 
78  config_example = __import__('%s.config_example' % module, fromlist=[module.lower()])
79  defaults = __import__('%s.defaults' % module, fromlist=[module.lower()])
80 
81  missing = 0
82  default = 0
83  unused = 0
84  # invalid = 0 # possible future use
85 
86  for option_name in options(config_example):
87  value = None
88  try:
89  value = getattr(config, option_name)
90  except AttributeError:
91  try:
92  value = getattr(defaults, option_name)
93  default += 1
94  # Print to force logs appear in WSGI log
95  print "[info] '%s' configuration option '%s' not present, using default: %s" % (module, option_name, str(value))
96  except AttributeError:
97  missing += 1
98  # Print to force logs appear in WSGI log
99  print "[error] '%s' configuration option '%s' missing!" % (module, option_name)
100  # if value is invalid, invalid += 1
101 
102  for option_name in options(config):
103  try:
104  getattr(config_example, option_name)
105  except AttributeError:
106  print "[warning] '%s' configuration option '%s' is not used by CC1 and maybe should be removed" % (module, option_name)
107  unused += 1
108 
109  if missing > 0: # if missing + invalid > 0:
110  raise Exception("'%s' configuration file is invalid. Please, edit your config.py file!." % module)
111  else:
112  print "[info] CC1 %s configuration is valid." % module
113 
114 
115 def configure(site):
116  shutil.copyfile('/usr/lib/cc1/%s/config_example.py' % site, '/etc/cc1/%s/config.example' % site)
117  if os.path.exists('/etc/cc1/%s/config.py' % site):
118  # Create symlink, if not exists
119  if not os.path.exists('/usr/lib/cc1/%s/config.py' % site):
120  os.symlink('/etc/cc1/%s/config.py' % site, '/usr/lib/cc1/%s/config.py' % site)
121 
122  print "WARNING: %s alredy configured" % site
123  return 0
124 
125  shutil.copyfile('/etc/cc1/%s/config.example' % site, '/etc/cc1/%s/config.py' % site)
126 
127  os.symlink('/etc/cc1/%s/config.py' % site, '/usr/lib/cc1/%s/config.py' % site)
128 
129  return 0
130 
131 
132 def remove(site):
133  if os.path.exists('/usr/lib/cc1/%s/config.py' % site):
134  os.unlink('/usr/lib/cc1/%s/config.py' % site)
135  return 0
136 
137 
138 def purge(site):
139  remove(site)
140  if os.path.exists('/etc/cc1/%s/config.py' % site):
141  os.remove('/etc/cc1/%s/config.py' % site)
142 
143  if os.path.exists('/etc/cc1/%s/config.example' % site):
144  os.remove('/etc/cc1/%s/config.example' % site)
145  return 0
146 
147