blob: 2e4593722e7f86d9ee5e6a112b5c8278fdc51623 [file] [log] [blame]
Yilin Yang19da6932019-12-10 13:39:28 +08001#!/usr/bin/env python3
Meng-Huan Yua7758562019-11-28 18:40:24 +08002#
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
9Remove all factory toolkit related files on CrOS device.
10"""
11
Meng-Huan Yua7758562019-11-28 18:40:24 +080012import argparse
13import os
14import sys
15
Meng-Huan Yua7758562019-11-28 18:40:24 +080016from cros.factory.test.env import paths
17from cros.factory.tools import install_symlinks
18from cros.factory.utils import file_utils
19from cros.factory.utils import log_utils
20from cros.factory.utils import process_utils
21from cros.factory.utils import sys_utils
22
23HELP_HEADER = """
24Uninstall the factory toolkit on a CrOS device.
25"""
26
Meng-Huan Yuf68f7892020-03-06 19:59:27 +080027FACTORY_PATHS = [
28 '/var/factory', '/run/factory', paths.FACTORY_DIR,
29 '/var/log/factory-init.log', '/var/log/factory-session.log',
30 '/var/log/factory.log'
31]
Meng-Huan Yua7758562019-11-28 18:40:24 +080032
33
34def 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
45def 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
Meng-Huan Yuf68f7892020-03-06 19:59:27 +080052 for p in FACTORY_PATHS:
Meng-Huan Yua7758562019-11-28 18:40:24 +080053 ret += '*** %s\n' % p
54
55 ret += '***'
56
57 return ret
58
59
60def 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.
Meng-Huan Yuf68f7892020-03-06 19:59:27 +080087 for p in FACTORY_PATHS:
Meng-Huan Yua7758562019-11-28 18:40:24 +080088 process_utils.Spawn(['rm', '-rf', p], check_call=True, log=True)
89
90
91if __name__ == '__main__':
92 Main()