Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Refactor for Python3 (#28091) #23

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ with gtalk users.
*Twitter*: Receive requests via Twitter direct messages, contact the core module
if necessary and respond to the user in the specified language. Unfinished.

*Github*: Run `torsocks python3 create_gh_mirrors.py` to update the github
archive at https://github.com/TheTorProject/gettor - it depends on the github3
and urllib modules.

*DB*: Store anonymous info about the people that interact with GetTor in order
to keep count of the number of requests per person and avoid malicious users
that try to collapse the service. It also keeps count of how many requests
Expand Down
10 changes: 5 additions & 5 deletions core_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
try:
core = gettor.core.Core()
links = core.get_links('dummy service', 'linux', 'en')
print links
print(links)
except gettor.core.ConfigError as e:
print "Misconfiguration: " + str(e)
print("Misconfiguration: " + str(e))
except gettor.core.UnsupportedOSError as e:
print "Unsupported OS: " + str(e)
print("Unsupported OS: " + str(e))
except gettor.core.UnsupportedLocaleError as e:
print "Unsupported Locale: " + str(e)
print("Unsupported Locale: " + str(e))
except gettor.core.InternalError as e:
print "Internal error: " + str(e)
print("Internal error: " + str(e))
27 changes: 16 additions & 11 deletions create_gh_mirrors.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""create_gh_mirrors -- Create landing page and readme for Github."""

import os
import ConfigParser
import configparser

import github3

Expand Down Expand Up @@ -78,7 +78,7 @@ def create_readme(tpl_path, md_path, tb_version, links):
content_md = content_md.replace("%TB_VERSION%", tb_version)
md_file.write(content_md)

print "README generated with Tor Browser %s" % tb_version
print(("README generated with Tor Browser %s" % tb_version))


def create_landing_html(tpl_path, html_path, tb_version, links):
Expand Down Expand Up @@ -111,7 +111,7 @@ def create_landing_html(tpl_path, html_path, tb_version, links):
linux64_pkg, linux64_sig, linux64_sha = [
e for e in linux64_link.split("$") if e
]

content_html = content_html.replace(
"%WINDOWS_{}%".format(lc), win_pkg
)
Expand Down Expand Up @@ -144,13 +144,13 @@ def create_landing_html(tpl_path, html_path, tb_version, links):
"%LINUX64_{}_SIG%".format(lc), linux64_sig
)
"""

content_html = content_html.replace(
"%TB_VERSION%", tb_version
)
html_file.write(content_html)

print "HTML generated with Tor Browser %s" % tb_version
print(("HTML generated with Tor Browser %s" % tb_version))


def main():
Expand All @@ -163,11 +163,16 @@ def main():
html_tpl_path = 'upload/landing_gh.tpl'
github_access_token = ''

tb_version_config = ConfigParser.ConfigParser()
tb_version_config.read(tb_version_path)
tb_version = tb_version_config.get('version', 'current')
try:
tb_version_config = configparser.ConfigParser()
tb_version_config.read(tb_version_path)
tb_version = tb_version_config['version']['current']

except:

Choose a reason for hiding this comment

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

Let's not do an unconditional except: -- instead, let's catch specific exceptions.

raise SystemExit("Failed to parse %s. Does it exist?" % tb_version_path)
# TODO add some hint how to generate it

links = ConfigParser.ConfigParser()
links = configparser.ConfigParser()
links.read(github_links)

create_landing_html(html_tpl_path, html_path, tb_version, links)
Expand Down Expand Up @@ -198,10 +203,10 @@ def main():
}

file_landing_gh.update(**data_landing)
print "Landing page updated in gettor"
print ("Landing page updated in gettor")

file_readme_gh.update(**data_readme)
print "README updated in gettorbrowser"
print ("README updated in gettorbrowser")

if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions get_mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def handle(failure):
else:
logging.error("Couldn't download file.")

print "Couldn't download file. Check the log."
print("Couldn't download file. Check the log.")
os._exit(-1)


Expand All @@ -153,7 +153,7 @@ def is_json(my_json):
"""Check if json generated is valid."""
try:
json_object = json.loads(my_json)
except ValueError, e:
except ValueError as e:

Choose a reason for hiding this comment

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

Here (and elsewhere) it looks like you're ignoring the value of the exception -- you can just delete the as e.

return False
return True

Expand Down
10 changes: 5 additions & 5 deletions gettor/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import logging
import sqlite3
import datetime
import ConfigParser
import configparser

import db
import utils
from . import db
from . import utils

"""Blacklist module for managing blacklisting of users."""

Expand Down Expand Up @@ -57,7 +57,7 @@ def __init__(self, cfg=None):

"""
default_cfg = 'blacklist.cfg'
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()

if cfg is None or not os.path.isfile(cfg):
cfg = default_cfg
Expand All @@ -75,7 +75,7 @@ def __init__(self, cfg=None):
loglevel = config.get('log', 'level')
self.db = db.DB(dbname)

except ConfigParser.Error as e:
except configparser.Error as e:
raise ConfigError("%s" % e)
except db.Exception as e:
raise ConfigError("%s" % e)
Expand Down
8 changes: 4 additions & 4 deletions gettor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import logging
import gettext
import tempfile
import ConfigParser
import configparser

import db
import utils
from . import db
from . import utils

"""Core module for getting links from providers."""

Expand Down Expand Up @@ -65,7 +65,7 @@ class Core(object):
"""

def __init__(self, cfg=None):
"""Create a new core object by reading a configuration file.
"""Create a new core object by reading a configuration file.

