|
21 | 21 | # Visit https://github.com/aboutcode-org/scancode.io for support and download. |
22 | 22 |
|
23 | 23 |
|
| 24 | +from dataclasses import dataclass, field |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
24 | 29 | class EcosystemConfig: |
25 | 30 | """ |
26 | | - Base class for ecosystem specific configurations to be defined |
27 | | - for each ecosystems. |
| 31 | + Base class for ecosystem-specific configurations to be defined |
| 32 | + for each ecosystem. |
28 | 33 | """ |
29 | 34 |
|
30 | 35 | # This should be defined for each ecosystem which |
31 | 36 | # are options in the pipelines |
32 | | - ecosystem_option = None |
| 37 | + ecosystem_option: str = "Default" |
33 | 38 |
|
34 | 39 | # These are extensions for packages of this ecosystem which |
35 | | - # needs to be matched from purldb |
36 | | - purldb_package_extensions = [] |
| 40 | + # need to be matched from purldb |
| 41 | + purldb_package_extensions: list = field(default_factory=list) |
37 | 42 |
|
38 | 43 | # These are extensions for resources of this ecosystem which |
39 | | - # needs to be macthed from purldb |
40 | | - purldb_resource_extensions = [] |
| 44 | + # need to be matched from purldb |
| 45 | + purldb_resource_extensions: list = field(default_factory=list) |
41 | 46 |
|
42 | 47 | # Extensions for document files which do not require review |
43 | | - doc_extensions = [] |
| 48 | + doc_extensions: list = field(default_factory=list) |
44 | 49 |
|
45 | 50 | # Paths in the deployed binaries/archives (on the to/ side) which |
46 | 51 | # do not need review even if they are not matched to the source side |
47 | | - deployed_resource_path_exclusions = [] |
| 52 | + deployed_resource_path_exclusions: list = field(default_factory=list) |
48 | 53 |
|
49 | | - # Paths in the developement/source archive (on the from/ side) which |
| 54 | + # Paths in the development/source archive (on the from/ side) which |
50 | 55 | # should not be considered even if unmapped to the deployed side when |
51 | | - # assesing what to review on the deployed side |
52 | | - devel_resource_path_exclusions = [] |
| 56 | + # assessing what to review on the deployed side |
| 57 | + devel_resource_path_exclusions: list = field(default_factory=list) |
53 | 58 |
|
54 | | - # Symbols which are found in ecosystem specific standard libraries |
| 59 | + # Symbols which are found in ecosystem-specific standard libraries |
55 | 60 | # which are not so useful in mapping |
56 | | - standard_symbols_to_exclude = [] |
57 | | - |
58 | | - |
59 | | -class DefaultEcosystemConfig(EcosystemConfig): |
60 | | - """Configurations which are common across multiple ecosystems.""" |
61 | | - |
62 | | - ecosystem_option = "Default" |
63 | | - purldb_package_extensions = [".zip", ".tar.gz", ".tar.xz"] |
64 | | - devel_resource_path_exclusions = ["*/tests/*"] |
65 | | - doc_extensions = [ |
66 | | - ".pdf", |
67 | | - ".doc", |
68 | | - ".docx", |
69 | | - ".ppt", |
70 | | - ".pptx", |
71 | | - ".tex", |
72 | | - ".odt", |
73 | | - ".odp", |
74 | | - ] |
| 61 | + standard_symbols_to_exclude: list = field(default_factory=list) |
| 62 | + |
| 63 | + |
| 64 | +# Dictionary of ecosystem configurations |
| 65 | +ECOSYSTEM_CONFIGS = { |
| 66 | + "Default": EcosystemConfig( |
| 67 | + purldb_package_extensions=[".zip", ".tar.gz", ".tar.xz"], |
| 68 | + devel_resource_path_exclusions=["*/tests/*"], |
| 69 | + doc_extensions=[ |
| 70 | + ".pdf", |
| 71 | + ".doc", |
| 72 | + ".docx", |
| 73 | + ".ppt", |
| 74 | + ".pptx", |
| 75 | + ".tex", |
| 76 | + ".odt", |
| 77 | + ".odp", |
| 78 | + ], |
| 79 | + ), |
| 80 | + "Java": EcosystemConfig( |
| 81 | + ecosystem_option="Java", |
| 82 | + purldb_package_extensions=[".jar", ".war"], |
| 83 | + purldb_resource_extensions=[".class"], |
| 84 | + ), |
| 85 | + "JavaScript": EcosystemConfig( |
| 86 | + ecosystem_option="JavaScript", |
| 87 | + purldb_resource_extensions=[ |
| 88 | + ".map", |
| 89 | + ".js", |
| 90 | + ".mjs", |
| 91 | + ".ts", |
| 92 | + ".d.ts", |
| 93 | + ".jsx", |
| 94 | + ".tsx", |
| 95 | + ".css", |
| 96 | + ".scss", |
| 97 | + ".less", |
| 98 | + ".sass", |
| 99 | + ".soy", |
| 100 | + ], |
| 101 | + ), |
| 102 | + "Go": EcosystemConfig( |
| 103 | + ecosystem_option="Go", |
| 104 | + purldb_resource_extensions=[".go"], |
| 105 | + ), |
| 106 | + "Rust": EcosystemConfig( |
| 107 | + ecosystem_option="Rust", |
| 108 | + purldb_resource_extensions=[".rs"], |
| 109 | + ), |
| 110 | + "Ruby": EcosystemConfig( |
| 111 | + ecosystem_option="Ruby", |
| 112 | + purldb_package_extensions=[".gem"], |
| 113 | + purldb_resource_extensions=[".rb"], |
| 114 | + deployed_resource_path_exclusions=["*checksums.yaml.gz*", "*metadata.gz*"], |
| 115 | + ), |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +def get_ecosystem_config(ecosystem): |
| 120 | + """Return the ``ecosystem`` config.""" |
| 121 | + return ECOSYSTEM_CONFIGS.get(ecosystem, ECOSYSTEM_CONFIGS["Default"]) |
| 122 | + |
| 123 | + |
| 124 | +def load_ecosystem_config(pipeline, options): |
| 125 | + """ |
| 126 | + Add ecosystem specific configurations for each ecosystem selected |
| 127 | + as `options` to the `pipeline`. These configurations are used for: |
| 128 | + - which resource/package extensions to match to purldb |
| 129 | + - which source files to get source symbols from |
| 130 | + - which unmapped paths to ignore in deployed binaries |
| 131 | + """ |
| 132 | + configs_by_ecosystem = { |
| 133 | + ecosystem.ecosystem_option: ecosystem for ecosystem in ECOSYSTEM_CONFIGS.values() |
| 134 | + } |
75 | 135 |
|
| 136 | + # Add default configurations which are common accross ecosystems |
| 137 | + pipeline.ecosystem_config = ECOSYSTEM_CONFIGS.get("Default") |
76 | 138 |
|
77 | | -class JavaEcosystemConfig(EcosystemConfig): |
78 | | - ecosystem_option = "Java" |
79 | | - purldb_package_extensions = [".jar", ".war"] |
80 | | - purldb_resource_extensions = [".class"] |
81 | | - |
82 | | - |
83 | | -class JavaScriptEcosystemConfig(EcosystemConfig): |
84 | | - ecosystem_option = "JavaScript" |
85 | | - purldb_resource_extensions = [ |
86 | | - ".map", |
87 | | - ".js", |
88 | | - ".mjs", |
89 | | - ".ts", |
90 | | - ".d.ts", |
91 | | - ".jsx", |
92 | | - ".tsx", |
93 | | - ".css", |
94 | | - ".scss", |
95 | | - ".less", |
96 | | - ".sass", |
97 | | - ".soy", |
98 | | - ] |
| 139 | + # Add configurations for each selected ecosystem |
| 140 | + for selected_option in options: |
| 141 | + if selected_option not in configs_by_ecosystem: |
| 142 | + continue |
| 143 | + |
| 144 | + add_ecosystem_config( |
| 145 | + pipeline_ecosystem_config=pipeline.ecosystem_config, |
| 146 | + configs_by_ecosystem=configs_by_ecosystem, |
| 147 | + selected_option=selected_option, |
| 148 | + ) |
99 | 149 |
|
100 | 150 |
|
101 | | -class GoEcosystemConfig(EcosystemConfig): |
102 | | - ecosystem_option = "Go" |
103 | | - purldb_resource_extensions = [".go"] |
| 151 | +def add_ecosystem_config(pipeline_ecosystem_config, configs_by_ecosystem, selected_option): |
| 152 | + """ |
| 153 | + Set the `pipeline_ecosystem_config` values from all the individual ecosystem based configurations |
| 154 | + defined in `configs_by_ecosystem`, based on pipeline `selected_option` which selects an |
| 155 | + ecosytem. |
| 156 | + """ |
| 157 | + d2d_pipeline_configs = [ |
| 158 | + "purldb_package_extensions", |
| 159 | + "purldb_resource_extensions", |
| 160 | + "deployed_resource_path_exclusions", |
| 161 | + ] |
104 | 162 |
|
| 163 | + ecosystem_config = configs_by_ecosystem.get(selected_option) |
105 | 164 |
|
106 | | -class RustEcosystemConfig(EcosystemConfig): |
107 | | - ecosystem_option = "Rust" |
108 | | - purldb_resource_extensions = [".rs"] |
| 165 | + for config_name in d2d_pipeline_configs: |
| 166 | + config_value = getattr(ecosystem_config, config_name) |
| 167 | + pipeline_config_value = getattr(pipeline_ecosystem_config, config_name) |
| 168 | + if config_value: |
| 169 | + if not pipeline_config_value: |
| 170 | + new_config_value = config_value |
| 171 | + else: |
| 172 | + new_config_value = pipeline_config_value.extend(config_value) |
109 | 173 |
|
| 174 | + setattr(pipeline_ecosystem_config, config_name, new_config_value) |
110 | 175 |
|
111 | | -class RubyEcosystemConfig(EcosystemConfig): |
112 | | - ecosystem_option = "Ruby" |
113 | | - purldb_package_extensions = [".gem"] |
114 | | - purldb_resource_extensions = [".rb"] |
115 | | - deployed_resource_path_exclusions = ["*checksums.yaml.gz*", "*metadata.gz*"] |
|
0 commit comments