#!/usr/bin/python ## ## THIS FILE IS UNDER PUPPET CONTROL. DON'T EDIT IT HERE. ## USE: git clone git+ssh://$USER@puppet.debian.org/srv/puppet.debian.org/git/dsa-puppet.git ## # Copyright (c) 2013 Peter Palfrader # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # List the schroot sessions that a user has access to. import ConfigParser import datetime import fnmatch import getpass import optparse import os import sys def die(s): print >> sys.stderr, s sys.exit(1) SESSION_PATH = '/var/lib/schroot/session' class Session(): def __init__(self, name): self.name = name self.path = os.path.join(SESSION_PATH, self.name) self.mtime = os.path.getmtime(self.path) self.__read_session_config() self.__set_session_owner() def __read_session_config(self): self.config = ConfigParser.RawConfigParser() self.config.read(self.path) def __set_session_owner(self): owner = [] try: owner.append(self.config.get(self.name, 'users')) owner.append(self.config.get(self.name, 'root-users')) except ConfigParser.NoSectionError: die("Did not find session definition in session file.") except ConfigParser.NoOptionError: die("Did not find user information in session file.") self.owners = filter(lambda a: a!="", owner) def __str__(self, pretty=False): if pretty: ts = datetime.datetime.fromtimestamp(self.mtime).isoformat().replace(':', '') else: ts = '%d'%(self.mtime,) return ':'.join([self.name, ','.join(self.owners), ts]) def pretty_str(self): return self.__str__(pretty = True) def ownermatch(self, users): for gl in users: for o in self.owners: if fnmatch.fnmatch(o, gl): return True return False parser = optparse.OptionParser() parser.set_usage("""%prog [user]""") parser.add_option("", "--mr", dest="mr", action="store_true", default=False, help="print some data in a format better for post-processing (e.g. timestamps).",) (options, args) = parser.parse_args() if len(args) < 1: users = [getpass.getuser()] else: users = args for name in os.listdir(SESSION_PATH): s = Session(name) if s.ownermatch(users): if options.mr: print s else: print s.pretty_str()