Create an initial chrome://supervised-user-internals page
[chromium-blink-merge.git] / chrome / test / mini_installer / variable_expander.py
blob318367b2d0d653ea0d7fe31175ff61f3f98bd554
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import base64
6 import hashlib
7 import os
8 import string
9 import win32api
10 import win32com.client
11 from win32com.shell import shell, shellcon
12 import win32security
15 def _GetFileVersion(file_path):
16 """Returns the file version of the given file."""
17 return win32com.client.Dispatch('Scripting.FileSystemObject').GetFileVersion(
18 file_path)
21 def _GetProductName(file_path):
22 """Returns the product name of the given file.
24 Args:
25 file_path: The absolute or relative path to the file.
27 Returns:
28 A string representing the product name of the file, or None if the product
29 name was not found.
30 """
31 language_and_codepage_pairs = win32api.GetFileVersionInfo(
32 file_path, '\\VarFileInfo\\Translation')
33 if not language_and_codepage_pairs:
34 return None
35 product_name_entry = ('\\StringFileInfo\\%04x%04x\\ProductName' %
36 language_and_codepage_pairs[0])
37 return win32api.GetFileVersionInfo(file_path, product_name_entry)
40 def _GetUserSpecificRegistrySuffix():
41 """Returns '.' plus the unpadded Base32 encoding of the MD5 of the user's SID.
43 The result must match the output from the method
44 UserSpecificRegistrySuffix::GetSuffix() in
45 chrome/installer/util/shell_util.cc. It will always be 27 characters long.
46 """
47 token_handle = win32security.OpenProcessToken(win32api.GetCurrentProcess(),
48 win32security.TOKEN_QUERY)
49 user_sid, _ = win32security.GetTokenInformation(token_handle,
50 win32security.TokenUser)
51 user_sid_string = win32security.ConvertSidToStringSid(user_sid)
52 md5_digest = hashlib.md5(user_sid_string).digest()
53 return '.' + base64.b32encode(md5_digest).rstrip('=')
56 class VariableExpander:
57 """Expands variables in strings."""
59 def __init__(self, mini_installer_path):
60 """Constructor.
62 The constructor initializes a variable dictionary that maps variables to
63 their values. These are the only acceptable variables:
64 * $CHROME_DIR: the directory of Chrome (or Chromium) from the base
65 installation directory.
66 * $CHROME_HTML_PROG_ID: 'ChromeHTML' (or 'ChromiumHTM').
67 * $CHROME_LONG_NAME: 'Google Chrome' (or 'Chromium').
68 * $CHROME_LONG_NAME_SXS: 'Google Chrome SxS' if $SUPPORTS_SXS.
69 * $CHROME_SHORT_NAME: 'Chrome' (or 'Chromium').
70 * $CHROME_SHORT_NAME_SXS: 'ChromeCanary' if $SUPPORTS_SXS.
71 * $CHROME_UPDATE_REGISTRY_SUBKEY: the registry key, excluding the root
72 key, of Chrome for Google Update.
73 * $CHROME_UPDATE_REGISTRY_SUBKEY_SXS: the registry key, excluding the
74 root key, of Chrome SxS for Google Update.
75 * $LAUNCHER_UPDATE_REGISTRY_SUBKEY: the registry key, excluding the root
76 key, of the app launcher for Google Update if $SUPPORTS_SXS.
77 * $LOCAL_APPDATA: the unquoted path to the Local Application Data
78 folder.
79 * $MINI_INSTALLER: the unquoted path to the mini_installer.
80 * $MINI_INSTALLER_FILE_VERSION: the file version of the mini_installer.
81 * $PROGRAM_FILES: the unquoted path to the Program Files folder.
82 * $SUPPORTS_SXS: a boolean indicating whether or not SxS installs
83 are supported by the mini_installer under test.
84 * $USER_SPECIFIC_REGISTRY_SUFFIX: the output from the function
85 _GetUserSpecificRegistrySuffix().
86 * $VERSION_[XP/SERVER_2003/VISTA/WIN7/WIN8/WIN8_1/WIN10]: a 2-tuple
87 representing the version of the corresponding OS.
88 * $WINDOWS_VERSION: a 2-tuple representing the current Windows version.
90 Args:
91 mini_installer_path: The path to mini_installer.exe.
92 """
93 mini_installer_abspath = os.path.abspath(mini_installer_path)
94 windows_major_ver, windows_minor_ver, _, _, _ = win32api.GetVersionEx()
95 self._variable_mapping = {
96 'LOCAL_APPDATA': shell.SHGetFolderPath(0, shellcon.CSIDL_LOCAL_APPDATA,
97 None, 0),
98 'MINI_INSTALLER': mini_installer_abspath,
99 'MINI_INSTALLER_FILE_VERSION': _GetFileVersion(mini_installer_abspath),
100 'PROGRAM_FILES': shell.SHGetFolderPath(0, shellcon.CSIDL_PROGRAM_FILES,
101 None, 0),
102 'USER_SPECIFIC_REGISTRY_SUFFIX': _GetUserSpecificRegistrySuffix(),
103 'VERSION_SERVER_2003': '(5, 2)',
104 'VERSION_VISTA': '(6, 0)',
105 'VERSION_WIN10': '(10, 0)',
106 'VERSION_WIN7': '(6, 1)',
107 'VERSION_WIN8': '(6, 2)',
108 'VERSION_WIN8_1': '(6, 3)',
109 'VERSION_XP': '(5, 1)',
110 'WINDOWS_VERSION': '(%s, %s)' % (windows_major_ver, windows_minor_ver)
113 mini_installer_product_name = _GetProductName(mini_installer_abspath)
114 if mini_installer_product_name == 'Google Chrome Installer':
115 self._variable_mapping.update({
116 'BINARIES_UPDATE_REGISTRY_SUBKEY': (
117 'Software\\Google\\Update\\Clients\\'
118 '{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}'),
119 'CHROME_DIR': 'Google\\Chrome',
120 'CHROME_HTML_PROG_ID': 'ChromeHTML',
121 'CHROME_LONG_NAME': 'Google Chrome',
122 'CHROME_SHORT_NAME': 'Chrome',
123 'CHROME_UPDATE_REGISTRY_SUBKEY': (
124 'Software\\Google\\Update\\Clients\\'
125 '{8A69D345-D564-463c-AFF1-A69D9E530F96}'),
126 'SUPPORTS_SXS': True,
127 'CHROME_DIR_SXS': 'Google\\Chrome SxS',
128 'CHROME_LONG_NAME_SXS': 'Google Chrome SxS',
129 'CHROME_SHORT_NAME_SXS': 'ChromeCanary',
130 'CHROME_UPDATE_REGISTRY_SUBKEY_SXS': (
131 'Software\\Google\\Update\\Clients\\'
132 '{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}'),
133 'LAUNCHER_UPDATE_REGISTRY_SUBKEY': (
134 'Software\\Google\\Update\\Clients\\'
135 '{FDA71E6F-AC4C-4a00-8B70-9958A68906BF}')
137 elif mini_installer_product_name == 'Chromium Installer':
138 self._variable_mapping.update({
139 'BINARIES_UPDATE_REGISTRY_SUBKEY': 'Software\\Chromium Binaries',
140 'CHROME_DIR': 'Chromium',
141 'CHROME_HTML_PROG_ID': 'ChromiumHTM',
142 'CHROME_LONG_NAME': 'Chromium',
143 'CHROME_SHORT_NAME': 'Chromium',
144 'CHROME_UPDATE_REGISTRY_SUBKEY': 'Software\\Chromium',
145 'SUPPORTS_SXS': False
147 else:
148 raise KeyError("Unknown mini_installer product name '%s'" %
149 mini_installer_product_name)
152 def Expand(self, str):
153 """Expands variables in the given string.
155 This method resolves only variables defined in the constructor. It does not
156 resolve environment variables. Any dollar signs that are not part of
157 variables must be escaped with $$, otherwise a KeyError or a ValueError will
158 be raised.
160 Args:
161 str: A string.
163 Returns:
164 A new string created by replacing variables with their values.
166 return string.Template(str).substitute(self._variable_mapping)