13
13
from dynaconf import Dynaconf
14
14
15
15
# Configuration
16
- settings = Dynaconf (settings_files = [' ../config/settings.yaml' ])
16
+ settings = Dynaconf (settings_files = [" ../config/settings.yaml" ])
17
17
cpe_path = settings .cpe .path
18
18
cpe_source = settings .cpe .source
19
19
rdb = redis .Redis (host = settings .redis .host , port = settings .redis .port , db = 8 )
@@ -33,32 +33,32 @@ def __init__(self):
33
33
34
34
def startElement (self , tag , attributes ):
35
35
self .CurrentData = tag
36
- if tag == ' cpe-23:cpe23-item' :
37
- self .record [' cpe-23' ] = attributes [' name' ]
38
- if tag == ' title' :
36
+ if tag == " cpe-23:cpe23-item" :
37
+ self .record [" cpe-23" ] = attributes [" name" ]
38
+ if tag == " title" :
39
39
self .title_seen = True
40
- if tag == ' reference' :
41
- self .refs .append (attributes [' href' ])
40
+ if tag == " reference" :
41
+ self .refs .append (attributes [" href" ])
42
42
43
43
def characters (self , data ):
44
44
if self .title_seen :
45
45
self .title = self .title + data
46
46
47
47
def endElement (self , tag ):
48
- if tag == ' title' :
49
- self .record [' title' ] = self .title
48
+ if tag == " title" :
49
+ self .record [" title" ] = self .title
50
50
self .title = ""
51
51
self .title_seen = False
52
- if tag == ' references' :
53
- self .record [' refs' ] = self .refs
52
+ if tag == " references" :
53
+ self .record [" refs" ] = self .refs
54
54
self .refs = []
55
- if tag == ' cpe-item' :
56
- to_insert = CPEExtractor (cpe = self .record [' cpe-23' ])
57
- for word in canonize (to_insert [' vendor' ]):
58
- insert (word = word , cpe = to_insert [' cpeline' ])
55
+ if tag == " cpe-item" :
56
+ to_insert = CPEExtractor (cpe = self .record [" cpe-23" ])
57
+ for word in canonize (to_insert [" vendor" ]):
58
+ insert (word = word , cpe = to_insert [" cpeline" ])
59
59
self .wordcount += 1
60
- for word in canonize (to_insert [' product' ]):
61
- insert (word = word , cpe = to_insert [' cpeline' ])
60
+ for word in canonize (to_insert [" product" ]):
61
+ insert (word = word , cpe = to_insert [" cpeline" ])
62
62
self .wordcount += 1
63
63
self .record = {}
64
64
self .itemcount += 1
@@ -74,18 +74,18 @@ def CPEExtractor(cpe=None):
74
74
return False
75
75
record = {}
76
76
cpefield = cpe .split (":" )
77
- record [' vendor' ] = cpefield [3 ]
78
- record [' product' ] = cpefield [4 ]
77
+ record [" vendor" ] = cpefield [3 ]
78
+ record [" product" ] = cpefield [4 ]
79
79
cpeline = ""
80
80
for cpeentry in cpefield [:5 ]:
81
81
cpeline = f"{ cpeline } :{ cpeentry } "
82
- record [' cpeline' ] = cpeline [1 :]
82
+ record [" cpeline" ] = cpeline [1 :]
83
83
return record
84
84
85
85
86
86
def canonize (value = None ):
87
87
value = value .lower ()
88
- words = value .split ('_' )
88
+ words = value .split ("_" )
89
89
return words
90
90
91
91
@@ -97,30 +97,30 @@ def insert(word=None, cpe=None):
97
97
rdb .zadd ("rank:cpe" , {cpe : 1 }, incr = True )
98
98
99
99
100
- if __name__ == ' __main__' :
100
+ if __name__ == " __main__" :
101
101
argparser = argparse .ArgumentParser (
102
- description = ' Initializes the Redis database with CPE dictionary.'
102
+ description = " Initializes the Redis database with CPE dictionary."
103
103
)
104
104
argparser .add_argument (
105
- ' --download' ,
106
- '-d' ,
107
- action = ' count' ,
105
+ " --download" ,
106
+ "-d" ,
107
+ action = " count" ,
108
108
default = 0 ,
109
- help = ' Download the CPE dictionary even if it already exists.' ,
109
+ help = " Download the CPE dictionary even if it already exists." ,
110
110
)
111
111
argparser .add_argument (
112
- ' --replace' ,
113
- '-r' ,
114
- action = ' count' ,
112
+ " --replace" ,
113
+ "-r" ,
114
+ action = " count" ,
115
115
default = 0 ,
116
- help = ' Flush and repopulated the CPE database.' ,
116
+ help = " Flush and repopulated the CPE database." ,
117
117
)
118
118
argparser .add_argument (
119
- ' --update' ,
120
- '-u' ,
121
- action = ' store_true' ,
119
+ " --update" ,
120
+ "-u" ,
121
+ action = " store_true" ,
122
122
default = False ,
123
- help = ' Update the CPE database without flushing' ,
123
+ help = " Update the CPE database without flushing" ,
124
124
)
125
125
args = argparser .parse_args ()
126
126
@@ -144,8 +144,8 @@ def insert(word=None, cpe=None):
144
144
145
145
print (f"Uncompressing { cpe_path } .gz ..." )
146
146
try :
147
- with gzip .open (f"{ cpe_path } .gz" , 'rb' ) as cpe_gz :
148
- with open (cpe_path , 'wb' ) as cpe_xml :
147
+ with gzip .open (f"{ cpe_path } .gz" , "rb" ) as cpe_gz :
148
+ with open (cpe_path , "wb" ) as cpe_xml :
149
149
shutil .copyfileobj (cpe_gz , cpe_xml )
150
150
os .remove (f"{ cpe_path } .gz" )
151
151
except (FileNotFoundError , PermissionError ) as e :
0 commit comments