uninstall-mmgen.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. import sys,os
  19. from mmgen import opts
  20. from mmgen.cfg import gc,Config
  21. from mmgen.util import msg,die
  22. def normalize_path(p):
  23. return os.path.normpath(os.path.realpath(os.path.abspath(p)))
  24. curdir = normalize_path(os.curdir)
  25. for n in reversed(range(len(sys.path))):
  26. if normalize_path(sys.path[n]) == curdir:
  27. del sys.path[n]
  28. try:
  29. import mmgen.main
  30. except:
  31. sys.stderr.write('Failed to import mmgen.main module. Is MMGen installed?\n')
  32. sys.exit(1)
  33. modpath_save = sys.modules['mmgen.main'].__spec__.origin
  34. opts_data = {
  35. 'text': {
  36. 'desc': 'Remove MMGen from your system',
  37. 'usage': '[opts]',
  38. 'options': """
  39. -h, --help Print this help message
  40. -l, --list-paths List the directories and files that would be deleted
  41. -n, --no-prompt Don't prompt before deleting
  42. """
  43. }
  44. }
  45. cfg = Config(opts_data=opts_data)
  46. if gc.platform == 'linux' and os.getenv('USER') != 'root':
  47. die(1,'This program must be run as root')
  48. if len(cfg._args):
  49. cfg._opts.usage()
  50. mod_dir = os.path.split(normalize_path(modpath_save))[0]
  51. mod_pardir = os.path.split(mod_dir)[0]
  52. ull = '/usr/local/lib/'
  53. ulb = '/usr/local/bin/'
  54. if curdir == mod_dir[:len(curdir)] or mod_dir[:len(ull)] != ull:
  55. die(1,"Can't find system install directory! Aborting")
  56. del_list = ['/usr/local/share/mmgen',mod_dir]
  57. import stat
  58. def is_reg(pn): return stat.S_ISREG(os.stat(pn).st_mode)
  59. def is_dir(pn): return stat.S_ISDIR(os.stat(pn).st_mode)
  60. for d in (ulb,mod_pardir):
  61. # add files only, not directories
  62. del_list += [os.path.join(d,e) for e in os.listdir(d) if is_reg(os.path.join(d,e)) and e[:6] == 'mmgen-']
  63. if cfg.list_paths:
  64. die(1,'\n'.join(del_list))
  65. if not cfg.no_prompt:
  66. m = 'Deleting the following paths and files:\n {}\nProceed?'
  67. from mmgen.ui import keypress_confirm
  68. if not keypress_confirm( cfg, m.format('\n '.join(del_list)) ):
  69. die(1,'Exiting at user request')
  70. import shutil
  71. for pn in del_list:
  72. if is_dir(pn): shutil.rmtree(pn)
  73. else: os.unlink(pn)
  74. msg('Deleted: ' + pn)