From c59c8c23eb948ca25f736bc8fc874cf7030a3fd5 Mon Sep 17 00:00:00 2001 From: Adam Waldenberg Date: Mon, 28 May 2012 16:35:47 +0200 Subject: [PATCH] More changes done to improve compatibility with Python 3. Changed from using the deprecated functions in the os module to using the new ones in the subprocess module. All string reading now also uses the string.decode() function, which seems to be recommended practice whenever the input is a little "iffy". --- blame.py | 9 ++++++--- changes.py | 9 ++++++--- metrics.py | 8 +++++--- missing.py | 3 ++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/blame.py b/blame.py index fa4f8d7..fb2b017 100644 --- a/blame.py +++ b/blame.py @@ -23,8 +23,8 @@ import comment import filtering import missing import multiprocessing -import os import re +import subprocess import sys import terminal import threading @@ -49,10 +49,11 @@ class BlameThread(threading.Thread): self.filename = filename def run(self): - git_blame_r = os.popen(self.blame_string) + git_blame_r = subprocess.Popen(self.blame_string, shell=True, bufsize=1, stdout=subprocess.PIPE).stdout is_inside_comment = False for j in git_blame_r.readlines(): + j = j.decode("utf-8", errors="replace") if Blame.is_blame_line(j): author = Blame.get_author(j) content = Blame.get_content(j) @@ -74,15 +75,17 @@ class BlameThread(threading.Thread): self.blames[(author, self.filename)].rows += 1 __blame_lock__.release() # ...to here. + git_blame_r.close() __thread_lock__.release() # Lock controlling the number of threads running class Blame: def __init__(self, hard): self.blames = {} - ls_tree_r = os.popen("git ls-tree --name-only -r HEAD") + ls_tree_r = subprocess.Popen("git ls-tree --name-only -r HEAD", shell=True, bufsize=1, stdout=subprocess.PIPE).stdout lines = ls_tree_r.readlines() for i, row in enumerate(lines): + row = row.decode("utf-8", errors="replace") if FileDiff.is_valid_extension(row) and not filtering.set_filtered(FileDiff.get_filename(row)): if not missing.add(row.strip()): blame_string = "git blame -w {0} \"".format("-C -C -M" if hard else "") + row.strip() + "\"" diff --git a/changes.py b/changes.py index 8b39d3b..50fdbf4 100644 --- a/changes.py +++ b/changes.py @@ -20,8 +20,9 @@ from __future__ import print_function import extensions import filtering -import os import re +import os +import subprocess import terminal class FileDiff: @@ -86,13 +87,15 @@ class AuthorInfo: class Changes: def __init__(self, hard): self.commits = [] - git_log_r = os.popen("git log --pretty='%ad|%t|%aN|%s' --stat=100000,8192 --no-merges -w " + - "{0} --date=short".format("-C -C -M" if hard else "")) + git_log_r = subprocess.Popen("git log --pretty='%ad|%t|%aN|%s' --stat=100000,8192 --no-merges -w " + + "{0} --date=short".format("-C -C -M" if hard else ""), + shell=True, bufsize=1, stdout=subprocess.PIPE).stdout commit = None found_valid_extension = False lines = git_log_r.readlines() for i in lines: + i = i.decode("utf-8", errors="replace") if Commit.is_commit_line(i) or i == lines[-1]: if found_valid_extension: self.commits.append(commit) diff --git a/metrics.py b/metrics.py index 397d725..ea7bc1f 100644 --- a/metrics.py +++ b/metrics.py @@ -22,7 +22,7 @@ from changes import FileDiff import comment import filtering import missing -import os +import subprocess __metric_eloc__ = {"java": 500, "c": 500, "cpp": 500, "h": 300, "hpp": 300, "py": 500, "glsl": 1000, "rb": 500, "js": 500, "sql": 1000, "xml": 1000} @@ -30,12 +30,13 @@ __metric_eloc__ = {"java": 500, "c": 500, "cpp": 500, "h": 300, "hpp": 300, "py" class Metrics: def __init__(self): self.eloc = {} - ls_tree_r = os.popen("git ls-tree --name-only -r HEAD") + ls_tree_r = subprocess.Popen("git ls-tree --name-only -r HEAD", shell=True, bufsize=1, stdout=subprocess.PIPE).stdout for i in ls_tree_r.readlines(): + i = i.decode("utf-8", errors="replace") if FileDiff.is_valid_extension(i) and not filtering.set_filtered(FileDiff.get_filename(i)): if not missing.add(i.strip()): - file_r = open(i.strip(), "r") + file_r = open(i.strip(), "rb") extension = FileDiff.get_extension(i) lines = Metrics.get_eloc(file_r, extension) @@ -48,6 +49,7 @@ class Metrics: eloc_counter = 0 for j in file_r.readlines(): + j = j.decode("utf-8", errors="replace") if is_inside_comment and comment.has_comment_end(extension, j): is_inside_comment = False elif comment.has_comment_begining(extension, j): diff --git a/missing.py b/missing.py index 1115790..2484902 100644 --- a/missing.py +++ b/missing.py @@ -19,6 +19,7 @@ from __future__ import print_function import os +import subprocess import terminal __checkout_missing__ = False @@ -27,7 +28,7 @@ __missing_files__ = set() def add(file_name): if not os.path.exists(file_name): if __checkout_missing__: - os.popen("git checkout \"" + file_name.strip() + "\"") + subprocess.call("git checkout \"" + file_name.strip() + "\"") else: __missing_files__.add(file_name) return True