python: Add convenience function for getting command line loadparm context
[ab/samba.git/.git] / source4 / scripting / bin / winreg.py
1 #!/usr/bin/python
2 #
3 #  tool to manipulate a remote registry
4 #  Copyright Andrew Tridgell 2005
5 #  Copyright Jelmer Vernooij 2007
6 #  Released under the GNU GPL v3 or later
7 #
8
9 import sys
10 import winreg
11 import optparse
12 import samba.getopt as options
13
14 parser = optparse.OptionParser("%s <BINDING> [path]" % sys.argv[0])
15 sambaopts = options.SambaOptions(parser)
16 parser.add_option_group(sambaopts)
17 parser.add_option("--createkey", type="string", metavar="KEYNAME", 
18         help="create a key")
19
20 opts, args = parser.parse_args()
21
22 if len(args) < 1:
23     parser.print_usage()
24     sys.exit(-1)
25
26 binding = args[0]
27
28 print "Connecting to " + binding
29 conn = winreg.winreg(binding, sambaopts.get_loadparm())
30
31 def list_values(key):
32     (num_values, max_valnamelen, max_valbufsize) = conn.QueryInfoKey(key, winreg.String())[4:8]
33     for i in range(num_values):
34         name = winreg.StringBuf()
35         name.size = max_valnamelen
36         (name, type, data, _, data_len) = conn.EnumValue(key, i, name, 0, "", max_valbufsize, 0)
37         print "\ttype=%-30s size=%4d  '%s'" % type, len, name
38         if type in (winreg.REG_SZ, winreg.REG_EXPAND_SZ):
39             print "\t\t'%s'" % data
40 #        if (v.type == reg.REG_MULTI_SZ) {
41 #            for (j in v.value) {
42 #                printf("\t\t'%s'\n", v.value[j])
43 #            }
44 #        }
45 #        if (v.type == reg.REG_DWORD || v.type == reg.REG_DWORD_BIG_ENDIAN) {
46 #            printf("\t\t0x%08x (%d)\n", v.value, v.value)
47 #        }
48 #        if (v.type == reg.REG_QWORD) {
49 #            printf("\t\t0x%llx (%lld)\n", v.value, v.value)
50 #        }
51
52 def list_path(key, path):
53     count = 0
54     (num_subkeys, max_subkeylen, max_subkeysize) = conn.QueryInfoKey(key, winreg.String())[1:4]
55     for i in range(num_subkeys):
56         name = winreg.StringBuf()
57         name.size = max_subkeysize
58         keyclass = winreg.StringBuf()
59         keyclass.size = max_subkeysize
60         (name, _, _) = conn.EnumKey(key, i, name, keyclass=keyclass, last_changed_time=None)[0]
61         subkey = conn.OpenKey(key, name, 0, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS)
62         count += list_path(subkey, "%s\\%s" % (path, name))
63         list_values(subkey)
64     return count
65
66 if len(args) > 1:
67     root = args[1]
68 else:
69     root = "HKLM"
70
71 if opts.createkey:
72     reg.create_key("HKLM\\SOFTWARE", opt.createkey)
73 else:
74     print "Listing registry tree '%s'" % root
75     try:
76         root_key = getattr(conn, "Open%s" % root)(None, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS)
77     except AttributeError:
78         print "Unknown root key name %s" % root
79         sys.exit(1)
80     count = list_path(root_key, root)
81     if count == 0:
82         print "No entries found"
83         sys.exit(1)