[GRASS-SVN] r49608 - grass/branches/develbranch_6/gui/scripts

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 7 07:55:54 EST 2011


Author: martinl
Date: 2011-12-07 04:55:54 -0800 (Wed, 07 Dec 2011)
New Revision: 49608

Modified:
   grass/branches/develbranch_6/gui/scripts/g.extension.py
Log:
g.extension.py: sync with trunk (keep G6-specific code)


Modified: grass/branches/develbranch_6/gui/scripts/g.extension.py
===================================================================
--- grass/branches/develbranch_6/gui/scripts/g.extension.py	2011-12-07 12:30:25 UTC (rev 49607)
+++ grass/branches/develbranch_6/gui/scripts/g.extension.py	2011-12-07 12:55:54 UTC (rev 49608)
@@ -81,6 +81,10 @@
 #% key: i
 #% description: Don't install new extension, just compile it
 #%end
+#%flag
+#% key: f
+#% description: Force removal when uninstalling extension (operation=remove)
+#%end
 
 import os
 import sys
@@ -104,11 +108,13 @@
 # temp dir
 remove_tmpdir = True
 
+# check requirements
 def check():
     for prog in ('svn', 'make', 'gcc'):
         if not grass.find_program(prog, ['--help']):
             grass.fatal(_("'%s' required. Please install '%s' first.") % (prog, prog))
     
