SAGA Python Examples

0.9.2


1. Test the Python bindings

NOTE: If you want to use the Python bindings, you have to add the installation directory to your $PYTHONPATH:

export PYTHONPATH=$SAGA_LOCATION/lib/python2.5/site-packages/

You can test the Python bindings using the Python interpreter from the commandline. The following set of commands should display the interface definition for the SAGA file package:

> python Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import saga
>>> help(saga.file)

If this works, the SAGA Python binding seems to be installed properly on your system.


2. Using the saga.file package

This example shows how to use the saga.file package to copy a file from the local system to a (remote) Globus GridFTP location. Please note that this example will only work if you have the SAGA Globus Adaptors installed and a valid X.509 Grid Proxy (grid-proxy-init).

[Download source code]

# Copyright (c) 2005-2008 Ole Weidner (oweidner@cct.lsu.edu)
#
# Use, modification and distribution is subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

import saga

try:
  source_url = saga.url("file:////etc/profile")
  target_url = saga.url("gridftp://gg201.cct.lsu.edu//tmp/")

  my_file = saga.file.file(source_url)
  my_file.copy(target_url)

except saga.exception, e:
  print "SAGA Error: ", e

3. Using the saga.job package

This example shows how to use the saga.job package to submit a simple, non-interactive job to a (remote) GRAM gatekeeper. Please note that this example will only work if you have the SAGA Globus Adaptors installed and a valid X.509 Grid Proxy (grid-proxy-init).

[Download source code]

# Copyright (c) 2005-2008 Ole Weidner (oweidner@cct.lsu.edu)
#
# Use, modification and distribution is subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

import saga

try:
  js_url = saga.url("gram://gg201.cct.lsu.edu/")
  job_service = saga.job.service(js_url)

  job_desc = saga.job.description()
  job_desc.executable = "/bin/touch"
  job_desc.arguments = ["-a", "/tmp/aloha"]

  my_job = job_service.create_job(job_desc)
  my_job.run()

except saga.exception, e:
  print "SAGA Error: ", e

4. Using the saga.advert package

This example shows how to use the saga.advert package to to connect to a (remote) Advert database, create an entry and associate some metadata with it. Please note that you need the default advert adaptor with PostgreSQL support compiled to access remote Advert databases.

[Download source code]

# Copyright (c) 2005-2008 Ole Weidner (oweidner@cct.lsu.edu)
#
# Use, modification and distribution is subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

import saga

try:
  permission = saga.advert.entry.ReadWrite
  my_base_url = saga.url("advert://fortytwo.cct.lsu.edu:5432/")

  my_advert_dir = saga.advert.directory(my_base_url)
  my_advert = my_advert_dir.open("./my_entry", permission | saga.advert.entry.Create)

  my_advert.set_attribute("creator","oweidner")
  print my_advert.get_attribute("creator")

except saga.exception, e:
  print "SAGA Error: ", e

5. Using the saga.replica package

This example shows how to use the saga.replica package to to connect to a (remote) Globus RLS server, query the replica locations for an entry and play around with the attributes. Please note that this example will only work if you have the SAGA Globus Adaptors installed and a valid X.509 Grid Proxy (grid-proxy-init).

[Download source code]

# Copyright (c) 2005-2008 Ole Weidner (oweidner@cct.lsu.edu)
#
# Use, modification and distribution is subject to the Boost Software
# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

import saga

try:
  permission = saga.advert.entry.ReadWrite
  lfn_url = saga.url("rls://gridhub.cct.lsu.edu:39281//python/apidoc/index.html")
  
  lfn_entry = saga.replica.entry(lfn_url, permission)
  ## add an attribute to lfn_entry
  lfn_entry.set_attribute("creator","oweidner")

  ## add another pysical location
  lfn_entry.add_location(saga.url("file://my/local/copy"))
  
  ## list all physical file names for lfn_entry
  ## list all physical file names for lfn_entry
  for e in lfn_entry.list_locations():
    print e
  
  ## remove our pysical location and the attribute again
  lfn_entry.remove_location(saga.url("file://my/local/copy"))
  
except saga.exception, e:
  print "SAGA Error: ", e