Skip to content

Commit

Permalink
Remove support for deprecated macOS XML wifi list. (#868)
Browse files Browse the repository at this point in the history
* Use pytest-cov plugin to avoid hangers and subproc issues.
  • Loading branch information
m3nu authored Feb 18, 2021
1 parent bd0a1b8 commit 9a4bbf0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 48 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ jobs:
brew install openssl readline xz borgbackup
- name: Install Vorta
run: |
pip install .
pip install -e .
pip install -r requirements.d/dev.txt
# - name: Setup tmate session
# uses: mxschmitt/action-tmate@v1
- name: Test with pytest (Linux)
if: runner.os == 'Linux'
run: |
xvfb-run --server-args="-screen 0 1024x768x24+32" \
-a dbus-run-session -- coverage run -m pytest
-a dbus-run-session -- pytest --cov=vorta
- name: Test with pytest (macOS)
if: runner.os == 'macOS'
run: |
coverage run -m pytest
pytest --cov=vorta
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
env:
Expand Down
1 change: 1 addition & 0 deletions requirements.d/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ flake8
pyinstaller
pylint
pytest
pytest-cov
pytest-faulthandler
pytest-mock
pytest-qt
Expand Down
6 changes: 3 additions & 3 deletions src/vorta/network_status/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_current_wifi(self) -> Optional[str]:
"""Get current SSID or None if Wifi is off."""
raise NotImplementedError()

def get_known_wifis(self) -> Optional[List['SystemWifiInfo']]:
def get_known_wifis(self) -> List['SystemWifiInfo']:
"""Get WiFi networks known to system."""
raise NotImplementedError()

Expand All @@ -50,5 +50,5 @@ def is_network_metered(self) -> bool:
def get_current_wifi(self) -> Optional[str]:
pass

def get_known_wifis(self) -> Optional[List['SystemWifiInfo']]:
pass
def get_known_wifis(self) -> List['SystemWifiInfo']:
return []
37 changes: 10 additions & 27 deletions src/vorta/network_status/darwin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import plistlib
import subprocess
import xml
from typing import Optional, Iterator

from peewee import format_date_time
from datetime import datetime as dt

from vorta.log import logger
from vorta.network_status.abc import NetworkStatusMonitor, SystemWifiInfo
Expand All @@ -29,30 +26,16 @@ def get_current_wifi(self) -> Optional[str]:
return split_line[1].strip()

def get_known_wifis(self):
from vorta.models import WifiSettingModel
plist_path = '/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist'

try:
plist_file = open(plist_path, 'rb')
wifis = plistlib.load(plist_file).get('KnownNetworks')
except xml.parsers.expat.ExpatError:
logger.error('Unable to parse list of Wifi networks.')
return None

result = []
if wifis is not None:
for wifi in wifis.values():
ssid = wifi.get('SSIDString', None)
if ssid is None:
continue

last_connected = wifi.get('LastConnected', None) or wifi.get('LastAutoJoinAt', None)
if isinstance(last_connected, str): # TODO: Maybe not needed any more?
last_connected = format_date_time(last_connected, WifiSettingModel.last_connected.formats)

result.append(SystemWifiInfo(ssid=ssid, last_connected=last_connected))
"""
Listing all known Wifi networks isn't possible any more from macOS 11. Instead we
just return the current Wifi.
"""
wifis = []
current_wifi = self.get_current_wifi()
if current_wifi is not None:
wifis.append(SystemWifiInfo(ssid=current_wifi, last_connected=dt.now()))

return result
return wifis


def get_network_devices() -> Iterator[str]:
Expand Down
6 changes: 3 additions & 3 deletions src/vorta/network_status/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def get_current_wifi(self) -> Optional[str]:
logger.exception("Failed to get currently connected WiFi network, assuming none")
return None

def get_known_wifis(self) -> Optional[List[SystemWifiInfo]]:
def get_known_wifis(self) -> List[SystemWifiInfo]:
wifis = []
try:
connections_paths = self._nm.get_connections_paths()
except DBusException:
logger.exception("Failed to list connections")
return None
return wifis

wifis = []
for connection_path in connections_paths:
try:
settings = self._nm.get_settings(connection_path)
Expand Down
22 changes: 10 additions & 12 deletions src/vorta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,31 @@ def get_asset(path):


def get_sorted_wifis(profile):
"""Get SSIDs from OS and merge with settings in DB."""
"""
Get Wifi networks known to the OS (only current one on macOS) and
merge with networks from other profiles. Update last connected time.
"""

from vorta.models import WifiSettingModel

# Pull networks known to OS and all other backup profiles
system_wifis = get_network_status_monitor().get_known_wifis()
if system_wifis is None:
# Don't show any networks if we can't get the current list
return []
from_other_profiles = WifiSettingModel.select() \
.where(WifiSettingModel.profile != profile.id).execute()

for wifi in system_wifis:
for wifi in list(from_other_profiles) + system_wifis:
db_wifi, created = WifiSettingModel.get_or_create(
ssid=wifi.ssid,
profile=profile.id,
defaults={'last_connected': wifi.last_connected, 'allowed': True}
)

# update last connected time
# Update last connected time
if not created and db_wifi.last_connected != wifi.last_connected:
db_wifi.last_connected = wifi.last_connected
db_wifi.save()

# remove Wifis that were deleted in the system.
deleted_wifis = WifiSettingModel.select() \
.where(WifiSettingModel.ssid.not_in([wifi.ssid for wifi in system_wifis]))
for wifi in deleted_wifis:
wifi.delete_instance()

# Finally return list of networks and settings for that profile
return WifiSettingModel.select() \
.where(WifiSettingModel.profile == profile.id).order_by(-WifiSettingModel.last_connected)

Expand Down

0 comments on commit 9a4bbf0

Please sign in to comment.