|
20 | 20 | import time |
21 | 21 |
|
22 | 22 | from kibble.configuration import conf |
| 23 | +from kibble.scanners.scanners.base_scanner import BaseScanner |
23 | 24 | from kibble.scanners.utils import git, sloc |
24 | 25 |
|
25 | | -""" Source Lines of Code counter for Git """ |
26 | 26 |
|
27 | | -title = "SloC Counter for Git" |
28 | | -version = "0.1.0" |
| 27 | +class GitSlocScanner(BaseScanner): |
| 28 | + """Source Lines of Code counter for Git""" |
29 | 29 |
|
| 30 | + title = "SloC Counter for Git" |
| 31 | + version = "0.1.0" |
30 | 32 |
|
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 | + @staticmethod |
| 34 | + def accepts(source): |
| 35 | + """ Do we accept this source? """ |
| 36 | + if 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 source["type"] == "github" and source.get("issuesonly"): |
| 40 | + return True |
| 41 | + return False |
39 | 42 |
|
| 43 | + def scan(self): |
| 44 | + source = self.source |
| 45 | + source_id = source["sourceID"] |
40 | 46 |
|
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) |
42 | 52 |
|
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 |
50 | 55 |
|
51 | | - if source["steps"]["sync"]["good"] and os.path.exists(gpath): |
52 | 56 | source["steps"]["count"] = { |
53 | 57 | "time": time.time(), |
54 | 58 | "status": "SLoC count started at " |
55 | 59 | + time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()), |
56 | 60 | "running": True, |
57 | 61 | "good": True, |
58 | 62 | } |
59 | | - kibble_bit.update_source(source) |
| 63 | + self.kibble_bit.update_source(source) |
60 | 64 |
|
61 | 65 | try: |
62 | 66 | branch = git.default_branch(source, gpath) |
63 | 67 | subprocess.call("cd %s && git checkout %s" % (gpath, branch), shell=True) |
64 | 68 | 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) |
66 | 70 | return False |
67 | 71 |
|
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) |
70 | 74 |
|
71 | | - sloc_ = { |
72 | | - "sourceID": source["sourceID"], |
73 | | - "loc": codecount, |
| 75 | + source["sloc"] = { |
| 76 | + "sourceID": source_id, |
| 77 | + "loc": code_count, |
74 | 78 | "comments": comment, |
75 | 79 | "blanks": blank, |
76 | 80 | "years": years, |
77 | 81 | "cost": cost, |
78 | 82 | "languages": languages, |
79 | 83 | } |
80 | | - source["sloc"] = sloc_ |
81 | 84 | source["steps"]["count"] = { |
82 | 85 | "time": time.time(), |
83 | 86 | "status": "SLoC count completed at " |
84 | 87 | + time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()), |
85 | 88 | "running": False, |
86 | 89 | "good": True, |
87 | 90 | } |
88 | | - kibble_bit.update_source(source) |
| 91 | + self.kibble_bit.update_source(source) |
| 92 | + |
| 93 | + |
| 94 | +def scan(kibble_bit, source): |
| 95 | + GitSlocScanner(kibble_bit, source).scan() |
0 commit comments