traffic_replay: Split out random group membership generation logic
[amitay/samba.git] / python / samba / tdb_util.py
1 # Unix SMB/CIFS implementation.
2 # tdb util helpers
3 #
4 # Copyright (C) Kai Blin <kai@samba.org> 2011
5 # Copyright (C) Amitay Isaacs <amitay@gmail.com> 2011
6 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2013
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 import samba
22 import subprocess
23 import os
24
25
26 def tdb_copy(file1, file2, readonly=False):
27     """Copy tdb file using tdbbackup utility and rename it
28     """
29     # Find the location of tdbbackup tool
30     dirs = ["bin", samba.param.bin_dir()] + os.getenv('PATH').split(os.pathsep)
31     for d in dirs:
32         toolpath = os.path.join(d, "tdbbackup")
33         if os.path.exists(toolpath):
34             break
35
36     tdbbackup_cmd = [toolpath, "-s", ".copy.tdb", file1]
37     if readonly:
38         tdbbackup_cmd.append("-r")
39
40     status = subprocess.check_call(tdbbackup_cmd, close_fds=True, shell=False)
41
42     os.rename("%s.copy.tdb" % file1, file2)