125654e4a5f233c0d9348e63429c0cbaac8775e8
[plcapi.git] / PLC / Methods / GetPlcRelease.py
1 # $Id$
2 from PLC.Method import Method
3 from PLC.Auth import Auth
4 from PLC.Faults import *
5
6 import re
7
8 comment_regexp = '\A\s*#.|\A\s*\Z|\Axxxxx' 
9
10 regexps = { 'build'   : '\A[bB]uild\s+(?P<key>[^:]+)\s*:\s*(?P<value>.*)\Z',
11             'tags'    : '\A(?P<key>[^:]+)\s*:=\s*(?P<value>.*)\Z',
12 # spaces not part of key : ungreedy
13             'rpms'    : '\A(?P<key>[^:]+?)\s*::\s*(?P<value>.*)\Z',
14 }
15
16 class GetPlcRelease(Method):
17     """
18     Returns various information about the current myplc installation.
19     """
20
21     roles = ['admin', 'pi', 'user', 'tech', 'node', 'anonymous']
22
23     accepts = [
24         Auth(),
25         ]
26
27     # for now only return /etc/myplc-release verbatim
28     returns = { 'build' : 'information about the build',
29                 'tags' : 'describes the codebase location and tags used for building',
30                 'rpms' : 'details the rpm installed in the myplc chroot jail' }
31
32     def call(self, auth):
33
34         comment_matcher = re.compile(comment_regexp)
35
36         matchers = {}
37         result = {} 
38         for field in regexps.keys():
39             matchers[field] = re.compile(regexps[field])
40             result[field]={}
41
42         try:
43             release = open('/etc/myplc-release')
44             for line in release.readlines():
45                 line=line.strip()
46                 if comment_matcher.match(line):
47                     continue
48                 for field in regexps.keys():
49                     m=matchers[field].match(line)
50                     if m:
51                         (key,value)=m.groups(['key','value'])
52                         result[field][key]=value
53                         break
54                 else:
55                     if not result.has_key('unexpected'):
56                         result['unexpected']=""
57                     result['unexpected'] += (line+"\n")
58         except:
59             raise PLCNotImplemented, 'Cannot open /etc/myplc-release'
60         return result