uptodateness: add new module and migrate functions from visualize
[metze/samba/wip.git] / python / samba / uptodateness.py
1 # Uptodateness utils
2 #
3 # Copyright (C) Andrew Bartlett 2015, 2018
4 # Copyright (C) Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
5 # Copyright (C) Joe Guo <joeg@catalyst.net.nz>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from samba.netcmd import CommandError
21
22
23 def get_partition_maps(samdb):
24     """Generate dictionaries mapping short partition names to the
25     appropriate DNs."""
26     base_dn = samdb.domain_dn()
27     short_to_long = {
28         "DOMAIN": base_dn,
29         "CONFIGURATION": str(samdb.get_config_basedn()),
30         "SCHEMA": "CN=Schema,%s" % samdb.get_config_basedn(),
31         "DNSDOMAIN": "DC=DomainDnsZones,%s" % base_dn,
32         "DNSFOREST": "DC=ForestDnsZones,%s" % base_dn
33     }
34
35     long_to_short = {}
36     for s, l in short_to_long.items():
37         long_to_short[l] = s
38
39     return short_to_long, long_to_short
40
41
42 def get_partition(samdb, part):
43     # Allow people to say "--partition=DOMAIN" rather than
44     # "--partition=DC=blah,DC=..."
45     if part is not None:
46         short_partitions, long_partitions = get_partition_maps(samdb)
47         part = short_partitions.get(part.upper(), part)
48         if part not in long_partitions:
49             raise CommandError("unknown partition %s" % part)
50     return part