diff options
Diffstat (limited to 'sphinx/util/osutil.py')
| -rw-r--r-- | sphinx/util/osutil.py | 72 | 
1 files changed, 18 insertions, 54 deletions
diff --git a/sphinx/util/osutil.py b/sphinx/util/osutil.py index fb9a2cfa1..c7c0b0690 100644 --- a/sphinx/util/osutil.py +++ b/sphinx/util/osutil.py @@ -13,7 +13,6 @@ from __future__ import print_function  import contextlib  import errno  import filecmp -import locale  import os  import re  import shutil @@ -23,9 +22,9 @@ import warnings  from io import BytesIO, StringIO  from os import path -from six import PY2, PY3, text_type +from six import text_type -from sphinx.deprecation import RemovedInSphinx30Warning +from sphinx.deprecation import RemovedInSphinx30Warning, RemovedInSphinx40Warning  if False:      # For type annotation @@ -37,9 +36,6 @@ ENOENT = getattr(errno, 'ENOENT', 0)  EPIPE = getattr(errno, 'EPIPE', 0)  EINVAL = getattr(errno, 'EINVAL', 0) -if PY3: -    unicode = str  # special alias for static typing... -  # SEP separates path elements in the canonical file names  #  # Define SEP as a manifest constant, not so much because we expect it to change @@ -95,39 +91,12 @@ def ensuredir(path):              raise -# This function is same as os.walk of Python2.7 except a customization -# that check UnicodeError. -# The customization obstacle to replace the function with the os.walk.  def walk(top, topdown=True, followlinks=False):      # type: (unicode, bool, bool) -> Iterator[Tuple[unicode, List[unicode], List[unicode]]] -    """Backport of os.walk from 2.6, where the *followlinks* argument was -    added. -    """ -    names = os.listdir(top) - -    dirs, nondirs = [], [] -    for name in names: -        try: -            fullpath = path.join(top, name) -        except UnicodeError: -            print('%s:: ERROR: non-ASCII filename not supported on this ' -                  'filesystem encoding %r, skipped.' % (name, fs_encoding), -                  file=sys.stderr) -            continue -        if path.isdir(fullpath): -            dirs.append(name) -        else: -            nondirs.append(name) - -    if topdown: -        yield top, dirs, nondirs -    for name in dirs: -        fullpath = path.join(top, name) -        if followlinks or not path.islink(fullpath): -            for x in walk(fullpath, topdown, followlinks): -                yield x -    if not topdown: -        yield top, dirs, nondirs +    warnings.warn('sphinx.util.osutil.walk() is deprecated for removal. ' +                  'Please use os.walk() instead.', +                  RemovedInSphinx40Warning) +    return os.walk(top, topdown=topdown, followlinks=followlinks)  def mtimes_of_files(dirnames, suffix): @@ -196,19 +165,13 @@ def ustrftime(format, *args):          if source_date_epoch is not None:              time_struct = time.gmtime(float(source_date_epoch))              args = [time_struct]  # type: ignore -    if PY2: -        # if a locale is set, the time strings are encoded in the encoding -        # given by LC_TIME; if that is available, use it -        enc = locale.getlocale(locale.LC_TIME)[1] or 'utf-8' -        return time.strftime(text_type(format).encode(enc), *args).decode(enc) -    else:  # Py3 -        # On Windows, time.strftime() and Unicode characters will raise UnicodeEncodeError. -        # https://bugs.python.org/issue8304 -        try: -            return time.strftime(format, *args) -        except UnicodeEncodeError: -            r = time.strftime(format.encode('unicode-escape').decode(), *args) -            return r.encode().decode('unicode-escape') +    # On Windows, time.strftime() and Unicode characters will raise UnicodeEncodeError. +    # https://bugs.python.org/issue8304 +    try: +        return time.strftime(format, *args)  # type: ignore +    except UnicodeEncodeError: +        r = time.strftime(format.encode('unicode-escape').decode(), *args)  # type: ignore +        return r.encode().decode('unicode-escape')  def relpath(path, start=os.curdir): @@ -244,15 +207,16 @@ def abspath(pathdir):  def getcwd():      # type: () -> unicode -    if hasattr(os, 'getcwdu'): -        return os.getcwdu() +    warnings.warn('sphinx.util.osutil.getcwd() is deprecated. ' +                  'Please use os.getcwd() instead.', +                  RemovedInSphinx40Warning)      return os.getcwd()  @contextlib.contextmanager  def cd(target_dir):      # type: (unicode) -> Iterator[None] -    cwd = getcwd() +    cwd = os.getcwd()      try:          os.chdir(target_dir)          yield @@ -260,7 +224,7 @@ def cd(target_dir):          os.chdir(cwd) -class FileAvoidWrite(object): +class FileAvoidWrite:      """File-like object that buffers output and only writes if content changed.      Use this class like when writing to a file to avoid touching the original  | 
