[tools 1/2] testbot/SetWinLocale: Use Powershell to collect all the locales at once.

Francois Gouget fgouget at codeweavers.com
Thu Jan 27 07:58:44 CST 2022


The new Powershell script can also be used independently on Windows.

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---
 testbot/bin/SetWinLocale     | 103 +++++++++++++++++++-------------
 testbot/bin/SetWinLocale.ps1 | 110 +++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+), 42 deletions(-)
 create mode 100644 testbot/bin/SetWinLocale.ps1

diff --git a/testbot/bin/SetWinLocale b/testbot/bin/SetWinLocale
index 42a106b77d..25023bb94f 100755
--- a/testbot/bin/SetWinLocale
+++ b/testbot/bin/SetWinLocale
@@ -3,7 +3,7 @@
 #
 # Sets the locale of the specified Windows machine.
 #
-# Copyright 2018, 2021 Francois Gouget
+# Copyright 2018, 2021-2022 Francois Gouget
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -41,12 +41,7 @@ use WineTestBot::Log;
 use WineTestBot::TestAgent;
 use WineTestBot::Utils;
 
-my $HKCU_INTERNATIONAL = "HKCU\\Control Panel\\International";
-my $HKCU_GEO = "HKCU\\Control Panel\\International\\Geo";
 my $HKCU_USER_PROFILE = "HKCU\\Control Panel\\International\\User Profile";
-my $HKCU_DESKTOP = "HKCU\\Control Panel\\Desktop";
-my $HKLM_LANGUAGE = "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Nls\\Language";
-my $HKLM_LOCALE = "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Nls\\Locale";
 my $HKLM_CODE_PAGE = "HKLM\\System\\CurrentControlSet\\Control\\Nls\\CodePage";
 
 
@@ -74,11 +69,26 @@ sub Debug(@)
   print STDERR @_ if ($Debug);
 }
 
+my $TA;
+sub Cleanup()
+{
+  return if (!$TA);
+  if ($Debug)
+  {
+    Debug("Not deleting the $name0* files for debugging\n");
+  }
+  else
+  {
+    $TA->Rm("$name0.out", "$name0.ps1");
+  }
+}
+
 my $Start = Time();
 sub FatalError(@)
 {
-  Error( @_);
+  Error(@_);
   Debug(Elapsed($Start), " Aborting\n");
+  Cleanup();
   exit(1);
 }
 
@@ -487,7 +497,7 @@ if (defined $Usage)
   exit 0;
 }
 
-my $TA = TestAgent->new($OptHostName, $AgentPort);
+$TA = TestAgent->new($OptHostName, $AgentPort);
 
 
 #
@@ -558,42 +568,44 @@ sub RegSetValue($$$$)
 # Show the host's locale settings
 #
 
