annotate src/name/blackcap/osdep/Osdep.kt @ 3:394bdafe0cdf default tip

Fix setup.
author David Barts <n5jrn@me.com>
date Tue, 14 Apr 2020 11:51:48 -0700
parents 5ea54efa8e45
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 /*
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 * Recursively examines the specified directories for files that end in
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 * .<type>.osdep, where <type> is one of unix, mac, windows, or default.
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 * The file that matches the current OS type (or the type specified with
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 * the -t option)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 */
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 package name.blackcap.osdep
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
8
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import java.io.File
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import java.io.FileInputStream
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
11 import java.io.FileOutputStream
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 import java.io.IOException
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 import kotlin.collections.HashMap
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 import kotlin.collections.HashSet
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 import org.apache.commons.cli.CommandLine
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 import org.apache.commons.cli.DefaultParser
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 import org.apache.commons.cli.HelpFormatter
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 import org.apache.commons.cli.Option
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 import org.apache.commons.cli.Options
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 import org.apache.commons.cli.ParseException
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
21
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 var ostype = run {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 val rawType = System.getProperty("os.name")?.toLowerCase()
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 if (rawType == null) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 "default"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 } else if (rawType.contains("win")) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 "windows"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 } else if (rawType.contains("mac")) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 "mac"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 } else if (rawType.contains("nix") || rawType.contains("nux") || rawType.contains("aix") || rawType.contains("sunos")) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 "unix"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 } else {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 "default"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
36
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 val MYNAME = "osdep"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 val MYEXT = ".osdep"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
39
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 val files = HashMap<String, HashSet<String>>()
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
41
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 var errors = 0
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
43
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 fun main(args: Array<String>) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 val options = Options().apply {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 addOption("?", "help", false, "Print this help message.")
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 addOption("c", "clean", false, "Clean instead of copying.")
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 addOption("t", "type", true, "Specify OS type (default ${ostype}).")
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 }
1
5ea54efa8e45 Commit un-checked-in stuff.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
50 val cmdLine: CommandLine? = try {
5ea54efa8e45 Commit un-checked-in stuff.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
51 DefaultParser().parse(options, args)
0
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 } catch (e: ParseException) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 System.err.format("%s: %s%n", MYNAME, e.message)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 System.exit(2)
1
5ea54efa8e45 Commit un-checked-in stuff.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
55 null
0
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
57
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 if (cmdLine!!.hasOption("help")) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 val usage = MYNAME + " [--help] [--clean] [--type=type] directory [...]"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 HelpFormatter().printHelp(usage, options, false);
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 System.exit(0);
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
63
1
5ea54efa8e45 Commit un-checked-in stuff.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
64 val clOstype = cmdLine.getOptionValue("type")
0
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 if (clOstype != null) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 ostype = clOstype
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
68
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 for (arg in cmdLine.args) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 try {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 File(arg).walk().forEach { build(it) }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 } catch (e: Exception) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 handle(e, "cannot walk '${arg}'")
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
76
1
5ea54efa8e45 Commit un-checked-in stuff.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
77 val process = if (cmdLine.hasOption("clean")) ::clean else ::copy
0
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 files.forEach {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 process(it.key, it.value)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 System.exit(if (errors == 0) 0 else 1)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
83
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 fun build(file: File) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 if (!file.isFile()) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 return
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 val path = file.toString()
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 if (!path.endsWith(MYEXT)) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 return
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 val dot2 = path.lastIndexOf('.')
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 if (dot2 <= 0) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 return
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 val dot1 = path.lastIndexOf('.', dot2 - 1)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 if (dot1 <= 0) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 return
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
99 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 val base = path.substring(0 .. (dot1 - 1))
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 val type = path.substring((dot1 + 1) .. (dot2 - 1))
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 val fset = files[base]
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
103 if (fset != null) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
104 fset.add(type)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
105 } else {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 files[base] = HashSet<String>().apply { add(type) }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
107 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
108 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
109
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
110 fun copy(base: String, types: HashSet<String>) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
111 val type = if (ostype in types) ostype else "default"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
112 if (type !in types) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
113 System.err.format("%s: type '%s' missing for '%s'%n", MYNAME, type, base)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
114 errors += 1
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
115 return
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
116 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
117 copf("${base}.${type}${MYEXT}", base)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
118 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
119
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
120 fun copf(source: String, target: String) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
121 val sf = File(source)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
122 val tf = File(target)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
123 try {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
124 FileInputStream(sf).use { sfp ->
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
125 FileOutputStream(tf).use { tfp ->
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
126 sfp.copyTo(tfp)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
127 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
128 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
129 } catch (e: Exception) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
130 handle(e, "cannot copy '${source}' to '${target}'")
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
131 return
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
132 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
133 try {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
134 tf.setLastModified(sf.lastModified())
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
135 } catch (e: Exception) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
136 handle(e, "cannot set time of '${target}'")
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
137 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
138 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
139
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
140 fun clean(base: String, @Suppress("UNUSED_PARAMETER") ignored: HashSet<String>) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
141 val file = File(base)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
142 if (file.exists()) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
143 try {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
144 if (!file.delete()) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
145 System.err.format("%s: '%s' not deleted%n", MYNAME, base)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
146 errors += 1
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
147 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
148 } catch (e: Exception) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
149 handle(e, "cannot delete '${base}'")
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
150 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
151 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
152 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
153
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
154 fun handle(exception: Exception, message: String) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
155 val fallback = when (exception) {
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
156 is SecurityException -> "security violation"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
157 is IOException -> "I/O error"
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
158 else -> throw exception
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
159 }
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
160 System.err.format("%s: %s%n", MYNAME, exception.message ?: fallback)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
161 System.err.format("%s: %s%n", MYNAME, message)
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
162 errors += 1
895aa9a8d628 Initial commit.
David Barts <n5jrn@me.com>
parents:
diff changeset
163 }