Skip to content

Commit 6dbf87b

Browse files
authored
Merge pull request #11 from oh2fih/workflows
Add ShellCheck & Black formatting workflows
2 parents a228c7b + 01621fb commit 6dbf87b

File tree

6 files changed

+100
-54
lines changed

6 files changed

+100
-54
lines changed

.github/workflows/black.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Black formatting
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
jobs:
11+
black:
12+
name: Black
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Check Black formatting for Python scripts
17+
uses: psf/black@stable
18+
with:
19+
options: --check --diff --verbose
20+
src: .

.github/workflows/shell.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: ShellCheck
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
jobs:
11+
shellcheck:
12+
name: ShellCheck
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Run ShellCheck for shell scripts
17+
uses: ludeeus/action-shellcheck@master
18+
with:
19+
severity: style
20+
scandir: .
21+
format: gcc
22+
version: stable

bin/import.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from dynaconf import Dynaconf
1414

1515
# Configuration
16-
settings = Dynaconf(settings_files=['../config/settings.yaml'])
16+
settings = Dynaconf(settings_files=["../config/settings.yaml"])
1717
cpe_path = settings.cpe.path
1818
cpe_source = settings.cpe.source
1919
rdb = redis.Redis(host=settings.redis.host, port=settings.redis.port, db=8)
@@ -33,32 +33,32 @@ def __init__(self):
3333

3434
def startElement(self, tag, attributes):
3535
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":
3939
self.title_seen = True
40-
if tag == 'reference':
41-
self.refs.append(attributes['href'])
40+
if tag == "reference":
41+
self.refs.append(attributes["href"])
4242

4343
def characters(self, data):
4444
if self.title_seen:
4545
self.title = self.title + data
4646

4747
def endElement(self, tag):
48-
if tag == 'title':
49-
self.record['title'] = self.title
48+
if tag == "title":
49+
self.record["title"] = self.title
5050
self.title = ""
5151
self.title_seen = False
52-
if tag == 'references':
53-
self.record['refs'] = self.refs
52+
if tag == "references":
53+
self.record["refs"] = self.refs
5454
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"])
5959
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"])
6262
self.wordcount += 1
6363
self.record = {}
6464
self.itemcount += 1
@@ -74,18 +74,18 @@ def CPEExtractor(cpe=None):
7474
return False
7575
record = {}
7676
cpefield = cpe.split(":")
77-
record['vendor'] = cpefield[3]
78-
record['product'] = cpefield[4]
77+
record["vendor"] = cpefield[3]
78+
record["product"] = cpefield[4]
7979
cpeline = ""
8080
for cpeentry in cpefield[:5]:
8181
cpeline = f"{cpeline}:{cpeentry}"
82-
record['cpeline'] = cpeline[1:]
82+
record["cpeline"] = cpeline[1:]
8383
return record
8484

8585

8686
def canonize(value=None):
8787
value = value.lower()
88-
words = value.split('_')
88+
words = value.split("_")
8989
return words
9090

9191

@@ -97,30 +97,30 @@ def insert(word=None, cpe=None):
9797
rdb.zadd("rank:cpe", {cpe: 1}, incr=True)
9898

9999

100-
if __name__ == '__main__':
100+
if __name__ == "__main__":
101101
argparser = argparse.ArgumentParser(
102-
description='Initializes the Redis database with CPE dictionary.'
102+
description="Initializes the Redis database with CPE dictionary."
103103
)
104104
argparser.add_argument(
105-
'--download',
106-
'-d',
107-
action='count',
105+
"--download",
106+
"-d",
107+
action="count",
108108
default=0,
109-
help='Download the CPE dictionary even if it already exists.',
109+
help="Download the CPE dictionary even if it already exists.",
110110
)
111111
argparser.add_argument(
112-
'--replace',
113-
'-r',
114-
action='count',
112+
"--replace",
113+
"-r",
114+
action="count",
115115
default=0,
116-
help='Flush and repopulated the CPE database.',
116+
help="Flush and repopulated the CPE database.",
117117
)
118118
argparser.add_argument(
119-
'--update',
120-
'-u',
121-
action='store_true',
119+
"--update",
120+
"-u",
121+
action="store_true",
122122
default=False,
123-
help='Update the CPE database without flushing',
123+
help="Update the CPE database without flushing",
124124
)
125125
args = argparser.parse_args()
126126

