GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g.extension.rebuild.all.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ############################################################################
4 #
5 # MODULE: g.extension.rebuild.all
6 #
7 # AUTHOR(S): Martin Landa <landa.martin gmail.com>
8 #
9 # PURPOSE: Rebuild locally installed GRASS Addons extensions
10 #
11 # COPYRIGHT: (C) 2011 by Martin Landa, and the GRASS Development Team
12 #
13 # This program is free software under the GNU General
14 # Public License (>=v2). Read the file COPYING that
15 # comes with GRASS for details.
16 #
17 #############################################################################
18 
19 #%module
20 #% label: Rebuilds all locally installed GRASS Addons extensions.
21 #% description: By default only extensions built against different GIS Library are rebuilt.
22 #% keywords: general, installation, extensions
23 #%end
24 #%flag
25 #% key: f
26 #% description: Force to rebuild all extensions
27 #% end
28 
29 import os
30 import sys
31 
32 try:
33  import xml.etree.ElementTree as etree
34 except ImportError:
35  import elementtree.ElementTree as etree # Python <= 2.4
36 
37 import grass.script as grass
38 
40  addon_base = os.getenv('GRASS_ADDON_PATH')
41  if not addon_base:
42  grass.fatal(_("%s not defined") % "GRASS_ADDON_PATH")
43  fXML = os.path.join(addon_base, 'modules.xml')
44  if not os.path.exists(fXML):
45  return []
46 
47  # read XML file
48  fo = open(fXML, 'r')
49  try:
50  tree = etree.fromstring(fo.read())
51  except StandardError, e:
52  grass.error(_("Unable to parse metadata file: %s") % e)
53  fo.close()
54  return []
55 
56  fo.close()
57 
58  libgis_rev = grass.version()['libgis_revision']
59  ret = list()
60  for tnode in tree.findall('task'):
61  gnode = tnode.find('libgis')
62  if gnode is not None and \
63  gnode.get('revision', '') != libgis_rev:
64  ret.append(tnode.get('name'))
65 
66  return ret
67 
68 def main():
69  if flags['f']:
70  extensions = grass.read_command('g.extension.py',
71  quiet = True, flags = 'a').splitlines()
72  else:
73  extensions = get_extensions()
74 
75  if not extensions:
76  grass.info(_("Nothing to rebuild."))
77  return 0
78 
79  for ext in extensions:
80  grass.message('-' * 60)
81  grass.message(_("Reinstalling extension <%s>...") % ext)
82  grass.message('-' * 60)
83  grass.run_command('g.extension.py',
84  extension = ext)
85 
86  return 0
87 
88 if __name__ == "__main__":
89  options, flags = grass.parser()
90  sys.exit(main())