mirror of
https://github.com/ytdl-org/youtube-dl.git
synced 2024-12-23 16:36:48 +00:00
[utils] Add posix expanduser implementation and clarify the original source
This commit is contained in:
parent
4644ac5527
commit
fc66e4a0d5
@ -204,45 +204,79 @@ def compat_ord(c):
|
|||||||
else: return ord(c)
|
else: return ord(c)
|
||||||
|
|
||||||
|
|
||||||
# Environment variables should be decoded with filesystem encoding
|
|
||||||
# otherwise this results in issues like #3854 #2918 #3217
|
|
||||||
if sys.version_info >= (3, 0):
|
if sys.version_info >= (3, 0):
|
||||||
compat_getenv = os.getenv
|
compat_getenv = os.getenv
|
||||||
compat_expanduser = os.path.expanduser
|
compat_expanduser = os.path.expanduser
|
||||||
else:
|
else:
|
||||||
|
# Environment variables should be decoded with filesystem encoding.
|
||||||
|
# Otherwise it will fail if any non-ASCII characters present (see #3854 #3217 #2918)
|
||||||
|
|
||||||
def compat_getenv(key, default=None):
|
def compat_getenv(key, default=None):
|
||||||
env = os.getenv(key, default)
|
env = os.getenv(key, default)
|
||||||
if env:
|
if env:
|
||||||
env = env.decode(get_filesystem_encoding())
|
env = env.decode(get_filesystem_encoding())
|
||||||
return env
|
return env
|
||||||
|
|
||||||
def compat_expanduser(path):
|
# HACK: The default implementations of os.path.expanduser from cpython do not decode
|
||||||
"""Expand ~ and ~user constructs.
|
# environment variables with filesystem encoding. We will work around this by
|
||||||
|
# providing adjusted implementations.
|
||||||
|
# The following are os.path.expanduser implementations from cpython 2.7.8 stdlib
|
||||||
|
# for different platforms with correct environment variables decoding.
|
||||||
|
|
||||||
If user or $HOME is unknown, do nothing."""
|
if os.name == 'posix':
|
||||||
if path[:1] != '~':
|
def compat_expanduser(path):
|
||||||
return path
|
"""Expand ~ and ~user constructions. If user or $HOME is unknown,
|
||||||
i, n = 1, len(path)
|
do nothing."""
|
||||||
while i < n and path[i] not in '/\\':
|
if not path.startswith('~'):
|
||||||
i += 1
|
return path
|
||||||
|
i = path.find('/', 1)
|
||||||
|
if i < 0:
|
||||||
|
i = len(path)
|
||||||
|
if i == 1:
|
||||||
|
if 'HOME' not in os.environ:
|
||||||
|
import pwd
|
||||||
|
userhome = pwd.getpwuid(os.getuid()).pw_dir
|
||||||
|
else:
|
||||||
|
userhome = compat_getenv('HOME')
|
||||||
|
else:
|
||||||
|
import pwd
|
||||||
|
try:
|
||||||
|
pwent = pwd.getpwnam(path[1:i])
|
||||||
|
except KeyError:
|
||||||
|
return path
|
||||||
|
userhome = pwent.pw_dir
|
||||||
|
userhome = userhome.rstrip('/')
|
||||||
|
return (userhome + path[i:]) or '/'
|
||||||
|
elif os.name == 'nt' or os.name == 'ce':
|
||||||
|
def compat_expanduser(path):
|
||||||
|
"""Expand ~ and ~user constructs.
|
||||||
|
|
||||||
if 'HOME' in os.environ:
|
If user or $HOME is unknown, do nothing."""
|
||||||
userhome = compat_getenv('HOME')
|
if path[:1] != '~':
|
||||||
elif 'USERPROFILE' in os.environ:
|
return path
|
||||||
userhome = compat_getenv('USERPROFILE')
|
i, n = 1, len(path)
|
||||||
elif not 'HOMEPATH' in os.environ:
|
while i < n and path[i] not in '/\\':
|
||||||
return path
|
i = i + 1
|
||||||
else:
|
|
||||||
try:
|
|
||||||
drive = compat_getenv('HOMEDRIVE')
|
|
||||||
except KeyError:
|
|
||||||
drive = ''
|
|
||||||
userhome = os.path.join(drive, compat_getenv('HOMEPATH'))
|
|
||||||
|
|
||||||
if i != 1: # ~user
|
if 'HOME' in os.environ:
|
||||||
userhome = os.path.join(os.path.dirname(userhome), path[1:i])
|
userhome = compat_getenv('HOME')
|
||||||
|
elif 'USERPROFILE' in os.environ:
|
||||||
|
userhome = compat_getenv('USERPROFILE')
|
||||||
|
elif not 'HOMEPATH' in os.environ:
|
||||||
|
return path
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
drive = compat_getenv('HOMEDRIVE')
|
||||||
|
except KeyError:
|
||||||
|
drive = ''
|
||||||
|
userhome = os.path.join(drive, compat_getenv('HOMEPATH'))
|
||||||
|
|
||||||
return userhome + path[i:]
|
if i != 1: #~user
|
||||||
|
userhome = os.path.join(os.path.dirname(userhome), path[1:i])
|
||||||
|
|
||||||
|
return userhome + path[i:]
|
||||||
|
else:
|
||||||
|
compat_expanduser = os.path.expanduser
|
||||||
|
|
||||||
|
|
||||||
# This is not clearly defined otherwise
|
# This is not clearly defined otherwise
|
||||||
|
Loading…
Reference in New Issue
Block a user