:param: cfg (string) the path of the configuration file.
:raise: ConfigurationError if the configuration file doesn't exists
Expand Down
2 changes: 1 addition & 1 deletion gettor/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DB(object):
"""

def __init__(self, dbname):
"""Create a new db object.
"""Create a new db object.

:param: dbname (string) the path of the database.

Expand Down
32 changes: 16 additions & 16 deletions gettor/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import re
import json
import codecs
import urllib2
import ConfigParser
import urllib.request, urllib.error, urllib.parse
import configparser

from time import gmtime, strftime

import core
import utils
from . import core
from . import utils

"""GetTor RESTful API"""

Expand Down Expand Up @@ -80,7 +80,7 @@ def __init__(self, cfg=None):

"""
default_cfg = 'http.cfg'
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()

if cfg is None or not os.path.isfile(cfg):
cfg = default_cfg
Expand All @@ -93,16 +93,16 @@ def __init__(self, cfg=None):

try:
# path to static tree of API
self.tree = config.get('general', 'tree')
self.tree = config['general']['tree']
# server that provides the RESTful API
self.server = config.get('general', 'url')
self.server = config['general']['url']
# path to the links files
self.links_path = config.get('general', 'links')
self.links_path = config['general']['links']
# path to mirrors in json
self.mirrors_path = config.get('general', 'mirrors')
self.mirrors_path = config['general']['mirrors']

# we will ask gettor.core for the links
core_cfg = config.get('general', 'core')
core_cfg = config['general']['core']
self.core = core.Core(core_cfg)

except ConfigParser.Error as e:
Expand All @@ -120,10 +120,10 @@ def _is_json(self, my_json):
"""
try:
json_object = json.loads(my_json)
except ValueError, e:
except ValueError as e:
return False
return True

def _write_json(self, path, content):
"""
"""
Expand All @@ -144,8 +144,8 @@ def _write_json(self, path, content):
)
except IOError as e:
#logging.error("Couldn't write json: %s" % str(e))
print "Error building %s: %s" % (path, str(e))
print "%s built" % path
print(("Error building %s: %s" % (path, str(e))))
print(("%s built" % path))

def _get_provider_name(self, p):
""" Return simplified version of provider's name.
Expand Down Expand Up @@ -202,7 +202,7 @@ def _add_links(self, lv, release, version, os):

def _load_latest_version(self):
""" Load latest version data. """
response = urllib2.urlopen(URL['version'])
response = urllib.request.urlopen(URL['version'])
json_response = json.load(response)

lv = {
Expand Down Expand Up @@ -377,7 +377,7 @@ def load_data(self):
def build(self):
""" Build RESTful API. """

print "Building API"
print ("Building API")

# resources
self._write_json(
Expand Down
12 changes: 6 additions & 6 deletions gettor/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
import logging
import smtplib
import datetime
import ConfigParser
import configparser

from email import Encoders
from email.MIMEBase import MIMEBase
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart

import core
import utils
import blacklist
from . import core
from . import utils
from . import blacklist

"""SMTP module for processing email requests."""

Expand Down Expand Up @@ -79,7 +79,7 @@ def __init__(self, cfg=None):

"""
default_cfg = 'smtp.cfg'
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()

if cfg is None or not os.path.isfile(cfg):
cfg = default_cfg
Expand Down Expand Up @@ -109,7 +109,7 @@ def __init__(self, cfg=None):
core_cfg = config.get('general', 'core_cfg')
self.core = core.Core(core_cfg)

except ConfigParser.Error as e:
except configparser.Error as e:
raise ConfigError("Configuration error: %s" % str(e))
except blacklist.ConfigError as e:
raise InternalError("Blacklist error: %s" % str(e))
Expand Down
12 changes: 6 additions & 6 deletions gettor/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import tweepy
import logging
import gettext
import ConfigParser
import configparser

import core
import utils
import blacklist
from . import core
from . import utils
from . import blacklist

"""Twitter channel for distributing links to download Tor Browser."""

Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(self, cfg=None):
"""

default_cfg = 'twitter.cfg'
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()

if cfg is None or not os.path.isfile(cfg):
cfg = default_cfg
Expand Down Expand Up @@ -94,7 +94,7 @@ def __init__(self, cfg=None):
core_cfg = config.get('general', 'core_cfg')
self.core = core.Core(core_cfg)

except ConfigParser.Error as e:
except configparser.Error as e:
raise ConfigError("Configuration error: %s" % str(e))
except blacklist.ConfigError as e:
raise InternalError("Blacklist error: %s" % str(e))
Expand Down
12 changes: 6 additions & 6 deletions gettor/xmpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
import gettext
import hashlib
import logging
import ConfigParser
import configparser

from sleekxmpp import ClientXMPP
from sleekxmpp.xmlstream.stanzabase import JID
from sleekxmpp.exceptions import IqError, IqTimeout

import core
import utils
import blacklist
from . import core
from . import utils
from . import blacklist


"""XMPP module for processing requests."""
Expand Down Expand Up @@ -104,7 +104,7 @@ def __init__(self, cfg=None):
"""
# define a set of default values
default_cfg = 'xmpp.cfg'
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()

if cfg is None or not os.path.isfile(cfg):
cfg = default_cfg
Expand Down Expand Up @@ -137,7 +137,7 @@ def __init__(self, cfg=None):
logfile = os.path.join(logdir, 'xmpp.log')
loglevel = config.get('log', 'level')

except ConfigParser.Error as e:
except configparser.Error as e:
raise ConfigError("Configuration error: %s" % str(e))
except blacklist.ConfigError as e:
raise InternalError("Blacklist error: %s" % str(e))
Expand Down
Loading