• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         // Never consider ignored as part of common locations
70         commonLocations.removeAll(ignoredChange);
71         return commonLocations;
72     }
73 
abortAnalysis()74     public boolean abortAnalysis() {
75         return invalidateAnalysis || information == null;
76     }
77 
abortReason()78     public String abortReason() {
79         return mInvalidationReason;
80     }
81 
addIgnoreChange(String path)82     public ContentAnalysisContext addIgnoreChange(String path) {
83         ignoredChange.add(path);
84         return this;
85     }
86 
addIgnoreChanges(Set<String> paths)87     public ContentAnalysisContext addIgnoreChanges(Set<String> paths) {
88         ignoredChange.addAll(paths);
89         return this;
90     }
91 
addCommonLocation(String path)92     public ContentAnalysisContext addCommonLocation(String path) {
93         commonLocations.add(path);
94         return this;
95     }
96 
addCommonLocations(Set<String> paths)97     public ContentAnalysisContext addCommonLocations(Set<String> paths) {
98         commonLocations.addAll(paths);
99         return this;
100     }
101 
invalidateAnalysis()102     public void invalidateAnalysis() {
103         invalidateAnalysis = true;
104     }
105 
invalidateAnalysis(String reason)106     public void invalidateAnalysis(String reason) {
107         invalidateAnalysis = true;
108         mInvalidationReason = reason;
109     }
110 }
111