g.extension.all.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python3
  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. def get_extensions():
  46. addon_base = os.getenv("GRASS_ADDON_BASE")
  47. if not addon_base:
  48. gscript.fatal(_("%s not defined") % "GRASS_ADDON_BASE")
  49. fXML = os.path.join(addon_base, "modules.xml")
  50. if not os.path.exists(fXML):
  51. return []
  52. # read XML file
  53. fo = open(fXML, "r")
  54. try:
  55. tree = etree.fromstring(fo.read())
  56. except Exception as e:
  57. gscript.error(_("Unable to parse metadata file: %s") % e)
  58. fo.close()
  59. return []
  60. fo.close()
  61. libgis_rev = gscript.version()["libgis_revision"]
  62. ret = list()
  63. for tnode in tree.findall("task"):
  64. gnode = tnode.find("libgis")
  65. if gnode is not None and gnode.get("revision", "") != libgis_rev:
  66. ret.append(tnode.get("name"))
  67. return ret
  68. def main():
  69. remove = options["operation"] == "remove"
  70. if remove or flags["f"]:
  71. extensions = gscript.read_command(
  72. "g.extension", quiet=True, flags="a"
  73. ).splitlines()
  74. else:
  75. extensions = get_extensions()
  76. if not extensions:
  77. if remove:
  78. gscript.info(_("No extension found. Nothing to remove."))
  79. else:
  80. gscript.info(
  81. _("Nothing to rebuild. Rebuilding process can be forced with -f flag.")
  82. )
  83. return 0
  84. if remove and not flags["f"]:
  85. gscript.message(_("List of extensions to be removed:"))
  86. print(os.linesep.join(extensions))
  87. gscript.message(
  88. _("You must use the force flag (-f) to actually remove them. Exiting.")
  89. )
  90. return 0
  91. for ext in extensions:
  92. gscript.message("-" * 60)
  93. if remove:
  94. gscript.message(_("Removing extension <%s>...") % ext)
  95. else:
  96. gscript.message(_("Reinstalling extension <%s>...") % ext)
  97. gscript.message("-" * 60)
  98. if remove:
  99. operation = "remove"
  100. operation_flags = "f"
  101. else:
  102. operation = "add"
  103. operation_flags = ""
  104. try:
  105. gscript.run_command(
  106. "g.extension", flags=operation_flags, extension=ext, operation=operation
  107. )
  108. except CalledModuleError:
  109. gscript.error(_("Unable to process extension:%s") % ext)
  110. return 0
  111. if __name__ == "__main__":
  112. options, flags = gscript.parser()
  113. sys.exit(main())