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