aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed/targetprep/FlashingResourcesParser.java
blob: 2e07c38347a269eb8c8b18368504c4c6a2e61afe (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
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tradefed.targetprep;

import com.android.tradefed.util.MultiMap;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
 * A class that parses out required versions of auxiliary image files needed to flash a device.
 * (e.g. bootloader, baseband, etc)
 */
public class FlashingResourcesParser implements IFlashingResourcesParser {
    /**
     * A filtering interface, intended to allow {@link FlashingResourcesParser} to ignore some
     * resources that it otherwise might use
     */
    public static interface Constraint {
        /**
         * Check if the provided {@code item} passes the constraint.
         * @return {@code true} for accept, {@code false} for reject
         */
        public boolean shouldAccept(String item);
    }

    private static final String ANDROID_INFO_FILE_NAME = "android-info.txt";
    /**
     * Some resource files use "require-foo=bar", others use "foo=bar". This expression handles
     * both.
     */
    private static final Pattern REQUIRE_PATTERN = Pattern.compile("(?:require\\s)?(.*?)=(.*)");
    /**
     * Some resource files have special product-specific requirements, for instance:
     * {@code require-for-product:product1 version-bootloader=xyz} would only require bootloader
     * {@code xyz} for device {@code product1}.  This pattern matches the require-for-product line
     */
    private static final Pattern PRODUCT_REQUIRE_PATTERN =
            Pattern.compile("require-for-product:(\\S+) +(.*?)=(.*)");

    // expected keys
    public static final String PRODUCT_KEY = "product";
    public static final String BOARD_KEY = "board";
    public static final String BOOTLOADER_VERSION_KEY = "version-bootloader";
    public static final String BASEBAND_VERSION_KEY = "version-baseband";

    // key-value pairs of build requirements
    private AndroidInfo mReqs;

    /**
     * A typedef for {@code Map<String, MultiMap<String, String>>}.  Useful parsed
     * format for storing the data encoded in ANDROID_INFO_FILE_NAME
     */
    @SuppressWarnings("serial")
    public static class AndroidInfo extends HashMap<String, MultiMap<String, String>> {}

    /**
     * Create a {@link FlashingResourcesParser} and have it parse the specified device image for
     * flashing requirements.  Flashing requirements must pass the appropriate constraint (if one
     * exists) before being added.  Rejected requirements will be dropped silently.
     *
     * @param deviceImgZipFile The {@code updater.zip} file to be flashed
     * @param c A map from key name to {@link Constraint}.  Image names will be checked against
     *        the appropriate constraint (if any) as a prereq for being added.  May be null to
     *        disable filtering.
     */
    public FlashingResourcesParser(File deviceImgZipFile, Map<String, Constraint> c)
            throws TargetSetupError {
        mReqs = getBuildRequirements(deviceImgZipFile, c);
    }

    /**
     * Create a {@link FlashingResourcesParser} and have it parse the specified device image for
     * flashing requirements.
     *
     * @param deviceImgZipFile The {@code updater.zip} file to be flashed
     */
    public FlashingResourcesParser(File deviceImgZipFile) throws TargetSetupError {
        this(deviceImgZipFile, null);
    }

    /**
     * Constructs a FlashingResourcesParser with the supplied AndroidInfo Reader
     * <p/>
     * Exposed for unit testing
     *
     * @param infoReader a {@link BufferedReader} containing the equivalent of android-info.txt to
     *        parse
     * @param c A map from key name to {@link Constraint}.  Image names will be checked against
     *        the appropriate constraint (if any) as a prereq for being added.  May be null to
     *        disable filtering.
     */
    public FlashingResourcesParser(BufferedReader infoReader, Map<String, Constraint> c)
            throws TargetSetupError, IOException {
        mReqs = parseAndroidInfo(infoReader, c);
    }

    /**
     * Constructs a FlashingResourcesParser with the supplied AndroidInfo Reader
     * <p/>
     * Exposed for unit testing
     *
     * @param infoReader a {@link BufferedReader} containing the equivalent of android-info.txt to
     *        parse
     */
    public FlashingResourcesParser(BufferedReader infoReader) throws TargetSetupError,
            IOException {
        this(infoReader, null);
    }

    /**
     * {@inheritDoc}
     * <p/>
     * If multiple versions are listed, get the latest with the assumption that versions sort from
     * oldest to newest alphabetically.
     */
    @Override
    public String getRequiredBootloaderVersion() {
        return getRequiredImageVersion(BOOTLOADER_VERSION_KEY);
    }

    /**
     * {@inheritDoc}
     * <p/>
     * If multiple versions are listed, get the latest with the assumption that versions sort from
     * oldest to newest alphabetically.
     */
    @Override
    public String getRequiredBasebandVersion() {
        return getRequiredImageVersion(BASEBAND_VERSION_KEY);
    }

    /**
     * {@inheritDoc}
     * <p/>
     * If multiple versions are listed, get the latest with the assumption that versions sort from
     * oldest to newest alphabetically.
     */
    @Override
    public String getRequiredImageVersion(String imageVersionKey) {
        // Use null to designate the global product requirements
        return getRequiredImageVersion(imageVersionKey, null);
    }

    /**
     * {@inheritDoc}
     * <p/>
     * If multiple versions are listed, get the latest with the assumption that versions sort from
     * oldest to newest alphabetically.
     */
    @Override
    public String getRequiredImageVersion(String imageVersionKey, String productName) {
        MultiMap<String, String> productReqs = mReqs.get(productName);

        if (productReqs == null && productName != null) {
            // There aren't any product-specific requirements for productName.  Fall back to global
            // requirements.
            return getRequiredImageVersion(imageVersionKey, null);
        }

        // Get the latest version assuming versions are sorted alphabetically.
        String result = getNewest(productReqs.get(imageVersionKey));

        if (result != null) {
            // If there's a result, return it
            return result;
        }
        if (result == null && productName != null) {
            // There aren't any product-specific requirements for this particular imageVersionKey
            // for productName.  Fall back to global requirements.
            return getRequiredImageVersion(imageVersionKey, null);
        }

        // Neither a specific nor a global result exists; return null
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<String> getRequiredBoards() {
        Collection<String> all = new ArrayList<String>();
        MultiMap<String, String> boardReqs = mReqs.get(null);
        if (boardReqs == null) {
            return null;
        }

        Collection<String> board = boardReqs.get(BOARD_KEY);
        Collection<String> product = boardReqs.get(PRODUCT_KEY);

        // board overrides product here
        if (board != null) {
            all.addAll(board);
        } else if (product != null) {
            all.addAll(product);
        } else {
            return null;
        }

        return all;
    }

    /**
     * Gets the newest element in the given {@link Collection} or <code>null</code> with the
     * assumption that newer elements follow older elements when sorted alphabetically.
     */
    private static String getNewest(Collection<String> values) {
        if (values == null || values.isEmpty()) {
            return null;
        }
        String newest = null;
        for (String element : values) {
            if (newest == null || element.compareTo(newest) > 0) {
                newest = element;
            }
        }
        return newest;
    }

    /**
     * This parses android-info.txt from system image zip and returns key value pairs of required
     * image files.
     * <p/>
     * Expects the following syntax:
     * <p/>
     * <i>[require] key=value1[|value2]</i>
     *
     * @return a {@link Map} of parsed key value pairs, or <code>null</code> if data could not be
     * parsed
     */
    static AndroidInfo getBuildRequirements(File deviceImgZipFile,
            Map<String, Constraint> constraints) throws TargetSetupError {
        ZipFile deviceZip = null;
        BufferedReader infoReader = null;
        try {
            deviceZip = new ZipFile(deviceImgZipFile);
            ZipEntry androidInfoEntry = deviceZip.getEntry(ANDROID_INFO_FILE_NAME);
            if (androidInfoEntry == null) {
                throw new TargetSetupError(String.format("Could not find %s in device image zip %s",
                        ANDROID_INFO_FILE_NAME, deviceImgZipFile.getName()));
            }
            infoReader = new BufferedReader(new InputStreamReader(
                    deviceZip.getInputStream(androidInfoEntry)));

            return parseAndroidInfo(infoReader, constraints);
        } catch (ZipException e) {
            throw new TargetSetupError(String.format("Could not read device image zip %s",
                    deviceImgZipFile.getName()), e);
        } catch (IOException e) {
            throw new TargetSetupError(String.format("Could not read device image zip %s",
                    deviceImgZipFile.getName()), e);
        } finally {
            if (deviceZip != null) {
                try {
                    deviceZip.close();
                } catch (IOException e) {
                    // ignore
                }
            }
            if (infoReader != null) {
                try {
                    infoReader.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }

    /**
     * Returns the current value for the provided key if one exists, or creates and returns a new
     * value if one does not exist.
     */
    private static MultiMap<String, String> getOrCreateEntry(AndroidInfo map, String key) {
        if (map.containsKey(key)) {
            return map.get(key);
        } else {
            MultiMap<String, String> value = new MultiMap<String, String>();
            map.put(key, value);
            return value;
        }
    }

    /**
     * Parses the required build attributes from an android-info data source.
     * <p/>
     * Exposed as package-private for unit testing.
     *
     * @param infoReader the {@link BufferedReader} to read android-info text data from
     * @return a Map of parsed attribute name-value pairs
     * @throws IOException
     */
    static AndroidInfo parseAndroidInfo(BufferedReader infoReader,
            Map<String, Constraint> constraints) throws IOException {
        AndroidInfo requiredImageMap = new AndroidInfo();

        boolean eof = false;
        while (!eof) {
            String line = infoReader.readLine();
            if (line != null) {
                Matcher matcher = PRODUCT_REQUIRE_PATTERN.matcher(line);
                if (matcher.matches()) {
                    String product = matcher.group(1);
                    String key = matcher.group(2);
                    String values = matcher.group(3);
                    // Requirements specific to product {@code product}
                    MultiMap<String, String> reqs = getOrCreateEntry(requiredImageMap, product);
                    for (String value : values.split("\\|")) {
                        reqs.put(key, value);
                    }
                } else {
                    matcher = REQUIRE_PATTERN.matcher(line);
                    if (matcher.matches()) {
                        String key = matcher.group(1);
                        String values = matcher.group(2);
                        Constraint c = null;
                        if (constraints != null) {
                            c = constraints.get(key);
                        }

                        // Use a null product identifier to designate requirements for all products
                        MultiMap<String, String> reqs = getOrCreateEntry(requiredImageMap, null);
                        for (String value : values.split("\\|")) {
                            if ((c == null) || c.shouldAccept(value)) {
                                reqs.put(key, value);
                            }
                        }
                    }
                }
            } else {
                eof = true;
            }
        }
        return requiredImageMap;
    }
}