Skip to content

Commit

Permalink
py2: Add unicode support for open and os.walk
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr. Outis committed Feb 8, 2019
1 parent 95368b8 commit 2be7232
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import unicode_literals

from dvc.utils.compat import str
from dvc.utils.compat import str, open

import os
import errno
Expand Down
2 changes: 1 addition & 1 deletion dvc/istextfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import unicode_literals

from dvc.utils.compat import is_py3
from dvc.utils.compat import is_py3, open

# Based on https://eli.thegreenplace.net/2011/10/19/
# perls-guess-if-file-is-text-or-binary-implemented-in-python
Expand Down
8 changes: 4 additions & 4 deletions dvc/project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals

from dvc.utils.compat import str, builtin_str
from dvc.utils.compat import str, builtin_str, open

import collections
import os
Expand Down Expand Up @@ -200,7 +200,7 @@ def add(self, fname, recursive=False):

fnames = []
if recursive and os.path.isdir(fname):
for root, dirs, files in os.walk(fname):
for root, dirs, files in os.walk(str(fname)):
for f in files:
path = os.path.join(root, f)
if Stage.is_stage_file(path):
Expand Down Expand Up @@ -351,7 +351,7 @@ def _unprotect_file(self, path):
os.chmod(path, os.stat(path).st_mode | stat.S_IWRITE)

def _unprotect_dir(self, path):
for root, dirs, files in os.walk(path):
for root, dirs, files in os.walk(str(path)):
for f in files:
path = os.path.join(root, f)
self._unprotect_file(path)
Expand Down Expand Up @@ -1335,7 +1335,7 @@ def stages(self, from_directory=None):

stages = []
outs = []
for root, dirs, files in os.walk(from_directory):
for root, dirs, files in os.walk(str(from_directory)):
for fname in files:
path = os.path.join(root, fname)
if not Stage.is_stage_file(path):
Expand Down
1 change: 1 addition & 0 deletions dvc/remote/http.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import unicode_literals
from dvc.utils.compat import open

import os
import threading
Expand Down
5 changes: 3 additions & 2 deletions dvc/remote/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def ntpath(path):
def collect_dir_cache(self, dname):
dir_info = []

for root, dirs, files in os.walk(dname):
for root, dirs, files in os.walk(str(dname)):
bar = False

if len(files) > LARGE_DIR_SIZE:
Expand Down Expand Up @@ -276,6 +276,7 @@ def dump_dir_cache(self, md5, dir_info):
# NOTE: Writing first and renaming after that
# to make sure that the operation is atomic.
tmp = "{}.{}".format(path, str(uuid.uuid4()))

with open(tmp, "w+") as fd:
json.dump(dir_info, fd, sort_keys=True)
move(tmp, path)
Expand Down Expand Up @@ -348,7 +349,7 @@ def already_cached(self, path_info):
def _discard_working_directory_changes(self, path, dir_info, force=False):
working_dir_files = set(
os.path.join(root, file)
for root, _, files in os.walk(path)
for root, _, files in os.walk(str(path))
for file in files
)

Expand Down
2 changes: 1 addition & 1 deletion dvc/scm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# (called git.py) instead of importing gitpython's git module.
from __future__ import absolute_import

from dvc.utils.compat import str
from dvc.utils.compat import str, open

import os

Expand Down
2 changes: 1 addition & 1 deletion dvc/stage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals

from dvc.utils.compat import str
from dvc.utils.compat import str, open

import os
import yaml
Expand Down
2 changes: 1 addition & 1 deletion dvc/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def _mtime_and_size(path):
mtime = os.path.getmtime(path)

if os.path.isdir(path):
for root, dirs, files in os.walk(path):
for root, dirs, files in os.walk(str(path)):
for name in dirs + files:
entry = os.path.join(root, name)
stat = os.stat(entry)
Expand Down
2 changes: 1 addition & 1 deletion dvc/system.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals

from dvc.utils.compat import str
from dvc.utils.compat import str, open

import os

Expand Down
1 change: 1 addition & 0 deletions dvc/updater.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import unicode_literals
from dvc.utils.compat import str, open

import sys
import os
Expand Down
2 changes: 1 addition & 1 deletion dvc/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Helpers for other modules."""

from __future__ import unicode_literals
from dvc.utils.compat import str, builtin_str
from dvc.utils.compat import str, builtin_str, open

import os
import sys
Expand Down
2 changes: 2 additions & 0 deletions dvc/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from BaseHTTPServer import HTTPServer # noqa: F401
from SimpleHTTPServer import SimpleHTTPRequestHandler # noqa: F401
import ConfigParser # noqa: F401
from io import open # noqa: F401

builtin_str = str # noqa: F821
bytes = str # noqa: F821
Expand All @@ -43,3 +44,4 @@
numeric_types = (int, float) # noqa: F821
integer_types = (int,) # noqa: F821
input = input # noqa: F821
open = open # noqa: F821
2 changes: 1 addition & 1 deletion scripts/innosetup/config_gen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This script generates config.ini for setup.iss script
from dvc import VERSION
from dvc.utils.compat import ConfigParser
from dvc.utils.compat import ConfigParser, open

config = ConfigParser.ConfigParser()
config.add_section("Version")
Expand Down
10 changes: 10 additions & 0 deletions tests/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ def test(self):
self.assertEqual(stage.cmd, None)
self.assertEqual(stage.outs[0].info["md5"], md5)

def test_unicode(self):
fname = "\xe1"

with open(fname, "w") as fobj:
fobj.write("something")

stage = self.dvc.add(fname)[0]

self.assertTrue(os.path.isfile(stage.path))


class TestAddUnupportedFile(TestDvc):
def test(self):
Expand Down

0 comments on commit 2be7232

Please sign in to comment.