blob: 60e148b9ed9d2bf0292b1529b6f3eb297eae2521 [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
Meng-Huan Yua7758562019-11-28 18:40:24 +080020from cros.factory.test.env import paths
21from cros.factory.tools import install_symlinks
22from cros.factory.utils import file_utils
23from cros.factory.utils import log_utils
24from cros.factory.utils import process_utils
25from cros.factory.utils import sys_utils
26
27HELP_HEADER = """
28Uninstall the factory toolkit on a CrOS device.
29"""
30
Meng-Huan Yufd067ce2020-01-14 15:36:07 +080031FACTORY_DIRS = ['/var/factory', '/run/factory', paths.FACTORY_DIR]
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
52 for p in FACTORY_DIRS:
53 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.
87 for p in FACTORY_DIRS:
88 process_utils.Spawn(['rm', '-rf', p], check_call=True, log=True)
89
90
91if __name__ == '__main__':
92 Main()