58113
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package jdk.jpackage.test;
|
|
24 |
|
|
25 |
import java.nio.file.Path;
|
|
26 |
import java.util.HashMap;
|
|
27 |
import java.util.Map;
|
|
28 |
import java.util.stream.Stream;
|
|
29 |
|
|
30 |
public class LinuxHelper {
|
|
31 |
private static String getRelease(JPackageCommand cmd) {
|
|
32 |
return cmd.getArgumentValue("--linux-app-release", () -> "1");
|
|
33 |
}
|
|
34 |
|
|
35 |
public static String getPackageName(JPackageCommand cmd) {
|
|
36 |
cmd.verifyIsOfType(PackageType.LINUX);
|
58115
|
37 |
return cmd.getArgumentValue("--linux-package-name",
|
58113
|
38 |
() -> cmd.name().toLowerCase());
|
|
39 |
}
|
|
40 |
|
|
41 |
static String getBundleName(JPackageCommand cmd) {
|
|
42 |
cmd.verifyIsOfType(PackageType.LINUX);
|
|
43 |
|
|
44 |
final PackageType packageType = cmd.packageType();
|
|
45 |
String format = null;
|
|
46 |
switch (packageType) {
|
|
47 |
case LINUX_DEB:
|
|
48 |
format = "%s_%s-%s_%s";
|
|
49 |
break;
|
|
50 |
|
|
51 |
case LINUX_RPM:
|
|
52 |
format = "%s-%s-%s.%s";
|
|
53 |
break;
|
|
54 |
}
|
|
55 |
|
|
56 |
final String release = getRelease(cmd);
|
|
57 |
final String version = cmd.version();
|
|
58 |
|
|
59 |
return String.format(format,
|
|
60 |
getPackageName(cmd), version, release, getPackageArch(packageType))
|
|
61 |
+ packageType.getSuffix();
|
|
62 |
}
|
|
63 |
|
|
64 |
public static Stream<Path> getPackageFiles(JPackageCommand cmd) {
|
|
65 |
cmd.verifyIsOfType(PackageType.LINUX);
|
|
66 |
|
|
67 |
final PackageType packageType = cmd.packageType();
|
|
68 |
final Path packageFile = cmd.outputBundle();
|
|
69 |
|
|
70 |
Executor exec = new Executor();
|
|
71 |
exec.saveOutput();
|
|
72 |
switch (packageType) {
|
|
73 |
case LINUX_DEB:
|
|
74 |
exec.setExecutable("dpkg")
|
|
75 |
.addArgument("--contents")
|
|
76 |
.addArgument(packageFile);
|
|
77 |
break;
|
|
78 |
|
|
79 |
case LINUX_RPM:
|
|
80 |
exec.setExecutable("rpm")
|
|
81 |
.addArgument("-qpl")
|
|
82 |
.addArgument(packageFile);
|
|
83 |
break;
|
|
84 |
}
|
|
85 |
|
|
86 |
Stream<String> lines = exec.execute().assertExitCodeIsZero().getOutput().stream();
|
|
87 |
if (packageType == PackageType.LINUX_DEB) {
|
|
88 |
// Typical text lines produced by dpkg look like:
|
|
89 |
// drwxr-xr-x root/root 0 2019-08-30 05:30 ./opt/appcategorytest/runtime/lib/
|
|
90 |
// -rw-r--r-- root/root 574912 2019-08-30 05:30 ./opt/appcategorytest/runtime/lib/libmlib_image.so
|
|
91 |
// Need to skip all fields but absolute path to file.
|
|
92 |
lines = lines.map(line -> line.substring(line.indexOf(" ./") + 2));
|
|
93 |
}
|
|
94 |
return lines.map(Path::of);
|
|
95 |
}
|
|
96 |
|
|
97 |
static Path getLauncherPath(JPackageCommand cmd) {
|
|
98 |
cmd.verifyIsOfType(PackageType.LINUX);
|
|
99 |
|
|
100 |
final String launcherName = cmd.name();
|
|
101 |
final String launcherRelativePath = Path.of("/bin", launcherName).toString();
|
|
102 |
|
|
103 |
return getPackageFiles(cmd).filter(path -> path.toString().endsWith(
|
|
104 |
launcherRelativePath)).findFirst().or(() -> {
|
|
105 |
Test.assertUnexpected(String.format(
|
|
106 |
"Failed to find %s in %s package", launcherName,
|
|
107 |
getPackageName(cmd)));
|
|
108 |
return null;
|
|
109 |
}).get();
|
|
110 |
}
|
|
111 |
|
|
112 |
static String getDebBundleProperty(Path bundle, String fieldName) {
|
|
113 |
return new Executor()
|
|
114 |
.saveFirstLineOfOutput()
|
|
115 |
.setExecutable("dpkg-deb")
|
|
116 |
.addArguments("-f", bundle.toString(), fieldName)
|
|
117 |
.execute()
|
|
118 |
.assertExitCodeIsZero().getFirstLineOfOutput();
|
|
119 |
}
|
|
120 |
|
|
121 |
static String geRpmBundleProperty(Path bundle, String fieldName) {
|
|
122 |
return new Executor()
|
|
123 |
.saveFirstLineOfOutput()
|
|
124 |
.setExecutable("rpm")
|
|
125 |
.addArguments(
|
|
126 |
"-qp",
|
|
127 |
"--queryformat",
|
|
128 |
String.format("%%{%s}", fieldName),
|
|
129 |
bundle.toString())
|
|
130 |
.execute()
|
|
131 |
.assertExitCodeIsZero().getFirstLineOfOutput();
|
|
132 |
}
|
|
133 |
|
|
134 |
private static String getPackageArch(PackageType type) {
|
|
135 |
if (archs == null) {
|
|
136 |
archs = new HashMap<>();
|
|
137 |
}
|
|
138 |
|
|
139 |
String arch = archs.get(type);
|
|
140 |
if (arch == null) {
|
|
141 |
Executor exec = new Executor();
|
|
142 |
exec.saveFirstLineOfOutput();
|
|
143 |
switch (type) {
|
|
144 |
case LINUX_DEB:
|
|
145 |
exec.setExecutable("dpkg").addArgument(
|
|
146 |
"--print-architecture");
|
|
147 |
break;
|
|
148 |
|
|
149 |
case LINUX_RPM:
|
|
150 |
exec.setExecutable("rpmbuild").addArgument(
|
|
151 |
"--eval=%{_target_cpu}");
|
|
152 |
break;
|
|
153 |
}
|
|
154 |
arch = exec.execute().assertExitCodeIsZero().getFirstLineOfOutput();
|
|
155 |
archs.put(type, arch);
|
|
156 |
}
|
|
157 |
return arch;
|
|
158 |
}
|
|
159 |
|
|
160 |
static private Map<PackageType, String> archs;
|
|
161 |
}
|