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