PyLucene 3.4.0-1 import
[pylucene.git] / jcc / helpers / windows.py
1 #   Licensed under the Apache License, Version 2.0 (the "License");
2 #   you may not use this file except in compliance with the License.
3 #   You may obtain a copy of the License at
4 #
5 #       http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #   Unless required by applicable law or agreed to in writing, software
8 #   distributed under the License is distributed on an "AS IS" BASIS,
9 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 #   See the License for the specific language governing permissions and
11 #   limitations under the License.
12
13 import sys
14
15 global JAVAHOME
16 JAVAHOME = None
17
18 if sys.platform == "win32":
19     # figure out where the JDK lives
20
21     try:
22         import _winreg as wreg
23
24         class WindowsRegistry:
25             # see the Python Cookbook, #146305, Dirk Holtwick
26
27             def __init__(self, keyname):
28                 " handle registry access "
29                 self.reg = wreg.ConnectRegistry(None, wreg.HKEY_LOCAL_MACHINE)
30                 self.key = wreg.OpenKey(self.reg, keyname)
31
32             def get(self, name):
33                 " get value out of registry "
34                 v, t = wreg.QueryValueEx(self.key, name)
35                 return v, t
36
37             def close(self):
38                 " close the key finally "
39                 if hasattr(self, 'key'):
40                     self.key.Close()
41                 if hasattr(self, 'reg'):
42                     self.reg.Close()
43
44             def __del__(self):
45                 self.close()
46
47         def get_registry_value(vname, subname):
48             r = WindowsRegistry(vname)
49             v, t = r.get(subname)
50             return v
51
52         javaversion = get_registry_value(r"SOFTWARE\JavaSoft\Java Development Kit", "CurrentVersion")
53         JAVAHOME = get_registry_value(r"SOFTWARE\JavaSoft\Java Development Kit\%s" % javaversion, "JavaHome")
54
55     except:
56         JAVAHOME = 'c:/Program Files/Java/jdk1.6.0_18'