uninstall-mmgen.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 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. """
  19. uninstall-mmgen.py - Uninstall MMGen from a system
  20. """
  21. import sys,os
  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: import mmgen.main
  29. except:
  30. sys.stderr.write('Failed to import mmgen.main module. Is MMGen installed?\n')
  31. sys.exit(1)
  32. modpath_save = sys.modules['mmgen.main'].__spec__.origin
  33. from mmgen.common import *
  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. cmd_args = opts.init(opts_data)
  46. if len(cmd_args): opts.usage()
  47. mod_dir = os.path.split(normalize_path(modpath_save))[0]
  48. mod_pardir = os.path.split(mod_dir)[0]
  49. ull = '/usr/local/lib/'
  50. ulb = '/usr/local/bin/'
  51. if curdir == mod_dir[:len(curdir)] or mod_dir[:len(ull)] != ull:
  52. die(1,"Can't find system install directory! Aborting")
  53. del_list = ['/usr/local/share/mmgen',mod_dir]
  54. import stat
  55. def is_reg(pn): return stat.S_ISREG(os.stat(pn).st_mode)
  56. def is_dir(pn): return stat.S_ISDIR(os.stat(pn).st_mode)
  57. for d in (ulb,mod_pardir):
  58. # add files only, not directories
  59. 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-']
  60. if opt.list_paths:
  61. die(1,'\n'.join(del_list))
  62. if not opt.no_prompt:
  63. m = 'Deleting the following paths and files:\n {}\nProceed?'
  64. if not keypress_confirm(m.format('\n '.join(del_list))):
  65. die(1,'Exiting at user request')
  66. import shutil
  67. for pn in del_list:
  68. if is_dir(pn): shutil.rmtree(pn)
  69. else: os.unlink(pn)
  70. msg('Deleted: ' + pn)