Skip to content
Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Learn more
Permalink
Browse files

support python 3

  • Loading branch information...
tdurieux committed Oct 27, 2017
1 parent 056cd26 commit a4be59581dbbc27096c53293a69ce2399f898b85
Showing with 28 additions and 16 deletions.
  1. +1 −1 .travis.yml
  2. +16 −12 server.py
  3. +11 −3 test.py
@@ -1,7 +1,7 @@
language: python
python:
- '2.7'
- '3.2'
- '3.3'
install:
- pip install -r requirements.txt
script:
@@ -3,7 +3,10 @@
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 @@ def remove_terms(content, repository_configuration):
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 @@ def file_render(file, repository_configuration):
return Markup("The file %s is too big to be anonymized (beyond 1MB, Github limit)" % (file.name))
if ".md" in file.name:
return Markup("<div class='markdown-body'>%s</div>" % 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("<img src='%s' alt='%s'>" % (file.url, file.name))
if ".txt" in file.name \
@@ -113,8 +117,8 @@ def file_render(file, repository_configuration):
or ".h" in file.name \
or ".lua" in file.name \
or ".py" in file.name:
return remove_terms(Markup("<pre><code>%s</code></pre>") % Markup.escape(file.decoded_content),
repository_configuration)
return Markup("<pre><code>{}</code></pre>")\
.format(Markup.escape(remove_terms(file.decoded_content.decode("utf-8"), repository_configuration)))
return Markup("<b>%s has an unknown extension, we are unable to anonymize it (known extensions md/txt/json/java/...)</b>" % (file.name))

@application.route('/' + application.killurl, methods=['POST'])
@@ -133,7 +137,7 @@ def get_element_from_path(g_repo, path):
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 @@ def get_content(current_file, files, path, repository_config, g_repo):
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 @@ def get_content(current_file, files, path, repository_config, g_repo):
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 @@ def repository(id, path):
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)
14 test.py
@@ -1,4 +1,4 @@
import os
import ast
import unittest
import tempfile
import uuid
@@ -23,14 +23,22 @@ def create_repository(self, url, terms):
})
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 "<title>GitHub Anonymous</title>" in rv.data
assert b"<title>GitHub Anonymous</title>" 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__':

0 comments on commit a4be595

Please sign in to comment.
You can’t perform that action at this time.