1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 com.android.cts.releaseparser;
18 
19 import com.android.cts.releaseparser.ReleaseProto.*;
20 
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.logging.Logger;
28 
29 public class BuildPropParser extends FileParser {
30     private Entry.EntryType mType;
31     private HashMap<String, String> mProp;
32 
BuildPropParser(File file)33     public BuildPropParser(File file) {
34         super(file);
35     }
36 
37     @Override
getType()38     public Entry.EntryType getType() {
39         if (mType == null) {
40             parseFile();
41         }
42         return mType;
43     }
44 
45     @Override
setAdditionalInfo()46     public void setAdditionalInfo() {
47         Map<String, String> properties = getProperties();
48         if (properties != null) {
49             getFileEntryBuilder().putAllProperties(properties);
50         }
51     }
52 
getBuildNumber()53     public String getBuildNumber() {
54         return getProperty("ro.build.version.incremental");
55     }
56 
getVersion()57     public String getVersion() {
58         return getProperty("ro.build.id");
59     }
60 
getName()61     public String getName() {
62         return getProperty("ro.product.device");
63     }
64 
getFullName()65     public String getFullName() {
66         return getProperty("ro.build.flavor");
67     }
68 
getProperties()69     public Map<String, String> getProperties() {
70         if (mType == null) {
71             parseFile();
72         }
73         return mProp;
74     }
75 
getProperty(String propertyName)76     public String getProperty(String propertyName) {
77         if (mType == null) {
78             parseFile();
79         }
80         return mProp.get(propertyName);
81     }
82 
parseFile()83     private void parseFile() {
84         try {
85             FileReader fileReader = new FileReader(getFile());
86             BufferedReader buffReader = new BufferedReader(fileReader);
87             String line;
88             mProp = new HashMap<>();
89             while ((line = buffReader.readLine()) != null) {
90                 String trimLine = line.trim();
91                 // skips blank lines or comments e.g. # begin build properties
92                 if (trimLine.length() > 0 && !trimLine.startsWith("#")) {
93                     // gets name=value pair, e.g. ro.build.id=PPR1.180610.011
94                     String[] phases = trimLine.split("=");
95                     if (phases.length > 1) {
96                         mProp.put(phases[0], phases[1]);
97                     } else {
98                         mProp.put(phases[0], "");
99                     }
100                 }
101             }
102             fileReader.close();
103             mType = Entry.EntryType.BUILD_PROP;
104         } catch (IOException e) {
105             // file is not a Test Module Config
106             System.err.println("BuildProp err:" + getFileName() + "\n" + e.getMessage());
107             mType = super.getType();
108         }
109     }
110 
111     private static final String USAGE_MESSAGE =
112             "Usage: java -jar releaseparser.jar "
113                     + BuildPropParser.class.getCanonicalName()
114                     + " [-options <parameter>]...\n"
115                     + "           to prase build.prop file meta data\n"
116                     + "Options:\n"
117                     + "\t-i PATH\t The file path of the file to be parsed.\n"
118                     + "\t-of PATH\t The file path of the output file instead of printing to System.out.\n";
119 
main(String[] args)120     public static void main(String[] args) {
121         try {
122             ArgumentParser argParser = new ArgumentParser(args);
123             String fileName = argParser.getParameterElement("i", 0);
124             String outputFileName = argParser.getParameterElement("of", 0);
125 
126             File aFile = new File(fileName);
127             BuildPropParser aParser = new BuildPropParser(aFile);
128             Entry.Builder fileEntryBuilder = aParser.getFileEntryBuilder();
129             writeTextFormatMessage(outputFileName, fileEntryBuilder.build());
130         } catch (Exception ex) {
131             System.out.println(USAGE_MESSAGE);
132             ex.printStackTrace();
133         }
134     }
135 
getLogger()136     private static Logger getLogger() {
137         return Logger.getLogger(BuildPropParser.class.getSimpleName());
138     }
139 }
140