@@ -144,8 +144,8 @@ def insert(word=None, cpe=None):
144144

145145
print(f"Uncompressing {cpe_path}.gz ...")
146146
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:
149149
shutil.copyfileobj(cpe_gz, cpe_xml)
150150
os.remove(f"{cpe_path}.gz")
151151
except (FileNotFoundError, PermissionError) as e:

bin/lookup.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
sys.path.append(os.path.join(runPath, ".."))
1111
from lib.cpeguesser import CPEGuesser
1212

13-
if __name__ == '__main__':
13+
if __name__ == "__main__":
1414
parser = argparse.ArgumentParser(
15-
description='Find potential CPE names from a list of keyword(s) and return a JSON of the results'
15+
description="Find potential CPE names from a list of keyword(s) and return a JSON of the results"
1616
)
1717
parser.add_argument(
18-
'word',
19-
metavar='WORD',
18+
"word",
19+
metavar="WORD",
2020
type=str,
21-
nargs='+',
22-
help='One or more keyword(s) to lookup',
21+
nargs="+",
22+
help="One or more keyword(s) to lookup",
2323
)
2424
args = parser.parse_args()
2525

bin/server.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from dynaconf import Dynaconf
1010

1111
# Configuration
12-
settings = Dynaconf(settings_files=['../config/settings.yaml'])
12+
settings = Dynaconf(settings_files=["../config/settings.yaml"])
1313
port = settings.server.port
1414

1515
runPath = os.path.dirname(os.path.realpath(__file__))
@@ -20,31 +20,31 @@
2020
class Search:
2121
def on_post(self, req, resp):
2222
data_post = req.bounded_stream.read()
23-
js = data_post.decode('utf-8')
23+
js = data_post.decode("utf-8")
2424
try:
2525
q = json.loads(js)
2626
except ValueError:
2727
resp.status = falcon.HTTP_400
2828
resp.media = "Missing query array or incorrect JSON format"
2929
return
3030

31-
if 'query' in q:
31+
if "query" in q:
3232
pass
3333
else:
3434
resp.status = falcon.HTTP_400
3535
resp.media = "Missing query array or incorrect JSON format"
3636
return
3737

3838
cpeGuesser = CPEGuesser()
39-
resp.media = cpeGuesser.guessCpe(q['query'])
39+
resp.media = cpeGuesser.guessCpe(q["query"])
4040

4141

42-
if __name__ == '__main__':
42+
if __name__ == "__main__":
4343
app = falcon.App()
44-
app.add_route('/search', Search())
44+
app.add_route("/search", Search())
4545

4646
try:
47-
with make_server('', port, app) as httpd:
47+
with make_server("", port, app) as httpd:
4848
print(f"Serving on port {port}...")
4949
httpd.serve_forever()
5050
except OSError as e:

lib/cpeguesser.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
from dynaconf import Dynaconf
66

77
# Configuration
8-
settings = Dynaconf(
9-
settings_files=['../config/settings.yaml']
10-
)
8+
settings = Dynaconf(settings_files=["../config/settings.yaml"])
9+
1110

1211
class CPEGuesser:
1312
def __init__(self):
14-
self.rdb = redis.Redis(host=settings.redis.host, port=settings.redis.port, db=8, decode_responses=True)
13+
self.rdb = redis.Redis(
14+
host=settings.redis.host,
15+
port=settings.redis.port,
16+
db=8,
17+
decode_responses=True,
18+
)
1519

1620
def guessCpe(self, words):
1721
k = []
@@ -28,7 +32,7 @@ def guessCpe(self, words):
2832
ranked = []
2933

3034
for cpe in result:
31-
rank = self.rdb.zrank('rank:cpe', cpe)
35+
rank = self.rdb.zrank("rank:cpe", cpe)
3236
ranked.append((rank, cpe))
3337

3438
return sorted(ranked)

0 commit comments

Comments
 (0)