Skip to content

Commit

Permalink
Refactor sloc scanner to be a class
Browse files Browse the repository at this point in the history
  • Loading branch information
turbaszek committed Dec 17, 2020
1 parent 96959ac commit dee60e4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
11 changes: 10 additions & 1 deletion kibble/scanners/scanners/base_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class BaseScanner:
title: str
log = logging.getLogger(__name__)

def __init__(self, kibble_bit: KibbleBit, source: dict):
self.kibble_bit = kibble_bit
self.source = source

@abstractmethod
@property
def accepts(self) -> bool:
raise NotImplementedError

@abstractmethod
def scan(self, kibble_bit: KibbleBit, source: dict) -> None:
def scan(self) -> None:
raise NotImplementedError
61 changes: 32 additions & 29 deletions kibble/scanners/scanners/git-sloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,72 @@
import time

from kibble.configuration import conf
from kibble.scanners.scanners.base_scanner import BaseScanner
from kibble.scanners.utils import git, sloc

""" Source Lines of Code counter for Git """

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

title = "SloC Counter for Git"
version = "0.1.0"

def accepts(source):
""" Do we accept this source? """
if source["type"] == "git":
return True
# There are cases where we have a github repo, but don't wanna analyze the code, just issues
if source["type"] == "github" and source.get("issuesonly", False) == False:
return True
return False
@property
def accepts(self):
""" Do we accept this source? """
if self.source["type"] == "git":
return True
# There are cases where we have a github repo, but don't wanna analyze the code, just issues
if self.source["type"] == "github" and self.source.get("issuesonly"):
return True
return False

def scan(self):
source = self.source
source_id = source["sourceID"]

def scan(kibble_bit, source):
url = source["sourceURL"]
root_path = (
f'{conf.get("scanner", "scratchdir")}/{source["organisation"]}/{git}'
)
gpath = os.path.join(root_path, source_id)

rid = source["sourceID"]
url = source["sourceURL"]
rootpath = "%s/%s/git" % (
conf.get("scanner", "scratchdir"),
source["organisation"],
)
gpath = os.path.join(rootpath, rid)
if not source["steps"]["sync"]["good"] or not os.path.exists(gpath):
return

if source["steps"]["sync"]["good"] and os.path.exists(gpath):
source["steps"]["count"] = {
"time": time.time(),
"status": "SLoC count started at "
+ time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()),
"running": True,
"good": True,
}
kibble_bit.update_source(source)
self.kibble_bit.update_source(source)

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

kibble_bit.pprint("Running SLoC count for %s" % url)
languages, codecount, comment, blank, years, cost = sloc.count(gpath)
self.log.info("Running SLoC count for %s", url)
languages, code_count, comment, blank, years, cost = sloc.count(gpath)

sloc_ = {
"sourceID": source["sourceID"],
"loc": codecount,
source["sloc"] = {
"sourceID": source_id,
"loc": code_count,
"comments": comment,
"blanks": blank,
"years": years,
"cost": cost,
"languages": languages,
}
source["sloc"] = sloc_
source["steps"]["count"] = {
"time": time.time(),
"status": "SLoC count completed at "
+ time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()),
"running": False,
"good": True,
}
kibble_bit.update_source(source)
self.kibble_bit.update_source(source)

0 comments on commit dee60e4

Please sign in to comment.