Groovy scripting using multiple files

Today I completed the second evening of the free Groovy course at @InterAccess given by @Frans_van_Buul , very interesting to see how Groovy works with in combination with Java code. But I want to use Groovy scripts as replacements for Bash/Batch scripts, the only thing I couldn’t find out until now, was how to work with multiple groovy files when using groovy in its scripting context. Finally found it and it couldn’t be easier!

#!/usr/bin/env groovy
def sayer = new GhelloSayer()
println sayer.sayGHello()
println new Utils().gotTools()
view raw main.groovy This Gist brought to you by GitHub.
class GhelloSayer {
    def sayGHello() {
        return 'Ghello!'
    }
}
def gotTools() {
    return 'Yes I haz'
}
view raw Utils.groovy This Gist brought to you by GitHub.

If you look at these three groovy files, it’s very easy to see what Groovy does:

  • See if a .groovy file exists equal to the classname you want to instanciate, if so, it loads it
  • Non-main groovy scripts should have filenames that always start with a capital first-character, as if it’s a Java class source file. This way groovy will put the file in the classpath somehow and the file is loadable as if the filename is the classname and the groovy file content is the content of that class.
  • This also works with packages, packages just start from the directory of the main script.

Magic@work, but this finally makes it possible for me to really replace Bash/Batch scripts with Groovy.

Btw. main.groovy can ofcourse be named anything you like!

/me happy

Posted in groovy, Technical | Leave a comment

Version Control != Dependency Management

This article was originally featured in the August of 2010 issue of phpArchitect.

Are you using svn:externals, git-submodules or something similar with your Version Control System (VCS) of choice for connecting third-party libraries to your PHP projects? This article explores ways to handle dependencies in PHP projects, just like projects in other programming languages have done for ages.

Dependencies

Many projects have some kind of external dependency, e.g. libraries, language extensions, system tools or other applications. All these dependencies need to be available for the project to work. In PHP, dependencies like language extensions can be checked to be loaded with calls to the “extension_loaded” or “get_loaded_extensions” functions. The availability of system tools and other application can be checked by calling the command with for example the PHP file_exists function in combination with is_executable. Availability of a required library can be checked by asking for it through the class_exists function when using auto-loading or simply require_once the library and wait for it to fail.

For language extensions, system tools and applications, their availability should be checked. These are dependencies that should not be included in the project package, because they are often platform depended and there is a fair chance that these dependencies are already available on the system running the project. The previous paragraph explained ways of checking them. These checks can be easily executed from an install/release script and let the install fail when something is wrong.

But how about library dependencies? The distributed project package should already contain the required libraries or at least fetch them automatically. Fetching libraries automatically on install sounds good because then the project package for distribution is smaller, saving storage and bandwidth. But it makes your customers, during the installation of the project, depended on the availability of the required libraries from possibly 3rd parties. Think about it. If the current version of your project is still used 5 years from now, can you guarantee that the 3rd parties still provide that specific library version?
On the other hand, when the project is extendable with plugins and modules, fetching the those only when the customer chooses to use them is a fair solution. But the distributed project package should include all the default plugins and modules and all extensions should be fetched from your repository, so you have control over the availability.

svn:externals

How and when are required libraries included in your project?
Many PHP projects, that use Subversion (svn) as their Version Control System (VCS) of choice, use the svn:externals functionality to bind libraries to their project. It’s an easy solution; with every svn checkout or svn update the external libraries are also checked-out and updated into the local working copy. Meaning, it’s possible to directly edit and commit on the external libraries. And svn gives much freedom:

  • Add an external pointing to the latest (HEAD) revision
  • Add an external pointing to a specific revision
  • Add an external from an external repository
  • Set the externals property anywhere in the project and including that external somewhere else in the project structure

This means that there is no central location to configure the externals. Everyone working on the project should just know where it is set or search through the svn properties of all the project directories. Dependency management becomes magic “that just works”, or does it?

Ever tried to branch or tag a project that uses svn:externals for dependency management? First branching/tagging all the externals, then updating the svn:externals to those branched/tagged externals and finally create the branch/tag for the project. That’s not workable.

