Bug 1425399 - Added python 3 support to mozprofile. r=wlach

MozReview-Commit-ID: 9iAFA3JYagG

--HG--
extra : rebase_source : 151903d9c0920743c7564118106a85d7f97ff270
This commit is contained in:
Vedant Chakravadhanula 2017-12-22 12:11:06 +05:30
parent 18dd2a1cb6
commit 4a73233853
8 changed files with 48 additions and 48 deletions

View File

@ -9,12 +9,12 @@ import os
import sys
import shutil
import tempfile
import urllib2
import zipfile
import hashlib
from xml.dom import minidom
from six import reraise
from six import reraise, string_types
from six.moves.urllib import request
import mozfile
from mozlog.unstructured import getLogger
@ -113,7 +113,7 @@ class AddonManager(object):
:param target_folder: Folder to store the XPI file in
"""
response = urllib2.urlopen(url)
response = request.urlopen(url)
fd, path = tempfile.mkstemp(suffix='.xpi')
os.write(fd, response.read())
os.close(fd)
@ -177,14 +177,14 @@ class AddonManager(object):
# install addon paths
if addons:
if isinstance(addons, basestring):
if isinstance(addons, string_types):
addons = [addons]
for addon in set(addons):
self.install_from_path(addon)
# install addon manifests
if manifests:
if isinstance(manifests, basestring):
if isinstance(manifests, string_types):
manifests = [manifests]
for manifest in manifests:
self.install_from_manifest(manifest)
@ -235,7 +235,7 @@ class AddonManager(object):
.. _query-documentation: https://developer.mozilla.org/en/addons.mozilla.org_%28AMO%29_API_Developers%27_Guide/The_generic_AMO_API # noqa
"""
response = urllib2.urlopen(query)
response = request.urlopen(query)
dom = minidom.parseString(response.read())
for node in dom.getElementsByTagName('install')[0].childNodes:
if node.nodeType == node.TEXT_NODE:
@ -353,7 +353,7 @@ class AddonManager(object):
reraise(AddonFormatError(str(e)), None, sys.exc_info()[2])
# turn unpack into a true/false value
if isinstance(details['unpack'], basestring):
if isinstance(details['unpack'], string_types):
details['unpack'] = details['unpack'].lower() == 'true'
# If no ID is set, the add-on is invalid

View File

