From bf7eb5292ca16f46a77a4ca35a944c3397cb5d9d Mon Sep 17 00:00:00 2001 From: Jonas Termansen Date: Thu, 19 Jan 2023 17:56:40 +0000 Subject: [PATCH] [portability] Support unknown operating systems in gclient. Detect the name of unknown operating systems using uname, if available, since it doesn't append the operating system version. Change-Id: Idab7bd0db65a8d424ec2fd48f06247405b6649e4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4169240 Auto-Submit: Jonas Termansen Reviewed-by: Josip Sokcevic Commit-Queue: Josip Sokcevic --- gclient.py | 8 +++++++- gclient_utils.py | 11 +++++++---- metrics.py | 2 +- tests/metrics_test.py | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/gclient.py b/gclient.py index 3b7dc4b6c..8ec13f015 100755 --- a/gclient.py +++ b/gclient.py @@ -1410,7 +1410,13 @@ def merge_vars(result, new_vars): def _detect_host_os(): - return _PLATFORM_MAPPING[sys.platform] + if sys.platform in _PLATFORM_MAPPING: + return _PLATFORM_MAPPING[sys.platform] + + try: + return os.uname().sysname.lower() + except AttributeError: + return sys.platform class GitDependency(Dependency): diff --git a/gclient_utils.py b/gclient_utils.py index 84d96797a..6a1659df3 100644 --- a/gclient_utils.py +++ b/gclient_utils.py @@ -634,7 +634,7 @@ def CheckCallAndFilter(args, print_stdout=False, filter_fn=None, # If our stdout is a terminal, then pass in a psuedo-tty pipe to our # subprocess when filtering its output. This makes the subproc believe # it was launched from a terminal, which will preserve ANSI color codes. - os_type = GetMacWinAixOrLinux() + os_type = GetOperatingSystem() if sys.stdout.isatty() and os_type != 'win' and os_type != 'aix': pipe_reader, pipe_writer = os.openpty() else: @@ -780,8 +780,8 @@ def FindFileUpwards(filename, path=None): path = new_path -def GetMacWinAixOrLinux(): - """Returns 'mac', 'win', or 'linux', matching the current platform.""" +def GetOperatingSystem(): + """Returns 'mac', 'win', 'linux', or the name of the current platform.""" if sys.platform.startswith(('cygwin', 'win')): return 'win' @@ -794,7 +794,10 @@ def GetMacWinAixOrLinux(): if sys.platform.startswith('aix'): return 'aix' - raise Error('Unknown platform: ' + sys.platform) + try: + return os.uname().sysname.lower() + except AttributeError: + return sys.platform def GetGClientRootAndEntries(path=None): diff --git a/metrics.py b/metrics.py index c5b86b66b..11e43c6ad 100644 --- a/metrics.py +++ b/metrics.py @@ -241,7 +241,7 @@ class MetricsCollector(object): # Add metrics regarding environment information. self.add('timestamp', int(time.time())) self.add('python_version', metrics_utils.get_python_version()) - self.add('host_os', gclient_utils.GetMacWinAixOrLinux()) + self.add('host_os', gclient_utils.GetOperatingSystem()) self.add('host_arch', detect_host_arch.HostArch()) depot_tools_age = metrics_utils.get_repo_timestamp(DEPOT_TOOLS) diff --git a/tests/metrics_test.py b/tests/metrics_test.py index 19948b47d..8042b34c9 100644 --- a/tests/metrics_test.py +++ b/tests/metrics_test.py @@ -63,7 +63,7 @@ class MetricsCollectorTest(unittest.TestCase): mock.patch('metrics.metrics_utils.get_python_version', lambda: '2.7.13').start() mock.patch( - 'metrics.gclient_utils.GetMacWinAixOrLinux', lambda: 'linux').start() + 'metrics.gclient_utils.GetOperatingSystem', lambda: 'linux').start() mock.patch('metrics.detect_host_arch.HostArch', lambda: 'x86').start() mock.patch('metrics_utils.get_repo_timestamp',