Branching/tagging can be made easier by applying the convention that externals should always point to tags. It’s easier branching/tagging because the externals will then always be pointing to a stable tag version. But svn still offers commit permission on the external that is pointing to a tag. Specifying the revision that the external should point to is the safest way to work with externals, this way the external will always be exact at that revision.

So what’s wrong with using a VCS for dependency management?

  • No overview of external configuration, they can be defined and placed anywhere in the project
  • Externals must be controlled in the same type of VCS. There’s no way to add a git repository as external in svn, or a simple .tar.bz2-file
  • Committing on externals is possible

How do other programming language handle dependencies

The most common lines of an “INSTALL” file of Linux C/C++ projects are

$ ./configure
$ make
$ sudo make install
view raw file1.sh This Gist brought to you by GitHub.

Ever tried that on a big project? Did “make” error-out the first time it ran? And the second time? Telling it missed a specific library and therefore could not continue? Dependencies! The project could not be compiled without having the correct dependencies available.

Java build tools like Apache Maven and Apache Ant with Apache Ivy went a step further. Maven defines the project in a Project Object Model (pom.xml) file, which describes the software project being built. Containing it’s dependencies on other projects/modules and external libraries, a list of the involved developers, links to the version control repository, issue tracker and more. Maven has a central repository which can be used to automatically fetch the project dependencies from, when they exist. Everyone can create their own Maven repository that can be used as a proxy to the central repository. This means the Java project can be build and all needed dependencies are automatically fetched from the central repository or any other repository that is provided in the Maven global settings or project POM configuration. Apache Ant with Apache Ivy works somewhat the same.
This all leads to the fact that with a build tool like Maven, there is a single configuration file per project for defining dependencies. That’s easy and clear for everyone.

The C/C++/Java languages have the advantage to be compiled languages. If a required dependency is missing, the project won’t compile. PHP does not have this advantage. It will run any code until it fails. Therefore PHP projects need some checking for the availability of dependencies.

So, how to apply this on PHP projects?

There is nothing fully equivalent to Maven for PHP except Maven itself with a PHP plug-in. This is what the “Maven for PHP” project does. It’s probably also possible to use Apache Ant with Ivy or other build tools from other programming languages to build PHP projects with. But this has the disadvantage that the build tool is written in a different language than PHP, most probably has no support for PHP specific tools by default and wanting to extend it forces to use the language that the build tool is written in. This makes using a non-native PHP build tool a bit harder.

But there are native PHP tools that provide parts of the features that build tools like Apache Maven provides.
We have Phing, a build tool based on Apache Ant, which has many tasks by default for executing filesystem related commands. Optionally it also contains tasks for many commonly used tools and release methods in the PHP world, like; DbDeploy, FtpDeploy, JsMin, PearPackage, PharPackage, PhpCodeSniffer, PHPUnit and much more. It’s also very easy to extend, just create a PHP class which extends from the Phing Task class and implement a few functions. It can be made as complex as needed, but it’s all in PHP, your language of choice.
For the repository part of a Maven like native PHP tool, we have PEAR for PHP4 and 5 packages and PEAR2 for PHP 5.3.1+ packages. PEAR and PEAR2 are “a framework, packaging and distribution system for reusable PHP Components”. There is a PEAR and PEAR2 framework specific repository, “Channel” in PEAR terms, at http://pear.php.net and http://pear2.php.net respectively. PEAR2 is the next-generation version of PEAR using the new PHP 5+ language features where possible, like namespaces and supports the Phar packaging format. PEAR2 also has a new installer, for installing packages and dependencies, called Pyrus. Pyrus is an easy to use installer which can easily be used standalone without installation from the command-line or be integrated into a project. Just like with Maven, it’s possible to create your own PEAR channel as a repository for projects and dependencies.

All this completes the combined feature list of Phing, PEAR/PEAR2 and Pyrus equal to that of Maven.

One drawback is that Phing does not (yet) has a task for installing dependencies from Pear/Pear2/Pyrus, but tasks doing just that can easily be written. When you do, please contribute it to the Phing project. The same is true for Version Control Systems other than CVS and Subversion, and everything else you want support for.

