Skip to content

Commit

Permalink
Merge pull request #66 from mcdonnnj/improvement/improve_cloc_output_…
Browse files Browse the repository at this point in the history
…processing

Improve process output handling and streamline `cloc` JSON parsing
  • Loading branch information
IanLee1521 authored Aug 29, 2022
2 parents c56f819 + 5701f04 commit bdffe18
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions scraper/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ def execute(command, cwd=None):
elif not os.path.isdir(cwd):
raise ValueError("path does not exist: %s", cwd)

process = Popen(command, cwd=cwd, stdout=PIPE, stderr=STDOUT, shell=False) # nosec
out, err = process.communicate()
with Popen(
command, cwd=cwd, stdout=PIPE, stderr=STDOUT, shell=False
) as process: # nosec
# We redirect stderr to stdout so we can safely ignore stderr in the returned tuple
out, _ = process.communicate()

if process.returncode:
logging.error(
Expand All @@ -27,7 +30,7 @@ def execute(command, cwd=None):
process.returncode,
)

return str(out), str(err)
return out.decode("utf-8")


def configure_logging(verbose=False):
Expand Down Expand Up @@ -129,12 +132,10 @@ def git_repo_to_sloc(url):
execute(cmd)

cmd = ["cloc", "--json", tmp_clone]
out, _ = execute(cmd)
out = execute(cmd)

try:
json_start = out.find('{"header"')
json_blob = out[json_start:].replace("\\n", "").replace("'", "")
cloc_json = json.loads(json_blob)
cloc_json = json.loads(out)
sloc = cloc_json["SUM"]["code"]
except json.decoder.JSONDecodeError:
logger.error("Error Decoding: url=%s, out=%s", url, out)
Expand Down

0 comments on commit bdffe18

Please sign in to comment.