-sub GetWinSettings($)
+if (open(my $fh, "<", "$0.ps1"))
 {
-  my ($All) = @_;
-  my $Settings;
+  my $Script = join("", <$fh>);
+  close($fh);
 
-  if ($OptLocale or $All)
-  {
-    my $Values = RegGetValues($HKCU_INTERNATIONAL, "Locale*");
-    map { $Settings->{$_} = $Values->{$_} } ("Locale", "LocaleName");
-  }
-  if ($OptCountry or $All)
-  {
-    my $Values = RegGetValues($HKCU_GEO, "N*");
-    $Settings->{Country} = $Values->{Nation};
-    $Settings->{CountryName} = $Values->{Name};
-  }
-  if (($OptKeyboard || $OptDefault) or $All)
-  {
-    my $Values = RegGetValues($HKCU_USER_PROFILE, "*");
-    $Settings->{InputMethod} = $Values->{InputMethodOverride};
-    $Settings->{Languages} = $Values->{Languages};
-  }
-  if ($OptMUI or $All)
-  {
-    my $Values = RegGetValues($HKCU_DESKTOP, "*PreferredUILanguages*");
-    map { $Settings->{$_} = $Values->{$_} } ("PreferredUILanguages", "PreferredUILanguagesPending", "PreviousPreferredUILanguages");
-  }
-  if ($OptSystem or $All)
+  $Script =~ s~\n~\r\n~g;
+  if (!$TA->SendFileFromString($Script, "$name0.ps1", 0))
   {
-     $Settings->{SysLanguage} = RegGetValue($HKLM_LANGUAGE, "Default");
-     $Settings->{SysLocale} = RegGetValue($HKLM_LOCALE, "(Default)");
+    FatalError("could not send the batch file:", $TA->GetLastError(), "\n");
   }
-  if ($OptUTF8 or $All)
+}
+else
+{
+  FatalError("could not open '$0.ps1' for reading: $!\n");
+}
+
+sub GetWinSettings()
+{
+  my $Cmd = ["powershell.exe", "-ExecutionPolicy", "ByPass", "-File",
+             "$name0.ps1", "settings"];
+  my $Ret = $TA->RunAndWait($Cmd, 0, 30, undef, "$name0.out", "$name0.out");
+  FatalError("failed to run @$Cmd: ", $TA->GetLastError(), "\n") if ($Ret < 0);
+  my $Out = $TA->GetFileToString("$name0.out") if (!$Ret);
+
+  my ($Settings, $Key);
+  foreach my $Line (split /\n/, $Out || "")
   {
-    my $Values = RegGetValues($HKLM_CODE_PAGE, "*CP");
-    map { $Settings->{$_} = $Values->{$_} } ("ACP", "MACCP", "OEMCP");
+    $Line =~ s/\r$//;
+    if ($Line =~ /^([a-zA-Z]+)=\[(.*)\]$/)
+    {
+      my ($Name, $Value) = ($1, $2);
+      $Settings->{$Name} = [split /, /, $Value];
+    }
+    elsif ($Line =~ /^([a-zA-Z]+)=(.*)$/)
+    {
+      my ($Name, $Value) = ($1, $2);
+      $Settings->{$Name} = $Value eq "<undef>" ? undef : $Value;
+    }
   }
   return $Settings;
 }