To conclude

For more information on referenced tools and a direct link to the “Extending Phing” documentation of Phing see the related url’s list. I also want to hint you on the article “Going Industrial” by Stéphen Périn, which was published in the phpArchitect issue of Januari 2010, for more interesting insights and hints on what tools and practices could be used to improve you PHP development life cycle.

Requirements

  • PHP: 5.0.2+ (for Phing)
  • 5.2+ (for Phar)
  • 5.3.1+ (for Pyrus)

Related URLs

Posted in php, Technical | Tagged , , , , , , , , , , , , , | Leave a comment

(Download) File Organizer Tool

Update 2011-05-28:
Looks like someone else already did a very good job creating something like the (Download) File Organizer Tool. It’s called “Sick Beard” and does a bit more on some parts and a bit less on other parts.

Checkout http://sickbeard.com

You can use it standalone or in combination with SABnzbd+ (they both share a look-and-feel so even that is a plus.

So the “(Download) File Organizer Tool” project is now cancelled
(was it even started ;) )

My NAS download discs are getting full so I tried some disc cleaning. As I’ve automated my downloads I often download multiple versions of files (eg. tv series in different formats), so while cleaning out those duplicated files…{insert very boring process}… I thought of a great tool that could handle all this for me.

/me programmer == /me lazy == /me wants to automate as much as possible

I got the idea of a “(Download) File Organizer Tool”, a tool that will do everything you want with your files automatically, LOL. A bit much, so I cramped it to:

  • Define one or more directories to work on (incl. setting like recursive or not, how deep to go recursive, specifically including or excluding directories and/or files)
  • Define actions to handle, where an action can be something like:
    • (for tv-series)
      On directory … find all files that loosely have the same filename. Order them by mkv,x264,hdtv,avi. Remove the duplicates and keep top ordered file. Delete all files that don’t match one of the order keywords. Move all files into a directory structure based on pattern … (eg. {title}/season {season#}/{title}.s{season#}e{episode#}.{extension}). When unsure what to do, add file to “problem files”-list for manual fixing (and improving action for the next run).
  • When creating the action, it should be possible to dry-run it and see the result of what it would look like if it was executed for real
  • It should be possible to run all or specific actions at specific times, intervals or be trigger by an external tool (eg. cron job, SABnzbd+, …)

It looks to me that all this can be handled with basic *nix commands, but maybe Groovy will be a better fit because of it’s cross-platformness.

The steps to take should be easy (for the above mentioned action):

  1. Define base directory to work on
  2. Get list of all files (full paths) in directory (recursively)
  3. Find duplicated filenames (case-insensitive, loosely/fuzzy compare)
  4. Order group of duplicated filenames by the given order of keywords
  5. Remove all files except the first/top one
  6. Recognize tv serie title, season, episode and extention
  7. If not exists, create directory structure based on found params
  8. Move and rename file to new directory structure
  9. Show list of files that were not processed at the end of the process as “problem files”

/me happy with the idea, now trying to find some time to build it

I’ve started a public project on GitHub: http://github.com/pvdissel/dfot
And a Pivotaltracker project: http://www.pivotaltracker.com/projects/132698

Update (2010-10-29 @21:00) : After another major HD cleanup to get some free space. I also came to the conclusion that, to get a filtering algorithm to be able to work correctly with all kind of situations, I probably has to put weights to keywords. eg. the higher the order of a keyword, the higher is score. ” hdtv” and “x264″  always +10 as we want High Definition content when available. “pdtv” -10. etc.

Also some keywords should be automaticly be recorgnized to be names of release groups, but only when they are on specific locations in the filename. Because the title of a tv serie could be the same as a release group, but release groups are mostly placed at the front or somewhere at the end of the filename.

Posted in Technical | Tagged , , | Leave a comment

Spinning ideas for an advanced Gigaspaces shell

Update 2011-05-28:

A colleague of mine did a create job getting a Gigaspaces Shell project started and working, thanks Jos :)

Check it out on https://code.google.com/p/gash/

 

As the original GigaSpaces command-line scripts and maven OpenSpaces plugin are far to minimalistic to be usefull for *real* world use. I’m thinking of creating something to fill the gap!

So, spinning some ideas for an advanced GigaSpaces shell which is usable in *real* world situations. I’m thinking of features like:

  • Usable as interactive AND non-interactive shell
  • Platform independend; it should run on all common platforms (win32/64, linux, mac, solaris, etc.) without external dependencies to platform specific commands
  • Unified configuration and usage; configuration and usage of the shell must be the same on every platform and between interactive and non-interactive mode; there’s *only* one way to do something!
  • Option to go in a graphic Interactive shell, something Midnight Commander like so you can walk through a tree with all lookupgroups, gsa, gsc, gsm, lus, pu’s, etc. with the tree at the left side and detailed info of the selected item at the right
  • Support remote deployments

    eg. move defined files over ssh (with possible gateway in between) to GS server or one that can connect to a lookupgroup(s). And deploy the files to specified lookupgroup(s) with specific properties/sla when needed

    Some requirements can be gathered from the example above (with some extra fantasy :) ):

  • Deployable files must be definable
  • Definable files can come from all kind of resources, eg.:
    • Maven repo (configurable)
    • FTP?
    • HTTP(s)?
    • Local disc
    • ..?
  • Definable files should be deployed in order of definement, as there often are dependencies between PU’s. Undeploy should happen in reverse order.
  • All kind of settings must be definable per deployable file, eg.:
    • deployment properties
    • sla
    • …?
  • Ssh needs support for tunneling/gateway, where the gateway as possibly different login credentials as the final ssh login. Usage of Public-Private-keys must be possible.
  • Deployment config must be highly modular, eg.:
    • With a general config and possibility to override settings per deployment file
    • Possibility to create config groups and extend them
  • Statistics, all supported statistics by the GS Admin API must be usable
  • The interactive and non-interactive shell must be intuitive, eg.:
    • Has auto-completion support (eg. bash-like tab-tab auto-completion)
    • Has command history support (eg. bash-like up-down history)
    • Each command must have an usable and self-container help feature (eg. svn-like svn help {command} or svn {command} help)
  • Must be able to work with multiple hosts (something Capistrano/Fabric like functionality)
  • Must be configurable for multiple stages (eg. local, dev, integration, staging, preproduction, production)
  • Support for quering spaces
  • Show the full url’s that webapps are bound to

