tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

macosx_resolution_refreshrate.py (3171B)


      1 import platform
      2 import subprocess
      3 import sys
      4 
      5 from optparse import OptionParser
      6 
      7 TARGET_REFRESH_RATE = {
      8    "11.20": "53.00",
      9    "default": "60.00",
     10 }
     11 
     12 TARGET_RESOLUTION = {"default": "1920 x 1080"}
     13 
     14 
     15 def get_os_version():
     16    (release, versioninfo, machine) = platform.mac_ver()
     17    versionNums = release.split(".")[:2]
     18    os_version = "%s.%s" % (versionNums[0], versionNums[1].ljust(2, "0"))
     19    return os_version
     20 
     21 
     22 def get_target_rate():
     23    if platform.system() == "Darwin":
     24        os_version = get_os_version()
     25        if os_version in TARGET_REFRESH_RATE:
     26            return TARGET_REFRESH_RATE[os_version]
     27        return TARGET_REFRESH_RATE["default"]
     28    return 0
     29 
     30 
     31 def get_target_resolution():
     32    if platform.system() == "Darwin":
     33        os_version = get_os_version()
     34        if os_version in TARGET_RESOLUTION:
     35            return TARGET_RESOLUTION[os_version]
     36        return TARGET_RESOLUTION["default"]
     37    return 0
     38 
     39 
     40 def get_refresh_rate():
     41    if platform.system() == "Darwin":
     42        # 11.20/aarch64 (mac mini m1) - always has 53.00
     43        # 14.70 - mix between 60.00 and 24.00
     44        cmd = "system_profiler SPDisplaysDataType | grep 'UI' | cut -d '@' -f 2 | cut -d ' ' -f 2 | sed 's/Hz//'"
     45    else:
     46        return 0
     47 
     48    target_rate = get_target_rate()
     49 
     50    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
     51    refresh_rate = result.stdout.strip()
     52    try:
     53        refresh_rate = "%.02f" % refresh_rate
     54    except TypeError:
     55        pass  # have non numeric type
     56    print(f"Refresh Rate: {refresh_rate} Hz")
     57 
     58    if str(refresh_rate) != str(target_rate):
     59        print(
     60            f"ERROR: expected refresh rate = {target_rate}, instead got {refresh_rate}."
     61        )
     62        return 1
     63    return 0
     64 
     65 
     66 def get_resolution():
     67    if platform.system() == "Darwin":
     68        """
     69        system_profiler SPDisplaysDataType | grep Resolution
     70          Resolution: 1920 x 1080 (1080p FHD - Full High Definition)
     71        """
     72        cmd = "system_profiler SPDisplaysDataType | grep 'Resolution' | cut -d '(' -f 1 | cut -d ':' -f 2"
     73    else:
     74        return 0
     75 
     76    target_resolution = get_target_resolution()
     77 
     78    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
     79    resolution = result.stdout.strip()
     80    print(f"Resolution: {resolution}")
     81 
     82    if str(resolution) != str(target_resolution):
     83        print(
     84            f"ERROR: expected resolution = {target_resolution}, instead got {resolution}."
     85        )
     86        return 1
     87    return 0
     88 
     89 
     90 def main():
     91    # NOTE: this script is only designed for macosx.
     92    parser = OptionParser()
     93    parser.add_option(
     94        "--check",
     95        dest="check",
     96        type="string",
     97        default="resolution",
     98        help="Determines the test to run (refresh-rate || resolution).",
     99    )
    100    (options, args) = parser.parse_args()
    101    if options.check == "resolution":
    102        retVal = get_resolution()
    103    else:
    104        retVal = get_refresh_rate()
    105 
    106    os_version = get_os_version()
    107    # Currently the 10.15 machines are not all consistent with proper kvm, resolution, refresh rate
    108    if os_version == "10.15":
    109        return 0
    110    return retVal
    111 
    112 
    113 sys.exit(main())