Debian Codegraph @

maven-packager-utils/src/main/java/org/debian/maven/packager/util/PackageScanner.java

raw
1 /*
2 * Copyright 2012 Ludovic Claude.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.debian.maven.packager.util;
18
19 import java.io.*;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.debian.maven.packager.DebianDependency;
27 import org.debian.maven.repo.Dependency;
28
29 public class PackageScanner {
30
31 private final boolean offline;
32 // Keep the list of known files and their package
33 private Map<File, String> filesInPackages = new HashMap<File, String>();
34 private Map<String, List<String>> cacheOfSharedJars = new HashMap<String, List<String>>();
35
36 public PackageScanner(boolean offline) {
37 this.offline = offline;
38 }
39
40 public PackageScanner newInstanceWithFreshCaches() {
41 return new PackageScanner(offline);
42 }
43
44 public DebianDependency searchPkg(File dir, String extension) {
45 // lookup the cache first
46 File cacheId = new File(dir, "<ANY>" + extension);
47 if (filesInPackages.containsKey(cacheId)) {
48 return new DebianDependency(filesInPackages.get(cacheId));
49 }
50
51 GetPackageContainingPatternResult packageResult = new GetPackageContainingPatternResult(extension);
52
53 IOUtil.executeProcess(new String[]{"dpkg", "--search", dir.getAbsolutePath() + "/*/*"}, packageResult);
54
55 if (!packageResult.getPackages().isEmpty()) {
56 String pkg = packageResult.getPackages().iterator().next();
57 filesInPackages.put(cacheId, pkg);
58 return new DebianDependency(pkg);
59 }
60
61 // Debian policy prevents the use of apt-file during a build
62 if (offline) {
63 System.err.println("Offline mode. Give up looking for package containing " + dir);
64 return null;
65 }
66
67 if (!new File("/usr/bin/apt-file").exists()) {
68 System.err.println("/usr/bin/apt-file not found. Give up looking for package containing " + dir);
69 return null;
70 }
71 IOUtil.executeProcess(new String[]{"apt-file", "search", dir.getAbsolutePath()}, packageResult);
72 if (!packageResult.getPackages().isEmpty()) {
73 String pkg = packageResult.getPackages().iterator().next();
74 filesInPackages.put(cacheId, pkg);
75 new DebianDependency(pkg);
76 }
77
78 return null;
79 }
80
81
82 public DebianDependency searchPkgContainingPom(Dependency dependency) {
83 // TODO shouldn't we use the mavenRepo property of DependencySolver for the mavenRepo path?
84 return searchPkg(new File("/usr/share/maven-repo/" + dependency.getGroupId().replace('.', '/')
85 + "/" + dependency.getArtifactId()) , ".pom");
86 }
87
88 public DebianDependency searchPkgContainingJar(Dependency dependency) {
89 return searchPkg(new File("/usr/share/java/" + dependency.getArtifactId() + ".jar"));
90 }
91
92 /**
93 * Searches the package containing the Javadoc for the specified package.
94 *
95 * @param dependency
96 */
97 public DebianDependency searchJavaDocPkg(DebianDependency dependency) {
98 DebianDependency pkg = searchPkg(new File("/usr/share/doc/" + dependency.getPackageName() + "/api/index.html"));
99 if (pkg == null) {
100 pkg = searchPkg(new File("/usr/share/doc/" + dependency.getPackageName() + "-doc/api/index.html"));
101 }
102 if (pkg == null) {
103 pkg = searchPkg(new File("/usr/share/doc/" + dependency.getPackageName() + "/apidocs/index.html"));
104 }
105 if (pkg == null) {
106 pkg = searchPkg(new File("/usr/share/doc/" + dependency.getPackageName() + "-doc/apidocs/index.html"));
107 }
108 return pkg;
109 }
110
111 public DebianDependency searchPkg(File fileToSearch) {
112 // lookup the cache first
113 if (filesInPackages.containsKey(fileToSearch)) {
114 return new DebianDependency(filesInPackages.get(fileToSearch));
115 }
116
117 GetPackageResult packageResult = new GetPackageResult();
118
119 IOUtil.executeProcess(new String[]{"dpkg", "--search", fileToSearch.getAbsolutePath()}, packageResult);
120 if (!packageResult.getResult().isEmpty()) {
121 String pkg = packageResult.getResult().iterator().next();
122 filesInPackages.put(fileToSearch, pkg);
123 return new DebianDependency(pkg);
124 }
125
126 // Debian policy prevents the use of apt-file during a build
127 if (offline) {
128 System.err.println("Offline mode. Give up looking for package containing " + fileToSearch);
129 return null;
130 }
131
132 if (!new File("/usr/bin/apt-file").exists()) {
133 System.err.println("/usr/bin/apt-file not found. Give up looking for package containing " + fileToSearch);
134 return null;
135 }
136 IOUtil.executeProcess(new String[]{"apt-file", "search", fileToSearch.getAbsolutePath()}, packageResult);
137 if (!packageResult.getResult().isEmpty()) {
138 String pkg = packageResult.getResult().iterator().next();
139 filesInPackages.put(fileToSearch, pkg);
140 return new DebianDependency(pkg);
141 }
142
143 return null;
144 }
145
146 public String getPackageVersion(DebianDependency pkg, boolean onlyInstalled) {
147 GetPackageVersionResult packageResult = new GetPackageVersionResult();
148 IOUtil.executeProcess(new String[]{"dpkg", "--status", pkg.getPackageName()}, packageResult);
149 if (packageResult.getResult() != null) {
150 return packageResult.getResult();
151 }
152 if (!onlyInstalled) {
153 GetChangelogVersionResult versionResult = new GetChangelogVersionResult(pkg.getPackageName());
154 IOUtil.executeProcess(new String[]{"apt-get", "--no-act", "--verbose-versions", "install", pkg.getPackageName()}, versionResult);
155 if (versionResult.getResult() != null) {
156 return versionResult.getResult();
157 }
158 }
159 return null;
160 }
161
162 public List<String> listSharedJars(String library) {
163 if (cacheOfSharedJars.get(library) != null) {
164 return cacheOfSharedJars.get(library);
165 }
166
167 final List<String> jars = new ArrayList<String>();
168 if (library.indexOf("(") > 0) {
169 library = library.substring(0, library.indexOf("(")).trim();
170 }
171 System.out.println();
172 System.out.println("Looking for shared jars in package " + library + "...");
173 IOUtil.executeProcess(new String[]{"dpkg", "--listfiles", library}, new SharedJarOutputHandler(jars));
174 cacheOfSharedJars.put(library, jars);
175 return jars;
176 }
177
178 public List<DebianDependency> addDocDependencies(Collection<DebianDependency> debianDeps, Map<DebianDependency,
179 Dependency> versionedPackagesAndDependencies) {
180 List<DebianDependency> docDeps = new ArrayList<DebianDependency>();
181 for (DebianDependency dependency : debianDeps) {
182 Dependency runtimeDependency = versionedPackagesAndDependencies.get(dependency);
183 if (runtimeDependency != null && runtimeDependency.isPom()) {
184 continue;
185 }
186 DebianDependency docPkg = searchJavaDocPkg(dependency);
187 if (docPkg != null) {
188 docDeps.add(docPkg);
189 }
190 }
191 return docDeps;
192 }
193
194 }