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".
This commit is contained in:
Adam Waldenberg 2012-05-28 16:35:47 +02:00
parent 2d48510777
commit c59c8c23eb
4 changed files with 19 additions and 10 deletions

View File

@ -23,8 +23,8 @@ import comment
import filtering import filtering
import missing import missing
import multiprocessing import multiprocessing
import os
import re import re
import subprocess
import sys import sys
import terminal import terminal
import threading import threading
@ -49,10 +49,11 @@ class BlameThread(threading.Thread):
self.filename = filename self.filename = filename
def run(self): 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 is_inside_comment = False
for j in git_blame_r.readlines(): for j in git_blame_r.readlines():
j = j.decode("utf-8", errors="replace")
if Blame.is_blame_line(j): if Blame.is_blame_line(j):
author = Blame.get_author(j) author = Blame.get_author(j)
content = Blame.get_content(j) content = Blame.get_content(j)
@ -74,15 +75,17 @@ class BlameThread(threading.Thread):
self.blames[(author, self.filename)].rows += 1 self.blames[(author, self.filename)].rows += 1
__blame_lock__.release() # ...to here. __blame_lock__.release() # ...to here.
git_blame_r.close()
__thread_lock__.release() # Lock controlling the number of threads running __thread_lock__.release() # Lock controlling the number of threads running
class Blame: class Blame:
def __init__(self, hard): def __init__(self, hard):
self.blames = {} 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() lines = ls_tree_r.readlines()
for i, row in enumerate(lines): 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 FileDiff.is_valid_extension(row) and not filtering.set_filtered(FileDiff.get_filename(row)):
if not missing.add(row.strip()): if not missing.add(row.strip()):
blame_string = "git blame -w {0} \"".format("-C -C -M" if hard else "") + row.strip() + "\"" blame_string = "git blame -w {0} \"".format("-C -C -M" if hard else "") + row.strip() + "\""

View File

@ -20,8 +20,9 @@
from __future__ import print_function from __future__ import print_function
import extensions import extensions
import filtering import filtering
import os
import re import re
import os
import subprocess
import terminal import terminal
class FileDiff: class FileDiff:
@ -86,13 +87,15 @@ class AuthorInfo:
class Changes: class Changes:
def __init__(self, hard): def __init__(self, hard):
self.commits = [] self.commits = []
git_log_r = os.popen("git log --pretty='%ad|%t|%aN|%s' --stat=100000,8192 --no-merges -w " + 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 "")) "{0} --date=short".format("-C -C -M" if hard else ""),
shell=True, bufsize=1, stdout=subprocess.PIPE).stdout
commit = None commit = None
found_valid_extension = False found_valid_extension = False
lines = git_log_r.readlines() lines = git_log_r.readlines()
for i in lines: for i in lines:
i = i.decode("utf-8", errors="replace")
if Commit.is_commit_line(i) or i == lines[-1]: if Commit.is_commit_line(i) or i == lines[-1]:
if found_valid_extension: if found_valid_extension:
self.commits.append(commit) self.commits.append(commit)

View File

@ -22,7 +22,7 @@ from changes import FileDiff
import comment import comment
import filtering import filtering
import missing import missing
import os import subprocess
__metric_eloc__ = {"java": 500, "c": 500, "cpp": 500, "h": 300, "hpp": 300, "py": 500, "glsl": 1000, __metric_eloc__ = {"java": 500, "c": 500, "cpp": 500, "h": 300, "hpp": 300, "py": 500, "glsl": 1000,
"rb": 500, "js": 500, "sql": 1000, "xml": 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: class Metrics:
def __init__(self): def __init__(self):
self.eloc = {} 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(): 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 FileDiff.is_valid_extension(i) and not filtering.set_filtered(FileDiff.get_filename(i)):
if not missing.add(i.strip()): if not missing.add(i.strip()):
file_r = open(i.strip(), "r") file_r = open(i.strip(), "rb")
extension = FileDiff.get_extension(i) extension = FileDiff.get_extension(i)
lines = Metrics.get_eloc(file_r, extension) lines = Metrics.get_eloc(file_r, extension)
@ -48,6 +49,7 @@ class Metrics:
eloc_counter = 0 eloc_counter = 0
for j in file_r.readlines(): for j in file_r.readlines():
j = j.decode("utf-8", errors="replace")
if is_inside_comment and comment.has_comment_end(extension, j): if is_inside_comment and comment.has_comment_end(extension, j):
is_inside_comment = False is_inside_comment = False
elif comment.has_comment_begining(extension, j): elif comment.has_comment_begining(extension, j):

View File

@ -19,6 +19,7 @@
from __future__ import print_function from __future__ import print_function
import os import os
import subprocess
import terminal import terminal
__checkout_missing__ = False __checkout_missing__ = False
@ -27,7 +28,7 @@ __missing_files__ = set()
def add(file_name): def add(file_name):
if not os.path.exists(file_name): if not os.path.exists(file_name):
if __checkout_missing__: if __checkout_missing__:
os.popen("git checkout \"" + file_name.strip() + "\"") subprocess.call("git checkout \"" + file_name.strip() + "\"")
else: else:
__missing_files__.add(file_name) __missing_files__.add(file_name)
return True return True