@@ -609,6 +621,8 @@ sub Value2Str($)
 sub ShowWinSettings($)
 {
   my ($Settings) = @_;
+
+  print "Current account:\n";
   print "Locale            (--locale) = ", Value2Str($Settings->{Locale}), "\n";
   print "LocaleName        (--locale) = ", Value2Str($Settings->{LocaleName}), "\n";
   print "Geo:Nation       (--country) = ", Value2Str($Settings->{Country}), "\n";
@@ -618,6 +632,9 @@ sub ShowWinSettings($)
   print "PreferredUILanguages (--mui) = ", Value2Str($Settings->{PreferredUILanguages}), "\n";
   print "          ...Pending (--mui) = ", Value2Str($Settings->{PreferredUILanguagesPending}), "\n";
   print "         Previous... (--mui) = ", Value2Str($Settings->{PreviousPreferredUILanguages}), "\n";
+
+  print "\n";
+  print "System settings:\n";
   print "Nls:Language      (--system) = ", Value2Str($Settings->{SysLanguage}), "\n";
   print "Nls:Locale        (--system) = ", Value2Str($Settings->{SysLocale}), "\n";
   print "ACP                 (--utf8) = ", Value2Str($Settings->{ACP}), "\n";
@@ -627,7 +644,8 @@ sub ShowWinSettings($)
 
 if ($OptShow)
 {
-  ShowWinSettings(GetWinSettings("all"));
+  ShowWinSettings(GetWinSettings());
+  Cleanup();
   exit(0);
 }
 
@@ -856,7 +874,7 @@ sub CheckSetting($$$$;$)
   my ($Settings, $VName, $Expected, $For, $IgnoreCase) = @_;
 
   my $Value = $Settings->{$VName};
-  if (defined $Value)
+  if (defined $Value and $Value !~ /^<.+>$/)
   {
     if ((!$IgnoreCase and $Value ne $Expected) or
         ($IgnoreCase and uc($Value) ne uc($Expected)))
@@ -868,7 +886,7 @@ sub CheckSetting($$$$;$)
 }
 
 Debug(Elapsed($Start), " Checking the new locale settings\n");
-my $Settings = GetWinSettings($Debug);
+my $Settings = GetWinSettings();
 ShowWinSettings($Settings) if ($Debug);
 if ($OptLocale)
 {
@@ -901,6 +919,7 @@ if ($KeyboardIds)
   CheckSetting($Settings, "InputMethod", $KeyboardIds->[0], "for --keyboard $OptKeyboard", 1);
 }
 
+Cleanup();
 exit(1) if (!$Success);
 Debug(Elapsed($Start), " All done!\n");
 exit(0);
diff --git a/testbot/bin/SetWinLocale.ps1 b/testbot/bin/SetWinLocale.ps1
new file mode 100644
index 0000000000..8d6b3a94bc
--- /dev/null
+++ b/testbot/bin/SetWinLocale.ps1
@@ -0,0 +1,110 @@
+# Shows the Windows locale settings
+#
+# Copyright 2022 Francois Gouget
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+
+$Name0 = (Get-Item $MyInvocation.InvocationName).Basename
+
+
+#
+# Dump the Windows locales settings
+#
+
+$HKCU_INTERNATIONAL = "HKCU:\Control Panel\International"
+$HKCU_GEO = "HKCU:\Control Panel\International\Geo"
+$HKCU_USER_PROFILE = "HKCU:\Control Panel\International\User Profile"
+$HKCU_DESKTOP = "HKCU:\Control Panel\Desktop"
+$HKLM_LANGUAGE = "HKLM:\SYSTEM\CurrentControlSet\Control\Nls\Language"
+$HKLM_LOCALE = "HKLM:\SYSTEM\CurrentControlSet\Control\Nls\Locale"
+$HKLM_CODE_PAGE = "HKLM:\System\CurrentControlSet\Control\Nls\CodePage"
+$CODE_PAGES = "ACP", "MACCP", "OEMCP"
+
+
+function ShowSetting([string]$Key, [string]$VName, [string]$SName)
+{
+  if ($SName -eq "") { $SName = $VName }
+  # Don't use Get-ItemPropertyValue because it ignores -ErrorAction and is
+  # not available on Windows 7.
+  $Value = Get-ItemProperty -Path $Key -Name $VName -ErrorAction SilentlyContinue
+  $Value = if ($Value -eq $null) { "<undef>" } else { $Value.$($VName) }
+  if ($Value.GetType().Name -eq "Object[]")
+  {
+    $Value = "[" + ($Value -join ", ") + "]"
+  }
+  Write-Output "$SName=$Value"
+}
+
+function ShowSettings()
+{
+  Write-Output "Current account:"
+  ShowSetting $HKCU_INTERNATIONAL "Locale"
+  ShowSetting $HKCU_INTERNATIONAL "LocaleName"
+  ShowSetting $HKCU_GEO "Nation" "Country"
+  ShowSetting $HKCU_GEO "Name" "CountryName"
+  ShowSetting $HKCU_USER_PROFILE "InputMethodOverride" "InputMethod"
+  ShowSetting $HKCU_USER_PROFILE "Languages"
+  ShowSetting $HKCU_DESKTOP "PreferredUILanguages"
+  ShowSetting $HKCU_DESKTOP "PreferredUILanguagesPending"
+  ShowSetting $HKCU_DESKTOP "PreviousPreferredUILanguages"
+
+  Write-Output ""
+  Write-Output "System settings:"
+  ShowSetting $HKLM_LANGUAGE "Default" "SysLanguage"
+  try
+  {
+    ShowSetting $HKLM_LOCALE "(Default)" "SysLocale"
+  }
+  catch
+  {
+    # On Windows 7 Get-ItemProperty confuses the key's default value
+    # with the value called "(Default)"!
+    Write-Output "SysLocale=<cannot-get-on-windows-7>"
+  }
+  foreach ($CodePage in $CODE_PAGES)
+  {
+    ShowSetting $HKLM_CODE_PAGE $CodePage
+  }
+
+  exit 0
+}
+
+
+#
+# Main
+#
+
+function ShowUsage()
+{
+  Write-Output "Usage: $Name0 settings"
+  Write-Output "or     $Name0 -?"
+  Write-Output ""
+  Write-Output "Shows the Windows locales."
+  Write-Output ""
+  Write-Output "Where:"
+  Write-Output "  settings   Show the current Windows locale settings."
+  Write-Output "  -?         Shows this help message."
+}
+
+$Action = $args[0]
+if ($Action -eq "settings") { ShowSettings }
+$Rc = 0
+if ($Action -and $Action -ne "-?" -and $Action -ne "-h" -and $Action -ne "help")
+{
+  echo "$Name0:error: unknown action $Action"
+  $Rc = 2
+}
+ShowUsage
+exit $Rc
-- 
2.30.2




More information about the wine-devel mailing list