g.extension.all.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: g.extension.all
  5. #
  6. # AUTHOR(S): Martin Landa <landa.martin gmail.com>
  7. #
  8. # PURPOSE: Rebuilds or removes locally installed GRASS Addons extensions
  9. #
  10. # COPYRIGHT: (C) 2011-2013 by Martin Landa, and the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General
  13. # Public License (>=v2). Read the file COPYING that
  14. # comes with GRASS for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% label: Rebuilds or removes all locally installed GRASS Addons extensions.
  19. #% description: By default only extensions built against different GIS Library are rebuilt.
  20. #% keyword: general
  21. #% keyword: installation
  22. #% keyword: extensions
  23. #%end
  24. #%option
  25. #% key: operation
  26. #% type: string
  27. #% description: Operation to be performed
  28. #% required: no
  29. #% options: rebuild,remove
  30. #% answer: rebuild
  31. #%end
  32. #%flag
  33. #% key: f
  34. #% label: Force operation (required for removal)
  35. #% end
  36. from __future__ import print_function
  37. import os
  38. import sys
  39. try:
  40. import xml.etree.ElementTree as etree
  41. except ImportError:
  42. import elementtree.ElementTree as etree # Python <= 2.4
  43. import grass.script as gscript
  44. from grass.exceptions import CalledModuleError
  45. # i18N
  46. import gettext
  47. gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
  48. def get_extensions():
  49. addon_base = os.getenv('GRASS_ADDON_BASE')
  50. if not addon_base:
  51. gscript.fatal(_("%s not defined") % "GRASS_ADDON_BASE")
  52. fXML = os.path.join(addon_base, 'modules.xml')
  53. if not os.path.exists(fXML):
  54. return []
  55. # read XML file
  56. fo = open(fXML, 'r')
  57. try:
  58. tree = etree.fromstring(fo.read())
  59. except Exception as e:
  60. gscript.error(_("Unable to parse metadata file: %s") % e)
  61. fo.close()
  62. return []
  63. fo.close()
  64. libgis_rev = gscript.version()['libgis_revision']
  65. ret = list()
  66. for tnode in tree.findall('task'):
  67. gnode = tnode.find('libgis')
  68. if gnode is not None and \
  69. gnode.get('revision', '') != libgis_rev:
  70. ret.append(tnode.get('name'))
  71. return ret
  72. def main():
  73. remove = options['operation'] == 'remove'
  74. if remove or flags['f']:
  75. extensions = gscript.read_command(
  76. 'g.extension',
  77. quiet=True,
  78. flags='a').splitlines()
  79. else:
  80. extensions = get_extensions()
  81. if not extensions:
  82. if remove:
  83. gscript.info(_("No extension found. Nothing to remove."))
  84. else:
  85. gscript.info(
  86. _("Nothing to rebuild. Rebuilding process can be forced with -f flag."))
  87. return 0
  88. if remove and not flags['f']:
  89. gscript.message(_("List of extensions to be removed:"))
  90. print(os.linesep.join(extensions))
  91. gscript.message(
  92. _("You must use the force flag (-f) to actually remove them. Exiting."))
  93. return 0
  94. for ext in extensions:
  95. gscript.message('-' * 60)
  96. if remove:
  97. gscript.message(_("Removing extension <%s>...") % ext)
  98. else:
  99. gscript.message(_("Reinstalling extension <%s>...") % ext)
  100. gscript.message('-' * 60)
  101. if remove:
  102. operation = 'remove'
  103. operation_flags = 'f'
  104. else:
  105. operation = 'add'
  106. operation_flags = ''
  107. try:
  108. gscript.run_command('g.extension', flags=operation_flags,
  109. extension=ext, operation=operation)
  110. except CalledModuleError:
  111. gscript.error(_("Unable to process extension:%s") % ext)
  112. return 0
  113. if __name__ == "__main__":
  114. options, flags = gscript.parser()
  115. sys.exit(main())