summaryrefslogtreecommitdiff
path: root/ssjb.py
blob: d3e2bb52267d92503311d5ac612ca02153753b58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# stupidly simple jar builder
# Jeff Martin
# 2015-01-05

import os
import sys
import shutil
import subprocess
import zipfile
import tempfile
import re
import fnmatch


# setup tasks
tasks = {}

def registerTask(name, func):
    tasks[name] = func

def run():

    # get the task name
    taskName = "main"
    if len(sys.argv) > 1:
	taskName = sys.argv[1]
    
    # find that task
    try:
        task = tasks[taskName]
    except:
        print "Couldn't find task: %s" % taskName
        return

    # run it!
    task()


# set up the default main task
def mainTask():
    print "The main task doesn't do anything by default"

registerTask("main", mainTask)


# library of useful functions

def findFiles(dirSrc, pattern=None):
    out = []
    for root, dirs, files in os.walk(dirSrc):
        for file in files:
            path = os.path.join(root, file)[len(dirSrc) + 1:]
            if pattern is None or fnmatch.fnmatch(path, pattern):
                out.append(path)
    return out

def copyFile(dirDest, pathSrc, renameTo=None):
    (dirParent, filename) = os.path.split(pathSrc)
    if renameTo is None:
        renameTo = filename
    pathDest = os.path.join(dirDest, renameTo)
    shutil.copy2(pathSrc, pathDest)

def copyFiles(dirDest, dirSrc, paths):
    for path in paths:
        pathSrc = os.path.join(dirSrc, path)
        pathDest = os.path.join(dirDest, path)
        dirParent = os.path.dirname(pathDest)
        if not os.path.isdir(dirParent):
            os.makedirs(dirParent)
        shutil.copy2(pathSrc, pathDest)

def patternStringToRegex(patternString):

    # escape special chars
    patternString = re.escape(patternString)
    
    # process ** and * wildcards
    replacements = {
        re.escape("**"): ".*",
        re.escape("*"): "[^" + os.sep + "]+"
    }
    def getReplacement(match):
        print "matched", match
        return "a"
    patternString = re.compile("(\\\\\*)+").sub(lambda m: replacements[m.group(0)], patternString)

    return re.compile("^" + patternString + "$")

def matchesAnyPath(path, patternStrings):
    for patternString in patternStrings:
        pattern = patternStringToRegex(patternString)
        # TEMP
        print path, pattern.match(path) is not None
        if pattern.match(path) is not None:
            return True
    return False

def delete(path):
    try:
        if os.path.isdir(path):
            shutil.rmtree(path)
        elif os.path.isfile(path):
            os.remove(path)
    except:
        # don't care if it failed
        pass

def buildManifest(title, version, author, mainClass=None):
    manifest = {
        "Title": title,
        "Version": version,
        "Created-by": author
    }
    if mainClass is not None:
        manifest["Main-Class"] = mainClass
    return manifest

def jar(pathOut, dirIn, dirRoot=None, manifest=None):

    # build the base args
    if dirRoot is None:
        dirRoot = dirIn
        dirIn = "."
    invokeArgs = ["jar"]
    filesArgs = ["-C", dirRoot, dirIn]

    if manifest is not None:
        # make a temp file for the manifest
        tempFile, tempFilename = tempfile.mkstemp(text=True)
        try:
            # write the manifest
            for (key, value) in manifest.iteritems():
                os.write(tempFile, "%s: %s\n" % (key, value))
            os.close(tempFile)

            # build the jar with a manifest
            subprocess.call(invokeArgs + ["cmf", tempFilename, pathOut] + filesArgs)

        finally:
            os.remove(tempFilename)
    else:
        # just build the jar without a manifest
        subprocess.call(invokeArgs + ["cf", pathOut] + filesArgs)

    print "Wrote jar: %s" % pathOut

def unpackJar(dirOut, pathJar):
    with zipfile.ZipFile(pathJar) as zf:
        for member in zf.infolist():
            zf.extract(member, dirOut)
    print "Unpacked jar: %s" % pathJar

def unpackJars(dirOut, dirJars, recursive=False):
    for name in os.listdir(dirJars):
        path = os.path.join(dirJars, name)
        if os.path.isfile(path):
            if name[-4:] == ".jar":
                unpackJar(dirOut, path)
        elif os.path.isdir(path) and recursive:
            unpackJars(dirOut, path, recursive)