GRASS Programmer's Manual  6.4.2(2012)
g.extension.rebuild.all.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 ############################################################################
00004 #
00005 # MODULE:       g.extension.rebuild.all
00006 #
00007 # AUTHOR(S):    Martin Landa <landa.martin gmail.com>
00008 #
00009 # PURPOSE:      Rebuild locally installed GRASS Addons extensions 
00010 #
00011 # COPYRIGHT:    (C) 2011 by Martin Landa, and the GRASS Development Team
00012 #
00013 #               This program is free software under the GNU General
00014 #               Public License (>=v2). Read the file COPYING that
00015 #               comes with GRASS for details.
00016 #
00017 #############################################################################
00018 
00019 #%module
00020 #% label: Rebuilds all locally installed GRASS Addons extensions.
00021 #% description: By default only extensions built against different GIS Library are rebuilt.
00022 #% keywords: general, installation, extensions
00023 #%end
00024 #%flag
00025 #% key: f
00026 #% description: Force to rebuild all extensions
00027 #% end
00028 
00029 import os
00030 import sys
00031 
00032 try:
00033     import xml.etree.ElementTree as etree
00034 except ImportError:
00035     import elementtree.ElementTree as etree # Python <= 2.4
00036 
00037 import grass.script as grass
00038 
00039 def get_extensions():
00040     addon_base = os.getenv('GRASS_ADDON_PATH')
00041     if not addon_base:
00042         grass.fatal(_("%s not defined") % "GRASS_ADDON_PATH")
00043     fXML = os.path.join(addon_base, 'modules.xml')
00044     if not os.path.exists(fXML):
00045         return []
00046 
00047     # read XML file
00048     fo = open(fXML, 'r')
00049     try:
00050         tree = etree.fromstring(fo.read())
00051     except StandardError, e:
00052         grass.error(_("Unable to parse metadata file: %s") % e)
00053         fo.close()
00054         return []
00055     
00056     fo.close()
00057     
00058     libgis_rev = grass.version()['libgis_revision']
00059     ret = list()
00060     for tnode in tree.findall('task'):
00061         gnode = tnode.find('libgis')
00062         if gnode is not None and \
00063                 gnode.get('revision', '') != libgis_rev:
00064             ret.append(tnode.get('name'))
00065     
00066     return ret
00067 
00068 def main():
00069     if flags['f']:
00070         extensions = grass.read_command('g.extension.py',
00071                                         quiet = True, flags = 'a').splitlines()
00072     else:
00073         extensions = get_extensions()
00074     
00075     if not extensions:
00076         grass.info(_("Nothing to rebuild."))
00077         return 0
00078     
00079     for ext in extensions:
00080         grass.message('-' * 60)
00081         grass.message(_("Reinstalling extension <%s>...") % ext)
00082         grass.message('-' * 60)
00083         grass.run_command('g.extension.py',
00084                           extension = ext)
00085     
00086     return 0
00087 
00088 if __name__ == "__main__":
00089     options, flags = grass.parser()
00090     sys.exit(main())
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines