0

I wrote a script to concatenate a variable called classpath_augment using python. I was able to successfully concatenate directories and the contained jar files to the classpath_augment variable, however, I also need to add to the classpath variable those directories that contain .properties files.

How can I do that?
Below is my code:

#! /usr/bin/env python

import os
import sys
import glob

java_command = "/myappsjava/home/bin/java -classpath "

def run(project_dir, main_class, specific_args):

        classpath_augment = ""

        for r, d, f in os.walk(project_dir):
                for files in f:
                        if (files.endswith(".jar")):
                                classpath_augment += os.path.join(r, files)+":"

        if (classpath_augment[-1] == ":"):
                classpath_augment = classpath_augment[:-1]

        args_passed_in = '%s %s %s %s' % (java_command, classpath_augment, main_class, specific_args)
        print args_passed_in
        #os.system(args_passed_in)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Horse Voice
  • 8,138
  • 15
  • 69
  • 120

1 Answers1

0

Just look for .properties files:

def run(project_dir, main_class, specific_args):
    classpath = []

    for root, dirs, files in os.walk(project_dir):
        classpath.extend(os.path.join(root, f) for f in files if f.endswith('.jar'))
        if any(f.endswith('.properties') for f in files):
            classpath.append(root)

    classpath_augment = ':'.join(classpath)

    print java_command, classpath_augment, main_class, specific_args

I took the liberty of simplifying your code somewhat; using a list to collect all classpath paths first, then use str.join() to create a final string. This is faster than concatenating each new path one by one.

If you are using a very old Python version and any() is not yet available, use a for loop:

def run(project_dir, main_class, specific_args):
    classpath = []

    for root, dirs, files in os.walk(project_dir):
        has_properties = False
        for f in files:
            if f.endswith('.jar'):
                classpath.append(os.path.join(root, f))
            if f.endswith('.properties'):
                has_properties = True
        if has_properties:
            classpath.append(root)

    classpath_augment = ':'.join(classpath)

    print java_command, classpath_augment, main_class, specific_args
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Why is `classpath_augment = ':'.join(classpath)` outside of the loop? I need that seperating each file/directory entry in classpath – Horse Voice Sep 18 '13 at 21:02
  • @ImtiazAhmad: have you tried the code yet? You only need to join the path elements **once**. The `classpath` list is extended in the loop, once the walk is complete is the final string built. – Martijn Pieters Sep 18 '13 at 21:03
  • @ImtiazAhmad: See [`str.join()`](http://docs.python.org/2/library/stdtypes.html#str.join); `':'.join(classpath)` does just what you want; put `:` between each and every path element. – Martijn Pieters Sep 18 '13 at 21:06
  • Okay I see thanks. But there is a problem. What about concatenating the directories that contain the `.properties` files? Also please note, i dont want the directory concatenated twice to the classpath. – Horse Voice Sep 18 '13 at 21:17
  • @ImtiazAhmad: Directories are traversed exactly *once*, so they will only be added once. – Martijn Pieters Sep 18 '13 at 21:21
  • @ImtiazAhmad: I included a test for a `.properties` file already, adding the current `root` to the classpath. – Martijn Pieters Sep 18 '13 at 21:21
  • Only the jars with their directories are being concatenated. Not the directories that contain .properties file – Horse Voice Sep 18 '13 at 21:24
  • @ImtiazAhmad: Then there are no directories with a `.properties` file. The test `if '.properties' in files:` only is `True` if `os.path.join(root, '.properties')` exists. – Martijn Pieters Sep 18 '13 at 21:26
  • It does not concatenate the directory containing the properties files. Only the jar files with their directories are concatenated. – Horse Voice Sep 18 '13 at 21:38
  • @ImtiazAhmad: right, that's easy enough, but you should have made that clear to begin with. :-) – Martijn Pieters Sep 18 '13 at 21:39
  • Sorry, there was a `)` missing for the `str.endswith()` call. – Martijn Pieters Sep 18 '13 at 21:49
  • File "/apps/userprojects/SUper/main/src/bin/runMain.py", line 33, in run if any(f.endswith('.properties') for f in files): NameError: global name 'any' is not defined – Horse Voice Sep 18 '13 at 21:58
  • @ImtiazAhmad: You are using a very old version of Python; `any()` was added in 2.5. – Martijn Pieters Sep 18 '13 at 22:00