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 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() + "\""

View File

@ -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)

View File

@ -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):

View File

@ -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