0

I have this python script that concatenates a classpath variable. It concatenates the directories of all the files that endwith a ".properties" extension as well as a ".jar" extension.

However, some ".jar" files as well as ".properties" files are repeated in different directories. So I have decided to only search through 2 folders. Those named lib and those named properties. I'm using Python 2.4

#! /usr/bin/env python

import os
import sys
import glob

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

def any(s):
    for v in s:
        if v:
            return True
    return False


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)

    args_passed_in = '%s %s %s %s' % (java_command, classpath_augment, main_class, specific_args)
    print args_passed_in
    code = os.system(args_passed_in)
    print code
Doublespeed
  • 1,153
  • 4
  • 16
  • 29
  • You're possibly looking for Set functionality: http://docs.python.org/2/library/sets.html#sets.Set – Chris Arena Sep 24 '13 at 15:46
  • @Decency: sets are now built in, don't use the old library. – Martijn Pieters Sep 24 '13 at 15:46
  • @MartijnPieters He said he's on 2.4 – Chris Arena Sep 24 '13 at 15:49
  • @MartijnPieters, I only want to get jars and properties files from 2 specific directories. `lib` and `properties`. Isn't there a way to only process those? – Doublespeed Sep 24 '13 at 16:58
  • @user2081579: why not just scan those two directories with `os.listdir()` instead then? – Martijn Pieters Sep 24 '13 at 16:59
  • @MartijnPieters: I don't think I understand. I just want to append to the classpath variable those files that are in directories called lib and properties. How can I incorporate that in the above? – Doublespeed Sep 24 '13 at 17:05
  • Right, so you want to only search for directories that end in `lib` or `properties`? – Martijn Pieters Sep 24 '13 at 17:12
  • @MartijnPieters: Yes. Only search directories named lib and properties with the same rules above. – Doublespeed Sep 24 '13 at 17:35
  • @user2081579: Then test if `os.path.basename(root) in ('lib', 'properties')` first. – Martijn Pieters Sep 24 '13 at 17:37
  • @MartijnPieters: I dont know python well, where exactly will this statement go in the code above? – Doublespeed Sep 24 '13 at 17:44
  • You add a test before the `classpath.extend()` line. `if os.path.basename(root) not in ('lib', 'properties'): continue` would skip looking in a directory if it did not end in `lib` or `properties`. – Martijn Pieters Sep 24 '13 at 17:45
  • @MartijnPieters: aww man, im in a non pythonic java environment with no ide just the terminal with python 2.4. And im getting a syntax error by adding what you told me above the line, `classpath.extend(os.path.join(root, f) for f in files if f.endswith('.jar'))`... Am I missing anything here. Would continue fit there? – Doublespeed Sep 24 '13 at 17:59
  • @user2081579: then you forgot a `)` somewhere on the *previous* line, I'd say. – Martijn Pieters Sep 24 '13 at 18:01
  • No thats not the issue. I dont think the command you gave works as expected. Its still getting dirs that have other names other than lib and properties – Doublespeed Sep 24 '13 at 18:17

1 Answers1

0

Just make a Set of your objects from your generator comp:

classpath = set(os.path.join(root, f) for f in files if f.endswith('.jar') or f.endswith('.properties'))
professorDante
  • 2,290
  • 16
  • 26
  • Can you please elaborate what the above does and where to put it? – Doublespeed Sep 24 '13 at 16:23
  • This doesnt really work. Lets say there are 2 directories: `/app/lib/myjar.jar` and `/app/old_lib/myjar.jar` How will the above only include one of them?? – Doublespeed Sep 24 '13 at 16:28
  • Your question is phrased incorrectly then - those are two unique directories, so they should be added to the set. Can you update your question to be clear - what you want exactly, and what you're currently getting? – professorDante Sep 24 '13 at 17:44
  • I have updated the question. I want to use a simpler approach. Please provide an updated code. – Doublespeed Sep 24 '13 at 19:01