Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize Python 2 code to get ready for Python 3 #3514

Merged
merged 4 commits into from May 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions deploy/delete_stale_projects.py
@@ -1,3 +1,4 @@
from __future__ import print_function
import shutil
import os

Expand All @@ -11,11 +12,11 @@
if slug not in slugs and slug.replace('_', '-') not in slugs:
final.append(slug)

print "To delete: %s" % len(final)
print("To delete: %s" % len(final))

for to_del in final:
root = '/home/docs/checkouts/readthedocs.org'
print "Deleting " + to_del
print("Deleting " + to_del)
shutil.rmtree('{root}/user_builds/{slug}'.format(root=root, slug=to_del), ignore_errors=True)
shutil.rmtree('{root}/media/pdf/{slug}'.format(root=root, slug=to_del), ignore_errors=True)
shutil.rmtree('{root}/media/epub/{slug}'.format(root=root, slug=to_del), ignore_errors=True)
Expand Down
13 changes: 7 additions & 6 deletions deploy/flask-redirects.py
@@ -1,4 +1,5 @@
"""A Flask app for redirecting documentation from the root / URL."""
from __future__ import print_function

import json

Expand All @@ -17,7 +18,7 @@ def redirect_front():

SUBDOMAIN = CNAME = False

print "Got request {host}".format(host=request.host)
print("Got request {host}".format(host=request.host))
if PRODUCTION_DOMAIN in request.host:
SUBDOMAIN = True
slug = request.host.split('.')[0]
Expand All @@ -31,23 +32,23 @@ def redirect_front():
path = "/home/docs/checkouts/readthedocs.org/public_cname_project/{cname}/metadata.json".format(cname=cname)

try:
json_obj = json.load(file(path))
json_obj = json.load(open(path))
version = json_obj['version']
language = json_obj['language']
single_version = json_obj['single_version']
except Exception, e:
print e
except Exception as e:
print(e)

if single_version:
if SUBDOMAIN:
sendfile = "/user_builds/{slug}/translations/{language}/{version}/".format(slug=slug, language=language, version=version)
elif CNAME:
sendfile = "/public_cname_root/{cname}/".format(cname=cname, language=language, version=version)
print "Redirecting {host} to {sendfile}".format(host=request.host, sendfile=sendfile)
print("Redirecting {host} to {sendfile}".format(host=request.host, sendfile=sendfile))
return make_response('', 303, {'X-Accel-Redirect': sendfile})
else:
url = '/{language}/{version}/'.format(language=language, version=version)
print "Redirecting {host} to {url}".format(host=request.host, url=url)
print("Redirecting {host} to {url}".format(host=request.host, url=url))
return redirect(url)


Expand Down
11 changes: 6 additions & 5 deletions deploy/nginx-smoke-test.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python

"""Check Nginx config on readthedocs.org."""
from __future__ import print_function

import sys
import requests
Expand Down Expand Up @@ -66,15 +67,15 @@ def run_test(fn, *args):
ret_value = fn(*args)
result = 'ok' if ret_value else 'ERROR'
url = args[0]
print "{url: <65} ... {result}".format(url=url, result=result)
print("{url: <65} ... {result}".format(url=url, result=result))
return ret_value


def header(msg):
"""Give each test a sexy header."""
print
print msg
print "-----------------------------"
print()
print(msg)
print("-----------------------------")


def summary_results(num_tests, num_fails):
Expand Down Expand Up @@ -115,7 +116,7 @@ def main():
for url, redirect in redirected_urls:
run_test(redirected, url, redirect)

print summary_results(TESTS, FAILS)
print(summary_results(TESTS, FAILS))

exit_code = 1 if (FAILS > 0) else 0
return exit_code
Expand Down
5 changes: 5 additions & 0 deletions readthedocs/doc_builder/python_environments.py
Expand Up @@ -20,6 +20,11 @@

log = logging.getLogger(__name__)

try:
unicode # Python 2
except NameError:
unicode = str # Python 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a pattern for this using future. I believe this should just be:

from builtins import str

Correct?



class PythonEnvironment(object):

Expand Down