#! /usr/bin/env python

# This script is used to export an HDF5 file with a particular revision number
# from the Subversion configuration management system.  After export the revision
# number and the file's path is added into the file as an attribute.
# All the HDF5 action is in the import command and the last 3 lines of real code.

# Arguments are Subversion rev number and Subversion path to file.  No error checking is included.

import sys
import os
import h5py


Rev = sys.argv[1]
SVNFilepath = sys.argv[2]

# Construct Subversion export command...

command = 'svn export -r ' + Rev + ' ' + SVNFilepath

# ...and execute

InStream = os.popen(command,'r')

# The commands output will contain the full path to the file

# Capture the command output and return code, but just ignore it.
# May be useful for debugging.

ExportString = InStream.read()

ExportReturnCode = InStream.close()

# Parse Subversion path into list of components

Elements = SVNFilepath.split('/')

# Last value of list Elements will be base name, which is opened via H5PY.

fid = h5py.File(Elements[-1]) # Take last one

# Update an atribute with a string containing Subversion path and revision number
fid.attrs['SVN Path and Revision'] = SVNFilepath + '@' + Rev

#close the file and done.

fid.close()

