0

classpath_augmentPythonを使用して呼び出された変数を連結するスクリプトを作成しました。ディレクトリと含まれているjarファイルをclasspath_augment変数に正常に連結できましたが、ファイルを含むディレクトリをクラスパス変数に追加する必要もあり.propertiesます。

どうやってやるの?
以下は私のコードです:

#! /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)
4

1 に答える 1

0

.propertiesファイルを探すだけです:

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

コードをいくらか単純化する自由を取りました。リストを使用して最初にすべてのクラスパス パスを収集し、次に使用str.join()して最終的な文字列を作成します。これは、新しいパスを 1 つずつ連結するよりも高速です。

非常に古いバージョンの Python を使用してany()いて、まだ利用できない場合は、forループを使用します。

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
于 2013-09-18T20:41:02.767 に答える