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