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")


Using Jython to create users

April 4, 2008 at 4:51 pm | Posted in Jython | 2 Comments

A while back Bex Huff posted about using Jython to script Documentum tasks. I referenced that post in one of the answers I gave on the forums however I had never used Jython at that point. I made a note to try it out and so this post represents the beginning of my investigation. Consider a journey, possibly once started that never finishes….

First up you should be able to install Jython on any platform that runs Documentum (well any platform that runs DFC which is more or less the same thing). You can get the low down on the JPython site but the quick install steps are:

1) Download the installation, typically a file like jython_installer-2.2.1.jar
2) Execute the jar (there are command line invocations if you need it check on the web for details) to install.
3) For convenience add the installation path (e.g. c:\jython2.2.1)

All the instructions are based on a windows platform for convenience. It should translate to *nix without too much bother; I may try this out on a handy copy of Red Hat to confirm (but don’t hold your breath for that post!).

You could now run the Jython command interpreter in the following way:

1) start a command prompt
2) type ‘jython’

The following code will create a user called inline_user (it assumes you have a JRE of 1.4.2 or greater and DFC installed):

from com.documentum.com import DfClientX

clientx = DfClientX()
client  = clientx.getLocalClient()
li      = clientx.getLoginInfo()
li.setUser("dmadmin")            # login as installation owner
li.setPassword("dmadmin")         # insert install owner password here
s0      = client.newSession("fnet1",li)

print "Connected as dmadmin"

print "now creating user ..."

u = s0.newObject("dm_user")
# because we have to
u.setUserSourceAsString("inline password")
# IDfUser.setPassword seems to be missing from DFC5.3!!!

print "user inline_user has been created"

This code can either be typed directly into the Jython command line interface (a great way to test out dfc code snippets) or can be copied into a script file e.g. create_inline.py and called from the command line like this:

jython create_inline.py

Hopefully the user has been created sucessfully and you should now be able to test the login from the command line:

li2 = clientx.getLoginInfo()
s1 = client.newSession("fnet1",li2)
print "connected as " + s1.connectionConfig.getLoginUserName()

OK so this is a very simple example. There is no use of command line parameters, no user input, no functions and so on. Hopefully I’ll get round to further posts that expand and show examples of this. In the meantime enjoy.

Create a free website or blog at WordPress.com.
Entries and comments feeds.