@ -12,7 +12,9 @@ from __future__ import absolute_import
import codecs
import os
import sqlite3
import urlparse
from six import string_types
from six.moves.urllib import parse
__all__ = ['MissingPrimaryLocationError', 'MultiplePrimaryLocationsError',
'DEFAULT_PORTS', 'DuplicateLocationError', 'BadPortLocationError',
@ -139,7 +141,7 @@ class ServerLocations(object):
self.add_callback([location])
def add_host(self, host, port='80', scheme='http', options='privileged'):
if isinstance(options, basestring):
if isinstance(options, string_types):
options = options.split(',')
self.add(Location(scheme, host, port, options))
@ -182,7 +184,7 @@ class ServerLocations(object):
# parse the server url
if '://' not in server:
server = 'http://' + server
scheme, netloc, path, query, fragment = urlparse.urlsplit(server)
scheme, netloc, path, query, fragment = parse.urlsplit(server)
# get the host and port
try:
host, port = netloc.rsplit(':', 1)

View File

@ -11,8 +11,9 @@ import json
import mozfile
import os
import tokenize
from ConfigParser import SafeConfigParser as ConfigParser
from StringIO import StringIO
from six.moves.configparser import SafeConfigParser as ConfigParser
from six import StringIO, string_types
__all__ = ('PreferencesReadError', 'Preferences')
@ -64,7 +65,7 @@ class Preferences(object):
with the ''s removed from both sides
"""
if not isinstance(value, basestring):
if not isinstance(value, string_types):
return value # no op
quote = "'"
if value == 'true':
@ -146,7 +147,7 @@ class Preferences(object):
values = prefs.values()
else:
raise PreferencesReadError("Malformed preferences: %s" % path)
types = (bool, basestring, int)
types = (bool, string_types, int)
if [i for i in values if not [isinstance(i, j) for j in types]]:
raise PreferencesReadError("Only bool, string, and int values allowed")
return prefs
@ -186,7 +187,7 @@ class Preferences(object):
retval = []
def pref(a, b):
if interpolation and isinstance(b, basestring):
if interpolation and isinstance(b, string_types):
b = b.format(**interpolation)
retval.append((a, b))
lines = [i.strip().rstrip(';') for i in string.split('\n') if i.strip()]
@ -202,7 +203,7 @@ class Preferences(object):
# de-magic the marker
for index, (key, value) in enumerate(retval):
if isinstance(value, basestring) and marker in value:
if isinstance(value, string_types) and marker in value:
retval[index] = (key, value.replace(marker, '//'))
return retval
@ -211,8 +212,8 @@ class Preferences(object):
def write(cls, _file, prefs, pref_string='user_pref(%s, %s);'):
"""write preferences to a file"""
if isinstance(_file, basestring):
f = file(_file, 'a')
if isinstance(_file, string_types):
f = open(_file, 'a')
else:
f = _file
@ -229,5 +230,5 @@ class Preferences(object):
print(pref_string % _pref, file=f)
# close the file if opened internally
if isinstance(_file, basestring):
if isinstance(_file, string_types):
f.close()

View File

@ -255,7 +255,7 @@ class Profile(object):
"""
path = os.path.join(self.profile, filename)
with file(path) as f:
with open(path) as f:
lines = f.read().splitlines()
def last_index(_list, value):
@ -282,7 +282,7 @@ class Profile(object):
# write the prefs
cleaned_prefs = '\n'.join(lines[:s] + lines[e + 1:])
with file(path, 'w') as f:
with open(path, 'w') as f:
f.write(cleaned_prefs)
return True

View File

@ -4,15 +4,11 @@
from __future__ import absolute_import
import sys
from setuptools import setup
PACKAGE_NAME = 'mozprofile'
PACKAGE_VERSION = '0.29'
# we only support python 2 right now
assert sys.version_info[0] == 2
deps = ['mozfile >= 1.0',
'mozlog >= 3.0',
'six >= 1.10.0'
@ -27,7 +23,8 @@ setup(name=PACKAGE_NAME,
'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Topic :: Software Development :: Libraries :: Python Modules',
],
keywords='mozilla',

View File

@ -65,21 +65,21 @@ http://example.org:80 privileged
# ensure that they're what we expect
self.assertEqual(len(locations), 6)
i = iter(locations)
self.compare_location(i.next(), 'http', 'mochi.test', '8888',
self.compare_location(next(i), 'http', 'mochi.test', '8888',
['primary', 'privileged'])
self.compare_location(i.next(), 'http', '127.0.0.1', '80',
self.compare_location(next(i), 'http', '127.0.0.1', '80',
['privileged'])
self.compare_location(i.next(), 'http', '127.0.0.1', '8888',
self.compare_location(next(i), 'http', '127.0.0.1', '8888',
['privileged'])
self.compare_location(i.next(), 'https', 'test', '80', ['privileged'])
self.compare_location(i.next(), 'http', 'example.org', '80',
self.compare_location(next(i), 'https', 'test', '80', ['privileged'])
self.compare_location(next(i), 'http', 'example.org', '80',
['privileged'])
self.compare_location(i.next(), 'http', 'test1.example.org', '8888',
self.compare_location(next(i), 'http', 'test1.example.org', '8888',
['privileged'])
locations.add_host('mozilla.org')
self.assertEqual(len(locations), 7)
self.compare_location(i.next(), 'http', 'mozilla.org', '80',
self.compare_location(next(i), 'http', 'mozilla.org', '80',
['privileged'])
# test some errors

View File

@ -10,7 +10,6 @@ import os
import shutil
import tempfile
import unittest
import urllib2
import zipfile
import mozunit
@ -22,6 +21,7 @@ import mozlog.unstructured as mozlog
import mozprofile
from addon_stubs import generate_addon, generate_manifest
from six.moves.urllib import error
here = os.path.dirname(os.path.abspath(__file__))
@ -104,7 +104,7 @@ class TestAddonsManager(unittest.TestCase):
# Download from an invalid URL
addon = server.get_url() + 'not_existent.xpi'
self.assertRaises(urllib2.HTTPError,
self.assertRaises(error.HTTPError,
self.am.download, addon, self.tmpdir)
self.assertEqual(os.listdir(self.tmpdir), [])
@ -166,7 +166,7 @@ class TestAddonsManager(unittest.TestCase):
self.am.install_from_path(temp_addon)
# Generate a list of addons installed in the profile
addons_installed = [unicode(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
addons_installed = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
self.profile.profile, 'extensions', 'staged'))]
self.assertEqual(addons_to_install.sort(), addons_installed.sort())
@ -331,7 +331,7 @@ class TestAddonsManager(unittest.TestCase):
self.am.install_from_manifest(temp_manifest)
# Generate a list of addons installed in the profile
addons_installed = [unicode(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
addons_installed = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
self.profile.profile, 'extensions', 'staged'))]
self.assertEqual(addons_installed.sort(), addons_to_install.sort())
@ -371,7 +371,7 @@ class TestAddonsManager(unittest.TestCase):
addon_two = generate_addon('test-addon-2@mozilla.org')
self.am.install_addons(addon_one)
installed_addons = [unicode(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
installed_addons = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
self.profile.profile, 'extensions', 'staged'))]
# Create a new profile based on an existing profile
@ -381,7 +381,7 @@ class TestAddonsManager(unittest.TestCase):
addons=addon_two)
duplicate_profile.addon_manager.clean()
addons_after_cleanup = [unicode(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
addons_after_cleanup = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
duplicate_profile.profile, 'extensions', 'staged'))]
# New addons installed should be removed by clean_addons()
self.assertEqual(installed_addons, addons_after_cleanup)

View File

@ -143,14 +143,14 @@ general.warnOnAboutConfig = False
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
self.assertFalse(initial_prefs)
initial_prefs = file(prefs_file).read().strip()
initial_prefs = open(prefs_file).read().strip()
self.assertFalse(initial_prefs)
# add some preferences
prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
profile.set_preferences(prefs1)
self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
lines = file(prefs_file).read().strip().splitlines()
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue(any(line.startswith('#MozRunner Prefs Start') for line in lines))
self.assertTrue(any(line.startswith('#MozRunner Prefs End') for line in lines))
@ -167,14 +167,14 @@ general.warnOnAboutConfig = False
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
self.assertFalse(initial_prefs)
initial_prefs = file(prefs_file).read().strip()
initial_prefs = open(prefs_file).read().strip()
self.assertFalse(initial_prefs)
# add some preferences
prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
profile.set_persistent_preferences(prefs1)
self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
lines = file(prefs_file).read().strip().splitlines()
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue(any(line.startswith('#MozRunner Prefs Start') for line in lines))
self.assertTrue(any(line.startswith('#MozRunner Prefs End') for line in lines))
@ -192,7 +192,7 @@ general.warnOnAboutConfig = False
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
self.assertFalse(initial_prefs)
initial_prefs = file(prefs_file).read().strip()
initial_prefs = open(prefs_file).read().strip()
self.assertFalse(initial_prefs)
# add some preferences
@ -200,7 +200,7 @@ general.warnOnAboutConfig = False
("zoom.minPercent", 30)]
profile.set_preferences(prefs1)
self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
lines = file(prefs_file).read().strip().splitlines()
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue(bool([line for line in lines
if line.startswith('#MozRunner Prefs Start')]))
self.assertTrue(bool([line for line in lines
@ -211,7 +211,7 @@ general.warnOnAboutConfig = False
("webgl.verbose", 'false')]
profile.set_preferences(prefs2)
self.assertEqual(prefs1 + prefs2, Preferences.read_prefs(prefs_file))
lines = file(prefs_file).read().strip().splitlines()
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue(len([line for line in lines
if line.startswith('#MozRunner Prefs Start')]) == 2)
self.assertTrue(len([line for line in lines
@ -221,7 +221,7 @@ general.warnOnAboutConfig = False
profile.clean_preferences()
final_prefs = Preferences.read_prefs(prefs_file)
self.assertFalse(final_prefs)
lines = file(prefs_file).read().strip().splitlines()
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue('#MozRunner Prefs Start' not in lines)
self.assertTrue('#MozRunner Prefs End' not in lines)
@ -238,7 +238,7 @@ user_pref("webgl.enabled_for_all_sites", true);
user_pref("webgl.force-enabled", true);
"""
user_js = os.path.join(tempdir, 'user.js')
f = file(user_js, 'w')
f = open(user_js, 'w')
f.write(contents)
f.close()