Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions eos-migrate-chrome-profile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ from gi.repository import GLib


USER_HOME_DIR = os.path.expanduser("~/")
DESKTOP_SHORTCUTS_DIR = os.path.join(USER_HOME_DIR, ".local", "share", "applications")
OLD_CHROME_CONFIG_DIR = os.path.join(USER_HOME_DIR, ".config", "google-chrome")
NEW_CHROME_DATA_DIR = os.path.join(USER_HOME_DIR, ".var", "app", "com.google.Chrome")
NEW_CHROME_CONFIG_DIR = os.path.join(NEW_CHROME_DATA_DIR, "config", "google-chrome")
Expand Down Expand Up @@ -89,6 +90,43 @@ def update_mimeapps_list(path):
keyfile.save_to_file(path)


def update_desktop_shortcut(path):
keyfile = GLib.KeyFile()
keyfile.load_from_file(path,
GLib.KeyFileFlags.KEEP_COMMENTS |
GLib.KeyFileFlags.KEEP_TRANSLATIONS)

groups, _ = keyfile.get_groups()
for group in groups:
try:
exec_cmd = keyfile.get_string(group, "Exec")
except GLib.GError as error:
if not error.matches(GLib.key_file_error_quark(),
GLib.KeyFileError.KEY_NOT_FOUND):
raise error
continue

if not exec_cmd.startswith("/usr/bin/eos-google-chrome"):
continue

keyfile.set_string(group, "Exec",
"/usr/bin/flatpak run com.google.Chrome" +
exec_cmd[len("/usr/bin/eos-google-chrome"):])

keyfile.save_to_file(path)


def update_desktop_shortcuts(path=DESKTOP_SHORTCUTS_DIR):
try:
shortcuts = os.listdir(path)
except FileNotFoundError:
return

for filename in shortcuts:
if filename.endswith(".desktop"):
update_desktop_shortcut(os.path.join(path, filename))


def update_old_config_references(path):
if not os.path.isfile(path):
return
Expand All @@ -107,6 +145,7 @@ def main():
sys.exit(1)

update_mimeapps_list(MIMEAPPS_LIST)
update_desktop_shortcuts()

if (
os.path.isdir(OLD_CHROME_CONFIG_DIR) and
Expand Down
120 changes: 120 additions & 0 deletions tests/test_migrate_chrome_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,126 @@ def _test(self, orig_data, expected_data):
self.assertEqual(expected_data, new_data)


class TestUpdateDesktopShortcuts(BaseTestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an actual file from my system:

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=YouTube
Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1" --app-id=agimnkijcaahngcdmfeangaknmldooml
Icon=chrome-agimnkijcaahngcdmfeangaknmldooml-Profile_1
StartupWMClass=crx_agimnkijcaahngcdmfeangaknmldooml
Actions=Explore;Subscriptions

[Desktop Action Explore]
Name=Explore
Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1" --app-id=agimnkijcaahngcdmfeangaknmldooml "--app-launch-url-for-shortcuts-menu-item=https://www.youtube.com/feed/explore?feature=app_shortcuts"

[Desktop Action Subscriptions]
Name=Subscriptions
Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1" --app-id=agimnkijcaahngcdmfeangaknmldooml "--app-launch-url-for-shortcuts-menu-item=https://www.youtube.com/feed/subscriptions?feature=app_shortcuts"

I suggest you add this as a test case.

I think you should also test the case where there is a group that does not have an Exec= key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Added as the new commit.

def setUp(self):
super().setUp()

self.tmp = tempfile.TemporaryDirectory()

def tearDown(self):
self.tmp.cleanup()

def test_no_shortcuts_dir(self):
emfp.update_desktop_shortcuts(os.path.join(self.tmp.name, "no-such-directory"))

def test_empty_shortcuts_dir(self):
emfp.update_desktop_shortcuts(self.tmp.name)

def test_not_there(self):
orig_data = textwrap.dedent(
"""
[Desktop Entry]
Exec=/usr/bin/chromium-browser --profile-directory=Default
"""
).lstrip()
expected_data = orig_data

self._test(orig_data, expected_data)

def test_there(self):
orig_data = textwrap.dedent(
"""
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=YouTube
Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1"
Icon=chrome-agimnkijcaahngcdmfeangaknmldooml-Profile_1
StartupWMClass=crx_agimnkijcaahngcdmfeangaknmldooml
Actions=Explore;Subscriptions

[Desktop Action Explore]
Name=Explore
Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1"

[Desktop Action Subscriptions]
Name=Subscriptions
Exec=/usr/bin/eos-google-chrome "--profile-directory=Profile 1"
"""
).lstrip()
expected_data = textwrap.dedent(
"""
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=YouTube
Exec=/usr/bin/flatpak run com.google.Chrome "--profile-directory=Profile 1"
Icon=chrome-agimnkijcaahngcdmfeangaknmldooml-Profile_1
StartupWMClass=crx_agimnkijcaahngcdmfeangaknmldooml
Actions=Explore;Subscriptions

[Desktop Action Explore]
Name=Explore
Exec=/usr/bin/flatpak run com.google.Chrome "--profile-directory=Profile 1"

[Desktop Action Subscriptions]
Name=Subscriptions
Exec=/usr/bin/flatpak run com.google.Chrome "--profile-directory=Profile 1"
"""
).lstrip()

self._test(orig_data, expected_data)

def test_multi_languages(self):
orig_data = textwrap.dedent(
"""
# Comment
[Desktop Entry]
Name=Chrome
Name[en_GB]=Chrome, milord
Exec=/usr/bin/eos-google-chrome --profile-directory=Default
"""
).lstrip()
expected_data = textwrap.dedent(
"""
# Comment
[Desktop Entry]
Name=Chrome
Name[en_GB]=Chrome, milord
Exec=/usr/bin/flatpak run com.google.Chrome --profile-directory=Default
"""
).lstrip()
Comment on lines +121 to +186
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
orig_data = textwrap.dedent(
"""
[Desktop Entry]
Exec=/usr/bin/eos-google-chrome --profile-directory=Default
"""
).lstrip()
expected_data = textwrap.dedent(
"""
[Desktop Entry]
Exec=/usr/bin/flatpak run com.google.Chrome --profile-directory=Default
"""
).lstrip()
orig_data = textwrap.dedent(
"""
# Comment
[Desktop Entry]
Name=Chrome
Name[en_GB]=Chrome, milord
Exec=/usr/bin/eos-google-chrome --profile-directory=Default
"""
).lstrip()
expected_data = textwrap.dedent(
"""
# Comment
[Desktop Entry]
Name=Chrome
Name[en_GB]=Chrome, milord
Exec=/usr/bin/flatpak run com.google.Chrome --profile-directory=Default
"""
).lstrip()

I believe this modified test would fail currently but would succeed with the changes I propose below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, added the multi language test in the new commit.


self._test(orig_data, expected_data)

def test_no_exec_in_group(self):
orig_data = textwrap.dedent(
"""
[Dummy Group]
foo=bar
"""
).lstrip()
expected_data = orig_data

self._test(orig_data, expected_data)

def _test(self, orig_data, expected_data):
test_desktop = os.path.join(self.tmp.name,
"test.desktop")
with open(test_desktop, "w") as f:
f.write(orig_data)

emfp.update_desktop_shortcuts(self.tmp.name)
with open(test_desktop, "r") as f:
new_data = f.read()

self.assertEqual(expected_data, new_data)


class TestUpdateOldConfigReferences(BaseTestCase):
def setUp(self):
super().setUp()
Expand Down