aboutsummaryrefslogtreecommitdiff
blob: c2cc9768dfed3418c4a01258cb1b9292bd9a16ab (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package org.gentoo.java.ebuilder.portage;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gentoo.java.ebuilder.Config;

/**
 * Parses portage tree and collects information needed for generating ebuilds.
 *
 * @author fordfrog
 */
public class PortageParser {

    /**
     * Cache version.
     */
    public static final String CACHE_VERSION = "1.0";
    /**
     * Current java package eclass name.
     */
    private static final String ECLASS_JAVA_PKG = "java-pkg-2";
    /**
     * Current java optional package eclass name.
     */
    private static final String ECLASS_JAVA_PKG_OPT = "java-pkg-opt-2";
    /**
     * Current java simple package eclass name.
     */
    private static final String ECLASS_JAVA_PKG_SIMPLE = "java-pkg-simple";
    /**
     * Current java utilities eclass name.
     */
    private static final String ECLASS_JAVA_UTILS = "java-utils-2";
    /**
     * Pattern for parsing SLOT with bash substring.
     */
    private static final Pattern PATTERN_SLOT_SUBSTRING = Pattern.compile(
            "^\\$\\{PV:(\\d+):(\\d+)\\}$");
    /**
     * Pattern for parsing version component range in SLOT.
     */
    private static final Pattern PATTERN_SLOT_VERSION_COMPOPONENT_RANGE
            = Pattern.compile(
                    "^\\$\\(get_version_component_range (\\d+)-(\\d+)\\)$");
    /**
     * Pattern for checking whether the line contains variable declaration. It
     * does not handle correctly variables spread across several lines but we
     * most probably do not care about these.
     */
    private static final Pattern PATTERN_VARIABLE = Pattern.compile(
            "^(\\S+?)=(.*)$");
    /**
     * List of cache items. This list is populated during parsing the tree.
     */
    private final List<CacheItem> cacheItems = new ArrayList<>(40_000);
    /**
     * Number of processed categories. Updated during parsing the tree.
     */
    private int processedCategories;
    /**
     * Number of processed ebuilds. Updated during parsing the tree.
     */
    private int processedEbuilds;
    /**
     * Number of processed packages. Updated during parsing the tree.
     */
    private int processedPackages;

    /**
     * Parses portage tree at specified path and create ebuild cache at
     * ~/.java-ebuilder/cache.
     *
     * @param config application configuration
     */
    public void parseTree(final Config config) {
        final long startTimestamp = System.currentTimeMillis();
        cacheItems.clear();
        processedCategories = 0;
        processedPackages = 0;
        processedEbuilds = 0;

        config.getStdoutWriter().println("Parsing portage tree @ "
                + config.getPortageTree() + " ...");
        parseCategories(config.getPortageTree());

        final long endTimestamp = System.currentTimeMillis();

        config.getStdoutWriter().println(MessageFormat.format(
                "Parsed {0} categories {1} packages {2} ebuilds in {3}ms and "
                + "found {4} java ebuilds",
                processedCategories, processedPackages, processedEbuilds,
                endTimestamp - startTimestamp, cacheItems.size()));

        config.getStdoutWriter().print("Writing cache file...");
        writeCacheFile(config);
        config.getStdoutWriter().println("done");
    }

    /**
     * Extracts the most important java eclass from ebuild inherit line.
     *
     * @param inheritLine ebuild inherit line
     *
     * @return java eclass or null
     */
    private String getJavaInheritEclass(final String inheritLine) {
        if (inheritLine.contains(ECLASS_JAVA_PKG)) {
            return ECLASS_JAVA_PKG;
        } else if (inheritLine.contains(ECLASS_JAVA_PKG_OPT)) {
            return ECLASS_JAVA_PKG_OPT;
        } else if (inheritLine.contains(ECLASS_JAVA_PKG_SIMPLE)) {
            return ECLASS_JAVA_PKG_SIMPLE;
        } else if (inheritLine.contains(ECLASS_JAVA_UTILS)) {
            return ECLASS_JAVA_UTILS;
        } else {
            return null;
        }
    }

    /**
     * Parses categories in the portage tree root.
     *
     * @param treePath portage tree path
     */
    private void parseCategories(final Path treePath) {
        final File[] categories = treePath.toFile().listFiles(
                (final File pathname) -> pathname.isDirectory());

        for (final File category : categories) {
            parseCategory(category);
            processedCategories++;
        }
    }

    /**
     * Parses category and its packages.
     *
     * @param category category path
     */
    private void parseCategory(final File category) {
        final File[] packages = category.listFiles(
                (final File pathname) -> pathname.isDirectory());

        for (final File pkg : packages) {
            parsePackage(pkg);
            processedPackages++;
        }
    }

    /**
     * Parses single ebuild.
     *
     * @param ebuild ebuild path
     */
    private void parseEbuild(final File ebuild) {
        final String filename = ebuild.getName().replaceAll("\\.ebuild$", "");
        final String category = ebuild.getParentFile().getParentFile().getName();
        final String pkg = ebuild.getParentFile().getName();
        final String version = filename.substring(pkg.length() + 1);
        final Map<String, String> variables = new HashMap<>(20);
        String eclass = null;
        String slot = "0";
        String useFlag = null;
        String mavenId = null;
        String groupId = null;
        String artifactId = null;
        String mavenVersion = null;

        try (final BufferedReader reader = new BufferedReader(
                new InputStreamReader(Files.newInputStream(ebuild.toPath(),
                        StandardOpenOption.READ)))) {
            String line = reader.readLine();

            while (line != null) {
                line = line.trim();

                if (!line.isEmpty()) {
                    final int pos = line.indexOf('#');

                    if (pos != -1) {
                        line = line.substring(0, pos).trim();
                    }
                }

                if (!line.isEmpty()) {
                    final Matcher matcher = PATTERN_VARIABLE.matcher(line);

                    if (matcher.matches()) {
                        variables.put(matcher.group(1), matcher.group(2).
                                replaceAll("(^\"|\"$)", ""));
                    }

                    if (line.startsWith("inherit ")) {
                        eclass = getJavaInheritEclass(line);

                        if (eclass == null) {
                            return;
                        }
                    } else if (line.startsWith("SLOT=")) {
                        slot = line.substring("SLOT=".length()).replace(
                                "\"", "").replaceAll("/.*", "");
                    } else if (line.startsWith("JAVA_PKG_OPT_USE=")) {
                        useFlag = line.substring("JAVA_PKG_OPT_USE=".length()).
                                replace("\"", "");
                    } else if (line.startsWith("MAVEN_ID=")) {
                        mavenId = line.substring("MAVEN_ID=".length()).
                                replace("\"", "");
                    }
                }

                line = reader.readLine();
            }
        } catch (final IOException ex) {
            throw new RuntimeException("Failed to read ebuild", ex);
        }

        if (eclass == null) {
            return;
        }

        if (ECLASS_JAVA_PKG_OPT.equals(eclass) && useFlag == null) {
            useFlag = "java";
        }

        final String pv;
        final int pos = version.indexOf('-');

        if (pos == -1) {
            pv = version;
        } else {
            pv = version.substring(0, pos);
        }

        slot = processSlot(slot, pv, variables);

        if (mavenId != null) {
            mavenId = mavenId.replaceAll("\\$(\\{PN\\}|PN)", pkg).
                    replaceAll("\\$(\\{PV\\}|PV)", pv);

            final String[] parts = mavenId.split(":");

            if (parts[0].isEmpty()) {
                groupId = pkg;
            } else {
                groupId = parts[0];
            }

            if (parts.length > 1) {
                artifactId = parts[1];
            } else {
                artifactId = pkg;
            }

            if (parts.length > 2) {
                mavenVersion = parts[2];
            } else {
                mavenVersion = version;
            }
        }

        cacheItems.add(new CacheItem(category, pkg, version, slot, useFlag,
                groupId, artifactId, mavenVersion));
    }

    /**
     * Parses package and its ebuilds.
     *
     * @param pkg package path
     */
    private void parsePackage(final File pkg) {
        final File[] ebuilds = pkg.listFiles(
                (final File pathname) -> pathname.isFile()
                && pathname.getName().endsWith(".ebuild"));

        for (final File ebuild : ebuilds) {
            parseEbuild(ebuild);
            processedEbuilds++;
        }
    }

    /**
     * Processes various instructions in SLOT string.
     *
     * @param slot      SLOT string
     * @param pv        PV variable
     * @param variables map of collected variables and their values
     *
     * @return processed SLOT string
     */
    private String processSlot(final String slot, final String pv,
            final Map<String, String> variables) {
        String result = slot.replaceAll("\\$(\\{PV\\}|PV)", pv);

        if (result.indexOf('$') != -1) {
            for (final Map.Entry<String, String> variable
                    : variables.entrySet()) {
                result = result.
                        replace("$" + variable.getKey(), variable.getValue()).
                        replace("${" + variable.getKey() + '}',
                                variable.getValue());
            }
        }

        if (result.indexOf('$') != -1) {
            final Matcher matcher = PATTERN_SLOT_SUBSTRING.matcher(result);

            if (matcher.matches()) {
                final int start = Integer.parseInt(matcher.group(1), 10);
                final int length = Integer.parseInt(matcher.group(2), 10);
                result = pv.substring(start, start + length);
            }
        }

        if (result.indexOf('$') != -1) {
            final Matcher matcher = PATTERN_SLOT_VERSION_COMPOPONENT_RANGE.
                    matcher(result);

            if (matcher.matches()) {
                final int start = Integer.parseInt(matcher.group(1), 10);
                final int end = Integer.parseInt(matcher.group(2), 10);
                final String[] parts = pv.split("\\.");
                final StringBuilder sbResult = new StringBuilder(10);

                for (int i = start; i <= end; i++) {
                    if (sbResult.length() > 0) {
                        sbResult.append('.');
                    }

                    sbResult.append(i <= parts.length ? parts[i - 1] : '0');
                }

                result = sbResult.toString();
            }
        }

        return result;
    }

    /**
     * Writes cache items to the cache file.
     *
     * @param config application configuration
     */
    private void writeCacheFile(final Config config) {
        final File cacheDir = config.getCacheFile().getParent().toFile();

        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }

        cacheItems.sort((
                final CacheItem o1,
                final CacheItem o2) -> {
            if (o1.getCategory().compareTo(o2.getCategory()) != 0) {
                return o1.getCategory().compareTo(o2.getCategory());
            } else if (o1.getPkg().compareTo(o2.getPkg()) != 0) {
                return o1.getPkg().compareTo(o2.getPkg());
            } else {
                return o1.getVersion().compareTo(o2.getVersion());
            }
        });

        try (final OutputStreamWriter writer = new OutputStreamWriter(
                Files.newOutputStream(config.getCacheFile(),
                        StandardOpenOption.CREATE, StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING),
                Charset.forName("UTF-8"))) {
            writer.write(CACHE_VERSION);
            writer.write('\n');

            for (final CacheItem cacheItem : cacheItems) {
                writer.write(cacheItem.getCategory());
                writer.write(':');
                writer.write(cacheItem.getPkg());
                writer.write(':');
                writer.write(cacheItem.getVersion());
                writer.write(':');
                writer.write(cacheItem.getSlot());
                writer.write(':');
                writer.write(cacheItem.getUseFlag() == null
                        ? "" : cacheItem.getUseFlag());

                if (cacheItem.getGroupId() != null) {
                    writer.write(':');
                    writer.write(cacheItem.getGroupId());
                    writer.write(':');
                    writer.write(cacheItem.getArtifactId());

                    if (cacheItem.getMavenVersion() != null) {
                        writer.write(':');
                        writer.write(cacheItem.getMavenVersion());
                    }
                }

                writer.write('\n');
            }
        } catch (final IOException ex) {
            throw new RuntimeException("Failed to write cache file @ "
                    + config.getCacheFile(), ex);
        }
    }
}