[webkitpy] PlatformInfo should have default argument for casual use
authorddkilzer@apple.com <ddkilzer@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 9 Jan 2018 19:14:32 +0000 (19:14 +0000)
committerddkilzer@apple.com <ddkilzer@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 9 Jan 2018 19:14:32 +0000 (19:14 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180827

Patch by Basuke Suzuki <Basuke.Suzuki@sony.com> on 2018-01-09
Reviewed by David Kilzer.

PlatformInfo should be the only way to check platform specific thing instead
of sys.platform or other various checks. If there is something you want to check,
it should be placed in PlatformInfo.

Currently to instantiate PlatformInfo, it requires arguments, which is usually
sys, platform and Executive(). They are straight forward and should be handled
by default arguments. Then we can instantiate PlatformInfo more casual.

* Scripts/webkitpy/common/system/platforminfo.py:
(PlatformInfo): Use relative module path import
(PlatformInfo.__init__):
(PlatformInfo.executive): Lazy instantiation of Executive()
(PlatformInfo.total_bytes_memory): Accessing via above property
(PlatformInfo.xcode_sdk_version): Ditto
(PlatformInfo.xcode_simctl_list): Ditto
(PlatformInfo.xcode_version): Ditto
(PlatformInfo._win_version_str): Ditto
* Scripts/webkitpy/common/system/systemhost.py:
(SystemHost.__init__):
* Scripts/webkitpy/common/system/user.py:
(User.__init__):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@226652 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/Scripts/webkitpy/common/system/platforminfo.py
Tools/Scripts/webkitpy/common/system/systemhost.py
Tools/Scripts/webkitpy/common/system/user.py

index 0260292c18bd303d76beef20b51a1380974d99fd..7eeb6ef03bf89b496d63d0ff98e09e80ba88f952 100644 (file)
@@ -1,3 +1,32 @@
+2018-01-09  Basuke Suzuki  <Basuke.Suzuki@sony.com>
+
+        [webkitpy] PlatformInfo should have default argument for casual use
+        https://bugs.webkit.org/show_bug.cgi?id=180827
+
+        Reviewed by David Kilzer.
+
+        PlatformInfo should be the only way to check platform specific thing instead
+        of sys.platform or other various checks. If there is something you want to check,
+        it should be placed in PlatformInfo.
+
+        Currently to instantiate PlatformInfo, it requires arguments, which is usually
+        sys, platform and Executive(). They are straight forward and should be handled
+        by default arguments. Then we can instantiate PlatformInfo more casual.
+
+        * Scripts/webkitpy/common/system/platforminfo.py:
+        (PlatformInfo): Use relative module path import
+        (PlatformInfo.__init__):
+        (PlatformInfo.executive): Lazy instantiation of Executive()
+        (PlatformInfo.total_bytes_memory): Accessing via above property
+        (PlatformInfo.xcode_sdk_version): Ditto
+        (PlatformInfo.xcode_simctl_list): Ditto
+        (PlatformInfo.xcode_version): Ditto
+        (PlatformInfo._win_version_str): Ditto
+        * Scripts/webkitpy/common/system/systemhost.py:
+        (SystemHost.__init__):
+        * Scripts/webkitpy/common/system/user.py:
+        (User.__init__):
+
 2018-01-09  Yoshiaki Jitsukawa  <Yoshiaki.Jitsukawa@sony.com>
 
         [Win] Remove workaround for Bug 176442 after stopping MSVC 2015 support
index aca0c3b3e72edf163dfda4094e99ce322428a15e..c5938acacb8e7b90923d59f7aae41706a9982411 100644 (file)
@@ -29,6 +29,7 @@
 
 import re
 import sys
+import platform
 
 from webkitpy.common.version import Version
 from webkitpy.common.version_name_map import PUBLIC_TABLE, VersionNameMap
@@ -48,13 +49,16 @@ class PlatformInfo(object):
     newer than one known to the code.
     """
 
-    def __init__(self, sys_module, platform_module, executive):
+    def __init__(self, sys_module=None, platform_module=None, executive=None):
+        sys_platform = (sys_module or sys).platform
+        platform_module = platform_module or platform
+
         self._executive = executive
         self._platform_module = platform_module
-        self.os_name = self._determine_os_name(sys_module.platform)
+        self.os_name = self._determine_os_name(sys_platform)
         self.os_version = None
 
-        self._is_cygwin = sys_module.platform == 'cygwin'
+        self._is_cygwin = sys_platform == 'cygwin'
 
         if self.os_name.startswith('mac'):
             self.os_version = Version.from_string(platform_module.mac_ver()[0])
@@ -66,6 +70,13 @@ class PlatformInfo(object):
             # Most other platforms (namely iOS) return conforming version strings.
             self.os_version = Version.from_string(platform_module.release())
 
+    @property
+    def executive(self):
+        if self._executive is None:
+            self._executive = Executive()
+
+        return self._executive
+
     def is_mac(self):
         return self.os_name == 'mac'
 
@@ -110,7 +121,7 @@ class PlatformInfo(object):
 
     def total_bytes_memory(self):
         if self.is_mac():
-            return long(self._executive.run_command(["sysctl", "-n", "hw.memsize"]))
+            return long(self.executive.run_command(["sysctl", "-n", "hw.memsize"]))
         return None
 
     def terminal_width(self):
@@ -141,7 +152,7 @@ class PlatformInfo(object):
     def xcode_sdk_version(self, sdk_name):
         if self.is_mac():
             # Assumes that xcrun does not write to standard output on failure (e.g. SDK does not exist).
-            xcrun_output = self._executive.run_command(['xcrun', '--sdk', sdk_name, '--show-sdk-version'], return_stderr=False, error_handler=Executive.ignore_error).rstrip()
+            xcrun_output = self.executive.run_command(['xcrun', '--sdk', sdk_name, '--show-sdk-version'], return_stderr=False, error_handler=Executive.ignore_error).rstrip()
             if xcrun_output:
                 return Version.from_string(xcrun_output)
         return None
@@ -149,13 +160,13 @@ class PlatformInfo(object):
     def xcode_simctl_list(self):
         if not self.is_mac():
             return ()
-        output = self._executive.run_command(['xcrun', 'simctl', 'list'], return_stderr=False)
+        output = self.executive.run_command(['xcrun', 'simctl', 'list'], return_stderr=False)
         return (line for line in output.splitlines())
 
     def xcode_version(self):
         if not self.is_mac():
             raise NotImplementedError
-        return Version.from_string(self._executive.run_command(['xcodebuild', '-version']).split()[1])
+        return Version.from_string(self.executive.run_command(['xcodebuild', '-version']).split()[1])
 
     def _determine_os_name(self, sys_platform):
         if sys_platform == 'darwin':
@@ -185,4 +196,4 @@ class PlatformInfo(object):
         if version:
             return version
         # Note that this should only ever be called on windows, so this should always work.
-        return self._executive.run_command(['cmd', '/c', 'ver'], decode_output=False)
+        return self.executive.run_command(['cmd', '/c', 'ver'], decode_output=False)
index cb84193fde6964928151b0c6f90ead9552178f50..a15342201d833414731ebd0f76f7179a6f0bd6e3 100644 (file)
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 import os
-import platform
-import sys
 
-from webkitpy.common.system import environment, executive, file_lock, filesystem, platforminfo, user, workspace
+from . import environment, executive, file_lock, filesystem, platforminfo, user, workspace
 
 
 class SystemHost(object):
     def __init__(self):
         self.executive = executive.Executive()
         self.filesystem = filesystem.FileSystem()
-        self.user = user.User()
-        self.platform = platforminfo.PlatformInfo(sys, platform, self.executive)
+        self.platform = platforminfo.PlatformInfo(executive=self.executive)
+        self.user = user.User(self.platform)
         self.workspace = workspace.Workspace(self.filesystem, self.executive)
 
     def copy_current_environment(self):
index e513ecb0aaaabb805fe87fcd08c166dcc334eaef..2a4ae31deb01bf9a08a3591038957d371f96f1cc 100644 (file)
 import getpass
 import logging
 import os
-import platform
 import re
 import shlex
 import subprocess
-import sys
 import webbrowser
 
-from webkitpy.common.system.executive import Executive
-from webkitpy.common.system.platforminfo import PlatformInfo
+from .platforminfo import PlatformInfo
 
 
 _log = logging.getLogger(__name__)
@@ -46,7 +43,7 @@ _log = logging.getLogger(__name__)
 try:
     import readline
 except ImportError:
-    if not sys.platform.startswith('win32'):
+    if not PlatformInfo().is_native_win():
         # There is no readline module for win32, not much to do except cry.
         _log.warn("Unable to import readline.")
 
@@ -58,7 +55,7 @@ class User(object):
     def __init__(self, platforminfo=None):
         # We cannot get the PlatformInfo object from a SystemHost because
         # User is part of SystemHost itself.
-        self._platforminfo = platforminfo or PlatformInfo(sys, platform, Executive())
+        self._platforminfo = platforminfo or PlatformInfo()
 
     # FIXME: These are @classmethods because bugzilla.py doesn't have a Tool object (thus no User instance).
     @classmethod