From a4be59581dbbc27096c53293a69ce2399f898b85 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 27 Oct 2017 11:55:40 +0200 Subject: [PATCH] support python 3 --- .travis.yml | 2 +- server.py | 28 ++++++++++++++++------------ test.py | 14 +++++++++++--- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 14e55a8..3de1eb9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: python python: - '2.7' -- '3.2' +- '3.3' install: - pip install -r requirements.txt script: diff --git a/server.py b/server.py index 2e0898b..700f79b 100644 --- a/server.py +++ b/server.py @@ -3,7 +3,10 @@ import uuid import json import socket import os -import urllib +try: + from urllib import quote # Python 2.X +except ImportError: + from urllib.parse import quote # Python 3+ import re import shutil import base64 @@ -71,9 +74,9 @@ class Anonymous_Github: repo = repository_configuration['repository'] if repo[-1] == '/': repo = repo[0:-1] - content = re.compile(repo + "/blob/master", re.IGNORECASE).sub( - self.public_url + "/repository/" + repository_configuration["id"], content) - content = re.compile(repo, re.IGNORECASE).sub(self.public_url + "/repository/" + repository_configuration["id"], content) + content = re.compile("%s/blob/master" % repo, re.IGNORECASE).sub( + "%s/repository/%s" % (self.public_url, repository_configuration["id"]), content) + content = re.compile(repo, re.IGNORECASE).sub("%s/repository/%s" % (self.public_url, repository_configuration["id"]), content) for term in repository_configuration['terms']: content = re.compile(term, re.IGNORECASE).sub("XXX", content) return content @@ -94,7 +97,8 @@ class Anonymous_Github: return Markup("The file %s is too big to be anonymized (beyond 1MB, Github limit)" % (file.name)) if ".md" in file.name: return Markup("
%s
" % remove_terms( - self.github.render_markdown(file.decoded_content), repository_configuration)) + self.github.render_markdown(file.decoded_content.decode('utf-8')).decode('utf-8'), + repository_configuration)) if ".jpg" in file.name or ".png" in file.name or ".png" in file.name or ".gif" in file.name: return Markup("%s" % (file.url, file.name)) if ".txt" in file.name \ @@ -113,8 +117,8 @@ class Anonymous_Github: or ".h" in file.name \ or ".lua" in file.name \ or ".py" in file.name: - return remove_terms(Markup("
%s
") % Markup.escape(file.decoded_content), - repository_configuration) + return Markup("
{}
")\ + .format(Markup.escape(remove_terms(file.decoded_content.decode("utf-8"), repository_configuration))) return Markup("%s has an unknown extension, we are unable to anonymize it (known extensions md/txt/json/java/...)" % (file.name)) @application.route('/' + application.killurl, methods=['POST']) @@ -133,7 +137,7 @@ class Anonymous_Github: if path == '': return g_repo.get_contents('/') current_element = os.path.basename(path) - folder_content = g_repo.get_contents(urllib.quote(os.path.dirname(path))) + folder_content = g_repo.get_contents(quote(os.path.dirname(path))) for file in folder_content: if file.name == current_element: return file @@ -240,9 +244,9 @@ class Anonymous_Github: if current_file.size > 1000000: blob = g_repo.get_git_blob(current_file.sha) if blob.encoding == 'base64': - content = base64.b64decode(blob.content) + content = base64.b64decode(blob.content).decode('utf-8') else: - content = blob.content + content = blob.content.decode('utf-8') else: content = current_file.decoded_content if ".html" in current_file.name \ @@ -255,7 +259,7 @@ class Anonymous_Github: or ".js" in current_file.name: content = remove_terms(content, repository_config) if ".md" in current_file.name: - content = remove_terms(self.github.render_markdown(content), repository_config) + content = remove_terms(self.github.render_markdown(content).decode('utf-8'), repository_config) else: content = render_template('repo.html', repository=repository_config, @@ -324,7 +328,7 @@ class Anonymous_Github: config_path = repo_path + "/config.json" if not os.path.exists(config_path): return render_template('404.html'), 404 - with open(config_path, 'rw') as f: + with open(config_path, 'r') as f: repository_configuration = json.load(f) repo = clean_github_repository(repository_configuration['repository']) g_repo = self.github.get_repo(repo) diff --git a/test.py b/test.py index 09c9375..cd3a2f7 100644 --- a/test.py +++ b/test.py @@ -1,4 +1,4 @@ -import os +import ast import unittest import tempfile import uuid @@ -23,14 +23,22 @@ class FlaskrTestCase(unittest.TestCase): }) return anonymous_id + def test_source_code_compatible(self): + try: + ast.parse(open('server.py').read()) + assert True + except SyntaxError as exc: + print(exc) + assert False + def test_index(self): rv = self.app.get("/") - assert "GitHub Anonymous" in rv.data + assert b"GitHub Anonymous" in rv.data def test_create_repository(self): anonymous_id = self.create_repository("https://github.com/tdurieux/anonymous_github/", "github") rv = self.app.get("/repository/%s/" % anonymous_id) - assert "Anonymous XXX" in rv.data + assert b"Anonymous XXX" in rv.data if __name__ == '__main__':