on Software

Tool Images

30 Apr 2015

Occasionally you want to automate recurring tasks in your Smallworld™ GIS installation. It may sound trivial, but there are a number of different ways this can be done, and not all are equally well suited …

Most frequently encountered, and seemingly the natural way, is to use input redirection onto the CLI of a session:

sw_magik.exe -image images\open.msf -cli < scripts\task.magik

This looks good. Many command lines can be used that way, like the *nix shells and tools, and emacs in batch mode. EOF on STDIN means quit. But not for sw_magik. And this “uncommon” behaviour can cause problems. Amongst are the fact that you tend to not launch sw_magik through the gis.exe, and also there is the stuck process in case of a traceback.

There is a better solution: the Tool Image. It is a lot more convenient to have an image that, upon startup, does the task due, and then terminates. Doing things at startup is accomplished by specifying a startup proc when saving an image. Thus what you need is a _proc that executes your task.

_global tool << _proc()
  "images/_tool.msf" ,
  :redirection_file , "images/tool.msf" ,
  :startup_proc , tool ,
  :quit? , _true )

Many “task scripts” can be converted into this structure easily. You could add command line options before saving the image, or use environment variables to control runtime behaviour.

An example minimal gis_aliases entry for this image would look like:

    image     = images\tool.msf

and you start it like so:

gis.exe -a config\gis_aliases -e config\environment.bat tool

The advantages of this approach are numerous. Personal gains include reliable termination in case of an error, better environment control through the use of gis.exe and the enablement of continuous integration for Smallworld™ GIS.

I wanted to share the approach. But this is just the start. The mechanics presented here can be extended in a million ways. You read about command line arguments and environment settings above, but really, in your tool image you can do anything Smallworld™ GIS can do.

Addendum January 2016

I found another and more general way of doing this: command line options. It is super simple - register the parameters, write a proc to evaluate these parameters, add that as a startup proc and optionally make the docs look nice. ¡mira!

  _true )

  _proc @acefael()
    _local files << smallworld_product.get_option(:f)
    _for f _over files.elements()
      _if system.file_readable?( f )
        write(f," is not readable")
  _endproc )

session_startup.magik is typically found in image modules. To get the help straight you can create a message for your option:

plain resources/en_gb/smallworld_product.msg :cli_f #1 (#2) uses load_file to evaluate files passed on the command line

You can see the message by passing -help to your image.

When started from sw_magik.exe the session kindly throws and terminates in case of a traceback.