Meng-Huan Yu | a775856 | 2019-11-28 18:40:24 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # |
| 3 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Factory toolkit uninstaller. |
| 8 | |
| 9 | Remove all factory toolkit related files on CrOS device. |
| 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | |
| 14 | import argparse |
| 15 | import os |
| 16 | import sys |
| 17 | |
| 18 | from six.moves import input |
| 19 | |
Meng-Huan Yu | a775856 | 2019-11-28 18:40:24 +0800 | [diff] [blame] | 20 | from cros.factory.test.env import paths |
| 21 | from cros.factory.tools import install_symlinks |
| 22 | from cros.factory.utils import file_utils |
| 23 | from cros.factory.utils import log_utils |
| 24 | from cros.factory.utils import process_utils |
| 25 | from cros.factory.utils import sys_utils |
| 26 | |
| 27 | HELP_HEADER = """ |
| 28 | Uninstall the factory toolkit on a CrOS device. |
| 29 | """ |
| 30 | |
Meng-Huan Yu | fd067ce | 2020-01-14 15:36:07 +0800 | [diff] [blame] | 31 | FACTORY_DIRS = ['/var/factory', '/run/factory', paths.FACTORY_DIR] |
Meng-Huan Yu | a775856 | 2019-11-28 18:40:24 +0800 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def AssertEnvironment(): |
| 35 | if not sys_utils.InCrOSDevice(): |
| 36 | raise Exception( |
| 37 | "ERROR: You're not on a CrOS device (for more details, please " |
| 38 | 'check sys_utils.py:InCrOSDevice). The uninstaller only works on ' |
| 39 | 'CrOS device.') |
| 40 | if os.getuid() != 0: |
| 41 | raise Exception('You must be root to uninstall the factory toolkit on a ' |
| 42 | 'CrOS device.') |
| 43 | |
| 44 | |
| 45 | def MakeWarningMessage(): |
| 46 | ret = file_utils.ReadFile(paths.FACTORY_TOOLKIT_VERSION_PATH) |
| 47 | ret += ( |
| 48 | '\n' |
| 49 | '\n' |
| 50 | '*** You are about to uninstall the factory toolkit at:\n') |
| 51 | |
| 52 | for p in FACTORY_DIRS: |
| 53 | ret += '*** %s\n' % p |
| 54 | |
| 55 | ret += '***' |
| 56 | |
| 57 | return ret |
| 58 | |
| 59 | |
| 60 | def Main(): |
| 61 | log_utils.InitLogging() |
| 62 | |
| 63 | parser = argparse.ArgumentParser( |
| 64 | description=HELP_HEADER, |
| 65 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 66 | parser.add_argument('--yes', '-y', action='store_true', |
| 67 | help="Don't ask for confirmation") |
| 68 | |
| 69 | args = parser.parse_args() |
| 70 | |
| 71 | AssertEnvironment() |
| 72 | |
| 73 | print(MakeWarningMessage()) |
| 74 | if not args.yes: |
| 75 | answer = input('*** Continue? [y/N] ') |
| 76 | if not answer or answer[0] not in 'yY': |
| 77 | sys.exit('Aborting.') |
| 78 | |
| 79 | # To recover the symlinks under /usr/local/bin. We need to re-create the links |
| 80 | # to factory-mini.par. |
| 81 | install_symlinks.UninstallSymlinks('/usr/local/bin', |
| 82 | install_symlinks.MODE_FULL) |
| 83 | install_symlinks.InstallSymlinks('../factory-mini/factory-mini.par', |
| 84 | '/usr/local/bin', install_symlinks.MODE_MINI) |
| 85 | |
| 86 | # Delete all factory related files. |
| 87 | for p in FACTORY_DIRS: |
| 88 | process_utils.Spawn(['rm', '-rf', p], check_call=True, log=True) |
| 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | Main() |