-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathGenPkgInfo.java
More file actions
executable file
·313 lines (296 loc) · 9.83 KB
/
Copy pathGenPkgInfo.java
File metadata and controls
executable file
·313 lines (296 loc) · 9.83 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package javaforce.utils;
import java.io.*;
import java.util.*;
import javaforce.*;
/** genpkginfo : Generates Linux package info files.
*
* debian : deb/control
* fedora : rpm.spec
* arch : .PKGINFO
*
* @author pquiring
*/
public class GenPkgInfo {
private void error(String msg) {
System.out.print(msg);
System.exit(1);
}
private BuildTools tools;
private String app, apptype, desc, arch, ver;
private String distro, release;
private String symlink;
private long size; //in bytes
private boolean service;
private String[] deps;
public static void main(String[] args) {
if (args == null || args.length < 3) {
System.out.println("GenPkgInfo : build linux package info files");
System.out.println(" Usage : GenPkgInfo distro archtype release files.list");
System.out.println(" distro = debian fedora centos arch");
System.exit(1);
}
try {
new GenPkgInfo().run(args);
} catch (Exception e) {
JFLog.log(e);
}
}
public void run(String[] args) throws Exception {
tools = new BuildTools();
if (!tools.loadXML("build.xml")) throw new Exception("error loading build.xml");
distro = args[0];
arch = args[1];
release = args[2];
size = calcSize(args[3]);
//load build.xml and extract app , desc , etc.
app = tools.getProperty("app");
apptype = tools.getProperty("apptype");
symlink = tools.getProperty("symlink");
if (symlink.length() == 0) symlink = null;
deps = getDepends(distro, release);
service = apptype.equals("service");
desc = tools.getTag("description");
ver = tools.getProperty("version");
switch (distro) {
case "debian": debian(); break;
case "fedora": fedora(); break;
case "centos": fedora(); break;
case "arch": arch(); break;
default: error("Unknown distro:" + distro);
}
}
private String getLinkCommand() {
return "ln -sf /usr/bin/" + app + " /usr/bin/" + symlink + "\n";
}
private String getUnlinkCommand() {
return "rm /usr/bin/" + symlink + "\n";
}
private long calcSize(String files_list) {
try {
String[] files = new String(JF.readAll(new FileInputStream(files_list))).replaceAll("\r", "").split("\n");
long size = 0;
for(int a=0;a<files.length;a++) {
size += new File(files[a]).length();
}
return size;
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
return -1;
}
}
private String[] getDepends(String distro, String release) {
ArrayList<String> depends = new ArrayList<String>();
String filename = null;
switch (distro) {
case "debian": filename = "deb.deps"; break;
case "fedora": filename = "rpm.deps"; break;
case "centos": filename = "rpm.deps"; break;
case "arch": filename = "pac.deps"; break;
default: return new String[0];
}
try {
FileInputStream fis = new FileInputStream(filename);
String deps = new String(fis.readAllBytes());
fis.close();
String[] list = deps.replaceAll("\r", "").split("\n");
for(int a=0;a<list.length;a++) {
String depend = list[a].trim();
if (depend.length() == 0) continue;
if (depend.equals("null")) continue;
if (depend.startsWith("#")) continue;
if (depend.contains(":")) {
//version specific
int idx = depend.indexOf(':');
String pkg_release = depend.substring(0, idx);
depend = depend.substring(idx + 1);
if (!pkg_release.equals(release)) continue;
}
depends.add(depend);
}
} catch (FileNotFoundException e) {
//ignore error
} catch (Exception e) {
JFLog.log(e);
}
if (!app.equals("javaforce") && !depends.contains("javaforce")) {
depends.add("javaforce");
}
return depends.toArray(JF.StringArrayType);
}
private void debian() {
try {
StringBuffer sb = new StringBuffer();
//mandatory
sb.append("Package: " + app + "\n");
sb.append("Version: " + ver + "\n");
sb.append("Architecture: " + arch + "\n");
sb.append("Description: " + desc + "\n");
//optional
sb.append("Installed-Size: " + Long.toString(size / 1024L) + "\n");
sb.append("Depends: ");
for(int a=0;a<deps.length;a++) {
if (a > 0) sb.append(",");
sb.append(deps[a]);
}
sb.append("\n");
new File("deb").mkdir();
{
FileOutputStream fos = new FileOutputStream("deb/control");
fos.write(sb.toString().getBytes());
fos.close();
}
//generate deb/preinst if not present
if (!new File("deb/preinst").exists()) {
FileOutputStream fos = new FileOutputStream("deb/preinst");
fos.write("#!/bin/sh\n".getBytes());
if (new File("folders.lst").exists()) {
FileInputStream fis = new FileInputStream("folders.lst");
String[] lns = new String(fis.readAllBytes()).replaceAll("\\r", "").split("\n");
fis.close();
for(String ln : lns) {
ln = ln.trim();
if (ln.length() == 0) continue;
fos.write(("mkdir -p " + ln + "\n").getBytes());
}
}
fos.close();
}
//install package here
//generate deb/postinst if not present
if (!new File("deb/postinst").exists()) {
FileOutputStream fos = new FileOutputStream("deb/postinst");
fos.write("#!/bin/sh\n".getBytes());
if (service) {
fos.write(("systemctl enable " + app + "\n").getBytes());
}
if (app.equals("javaforce")) {
fos.write(("systemctl reload dbus\n").getBytes());
}
if (symlink != null) {
fos.write(getLinkCommand().getBytes());
}
fos.close();
}
//generate deb/prerm if not present
if (!new File("deb/prerm").exists()) {
FileOutputStream fos = new FileOutputStream("deb/prerm");
fos.write("#!/bin/sh\n".getBytes());
if (service) {
fos.write(("systemctl disable " + app + "\n").getBytes());
}
fos.close();
}
//remove package here
//generate deb/postrm if not present
if (!new File("deb/postrm").exists()) {
FileOutputStream fos = new FileOutputStream("deb/postrm");
fos.write("#!/bin/sh\n".getBytes());
if (symlink != null) {
fos.write(getUnlinkCommand().getBytes());
}
fos.close();
}
System.out.println("Debian package info created");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
private void fedora() {
try {
StringBuffer sb = new StringBuffer();
sb.append("Buildroot: /.\n");
sb.append("Name: " + app + "\n");
sb.append("Version: " + ver.replaceAll("-", "_") + "\n");
sb.append("Release: 1\n");
sb.append("License: LGPL\n");
/*
sb.append("Architecture: ");
switch (archtype) {
case "any": sb.append("noarch"); break;
case "x32": sb.append("i686"); break;
case "x64": sb.append("x86_64"); break;
case "a32": sb.append("armv7hl"); break;
case "a64": sb.append("aarch64"); break;
}
sb.append("\n");
*/
sb.append("Summary: " + desc + "\n");
sb.append("Requires: ");
for(int a=0;a<deps.length;a++) {
if (a > 0) sb.append(",");
sb.append(deps[a]);
}
sb.append("\n");
sb.append("%description\n " + desc + "\n");
//pre install
sb.append("%pre\n");
sb.append("#!/bin/sh\n");
sb.append("set -e\n");
//post install
sb.append("%post\n");
sb.append("#!/bin/sh\n");
sb.append("set -e\n");
if (service) {
sb.append("%systemd_post " + app + ".service\n");
}
if (app.equals("javaforce")) {
sb.append("systemctl reload dbus\n");
}
if (symlink != null) {
sb.append(getLinkCommand());
}
//pre remove
sb.append("%preun\n");
sb.append("#!/bin/sh\n");
if (service) {
sb.append("%systemd_preun " + app + ".service\n");
}
//post remove
sb.append("%postun\n");
sb.append("#!/bin/sh\n");
if (service) {
sb.append("%systemd_postun_with_restart " + app + ".service\n");
}
if (symlink != null) {
sb.append(getUnlinkCommand());
}
sb.append("%files\n");
//files.lst is added by jfrpm
FileOutputStream fos = new FileOutputStream("rpm.spec");
fos.write(sb.toString().getBytes());
fos.close();
System.out.println("Fedora package info created");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
private void arch() {
try {
StringBuffer sb = new StringBuffer();
sb.append("pkgname = " + app + "\n");
sb.append("pkgver = " + ver + "-1\n");
sb.append("pkgdesc = " + desc + "\n");
sb.append("builddate = " + Long.toString(Calendar.getInstance().getTimeInMillis() / 1000L) + "\n");
sb.append("size = " + size + "\n");
sb.append("license = LGPL\n");
sb.append("arch = " + arch + "\n");
for(int a=0;a<deps.length;a++) {
sb.append("depend = ");
sb.append(deps[a]);
sb.append("\n");
}
FileOutputStream fos = new FileOutputStream(".PKGINFO");
fos.write(sb.toString().getBytes());
fos.close();
System.out.println("Arch package info created");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}