1 /*
2  * Copyright (C) 2023 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 package com.android.tradefed.build.content;
17 
18 import java.util.HashSet;
19 import java.util.Set;
20 
21 /** Provide the context surrounding a content to analyze it properly. */
22 public class ContentAnalysisContext {
23 
24     /** This describes what to expect from the content structure for proper analysis. */
25     public enum AnalysisMethod {
26         FILE,
27         MODULE_XTS,
28         SANDBOX_WORKDIR,
29         BUILD_KEY, // Search directly for a specific item in build info
30         DEVICE_IMAGE // Analyze the device image content
31     }
32 
33     private final String contentEntry;
34     private final ContentInformation information;
35     private final AnalysisMethod analysisMethod;
36     // This tracks path to ignore from analysis because known to always change but do not cause
37     // functional changes.
38     private Set<String> ignoredChange = new HashSet<>();
39     // Report what is considered a common locations which if modified invalidate the results.
40     private Set<String> commonLocations = new HashSet<>();
41     // Set this flag if somehow we need to invalidate the full analysis.
42     private boolean invalidateAnalysis = false;
43     private String mInvalidationReason;
44 
ContentAnalysisContext( String contentEntry, ContentInformation information, AnalysisMethod method)45     public ContentAnalysisContext(
46             String contentEntry, ContentInformation information, AnalysisMethod method) {
47         this.contentEntry = contentEntry;
48         this.information = information;
49         this.analysisMethod = method;
50     }
51 
contentEntry()52     public String contentEntry() {
53         return contentEntry;
54     }
55 
contentInformation()56     public ContentInformation contentInformation() {
57         return information;
58     }
59 
analysisMethod()60     public AnalysisMethod analysisMethod() {
61         return analysisMethod;
62     }
63 
ignoredChanges()64     public Set<String> ignoredChanges() {
65         return ignoredChange;
66     }
67 
commonLocations()68     public Set<String> commonLocations() {
69         return commonLocations;
70     }
71 
abortAnalysis()72     public boolean abortAnalysis() {
73         return invalidateAnalysis || information == null;
74     }
75 
abortReason()76     public String abortReason() {
77         return mInvalidationReason;
78     }
79 
addIgnoreChange(String path)80     public ContentAnalysisContext addIgnoreChange(String path) {
81         ignoredChange.add(path);
82         return this;
83     }
84 
addIgnoreChanges(Set<String> paths)85     public ContentAnalysisContext addIgnoreChanges(Set<String> paths) {
86         ignoredChange.addAll(paths);
87         return this;
88     }
89 
addCommonLocation(String path)90     public ContentAnalysisContext addCommonLocation(String path) {
91         commonLocations.add(path);
92         return this;
93     }
94 
addCommonLocations(Set<String> paths)95     public ContentAnalysisContext addCommonLocations(Set<String> paths) {
96         commonLocations.addAll(paths);
97         return this;
98     }
99 
invalidateAnalysis()100     public void invalidateAnalysis() {
101         invalidateAnalysis = true;
102     }
103 
invalidateAnalysis(String reason)104     public void invalidateAnalysis(String reason) {
105         invalidateAnalysis = true;
106         mInvalidationReason = reason;
107     }
108 }
109