88import java .util .HashMap ;
99import java .util .List ;
1010import java .util .Map ;
11- import java .util .Set ;
11+
12+ import org .eclipse .cdt .utils .WindowsRegistry ;
1213
1314import com .espressif .idf .core .logging .Logger ;
1415
1516/**
16- * Python Windows registry Wrapper
17+ * Python Windows registry Wrapper.
18+ *
19+ * <br>
20+ * Search for Python versions and identify the install location in the windows registry on the following locations: <br>
21+ *
22+ * HKEY_CURRENT_USER/SOFTWARE/Python/PythonCore>/<version>/InstallPath/ExecutablePath <br>
23+ * HKEY_LOCAL_MACHINE/SOFTWARE/Python/PythonCore>/<version>/InstallPath/ExecutablePath <br>
1724 *
1825 * @author Kondal Kolipaka <[email protected] > 1926 *
@@ -25,87 +32,50 @@ public class PyWinRegistryReader
2532 private static String PY_INSTALL_PATH = "InstallPath" ; //$NON-NLS-1$
2633 private static String PY_EXE_PATH = "ExecutablePath" ; //$NON-NLS-1$
2734
28- protected List <Integer > getHKeyRootNodes ()
29- {
30- List <Integer > rootNodes = new ArrayList <Integer >();
31- rootNodes .add (WinRegistry .HKEY_CURRENT_USER );
32- rootNodes .add (WinRegistry .HKEY_LOCAL_MACHINE );
33-
34- return rootNodes ;
35- }
36-
3735 /**
3836 * @return
3937 */
4038 public Map <String , String > getPythonVersions ()
4139 {
4240 Map <String , String > pythonRegistryMap = new HashMap <String , String >();
4341
44- List <Integer > hKeyRootNodes = getHKeyRootNodes ();
45- for (Integer rootNode : hKeyRootNodes )
42+ WindowsRegistry registry = WindowsRegistry .getRegistry ();
43+ List <String > py_version_list = new ArrayList <String >();
44+ String version ;
45+
46+ // HKEY_CURRENT_USER
47+ for (int i = 0 ; (version = registry .getCurrentUserKeyName (PY_REG_PATH , i )) != null ; i ++)
4648 {
47- List <String > py_version_list = null ;
48- try
49- {
50- py_version_list = WinRegistry .subKeysForPath (rootNode , PY_REG_PATH );
51- }
52- catch (Exception e )
53- {
54- Logger .log (e .getMessage ());
55- }
56- if (py_version_list == null )
49+ py_version_list .add (version );
50+ String compKey = PY_REG_PATH + '\\' + version + '\\' + PY_INSTALL_PATH ; // $NON-NLS-1$ //$NON-NLS-2$
51+ Logger .log (compKey );
52+
53+ String installLocation = registry .getCurrentUserValue (compKey , PY_EXE_PATH );
54+ Logger .log (installLocation );
55+ if (installLocation != null )
5756 {
58- return pythonRegistryMap ;
57+ pythonRegistryMap . put ( version , installLocation ) ;
5958 }
6059
61- Logger .log (py_version_list .toString ());
62- for (String version : py_version_list )
63- {
64- String pyRegistryPath = PY_REG_PATH + "\\ " + version + "\\ " + PY_INSTALL_PATH ;
65- Logger .log (pyRegistryPath );
66- List <String > py_exe_path_list = null ;
67- try
68- {
69- py_exe_path_list = WinRegistry .valuesForKeyPath (rootNode , pyRegistryPath , PY_EXE_PATH );
70- }
71- catch (Exception e )
72- {
73- Logger .log (e .getMessage ());
74- }
75- if (py_exe_path_list == null )
76- {
77- return pythonRegistryMap ;
78- }
60+ }
61+ // HKEY_LOCAL_MACHINE
62+ for (int i = 0 ; (version = registry .getLocalMachineKeyName (PY_REG_PATH , i )) != null ; i ++)
63+ {
64+ py_version_list .add (version );
65+ String compKey = PY_REG_PATH + '\\' + version + '\\' + PY_INSTALL_PATH ; // $NON-NLS-1$ //$NON-NLS-2$
66+ Logger .log (compKey );
7967
80- Logger .log (py_exe_path_list .toString ());
81- if (!py_exe_path_list .isEmpty ())
82- {
83- String pyExePath = py_exe_path_list .get (0 );
84- pythonRegistryMap .put (version , pyExePath );
85- }
68+ String installLocation = registry .getLocalMachineValue (compKey , PY_EXE_PATH );
69+ Logger .log (installLocation );
70+ if (installLocation != null )
71+ {
72+ pythonRegistryMap .put (version , installLocation );
8673 }
74+
8775 }
8876
8977 return pythonRegistryMap ;
9078
9179 }
9280
93- // Test
94- public static void main (String [] args )
95- {
96- try
97- {
98- Map <String , String > pyVersions = new PyWinRegistryReader ().getPythonVersions ();
99- Set <String > versionNumbers = pyVersions .keySet ();
100- for (String version : versionNumbers )
101- {
102- System .out .println ("Python " + version + " Path: " + pyVersions .get (version ));
103- }
104- }
105- catch (Exception e )
106- {
107- e .printStackTrace ();
108- }
109- }
110-
11181}
0 commit comments