+# expand prefix to class name
 def expand_module_class_name(c):
     name = { 'd'   : 'display',
              'db'  : 'database',
@@ -128,6 +134,7 @@
     
     return c
 
+# list modules (read XML file from grass.osgeo.org/addons)
 def list_available_modules():
     mlist = list()
 
@@ -159,19 +166,21 @@
     
     return mlist
 
+# list modules (scan SVN repo)
 def list_available_modules_svn():
     mlist = list()
     grass.message(_('Fetching list of modules from GRASS-Addons SVN (be patient)...'))
     pattern = re.compile(r'(<li><a href=".+">)(.+)(</a></li>)', re.IGNORECASE)
-    i = 0
+
+    if flags['c']:
+        grass.warning(_("Flag 'c' ignored, metadata file not available"))
+    if flags['g']:
+        grass.warning(_("Flag 'g' ignored, metadata file not available"))
+        
     prefix = ['d', 'db', 'g', 'i', 'm', 'ps',
               'p', 'r', 'r3', 's', 'v']
     nprefix = len(prefix)
     for d in prefix:
-        if flags['g']:
-            grass.percent(i, nprefix, 1)
-            i += 1
-        
         modclass = expand_module_class_name(d)
         grass.verbose(_("Checking for '%s' modules...") % modclass)
         
@@ -190,16 +199,14 @@
                 continue
             name = sline.group(2).rstrip('/')
             if name.split('.', 1)[0] == d:
-                print_module_desc(name, url)
+                print name
                 mlist.append(name)
     
     mlist += list_wxgui_extensions()
-    
-    if flags['g']:
-        grass.percent(1, 1, 1)
-    
+        
     return mlist
 
+# list wxGUI extensions
 def list_wxgui_extensions(print_module = True):
     mlist = list()
     grass.debug('Fetching list of wxGUI extensions from GRASS-Addons SVN (be patient)...')
@@ -208,11 +215,10 @@
     
     url = '%s/%s' % (options['svnurl'], 'gui/wxpython')
     grass.debug("url = %s" % url, debug = 2)
-    try:
-        f = urlopen(url)
-    except HTTPError:
-        grass.debug("Unable to fetch '%s'" % url, debug = 1)
-        return mlist
+    f = urlopen(url)
+    if not f:
+        grass.warning(_("Unable to fetch '%s'") % url)
+        return
         
     for line in f.readlines():
         # list modules
@@ -222,116 +228,19 @@
         name = sline.group(2).rstrip('/')
         if name not in ('..', 'Makefile'):
             if print_module:
-                print_module_desc(name, url)
+                print name
             mlist.append(name)
     
     return mlist
 
-def print_module_desc(name, url):
-    if not flags['c'] and not flags['g']:
-        print name
-        return
-    
-    if flags['g']:
-        print 'name=' + name
-    
-    # check main.c first
-    desc = get_module_desc(url + '/' + name + '/' + name)
-    if not desc:
-        desc = get_module_desc(url + '/' + name + '/main.c', script = False)
-    if not desc:
-        if not flags['g']:
-            print name + ' - '
-            return
-    
-    if flags['g']:
-        print 'description=' + desc.get('description', '')
-        print 'keywords=' + ','.join(desc.get('keywords', list()))
-    else:
-        print name + ' - ' + desc.get('description', '')
-    
-def get_module_desc(url, script = True):
-    grass.debug('url=%s' % url)
-    try:
-        f = urlopen(url)
-    except HTTPError:
-        return {}
-    
-    if script:
-        ret = get_module_script(f)
-    else:
-        ret = get_module_main(f)
-        
-    return ret
-
-def get_module_main(f):
-    if not f:
-        return dict()
-    
-    ret = { 'keyword' : list() }
-    
-    pattern = re.compile(r'(module.*->)(.+)(=)(.*)', re.IGNORECASE)
-    keyword = re.compile(r'(G_add_keyword\()(.+)(\);)', re.IGNORECASE)
-    
-    key   = ''
-    value = ''
-    for line in f.readlines():
-        line = line.strip()
-        find = pattern.search(line)
-        if find:
-            key = find.group(2).strip()
-            line = find.group(4).strip()
-        else:
-            find = keyword.search(line)
-            if find:
-                ret['keyword'].append(find.group(2).replace('"', '').replace('_(', '').replace(')', ''))
-        if key:
-            value += line
-            if line[-2:] == ');':
-                value = value.replace('"', '').replace('_(', '').replace(');', '')
-                if key == 'keywords':
-                    ret[key] = map(lambda x: x.strip(), value.split(','))
-                else:
-                    ret[key] = value
-                
-                key = value = ''
-    
-    return ret
-
-def get_module_script(f):
-    ret = dict()
-    if not f:
-        return ret
-    
-    begin = re.compile(r'#%.*module', re.IGNORECASE)
-    end   = re.compile(r'#%.*end', re.IGNORECASE)
-    mline = None
-    for line in f.readlines():
-        if not mline:
-            mline = begin.search(line)
-        if mline:
-            if end.search(line):
-                break
-            try:
-                key, value = line.split(':', 1)
-                key = key.replace('#%', '').strip()
-                value = value.strip()
-                if key == 'keywords':
-                    ret[key] = map(lambda x: x.strip(), value.split(','))
-                else:
-                    ret[key] = value
-            except ValueError:
-                pass
-    
-    return ret
-
 def cleanup():
     if remove_tmpdir:
         grass.try_rmdir(tmpdir)
     else:
         grass.message(_("Path to the source code:"))
         sys.stderr.write('%s\n' % os.path.join(tmpdir, options['extension']))
-                        
+
+# install extension on MS Windows
 def install_extension_win():
     ### TODO: do not use hardcoded url
     version = grass.version()['version'].split('.')
@@ -362,6 +271,7 @@
     except HTTPError:
         grass.fatal(_("GRASS Addons <%s> not found") % options['extension'])
     
+# install extension
 def install_extension():
     gisbase = os.getenv('GISBASE')
     if not gisbase:
@@ -432,8 +342,8 @@
         grass.warning(_('This add-on module will not function until you set the '
                         'GRASS_ADDON_PATH environment variable (see "g.manual variables")'))
 
+# install extension on other plaforms
 def install_extension_other():
-    # todo: rip all this out and replace it with a call to the g.extension shell script.
     gisbase = os.getenv('GISBASE')
     gui_list = list_wxgui_extensions(print_module = False)
 
@@ -446,7 +356,7 @@
         if not flags['s']:
             grass.fatal(_("Installation of wxGUI extension requires -%s flag.") % 's')
         
-    grass.message(_("Fetching '%s' from GRASS-Addons SVN (be patient)...") % options['extension'])
+    grass.message(_("Fetching <%s> from GRASS-Addons SVN (be patient)...") % options['extension'])
     
     os.chdir(tmpdir)
     if grass.verbosity() == 0:
@@ -492,7 +402,7 @@
     
     os.chdir(os.path.join(tmpdir, options['extension']))
     
-    grass.message(_("Compiling '%s'...") % options['extension'])    
+    grass.message(_("Compiling <%s>...") % options['extension'])    
     if options['extension'] not in gui_list:
         ret = grass.call(makeCmd,
                          stdout = outdev)
@@ -507,7 +417,7 @@
     if flags['i'] or options['extension'] in gui_list:
         return
     
-    grass.message(_("Installing '%s'...") % options['extension'])
+    grass.message(_("Installing <%s>...") % options['extension'])
     
     ret = grass.call(installCmd,
                      stdout = outdev)
@@ -515,7 +425,7 @@
     if ret != 0:
         grass.warning(_('Installation failed, sorry. Please check above error messages.'))
     else:
-        grass.message(_("Installation of '%s' successfully finished.") % options['extension'])
+        grass.message(_("Installation of <%s> successfully finished.") % options['extension'])
     
     # cleanup build cruft
     if not flags['s']:
@@ -526,35 +436,46 @@
         if os.path.exists(os.path.join(options['prefix'], 'scripts', options['extension'])):
             shutil.move(os.path.join(options['prefix'], 'scripts', options['extension']),
                         os.path.join(options['prefix'], options['extension']))
-
+        
         # if empty, rmdir scripts/ and bin/
         if os.path.exists(os.path.join(options['prefix'], 'bin')):
             if os.listdir(os.path.join(options['prefix'], 'bin')) == []:
                 os.removedirs(os.path.join(options['prefix'], 'bin'))
-
+        
         if os.path.exists(os.path.join(options['prefix'], 'scripts')):
             if os.listdir(os.path.join(options['prefix'], 'scripts')) == []:
                 os.removedirs(os.path.join(options['prefix'], 'scripts'))
-
+        
         # move man/ into docs/
         if os.path.exists(os.path.join(options['prefix'], 'man', 'man1', options['extension'] + '.1')):
             shutil.move(os.path.join(options['prefix'], 'man', 'man1', options['extension'] + '.1'),
                         os.path.join(options['prefix'], 'docs', 'man', 'man1', options['extension'] + '.1'))
-
+        
         # if empty, rmdir man/man1
         if os.path.exists(os.path.join(options['prefix'], 'man', 'man1')):
             if os.listdir(os.path.join(options['prefix'], 'man', 'man1')) == []:
                 os.removedirs(os.path.join(options['prefix'], 'man', 'man1'))
+        
         # if empty, rmdir man/
         if os.path.exists(os.path.join(options['prefix'], 'man')):
             if os.listdir(os.path.join(options['prefix'], 'man')) == []:
                 os.removedirs(os.path.join(options['prefix'], 'man'))
+    
+    if not os.environ.has_key('GRASS_ADDON_PATH') or \
+            not os.environ['GRASS_ADDON_PATH']:
+        grass.warning(_('This add-on module will not function until you set the '
+                        'GRASS_ADDON_PATH environment variable (see "g.manual variables")'))
 
-def remove_extension():
-    # the following relies on an online file manifest for each module; remove_extension_std() does not.
+# remove existing extension (reading XML file)
+def remove_extension(force = False):
     # try to download XML metadata file first
     url = "http://grass.osgeo.org/addons/grass%s.xml" % grass.version()['version'].split('.')[0]
     name = options['extension']
+    if force:
+        grass.verbose(_("List of removed files:"))
+    else:
+        grass.info(_("Files to be removed (use flag 'f' to force removal):"))
+    
     try:
         f = urlopen(url)
         tree = etree.fromstring(f.read())
@@ -573,66 +494,65 @@
                                 fpath[-1] += '.py'
                         
                         flist.append(fpath)
+        
         if flist:
             removed = False
             err = list()
             for f in flist:
                 fpath = os.path.join(options['prefix'], os.path.sep.join(f))
                 try:
-                    os.remove(fpath)
-                    removed = True
+                    if force:
+                        grass.verbose(fpath)
+                        os.remove(fpath)
+                        removed = True
+                    else:
+                        print fpath
                 except OSError:
                     err.append((_("Unable to remove file '%s'") % fpath))
-            if not removed:
+            if force and not removed:
                 grass.fatal(_("Extension <%s> not found") % options['extension'])
             
             if err:
                 for e in err:
                     grass.error(e)
         else:
-            remove_extension_std()
+            remove_extension_std(force)
     except HTTPError:
-        remove_extension_std()
+        remove_extension_std(force)
 
-    # symlink for binaries needed, see http://trac.osgeo.org/grass/changeset/49124
-    grass.try_remove(os.path.join(options['prefix'], options['extension']))
+    if force:
+        grass.message(_("Extension <%s> successfully uninstalled.") % options['extension'])
+    else:
+        grass.warning(_("Extension <%s> not removed. "
+                        "Re-run '%s' with 'f' flag to force removal") % (options['extension'], 'g.extension'))
     
-    grass.message(_("Extension <%s> successfully uninstalled.") % options['extension'])
+# remove exising extension (using standard files layout)
+def remove_extension_std(force = False):
+    # is module available?
+    if not os.path.exists(os.path.join(options['prefix'], 'bin', options['extension'])):
+        grass.fatal(_("Extension <%s> not found") % options['extension'])
     
-def remove_extension_std():
-    # is module available?     what if the user has leftover cruft to uninstall?
-    #if not os.path.exists(os.path.join(options['prefix'], options['extension'])):
-    #    grass.fatal(_("Extension <%s> not found") % options['extension'])
-    for file in [os.path.join(options['prefix'], options['extension']),
-                 os.path.join(options['prefix'], 'bin', options['extension']),
-                 os.path.join(options['prefix'], 'scripts', options['extension']),
-                 os.path.join(options['prefix'], 'docs', 'html', options['extension'] + '.html'),
-                 os.path.join(options['prefix'], 'docs', 'man', 'man1', options['extension'] + '.1'),
-                 os.path.join(options['prefix'], 'man', 'man1', options['extension'] + '.1')]:
-        if os.path.isfile(file):
-            #grass.message(_("Removing <%s> ..." % file))
-            os.remove(file)
-
-def create_dir(path):
-    if os.path.isdir(path):
-        return
+    for fpath in [os.path.join(options['prefix'], 'bin', options['extension']),
+                  os.path.join(options['prefix'], 'scripts', options['extension']),
+                  os.path.join(options['prefix'], 'docs', 'html', options['extension'] + '.html'),
+                  os.path.join(options['prefix'], 'man', 'man1', options['extension'] + '.1')]:
+        if os.path.isfile(fpath):
+            if force:
+                grass.verbose(fpath)
+                os.remove(fpath)
+            else:
+                print fpath
     
-    try:
-        os.makedirs(path)
-    except OSError, e:
-        grass.fatal(_("Unable to create '%s': %s") % (path, e))
-    
-    grass.debug("'%s' created" % path)
-
+# check links in CSS
 def check_style_files(fil):
-    #check the links to grassdocs.css/grass_logo.png to a correct manual page of addons
+    # check the links to grassdocs.css/grass_logo.png to a correct manual page of addons
     dist_file = os.path.join(os.getenv('GISBASE'), 'docs', 'html', fil)
     addons_file = os.path.join(options['prefix'], 'docs', 'html', fil)
-    #check if file already exists in the grass addons docs html path
+    # check if file already exists in the grass addons docs html path
     if os.path.isfile(addons_file):
 	return
-    #otherwise copy the file from $GISBASE/docs/html, it doesn't use link 
-    #because os.symlink it work only in Linux
+    # otherwise copy the file from $GISBASE/docs/html, it doesn't use link 
+    # because os.symlink it work only in Linux
     else:
 	try:
 	    shutil.copyfile(dist_file,addons_file)
@@ -640,11 +560,6 @@
 	    grass.fatal(_("Unable to create '%s': %s") % (addons_file, e))
 
 def check_dirs():
-    create_dir(os.path.join(options['prefix'], 'bin'))
-    create_dir(os.path.join(options['prefix'], 'scripts'))
-    create_dir(os.path.join(options['prefix'], 'man', 'man1'))
-    create_dir(os.path.join(options['prefix'], 'docs', 'man', 'man1'))
-    create_dir(os.path.join(options['prefix'], 'docs', 'html'))
     check_style_files('grass_logo.png')
     check_style_files('grassdocs.css')    
 
@@ -693,7 +608,7 @@
     if options['operation'] == 'add':
         install_extension()
     else: # remove
-        remove_extension()
+        remove_extension(flags['f'])
     
     return 0
 



More information about the grass-commit mailing list