77# See https://aboutcode.org for more information about nexB OSS projects.
88#
99import logging
10+ import os
1011from pathlib import Path
1112
1213import saneyaml
@@ -45,6 +46,7 @@ def export_data(self, git_path):
4546 ecosystems = [pkg .type for pkg in Package .objects .distinct ("type" )]
4647
4748 for ecosystem in ecosystems :
49+ version_files = {} # {"version path": "data" }
4850 package_files = {} # {"package path": "data" }
4951 vul_files = {} # {"vulnerability path": "data" }
5052
@@ -53,73 +55,76 @@ def export_data(self, git_path):
5355 .prefetch_related ("vulnerabilities" )
5456 .paginated ()
5557 ):
56-
5758 purl_without_version = PackageURL (
5859 type = purl .type ,
5960 namespace = purl .namespace ,
6061 name = purl .name ,
6162 )
62- package_dir = create_sub_paths (git_path , purl .type , purl .namespace , purl .name )
63- filename = f"{ purl .type } -{ purl .namespace } -{ purl .name } .yml" .replace ("/" , " " )
64- package_dir_file = package_dir .joinpath (filename )
65- if package_dir_file in package_files :
66- package_data = {
67- "purl" : str (purl ),
68- "affected_by_vulnerabilities" : [
69- vuln .vulnerability_id for vuln in purl .affected_by
70- ],
71- "fixing_vulnerabilities" : [vuln .vulnerability_id for vuln in purl .fixing ],
72- }
73- package_files [package_dir_file ]["versions" ].append (package_data )
63+
64+ # ./aboutcode-packages-ed5/maven/org.apache.log4j/log4j-core/versions/vulnerabilities.yml
65+ pkg_filepath = (
66+ f"./aboutcode-packages-ed5/{ purl .type } /{ purl .namespace } /{ purl .name } "
67+ f"/versions/vulnerabilities.yml"
68+ )
69+
70+ # ./aboutcode-packages-ed5/maven/org.apache.log4j/log4j-core/versions/1.2.3/vulnerabilities.yml
71+ version_filepath = (
72+ f"./aboutcode-packages-ed5/{ purl .type } /{ purl .namespace } /{ purl .name } /versions/"
73+ f"{ purl .version } /vulnerabilities.yml"
74+ )
75+
76+ package_data = {
77+ "purl" : str (purl ),
78+ "affected_by_vulnerabilities" : [
79+ vuln .vulnerability_id for vuln in purl .affected_by
80+ ],
81+ "fixing_vulnerabilities" : [vuln .vulnerability_id for vuln in purl .fixing ],
82+ }
83+
84+ if pkg_filepath in package_files :
85+ package_files [pkg_filepath ]["versions" ].append (package_data )
7486 else :
75- package_files [package_dir_file ] = {
87+ package_files [pkg_filepath ] = {
7688 "package" : str (purl_without_version ),
77- "versions" : [
78- {
79- "purl" : str (purl ),
80- "affected_by_vulnerabilities" : [
81- vuln .vulnerability_id for vuln in purl .affected_by
82- ],
83- "fixing_vulnerabilities" : [
84- vuln .vulnerability_id for vuln in purl .fixing
85- ],
86- }
87- ],
89+ "versions" : [package_data ],
8890 }
91+ version_files [version_filepath ] = package_data
8992
9093 for vul in purl .vulnerabilities .all ():
91- vul_filepath = package_dir .joinpath (f"{ vul .vulnerability_id } .yml" )
92- vul_files [vul_filepath ] = saneyaml .dump (
93- {
94- "vulnerability_id" : vul .vulnerability_id ,
95- "aliases" : [alias .alias for alias in vul .get_aliases ],
96- "summary" : vul .summary ,
97- "severities" : [severity for severity in vul .severities .values ()],
98- "references" : [ref for ref in vul .references .values ()],
99- "weaknesses" : [
100- "CWE-" + str (weakness ["cwe_id" ])
101- for weakness in vul .weaknesses .values ()
102- ],
103- }
94+ vulnerability_id = vul .vulnerability_id
95+ # ./aboutcode-vulnerabilities-1223/3434/VCID-1223-3434-34343/VCID-1223-3434-34343.yml
96+ vul_filepath = (
97+ f"./aboutcode-vulnerabilities-{ vulnerability_id [5 :9 ]} /{ vulnerability_id [10 :14 ]} "
98+ f"/{ vulnerability_id } /{ vulnerability_id } .yml"
10499 )
100+ vul_files [vul_filepath ] = {
101+ "vulnerability_id" : vul .vulnerability_id ,
102+ "aliases" : [alias .alias for alias in vul .get_aliases ],
103+ "summary" : vul .summary ,
104+ "severities" : [severity for severity in vul .severities .values ()],
105+ "references" : [ref for ref in vul .references .values ()],
106+ "weaknesses" : [
107+ "CWE-" + str (weakness ["cwe_id" ]) for weakness in vul .weaknesses .values ()
108+ ],
109+ }
105110
106- for k , v in package_files .items ():
107- data = saneyaml .dump (v )
108- with open (k , encoding = "utf-8" , mode = "w" ) as f :
109- f .write (data )
111+ for items in [package_files , version_files , vul_files ]:
112+ for filepath , data in items .items ():
113+ create_file (filepath , git_path , data )
110114
111- for k , v in vul_files .items ():
112- with open (k , encoding = "utf-8" , mode = "w" ) as f :
113- f .write (v )
114115 self .stdout .write (f"Successfully exported { ecosystem } data" )
115116
116117
117- def create_sub_paths ( git_path , purl_type , purl_namespace , purl_name ):
118+ def create_file ( filepath , git_path , data ):
118119 """
119- create the directories if it doesn't exist : `path/purl_type/purl_namespace/purl_name`
120+ Check if the directories exist if it doesn't exist create a new one then Create the file
121+ ./aboutcode-vulnerabilities-1223/3434/VCID-1223-3434-34343/VCID-1223-3434-34343.yml
122+ ./aboutcode-packages-ed5/maven/org.apache.log4j/log4j-core/versions/vulnerabilities.yml
123+ ./aboutcode-packages-ed5/maven/org.apache.log4j/log4j-core/versions/1.2.3/vulnerabilities.yml
120124 """
121- ecosystem_dir = git_path .joinpath (purl_type )
122- namespace_dir = ecosystem_dir .joinpath (purl_namespace )
123- package_dir = namespace_dir .joinpath (purl_name )
124- package_dir .mkdir (parents = True , exist_ok = True )
125- return package_dir
125+ filepath = git_path .joinpath (filepath )
126+ dirname = os .path .dirname (filepath )
127+ os .makedirs (dirname , exist_ok = True )
128+ data = saneyaml .dump (data )
129+ with open (filepath , encoding = "utf-8" , mode = "w" ) as f :
130+ f .write (data )
0 commit comments