3 # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 from daklib.config import Config
23 from daklib.dbconn import DBConn, Fingerprint, Uid, ACL
27 dak acl set-fingerprints <acl-name>
28 dak acl export-per-source <acl-name>
31 Reads list of fingerprints from stdin and sets the ACL <acl-name> to these.
32 Accepted input formats are "uid:<uid>", "name:<name>" and
36 Export per source upload rights for ACL <acl-name>.
39 def get_fingerprint(entry, session):
40 """get fingerprint for given ACL entry
42 The entry is a string in one of these formats::
49 @param entry: ACL entry
51 @param session: database session
53 @rtype: L{daklib.dbconn.Fingerprint} or C{None}
54 @return: fingerprint for the entry
56 field, value = entry.split(":", 1)
57 q = session.query(Fingerprint)
60 q = q.join(Fingerprint.uid).filter(Uid.uid == value)
62 q = q.join(Fingerprint.uid).filter(Uid.name == value)
64 q = q.filter(Fingerprint.fingerprint == value)
68 def acl_set_fingerprints(acl_name, entries):
69 session = DBConn().session()
70 acl = session.query(ACL).filter_by(name=acl_name).one()
72 acl.fingerprints.clear()
75 fps = get_fingerprint(entry, session)
77 print "Unknown key for '{0}'".format(entry)
79 acl.fingerprints.update(fps)
83 def acl_export_per_source(acl_name):
84 session = DBConn().session()
85 acl = session.query(ACL).filter_by(name=acl_name).one()
90 (SELECT COALESCE(u.name, '') || ' <' || u.uid || '>'
92 JOIN fingerprint f2 ON u.id = f2.uid
93 WHERE f2.id = f.id) AS name,
94 STRING_AGG(a.source, ' ' ORDER BY a.source)
96 JOIN fingerprint f ON a.fingerprint_id = f.id
97 LEFT JOIN uid u ON f.uid = u.id
98 WHERE a.acl_id = :acl_id
99 GROUP BY f.id, f.fingerprint
103 for row in session.execute(query, {'acl_id': acl.id}):
104 print "Fingerprint:", row[0]
106 print "Allow:", row[2]
120 if argv[1] == 'set-fingerprints':
121 acl_set_fingerprints(argv[2], sys.stdin)
122 elif argv[1] == 'export-per-source':
123 acl_export_per_source(argv[2])