Commit f2f416b2 authored by Jon Banafato's avatar Jon Banafato

Add compatibility for both Python 2 and Python 3

In order to install fswrap under the current stable version of Python
(3.4.3), the following changes are needed:

- Conditionally redefine `unicode`, as it has been removed in Python 3
- Explicitly use `b''` when bytes are required
- Encode unicode strings before hashing

In addition to these changes, installation is upgraded to the current
version of distribute. distribute has been merged back into setuptools.
The current version simply wraps setuptools. distribute_setup.py and
distribute_setup.use_setuptools are removed to prevent old versions of
distribute from being installed over the current wrapper. This should be
considered a stopgap solution, and migrating to setuptools is highly
recommended.

Finally, tox is added to support testing under multiple versions of
Python. Test environments for latest stable versions of Python 2.7, 3.3,
and 3.4 are added.

Note: changes were made to satisfy all tests under all environments.
Test coverage is high (96% - 97% depending on environment), but no
further use under Python 3 has been done so far.
parent 01958aed
......@@ -11,4 +11,6 @@ build
MANIFEST
dist
*egg*
*gz
\ No newline at end of file
*gz
.coverage
.tox
This diff is collapsed.
......@@ -15,6 +15,7 @@ from distutils import dir_util
import functools
import fnmatch
import logging
import sys
logger = logging.getLogger('fswrap')
......@@ -24,6 +25,12 @@ logger = logging.getLogger('fswrap')
__all__ = ['File', 'Folder']
PY3 = sys.version_info[0] == 3
if PY3:
unicode = str
class FS(object):
"""
The base file system object
......@@ -211,7 +218,7 @@ class File(FS):
CHUNKSIZE = 1024
while 1:
chunk = fin.read(CHUNKSIZE)
if '\0' in chunk:
if b'\0' in chunk:
return True
if len(chunk) < CHUNKSIZE:
break
......@@ -309,7 +316,7 @@ class File(FS):
with open(self.path) as fin:
chunk = fin.read(CHUNKSIZE)
while chunk:
hash.update(chunk)
hash.update(chunk.encode())
chunk = fin.read(CHUNKSIZE)
return hash.hexdigest()
......
#!/usr/bin/env python
from distribute_setup import use_setuptools
use_setuptools()
from setuptools import setup
try:
......@@ -29,6 +27,6 @@ setup(
py_modules=['fswrap'],
tests_require=(
'nose',
),
),
test_suite='nose.collector',
)
......@@ -9,9 +9,15 @@ from fswrap import FS, File, Folder
import codecs
import os
import shutil
import sys
from nose.tools import with_setup, nottest
PY3 = sys.version_info[0] == 3
if PY3:
unicode = str
def test_representation():
f = FS(__file__)
......
[tox]
envlist = py27,py33,py34
[testenv]
deps =
coverage
nose
commands =
python -m coverage run -m nose
python -m coverage report -m --include="fswrap.py"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment