Generating reports is a necessary part of the software development process. However, having to generate them frequently and manually can often be time-consuming and error-prone. This section looks at how to use cleartool commands and Perl to automate the generation of Base ClearCase baseline reports. Using the ClearCase Command LineThe ClearCase command line (and, in particular, the cleartool command) is one of the main mechanisms for automating the generation of baseline reports. ClearCase has a UNIX heritage; you can see this in how its command line is used. If you are a Windows user, the command line might take a bit of getting used to, but once you understand its power and capabilities, I'm sure you will find it extremely useful. Since a ClearCase VOB is essentially a database, you might expect a SQL interface to be available for querying it. Unfortunately, this is not the case; instead, ClearCase has its own query language, which is somewhat cryptic. This language supports a number of primitives such as brtype, created_by, and version for querying ClearCase objects such as branches and elements. It also supports logical and comparison operators similar to those found in the C programming language. A query typically searches one or more VOBs and returns information about the objects you specify. For example, the following command could be used to display the version of an element with a specific label: >cleartool describe -short -version "{lbtype(RATLBANK_01_INT)}"Account.java Account.java@@\main\2 In this example the cleartool describe command is used with the -short parameter to limit the display of the ClearCase element's object information to just its pathname. The ClearCase query is specified via a version selector and executes a query to find the version of the element that is labeled with RATLBANK_01_INT (and on any branch). You can carry out similar queries for ClearCase branches, attributes, and users. The full list of primitives and operators is described in detail on the ClearCase query_language manual page.
The query language is particularly powerful when used in combination with the cleartool find command, because you can return information on a set of objects rather than an individual object. For example, to find all elements labeled with RATLBANK_01_INT, you could execute the following: >cleartool find . -version "lbtype(RATLBANK_01_INT)" -print .\src\com\ratlbank@@\main\3 .\src\com\ratlbank\exception@@\main\2 .\src\com\ratlbank\model@@\main\2 .\src\com\ratlbank\model\Account.java@@\main\2 .\src\com\ratlbank\model\Bank.java@@\main\14 .\src\com\ratlbank\model\Customer.java@@\main\2 .\src\com\ratlbank\model\TransRecord.java@@\main\1 ... In this example the cleartool find command navigates from the current directory downward. For each element it checks whether the label RATLBANK_01_INT has been applied. If it has, the element name and version number are printed. As you can probably imagine, this command could be used as the beginnings of a baseline report. You will notice from the commands executed so far that the output is somewhat limited. If you want to customize this output, you can use the -fmt option. It is supported by a number of cleartool commands, such as describe, lshistory, and lscheckout. For example, you could modify the previous cleartool describe example to display information about the element, version, owner, last modified date, and check-in comment:
In this example you will see a number of conversion specifications, such as %En and %c, and also a number of escape sequences, such as \n for a new line. Together these produce a formatted string, rather like the C language printf statement. There are a vast array of conversion specifications, some of which have modifiers. For example, %n specifies an element's full name, including its version specification. Additionally including an E modifier, such as %En, limits the display to the element's name. For more information on format strings, refer to the ClearCase fmt_ccase manual page. With the commands just described, you should now have enough information to be able to generate a simple baseline report that compares two labels, nominally the current and previous builds, and that displays which elements have changed between them. A Windows batch file to achieve this is given in Listing 8.1. Listing 8.1. Windows baseline_report.bat File
It would be a relatively simple task to convert this batch file to a Linux or UNIX shell script, since the cleartool commands are invoked in exactly the same way. If this batch file were executed, you would see output similar to the following: >baseline_report.bat RATLBANK_01_INT RATLBANK_02_INT CLEARCASE BASELINE REPORT First Label: RATLBANK_01_INT created: 11-Jan-06 by: alex status: locked Second Label: RATLBANK_02_INT created: 12-Jan-06 by: chris status: locked ELEMENT VERSIONS CHANGED BETWEEN RATLBANK_01_INT AND RATLBANK_02_INT: C:\Views\RatlBank_bld\RatlBank\build.xml@@\main\8 C:\Views\RatlBank_bld\RatlBank\src\com\ratlbank\model\Account.java@@\main\2 C:\Views\RatlBank_bld\RatlBank\src\com\ratlbank\model\Bank.java@@\main\14 C:\Views\RatlBank_bld\RatlBank\src\com\ratlbank\model\Customer.java@@\main\12 ... As you can see, this gives you all the information you need about any Base ClearCase label. However, the output format is limited to what the cleartool command can produce. It would be nice to script up the sequence of commands and manipulate the information to produce a nicely formatted report. This type of scripting is probably beyond the capabilities of simple Windows batch files or UNIX shell scripts. What is needed is a Practical Extraction and Reporting Languagewhich is where Perl comes in. Using PerlAs its "expanded" name suggests, Perl is a great language for manipulating and reporting on textual information. Through its powerful built-in support for regular expressions and pattern matching, you can easily search, match, and replace text patterns, thus allowing you to construct more readable baseline or change request reports. To execute Perl scripts, it is recommended that you use the executable called ratlperl. This is a common shared component that is shipped with both ClearCase and ClearQuest. The main difference between Perl scripts and something like Windows batch files is that it is easy to retrieve the output of a cleartool command and manipulate it. For example, to retrieve the output of a cleartool command and place it in a variable, you could use the following: my $output = 'cleartool describe -short -version "{lbtype(RATLBANK_01_INT)}" Account.java'; print "Version is: $output\n"; This places the output of the cleartool command in the Perl variable $output and prints it as follows: Version is: Account.java@@\main\11 Once you have the output in a variable, you can start manipulating it. For example, you can use Perl's split function to separate the element's name and version: my ($elem_name, $elem_ver) = split /@@/, $output; print "Element $elem_name "; print "is at version $elem_ver\n"; This produces the following output: Element Account.java is at version \main\11 This example works by splitting the line returned by the cleartool command at the ClearCase special @@ symbols, the variables $elem_name and $elem_var being set to the values of the line on either side of the @@ symbols.
To execute a Perl script, you create a text file containing the code you want to execute (the normal convention is for the script to have a .pl extension). You can subsequently execute the script as follows: >ratlperl name_of_script.pl One of Perl's more powerful features is its capability to redirect a command's output through a filehandle so that it can be parsed and manipulated a line at a time. For example, the output of the cleartool find command could be parsed a line at a time using the following: open (IN, "cleartool find . -all -print|"); while (<IN>) { # remove extended name s{@@.*}{}g; print " $_"; } close (IN); In this example, a filehandle is opened to the cleartool command via the | operator. (This operator comes from UNIX and is usually called a pipe.) The while statement then reads from this pipe one line at a time. The content of each line is automatically placed in the special $_ variablehence the print statement. The other line, s{@@.*}{}g, is a Perl regular expression search and replace. It finds the first occurrence of the @@ symbol on the line and removes it and any subsequent characters. Consequently, when this script is executed, you see output similar to the following: ./build.xml ./default.properties ./src ./src/com ... Although the end result isn't very exciting, it gives you an idea of Perl's capabilities. To illustrate what Perl can do in more detail, the Windows batch file from Listing 8.1 can be rewritten quite easily in Perl. It is shown in Listing 8.2. Listing 8.2. Perl difflabel_report.pl File
In this example, information is displayed about the two labels passed as parameters to the script, and then the difference in element versions between the two labels is displayed. (The parameters are retrieved using the Perl @ARGV array, where $ARGV[0] retrieves the first parameter, $ARGV[1] retrieves the second, and so on.) The bit of magic in the middle of the script finds out the directory from which the script is being executed so that it can be removed from the output. This is made possible using the Perl cwd() function, which returns the current working directory, and the rather cryptic $^O Perl variable. It contains a string describing which operating system the script is being executed on, such as MSWin32, unix, or linux. If this script were executed, you would see output similar to the following: >ratlperl difflabel_report.pl RATLBANK_01_INT RATLBANK_02_INT CLEARCASE BASELINE REPORT First Label: RATLBANK_01_INT created: 11-Jan-06 by: alex status: locked Second Label: RATLBANK_02_INT created: 12-Jan-06 by: chris status: locked ELEMENT VERSIONS CHANGED BETWEEN RATLBANK_01_INT AND RATLBANK_02_INT: build.xml@@\main\8 src\com\ratlbank\model\Account.java@@\main\2 src\com\ratlbank\model\Bank.java@@\main\14 src\com\ratlbank\model\Customer.java@@\main\12 ... Notice how the display of elements is a bit more readable because it does not contain the absolute pathnames. The other advantage of this script is that it runs on any platform on which ClearCase can be installed.
|