Jython on Documentum part 3

March 12, 2009 at 5:44 pm | Posted in Continous Integration, Jython | Leave a comment
Tags: , ,

It’s been a while since I’ve blogged and even longer since I blogged about Jython on Documentum. Sadly excessive work schedules have prevented me from blogging as much as I would like and it’s unlikely to change for the forseeable future. Anyway I’m waiting for a build to run so I’ve got 5 minutes for a coffee and a swift blog.

Since my last Jython on Documentum post I’ve been using jython in a real-live project as part of a build system. The next couple of posts will record some things that I found out.

First up is organising script files. In my previous posts I had just been using jython by changing to the installation directory, saving a .py file and running it from the installation directory. This is not really a great way to organise your script files. I’ve found the following organisation to work well though no doubt there are other ways to do the same thing.

First I have my jython installation in say c:\jython2.2.1. So anytime I want to kick off a jython script I do the following:

  • Start a cmd prompt
  • set path=%path%;c:\jython2.2.1
  • jython

Next I organise my jython scripts in the source code repository in their own folder structure. Something like /scripts/jython/dctm or something similar. The scripts can then be included in builds or deployments as necessary.

A useful object to know about is the sys.path object. This is represents the classpath that jython uses to pickup python libraries, this includes libraries you write yourself (just standard jython code files). You can easily import an external library using the following code snippet (which I have seared into my brain!):

import sys
import dctm

sys is a standard python library that contains a lot of useful system functions/features. One of them is the aforementioned sys.path. The append method allows you to add extra paths that jython can use to look for libraries. In this case the relative path script/jython/dctm is where I keep my standard dctm.py library. After these lines I can use the dctm object to access any of the methods in dctm.py.

So my standard dctm.py is stored scripts/jython/dctm, my project specific jython file (e.g. myfile.py) is also stored in this folder structure. By navigating to the top-level folder (the one containing the scripts folder) I can run jython like this:

jython myfile.py

Documentum and Jython – part Deux

May 16, 2008 at 8:22 pm | Posted in Jython | 1 Comment
Tags: ,

In my first post about the joys of Jython and Documentum I just showed some bare bones code to login and perform an action. One of the things I like is the jython interpreter where you can effectively run DFC line-by-line at the command prompt and see the effect. However all that typing can get a little tedious so it makes sense to wrap up some of the useful stuff into a module that can be imported into Jython.

First lets see how we can wrap the DFC connection code into a function that can be called multiple times. Fire up the jython interpreter and enter the following:

>>> from com.documentum.com import DfClientX
>>> def connect(docbase, username, password):
...  cx=DfClientX()
...  c  =cx.getLocalClient()
...  li  =cx.getLoginInfo()
...  li.setUser(username)
...  li.setPassword(password)
...  s = c.newSession(docbase, li)
...  return s

The first line is the standard import of the DfClientX class from which we can dynamically create (directly or indirectly) most of the other DFC classes.

The second line is the way to create functions in Jython. The def keyword starts a function definition. It is followed by the function name and then the parameters in brackets – Jython is not strictly typed so we don’t need to specify types in the function definition. The line ends with a colon. This is the standard python/jython way of indicating a multi-line statement block; unlike java there are no curly brackets to delimit statement blocks. Following the function definition line each line of the function must be indented. Every indented line up to and including the return statement is part of the function (notice the interpreter changes the >>> to ...). The function definition is ended by a blank line – after this the interpreter returns to the >>>.

Now we can simply call the function like this:

>>> s1 = connect("mydocbase","dmadmin","dmadmin")
>>> s2 = connect("mydocbase","user1","user1pass")

to give us 2 sessions, one for dmadmin and one for user1.

Now we could use the session to create an object:

doc = s1.newObject("dm_document")
doc.setObjectName("my document")

I think you get the idea!

But we don't want to have to create the connect function every time, so copy all the code into a separate file:

from com.documentum.com import DfClientX

def connect(docbase, username, password):
 c  =cx.getLocalClient()
 li  =cx.getLoginInfo()
 s = c.newSession(docbase, li)
 return s

Name it dctm.py (py is the standard python/jython script extension) and save it in the main Jython directory (where you installed jython which in my case is c:\jython2.2.1). Now restart the the jython interpreter and enter the following:

>>> import dctm
>>> s = dctm.connect("docbase2","dmadmin","dmadmin")

The import statement pulls in the code from the dctm.py script and makes the function available via the dctm namespace. Once we have the IDfSession object s we can again do some work:

doc2 = s.newObject("dm_document")
doc2.setObjectName("another document")


Blog at WordPress.com.
Entries and comments feeds.