]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/porterbox/files/schroot-list-sessions
Only source /etc/schroot/default/config if it exists
[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 fnmatch
33 import getpass
34 import optparse
35 import os
36 import sys
37
38 def die(s):
39     print >> sys.stderr, s
40     sys.exit(1)
41
42 SESSION_PATH = '/var/lib/schroot/session'
43 def get_session_owner(session):
44     path = os.path.join(SESSION_PATH, session)
45     config = ConfigParser.RawConfigParser()
46     config.read(path)
47     owner = []
48     try:
49         owner.append(config.get(session, 'users'))
50         owner.append(config.get(session, 'root-users'))
51     except ConfigParser.NoSectionError:
52         die("Did not find session definition in session file.")
53     except ConfigParser.NoOptionError:
54         die("Did not find user information in session file.")
55     return filter(lambda a: a!="", owner)
56
57 def ownermatch(owners, users):
58     for gl in users:
59         for o in owners:
60             if fnmatch.fnmatch(o, gl): return True
61     return False
62
63 def list_schroots(users):
64     for session in os.listdir(SESSION_PATH):
65         owners = get_session_owner(session)
66         if ownermatch(owners, users):
67             print "%s:%s"%(session, ','.join(owners))
68
69 parser = optparse.OptionParser()
70 parser.set_usage("""%prog [user]""")
71 (options, args) = parser.parse_args()
72
73 if len(args) < 1:
74     users = [getpass.getuser()]
75 else:
76     users = args
77
78 list_schroots(users)