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