Skip to content

Commit

Permalink
Fix wifi nmcli backup/restore /delete on 24.04 Desktop (BugFix) (#1324)
Browse files Browse the repository at this point in the history
* Correct the backup/restore from/to the correct path

* Fix the delete nmcli will make the all connection will be disappeared

* minor changes

* remove the shell=True in subprocess

* Fix some minor bug

* Fix typo, some missings, and Stanley's advices

* Update providers/base/tests/test_wifi_nmcli_test.py

Simplify the unit test

Co-authored-by: Fernando Bravo <39527354+fernando79513@users.noreply.github.com>

* Update providers/base/bin/wifi_nmcli_test.py

Fix typo

Co-authored-by: Fernando Bravo <39527354+fernando79513@users.noreply.github.com>

* Update providers/base/tests/test_wifi_nmcli_test.py

Simplify the unit test

Co-authored-by: Fernando Bravo <39527354+fernando79513@users.noreply.github.com>

* Update providers/base/tests/test_wifi_nmcli_test.py

Simplify the unit test

Co-authored-by: Fernando Bravo <39527354+fernando79513@users.noreply.github.com>

* Update providers/base/bin/wifi_nmcli_test.py

Only catch the sp.CalledProcessError

Co-authored-by: Fernando Bravo <39527354+fernando79513@users.noreply.github.com>

* fix backup script not use the os.path join but Path

* Fix test_wifi_nmcli_test.py

* Tiny fix...

* Fix the unittest parameter order

---------

Co-authored-by: Fernando Bravo <39527354+fernando79513@users.noreply.github.com>
  • Loading branch information
seankingyang and fernando79513 authored Sep 13, 2024
1 parent ce70144 commit bf03f1a
Show file tree
Hide file tree
Showing 4 changed files with 828 additions and 91 deletions.
36 changes: 21 additions & 15 deletions providers/base/bin/wifi_nmcli_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import shutil
import subprocess as sp
import sys
import glob
from pathlib import Path

from packaging import version as version_parser

Expand Down Expand Up @@ -62,37 +64,41 @@ def reload_nm_connections():


def save_connections(keyfile_list):
if not os.path.exists(SAVE_DIR):
os.makedirs(SAVE_DIR)
if len(keyfile_list) == 0:
os.makedirs(SAVE_DIR, exist_ok=True)

if not keyfile_list:
print("No stored 802.11 connections to save")
return

for f in keyfile_list:
print("Save connection {}".format(f))

if not os.path.exists(f):
print(" No stored connection fount at {}".format(f))
print(" No stored connection found at {}".format(f))
continue

print(" Found file {}".format(f))
save_f = shutil.copy(f, SAVE_DIR)
basedir = Path(f).parent
backup_loc = SAVE_DIR / basedir

os.makedirs(backup_loc, exist_ok=True)
save_f = shutil.copy(f, backup_loc)
print(" Saved copy at {}".format(save_f))


def restore_connections():
saved_list = [
f
for f in os.listdir(SAVE_DIR)
if os.path.isfile(os.path.join(SAVE_DIR, f))
]
saved_list = glob.glob(
"{}/**/*.nmconnection".format(SAVE_DIR), recursive=True
)
if len(saved_list) == 0:
print("No stored 802.11 connections found")
return
for f in saved_list:
save_f = os.path.join(SAVE_DIR, f)
f_path = Path(f)
save_f = f_path.relative_to(SAVE_DIR)
dest_path = Path("/") / save_f
print("Restore connection {}".format(save_f))
restore_f = shutil.copy(save_f, NM_CON_DIR)
print(" Restored file at {}".format(restore_f))
os.remove(save_f)
print(" Removed copy from {}".format(save_f))
shutil.move(f, dest_path)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit bf03f1a

Please sign in to comment.