So for short:

All possible GS Admin API features must be usable

Interesting links:

Posted in Technical | Tagged , , , | Leave a comment

Bike trip, Veenendaal Doorwerth, NL

Did a bike trip yesterday evening after work, I rented a bike and started hitting the paddles around 18:00, Friday after work.

The plan: To see if it’s do-able to drive from Doorwerth to Veenendaal and back, on my bike, on a daily basis.

Reason: I’m currently living about 75Km from my work, driving about 3 hours per work-day to get to work and back home. Therefore I’m looking for a place to live closer to Veenendaal where my work is located. Looking for a while now, about 2 years, I finally found something I’m interested in. A maisonette apartment in  Doorwerth. A small village only about 20km from Veenendaal lying in the middle of the woods. I love the outdoors, I love nature, so living almost in the woods is ideal for me. And the supermarket is only a minute or two walk away, what more could I ask for? :)

Back to the trip. So I started from work in Veenendaal. I had checked the route infront via Google Maps, but hadn’t printed it (on purpose). I had my GPS with me just in case. Paddling out of Veenendaal was easy, needed to follow the round-way around Veenendaal to get to the other side of it. Reached my first decision point; going over the highway in the direction of Ede or paddle into middle of nowhere between the meadows? I asked the first biker that came my way and told me to take the way between the meadows, “it’s a maze but just keep going that way” he pointed. And there I went, left, right, left, right, keeping turns to make sure I kept my directions. Finally drove into a village called Bennekom. At the stoplight I asked the nice lady for direction and she replied “I’m going in that direction, you can drive with me for a while”, that was nice and easy. At the outerside of Bennekom I said thanks and goodbye to the lady and followed the route she had explained to me, well… the directions she gave were “just follow this road, after a while you’ll see road signs with directions to Doorwerth”. But hey, she was right :) Then the hard part of the trip came… I needed to climb some big hills, and we don’t have much of them in The Netherlands but it looked like I had them all at ones. It was exhausting! But I made it, pfff… I reached Doorwerth.
Conclusion of this part of the trip; amazingly beautiful nature, but way to many hills to paddle every day by bike. It took me about 1 hour, according to Google Maps it’s about a 20,5Km trip that I drove.

