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