|
| 1 | +#!/usr/bin/python3 |
| 2 | +# |
| 3 | +# eos-migrate-chrome-profile: move users' Chrome profile to its new home |
| 4 | +# |
| 5 | +# This script is based on eos-migrate-chromium-profile. |
| 6 | +# |
| 7 | +# Copyright © 2025 Endless OS Foundation LLC |
| 8 | +# |
| 9 | +# This program is free software; you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation; either version 2 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# This program is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with this program; if not, write to the Free Software |
| 21 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 | + |
| 23 | +import fileinput |
| 24 | +import os |
| 25 | +import subprocess |
| 26 | +import sys |
| 27 | +from gi.repository import GLib |
| 28 | + |
| 29 | + |
| 30 | +USER_HOME_DIR = os.path.expanduser("~/") |
| 31 | +OLD_CHROME_CONFIG_DIR = os.path.join(USER_HOME_DIR, ".config", "google-chrome") |
| 32 | +NEW_CHROME_DATA_DIR = os.path.join(USER_HOME_DIR, ".var", "app", "com.google.Chrome") |
| 33 | +NEW_CHROME_CONFIG_DIR = os.path.join(NEW_CHROME_DATA_DIR, "config", "google-chrome") |
| 34 | + |
| 35 | +OLD_WIDEVINE_SYSTEM_DIR = os.path.join(os.path.sep, "usr", "lib", |
| 36 | + "google-chrome", "WidevineCdm") |
| 37 | + |
| 38 | +MIMEAPPS_LIST = os.path.join(GLib.get_user_config_dir(), "mimeapps.list") |
| 39 | +MIMEAPPS_GROUPS = [ |
| 40 | + "Default Applications", |
| 41 | + "Added Associations", |
| 42 | + "Removed Associations", |
| 43 | +] |
| 44 | +OLD_DESKTOP_FILE = "google-chrome.desktop" |
| 45 | +NEW_DESKTOP_FILE = "com.google.Chrome.desktop" |
| 46 | + |
| 47 | + |
| 48 | +def update_mimeapps_list(path): |
| 49 | + """Update file associations, which in particular includes x-scheme-handler/http and |
| 50 | + friends to specify the default web browser. |
| 51 | +
|
| 52 | + We cannot use GLib's own API to query what mime types the old Chrome desktop file |
| 53 | + is a handler for, because we can't construct a GDesktopAppInfo for it, because it |
| 54 | + tries to execute the old Chrome which no longer exists. Update to the new installed |
| 55 | + Flathub Chrome's desktop. |
| 56 | + """ |
| 57 | + |
| 58 | + keyfile = GLib.KeyFile() |
| 59 | + try: |
| 60 | + keyfile.load_from_file( |
| 61 | + path, GLib.KeyFileFlags.KEEP_COMMENTS | GLib.KeyFileFlags.KEEP_TRANSLATIONS, |
| 62 | + ) |
| 63 | + except GLib.GError as gerror: |
| 64 | + if gerror.matches(GLib.file_error_quark(), GLib.FileError.NOENT): |
| 65 | + return |
| 66 | + |
| 67 | + raise |
| 68 | + |
| 69 | + changed = False |
| 70 | + |
| 71 | + for group in MIMEAPPS_GROUPS: |
| 72 | + if not keyfile.has_group(group): |
| 73 | + continue |
| 74 | + |
| 75 | + keys, _length = keyfile.get_keys(group) |
| 76 | + for key in keys: |
| 77 | + values = keyfile.get_string_list(group, key) |
| 78 | + try: |
| 79 | + i = values.index(OLD_DESKTOP_FILE) |
| 80 | + except ValueError: |
| 81 | + pass |
| 82 | + else: |
| 83 | + values[i] = NEW_DESKTOP_FILE |
| 84 | + keyfile.set_string_list(group, key, values) |
| 85 | + changed = True |
| 86 | + |
| 87 | + if changed: |
| 88 | + keyfile.save_to_file(path) |
| 89 | + |
| 90 | + |
| 91 | +def update_old_config_references(path): |
| 92 | + if not os.path.isfile(path): |
| 93 | + return |
| 94 | + |
| 95 | + for line in fileinput.input(path, inplace=True): |
| 96 | + line = line.replace(OLD_CHROME_CONFIG_DIR, NEW_CHROME_CONFIG_DIR) |
| 97 | + line = line.replace(OLD_WIDEVINE_SYSTEM_DIR, "") |
| 98 | + print(line, end='') |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + res = subprocess.run(["flatpak", "info", "-o", "com.google.Chrome//stable"], |
| 103 | + capture_output=True) |
| 104 | + if res.stdout != b"flathub\n": |
| 105 | + print("Flathub Chrome has not been installed", file=sys.stderr) |
| 106 | + sys.exit(1) |
| 107 | + |
| 108 | + update_mimeapps_list(MIMEAPPS_LIST) |
| 109 | + |
| 110 | + if ( |
| 111 | + os.path.isdir(OLD_CHROME_CONFIG_DIR) and |
| 112 | + not os.path.isdir(NEW_CHROME_CONFIG_DIR) |
| 113 | + ): |
| 114 | + update_old_config_references( |
| 115 | + os.path.join(OLD_CHROME_CONFIG_DIR, |
| 116 | + "WidevineCdm", "latest-component-updated-widevine-cdm")) |
| 117 | + os.makedirs(os.path.dirname(NEW_CHROME_CONFIG_DIR), exist_ok=True) |
| 118 | + os.rename(OLD_CHROME_CONFIG_DIR, NEW_CHROME_CONFIG_DIR) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + main() |
0 commit comments