Then the time came, I needed to get back to Veenendaal. I really didn’t wanted to go over those hills again, I wanted to find a better road. At the information road sign in Doorwerth I found a better one and there I went. While driving through Heelsum I found a supermarket. As I started this trip directly after work, I hadn’t eat yet, so I bought something to eat and drink that I could consume on the road. Out of Heelsum I drove into and out of Renkum following road signs Rhenen/Wageningen, the nature road. Out of Renkum a mighty view started to appear, a big steep forest hill at the right and open meadow at the left with a big river behind it. When I found a nice picnic bench I paused and consumed my food and drink while having an amazing view. Time went on so it was time to step on the paddles again and bike up the hill into Wageningen onto “de Nude” and onto a bicycle path between the meadows. Came across a nice skater girl, good time to ask for directions :D She explained “Just straight again and when you go over a small bridge, go over it and turn right. Follow that bicycle path and you’ll reach Veenendaal”, while explaining she hinted “btw. this is a very beautiful road to take, there is a faster one but this is the beautifulest one” so I went for beauty and took her advice. Many nice flat meadows later I reached Veenendaal and found my working place. Wow what a trip! My knees we crying for some relaxation but I did it. Conclusion; the trip that I took from Doorwerth to Veenendaal was much better and really do-able on a day-to-day basis, but it needs some practicing. I’ve no idea on timing because I took a lot of breaks in the this part of the trip. But it was already 20:30 when I reached my workplace. With the breaks that is a total of around 1,5 hour for a distance of about 26Km.

Total conclusion; I can really make this work. Taking the second part of the trip on a day-to-day basis is do-able, just need to buy a good bicycle. And I can always take the first part of the trip if I want some more excitement at the end of the workday


Show big map

Posted in trip | Leave a comment

Maven download sources of dependencies

With the following Maven command you can download all compiled and sources of dependencies of your Maven project:

mvn dependency:sources

Posted in Hints, Technical | Tagged | Leave a comment

Kungsleden, Sweden

Next-up on the TODO Adventure List is the Kungsleden trail in Sweden.

Kungsleden (“The King’s Trail“) is a hiking trail in northern Sweden, approximately 440-kilometre (270 mi) long, passing through one of Europe‘s largest remaining wilderness area” –wikipedia

This one on the list will not be there for long, as it’s already planned for the end-of-August/start-of-September this year! :D Together with my colleague Sander van Beek and a friend of him, I will be hiking about 134 km of this trail. We have 14 days for traveling from The Netherlands to Kiruna in Sweden, walk the trail and drive back to The Netherlands. Hopefully we have some time left to take a peek at North Cape in Norway.

A full experience impression will come in September!

Posted in Todo Adventure List | Tagged , , , , , | Leave a comment

From Mexico to Canada, hiking trail

Pacific Crest Trail (PCT) logo

Pacific Crest Trail (PCT) logo

I just found out about the Pacific Crest Trail (PCT) in episode 1 of seizoen 4 of the National Geographic tv-serie “America’s Wild Spaces“.

“Whether you visit the PCT for a few hours or for a few weeks you’ll surely find a uniquely Western scene that will rejuvenate, inspire and surprise you. Two thousand six hundred and fifty miles (2650 miles == 4 264,76 km) of adventure and discovery are waiting.” — pcta.org

Sound like the perfect way to roam through America, maybe not all by foot tho.

This is the first item on my list of holiday adventures that I would like to do, someday.
More will follow :)

Posted in Todo Adventure List | Tagged , , , , , | Leave a comment