Skip to content

Commit dee60e4

Browse files
committed
Refactor sloc scanner to be a class
1 parent 96959ac commit dee60e4

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

kibble/scanners/scanners/base_scanner.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ class BaseScanner:
3030
title: str
3131
log = logging.getLogger(__name__)
3232

33+
def __init__(self, kibble_bit: KibbleBit, source: dict):
34+
self.kibble_bit = kibble_bit
35+
self.source = source
36+
37+
@abstractmethod
38+
@property
39+
def accepts(self) -> bool:
40+
raise NotImplementedError
41+
3342
@abstractmethod
34-
def scan(self, kibble_bit: KibbleBit, source: dict) -> None:
43+
def scan(self) -> None:
3544
raise NotImplementedError

kibble/scanners/scanners/git-sloc.py

+32-29
Original file line numberDiff line numberDiff line change
@@ -20,69 +20,72 @@
2020
import time
2121

2222
from kibble.configuration import conf
23+
from kibble.scanners.scanners.base_scanner import BaseScanner
2324
from kibble.scanners.utils import git, sloc
2425

25-
""" Source Lines of Code counter for Git """
2626

27-
title = "SloC Counter for Git"
28-
version = "0.1.0"
27+
class GitSlocScanner(BaseScanner):
28+
"""Source Lines of Code counter for Git"""
2929

30+
title = "SloC Counter for Git"
31+
version = "0.1.0"
3032

31-
def accepts(source):
32-
""" Do we accept this source? """
33-
if source["type"] == "git":
34-
return True
35-
# There are cases where we have a github repo, but don't wanna analyze the code, just issues
36-
if source["type"] == "github" and source.get("issuesonly", False) == False:
37-
return True
38-
return False
33+
@property
34+
def accepts(self):
35+
""" Do we accept this source? """
36+
if self.source["type"] == "git":
37+
return True
38+
# There are cases where we have a github repo, but don't wanna analyze the code, just issues
39+
if self.source["type"] == "github" and self.source.get("issuesonly"):
40+
return True
41+
return False
3942

43+
def scan(self):
44+
source = self.source
45+
source_id = source["sourceID"]
4046

41-
def scan(kibble_bit, source):
47+
url = source["sourceURL"]
48+
root_path = (
49+
f'{conf.get("scanner", "scratchdir")}/{source["organisation"]}/{git}'
50+
)
51+
gpath = os.path.join(root_path, source_id)
4252

43-
rid = source["sourceID"]
44-
url = source["sourceURL"]
45-
rootpath = "%s/%s/git" % (
46-
conf.get("scanner", "scratchdir"),
47-
source["organisation"],
48-
)
49-
gpath = os.path.join(rootpath, rid)
53+
if not source["steps"]["sync"]["good"] or not os.path.exists(gpath):
54+
return
5055

51-
if source["steps"]["sync"]["good"] and os.path.exists(gpath):
5256
source["steps"]["count"] = {
5357
"time": time.time(),
5458
"status": "SLoC count started at "
5559
+ time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()),
5660
"running": True,
5761
"good": True,
5862
}
59-
kibble_bit.update_source(source)
63+
self.kibble_bit.update_source(source)
6064

6165
try:
6266
branch = git.default_branch(source, gpath)
6367
subprocess.call("cd %s && git checkout %s" % (gpath, branch), shell=True)
6468
except: # pylint: disable=bare-except
65-
kibble_bit.pprint("SLoC counter failed to find main branch for %s!!" % url)
69+
self.log.error("SLoC counter failed to find main branch for %s", url)
6670
return False
6771

68-
kibble_bit.pprint("Running SLoC count for %s" % url)
69-
languages, codecount, comment, blank, years, cost = sloc.count(gpath)
72+
self.log.info("Running SLoC count for %s", url)
73+
languages, code_count, comment, blank, years, cost = sloc.count(gpath)
7074

71-
sloc_ = {
72-
"sourceID": source["sourceID"],
73-
"loc": codecount,
75+
source["sloc"] = {
76+
"sourceID": source_id,
77+
"loc": code_count,
7478
"comments": comment,
7579
"blanks": blank,
7680
"years": years,
7781
"cost": cost,
7882
"languages": languages,
7983
}
80-
source["sloc"] = sloc_
8184
source["steps"]["count"] = {
8285
"time": time.time(),
8386
"status": "SLoC count completed at "
8487
+ time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()),
8588
"running": False,
8689
"good": True,
8790
}
88-
kibble_bit.update_source(source)
91+
self.kibble_bit.update_source(source)

0 commit comments

Comments
 (0)