summaryrefslogtreecommitdiff
path: root/ssjb.py
diff options
context:
space:
mode:
authorGravatar jeff2015-01-06 00:44:16 -0500
committerGravatar jeff2015-01-06 00:44:16 -0500
commite28d1efd91912a60bdbf30f01011b18018075629 (patch)
treecc9d1e2b2d29cdf2c3ea9e63850199e2ee46d593 /ssjb.py
parentreverting to GPL license (diff)
downloadenigma-e28d1efd91912a60bdbf30f01011b18018075629.tar.gz
enigma-e28d1efd91912a60bdbf30f01011b18018075629.tar.xz
enigma-e28d1efd91912a60bdbf30f01011b18018075629.zip
got rid of gradle in favor of ivy+ssjb
Diffstat (limited to 'ssjb.py')
-rw-r--r--ssjb.py163
1 files changed, 163 insertions, 0 deletions
diff --git a/ssjb.py b/ssjb.py
new file mode 100644
index 00000000..d3e2bb52
--- /dev/null
+++ b/ssjb.py
@@ -0,0 +1,163 @@
1
2# stupidly simple jar builder
3# Jeff Martin
4# 2015-01-05
5
6import os
7import sys
8import shutil
9import subprocess
10import zipfile
11import tempfile
12import re
13import fnmatch
14
15
16# setup tasks
17tasks = {}
18
19def registerTask(name, func):
20 tasks[name] = func
21
22def run():
23
24 # get the task name
25 taskName = "main"
26 if len(sys.argv) > 1:
27 taskName = sys.argv[1]
28
29 # find that task
30 try:
31 task = tasks[taskName]
32 except:
33 print "Couldn't find task: %s" % taskName
34 return
35
36 # run it!
37 task()
38
39
40# set up the default main task
41def mainTask():
42 print "The main task doesn't do anything by default"
43
44registerTask("main", mainTask)
45
46
47# library of useful functions
48
49def findFiles(dirSrc, pattern=None):
50 out = []
51 for root, dirs, files in os.walk(dirSrc):
52 for file in files:
53 path = os.path.join(root, file)[len(dirSrc) + 1:]
54 if pattern is None or fnmatch.fnmatch(path, pattern):
55 out.append(path)
56 return out
57
58def copyFile(dirDest, pathSrc, renameTo=None):
59 (dirParent, filename) = os.path.split(pathSrc)
60 if renameTo is None:
61 renameTo = filename
62 pathDest = os.path.join(dirDest, renameTo)
63 shutil.copy2(pathSrc, pathDest)
64
65def copyFiles(dirDest, dirSrc, paths):
66 for path in paths:
67 pathSrc = os.path.join(dirSrc, path)
68 pathDest = os.path.join(dirDest, path)
69 dirParent = os.path.dirname(pathDest)
70 if not os.path.isdir(dirParent):
71 os.makedirs(dirParent)
72 shutil.copy2(pathSrc, pathDest)
73
74def patternStringToRegex(patternString):
75
76 # escape special chars
77 patternString = re.escape(patternString)
78
79 # process ** and * wildcards
80 replacements = {
81 re.escape("**"): ".*",
82 re.escape("*"): "[^" + os.sep + "]+"
83 }
84 def getReplacement(match):
85 print "matched", match
86 return "a"
87 patternString = re.compile("(\\\\\*)+").sub(lambda m: replacements[m.group(0)], patternString)
88
89 return re.compile("^" + patternString + "$")
90
91def matchesAnyPath(path, patternStrings):
92 for patternString in patternStrings:
93 pattern = patternStringToRegex(patternString)
94 # TEMP
95 print path, pattern.match(path) is not None
96 if pattern.match(path) is not None:
97 return True
98 return False
99
100def delete(path):
101 try:
102 if os.path.isdir(path):
103 shutil.rmtree(path)
104 elif os.path.isfile(path):
105 os.remove(path)
106 except:
107 # don't care if it failed
108 pass
109
110def buildManifest(title, version, author, mainClass=None):
111 manifest = {
112 "Title": title,
113 "Version": version,
114 "Created-by": author
115 }
116 if mainClass is not None:
117 manifest["Main-Class"] = mainClass
118 return manifest
119
120def jar(pathOut, dirIn, dirRoot=None, manifest=None):
121
122 # build the base args
123 if dirRoot is None:
124 dirRoot = dirIn
125 dirIn = "."
126 invokeArgs = ["jar"]
127 filesArgs = ["-C", dirRoot, dirIn]
128
129 if manifest is not None:
130 # make a temp file for the manifest
131 tempFile, tempFilename = tempfile.mkstemp(text=True)
132 try:
133 # write the manifest
134 for (key, value) in manifest.iteritems():
135 os.write(tempFile, "%s: %s\n" % (key, value))
136 os.close(tempFile)
137
138 # build the jar with a manifest
139 subprocess.call(invokeArgs + ["cmf", tempFilename, pathOut] + filesArgs)
140
141 finally:
142 os.remove(tempFilename)
143 else:
144 # just build the jar without a manifest
145 subprocess.call(invokeArgs + ["cf", pathOut] + filesArgs)
146
147 print "Wrote jar: %s" % pathOut
148
149def unpackJar(dirOut, pathJar):
150 with zipfile.ZipFile(pathJar) as zf:
151 for member in zf.infolist():
152 zf.extract(member, dirOut)
153 print "Unpacked jar: %s" % pathJar
154
155def unpackJars(dirOut, dirJars, recursive=False):
156 for name in os.listdir(dirJars):
157 path = os.path.join(dirJars, name)
158 if os.path.isfile(path):
159 if name[-4:] == ".jar":
160 unpackJar(dirOut, path)
161 elif os.path.isdir(path) and recursive:
162 unpackJars(dirOut, path, recursive)
163