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