]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/porterbox/files/schroot-list-sessions
ferm: change ferm.conf to a template
[dsa-puppet.git] / modules / porterbox / files / schroot-list-sessions
1 #!/usr/bin/python
2
3 ##
4 ## THIS FILE IS UNDER PUPPET CONTROL. DON'T EDIT IT HERE.
5 ## USE: git clone git+ssh://$USER@puppet.debian.org/srv/puppet.debian.org/git/dsa-puppet.git
6 ##
7
8 # Copyright (c) 2013 Peter Palfrader <peter@palfrader.org>
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
17 #
18 # The above copyright notice and this permission notice shall be
19 # included in all copies or substantial portions of the Software.
20 #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 # List the schroot sessions that a user has access to.
30
31 import ConfigParser
32 import datetime
33 import fnmatch
34 import getpass
35 import optparse
36 import os
37 import sys
38
39 def die(s):
40     print >> sys.stderr, s
41     sys.exit(1)
42
43 SESSION_PATH = '/var/lib/schroot/session'
44
45 class Session():
46     def __init__(self, name):
47         self.name = name
48         self.path = os.path.join(SESSION_PATH, self.name)
49
50         self.mtime = os.path.getmtime(self.path)
51         self.__read_session_config()
52         self.__set_session_owner()
53
54     def __read_session_config(self):
55         self.config = ConfigParser.RawConfigParser()
56         self.config.read(self.path)
57
58     def __set_session_owner(self):
59         owner = []
60         try:
61             owner.append(self.config.get(self.name, 'users'))
62             owner.append(self.config.get(self.name, 'root-users'))
63         except ConfigParser.NoSectionError:
64             die("Did not find session definition in session file.")
65         except ConfigParser.NoOptionError:
66             die("Did not find user information in session file.")
67         self.owners = filter(lambda a: a!="", owner)
68
69     def __str__(self, pretty=False):
70         if pretty:
71             ts = datetime.datetime.fromtimestamp(self.mtime).isoformat().replace(':', '')
72         else:
73             ts = '%d'%(self.mtime,)
74
75         return ':'.join([self.name, ','.join(self.owners), ts])
76
77     def pretty_str(self):
78         return self.__str__(pretty = True)
79
80     def ownermatch(self, users):
81         for gl in users:
82             for o in self.owners:
83                 if fnmatch.fnmatch(o, gl): return True
84         return False
85
86
87 parser = optparse.OptionParser()
88 parser.set_usage("""%prog [user]""")
89 parser.add_option("", "--mr", dest="mr", action="store_true", default=False,
90   help="print some data in a format better for post-processing (e.g. timestamps).",)
91 (options, args) = parser.parse_args()
92
93 if len(args) < 1:
94     users = [getpass.getuser()]
95 else:
96     users = args
97
98 for name in os.listdir(SESSION_PATH):
99     s = Session(name)
100     if s.ownermatch(users):
101         if options.mr:
102             print s
103         else:
104             print s.pretty_str()