login page
This commit is contained in:
16
Lib/site-packages/virtualenv/util/path/__init__.py
Normal file
16
Lib/site-packages/virtualenv/util/path/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from ._pathlib import Path
|
||||
from ._permission import make_exe, set_tree
|
||||
from ._sync import copy, copytree, ensure_dir, safe_delete, symlink
|
||||
|
||||
__all__ = (
|
||||
"ensure_dir",
|
||||
"symlink",
|
||||
"copy",
|
||||
"copytree",
|
||||
"Path",
|
||||
"make_exe",
|
||||
"set_tree",
|
||||
"safe_delete",
|
||||
)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
63
Lib/site-packages/virtualenv/util/path/_pathlib/__init__.py
Normal file
63
Lib/site-packages/virtualenv/util/path/_pathlib/__init__.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import sys
|
||||
|
||||
import six
|
||||
|
||||
if six.PY3:
|
||||
from pathlib import Path
|
||||
|
||||
if sys.version_info[0:2] == (3, 4):
|
||||
# no read/write text on python3.4
|
||||
BuiltinPath = Path
|
||||
|
||||
class Path(type(BuiltinPath())):
|
||||
def read_text(self, encoding=None, errors=None):
|
||||
"""
|
||||
Open the file in text mode, read it, and close the file.
|
||||
"""
|
||||
with self.open(mode="r", encoding=encoding, errors=errors) as f:
|
||||
return f.read()
|
||||
|
||||
def read_bytes(self):
|
||||
"""
|
||||
Open the file in bytes mode, read it, and close the file.
|
||||
"""
|
||||
with self.open(mode="rb") as f:
|
||||
return f.read()
|
||||
|
||||
def write_text(self, data, encoding=None, errors=None):
|
||||
"""
|
||||
Open the file in text mode, write to it, and close the file.
|
||||
"""
|
||||
if not isinstance(data, str):
|
||||
raise TypeError("data must be str, not %s" % data.__class__.__name__)
|
||||
with self.open(mode="w", encoding=encoding, errors=errors) as f:
|
||||
return f.write(data)
|
||||
|
||||
def write_bytes(self, data):
|
||||
"""
|
||||
Open the file in bytes mode, write to it, and close the file.
|
||||
"""
|
||||
# type-check for the buffer interface before truncating the file
|
||||
view = memoryview(data)
|
||||
with self.open(mode="wb") as f:
|
||||
return f.write(view)
|
||||
|
||||
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
|
||||
try:
|
||||
super(type(BuiltinPath()), self).mkdir(mode, parents)
|
||||
except FileExistsError as exception:
|
||||
if not exist_ok:
|
||||
raise exception
|
||||
|
||||
|
||||
else:
|
||||
if sys.platform == "win32":
|
||||
# workaround for https://github.com/mcmtroffaes/pathlib2/issues/56
|
||||
from .via_os_path import Path
|
||||
else:
|
||||
from pathlib2 import Path
|
||||
|
||||
|
||||
__all__ = ("Path",)
|
||||
Binary file not shown.
Binary file not shown.
148
Lib/site-packages/virtualenv/util/path/_pathlib/via_os_path.py
Normal file
148
Lib/site-packages/virtualenv/util/path/_pathlib/via_os_path.py
Normal file
@@ -0,0 +1,148 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import os
|
||||
import platform
|
||||
from contextlib import contextmanager
|
||||
|
||||
from virtualenv.util.six import ensure_str, ensure_text
|
||||
|
||||
IS_PYPY = platform.python_implementation() == "PyPy"
|
||||
|
||||
|
||||
class Path(object):
|
||||
def __init__(self, path):
|
||||
if isinstance(path, Path):
|
||||
_path = path._path
|
||||
else:
|
||||
_path = ensure_text(path)
|
||||
if IS_PYPY:
|
||||
_path = _path.encode("utf-8")
|
||||
self._path = _path
|
||||
|
||||
def __repr__(self):
|
||||
return ensure_str("Path({})".format(ensure_text(self._path)))
|
||||
|
||||
def __unicode__(self):
|
||||
return ensure_text(self._path)
|
||||
|
||||
def __str__(self):
|
||||
return ensure_str(self._path)
|
||||
|
||||
def __div__(self, other):
|
||||
if isinstance(other, Path):
|
||||
right = other._path
|
||||
else:
|
||||
right = ensure_text(other)
|
||||
if IS_PYPY:
|
||||
right = right.encode("utf-8")
|
||||
return Path(os.path.join(self._path, right))
|
||||
|
||||
def __truediv__(self, other):
|
||||
return self.__div__(other)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self._path == (other._path if isinstance(other, Path) else None)
|
||||
|
||||
def __ne__(self, other):
|
||||
return not (self == other)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self._path)
|
||||
|
||||
def exists(self):
|
||||
return os.path.exists(self._path)
|
||||
|
||||
@property
|
||||
def parent(self):
|
||||
return Path(os.path.abspath(os.path.join(self._path, os.path.pardir)))
|
||||
|
||||
def resolve(self):
|
||||
return Path(os.path.realpath(self._path))
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return os.path.basename(self._path)
|
||||
|
||||
@property
|
||||
def parts(self):
|
||||
return self._path.split(os.sep)
|
||||
|
||||
def is_file(self):
|
||||
return os.path.isfile(self._path)
|
||||
|
||||
def is_dir(self):
|
||||
return os.path.isdir(self._path)
|
||||
|
||||
def mkdir(self, parents=True, exist_ok=True):
|
||||
try:
|
||||
os.makedirs(self._path)
|
||||
except OSError:
|
||||
if not exist_ok:
|
||||
raise
|
||||
|
||||
def read_text(self, encoding="utf-8"):
|
||||
return self.read_bytes().decode(encoding)
|
||||
|
||||
def read_bytes(self):
|
||||
with open(self._path, "rb") as file_handler:
|
||||
return file_handler.read()
|
||||
|
||||
def write_bytes(self, content):
|
||||
with open(self._path, "wb") as file_handler:
|
||||
file_handler.write(content)
|
||||
|
||||
def write_text(self, text, encoding="utf-8"):
|
||||
self.write_bytes(text.encode(encoding))
|
||||
|
||||
def iterdir(self):
|
||||
for p in os.listdir(self._path):
|
||||
yield Path(os.path.join(self._path, p))
|
||||
|
||||
@property
|
||||
def suffix(self):
|
||||
_, ext = os.path.splitext(self.name)
|
||||
return ext
|
||||
|
||||
@property
|
||||
def stem(self):
|
||||
base, _ = os.path.splitext(self.name)
|
||||
return base
|
||||
|
||||
@contextmanager
|
||||
def open(self, mode="r"):
|
||||
with open(self._path, mode) as file_handler:
|
||||
yield file_handler
|
||||
|
||||
@property
|
||||
def parents(self):
|
||||
result = []
|
||||
parts = self.parts
|
||||
for i in range(len(parts) - 1):
|
||||
result.append(Path(os.sep.join(parts[0 : i + 1])))
|
||||
return result[::-1]
|
||||
|
||||
def unlink(self):
|
||||
os.remove(self._path)
|
||||
|
||||
def with_name(self, name):
|
||||
return self.parent / name
|
||||
|
||||
def is_symlink(self):
|
||||
return os.path.islink(self._path)
|
||||
|
||||
def relative_to(self, other):
|
||||
if not self._path.startswith(other._path):
|
||||
raise ValueError("{} does not start with {}".format(self._path, other._path))
|
||||
return Path(os.sep.join(self.parts[len(other.parts) :]))
|
||||
|
||||
def stat(self):
|
||||
return os.stat(self._path)
|
||||
|
||||
def chmod(self, mode):
|
||||
os.chmod(self._path, mode)
|
||||
|
||||
def absolute(self):
|
||||
return Path(os.path.abspath(self._path))
|
||||
|
||||
|
||||
__all__ = ("Path",)
|
||||
32
Lib/site-packages/virtualenv/util/path/_permission.py
Normal file
32
Lib/site-packages/virtualenv/util/path/_permission.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import os
|
||||
from stat import S_IXGRP, S_IXOTH, S_IXUSR
|
||||
|
||||
from virtualenv.util.six import ensure_text
|
||||
|
||||
|
||||
def make_exe(filename):
|
||||
original_mode = filename.stat().st_mode
|
||||
levels = [S_IXUSR, S_IXGRP, S_IXOTH]
|
||||
for at in range(len(levels), 0, -1):
|
||||
try:
|
||||
mode = original_mode
|
||||
for level in levels[:at]:
|
||||
mode |= level
|
||||
filename.chmod(mode)
|
||||
break
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
|
||||
def set_tree(folder, stat):
|
||||
for root, _, files in os.walk(ensure_text(str(folder))):
|
||||
for filename in files:
|
||||
os.chmod(os.path.join(root, filename), stat)
|
||||
|
||||
|
||||
__all__ = (
|
||||
"make_exe",
|
||||
"set_tree",
|
||||
)
|
||||
98
Lib/site-packages/virtualenv/util/path/_sync.py
Normal file
98
Lib/site-packages/virtualenv/util/path/_sync.py
Normal file
@@ -0,0 +1,98 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
from stat import S_IWUSR
|
||||
|
||||
from six import PY2
|
||||
|
||||
from virtualenv.info import IS_CPYTHON, IS_WIN
|
||||
from virtualenv.util.six import ensure_text
|
||||
|
||||
if PY2 and IS_CPYTHON and IS_WIN: # CPython2 on Windows supports unicode paths if passed as unicode
|
||||
|
||||
def norm(src):
|
||||
return ensure_text(str(src))
|
||||
|
||||
|
||||
else:
|
||||
norm = str
|
||||
|
||||
|
||||
def ensure_dir(path):
|
||||
if not path.exists():
|
||||
logging.debug("create folder %s", ensure_text(str(path)))
|
||||
os.makedirs(norm(path))
|
||||
|
||||
|
||||
def ensure_safe_to_do(src, dest):
|
||||
if src == dest:
|
||||
raise ValueError("source and destination is the same {}".format(src))
|
||||
if not dest.exists():
|
||||
return
|
||||
if dest.is_dir() and not dest.is_symlink():
|
||||
logging.debug("remove directory %s", dest)
|
||||
safe_delete(dest)
|
||||
else:
|
||||
logging.debug("remove file %s", dest)
|
||||
dest.unlink()
|
||||
|
||||
|
||||
def symlink(src, dest):
|
||||
ensure_safe_to_do(src, dest)
|
||||
logging.debug("symlink %s", _Debug(src, dest))
|
||||
dest.symlink_to(src, target_is_directory=src.is_dir())
|
||||
|
||||
|
||||
def copy(src, dest):
|
||||
ensure_safe_to_do(src, dest)
|
||||
is_dir = src.is_dir()
|
||||
method = copytree if is_dir else shutil.copy
|
||||
logging.debug("copy %s", _Debug(src, dest))
|
||||
method(norm(src), norm(dest))
|
||||
|
||||
|
||||
def copytree(src, dest):
|
||||
for root, _, files in os.walk(src):
|
||||
dest_dir = os.path.join(dest, os.path.relpath(root, src))
|
||||
if not os.path.exists(dest_dir):
|
||||
os.makedirs(dest_dir)
|
||||
for name in files:
|
||||
src_f = os.path.join(root, name)
|
||||
dest_f = os.path.join(dest_dir, name)
|
||||
shutil.copy(src_f, dest_f)
|
||||
|
||||
|
||||
def safe_delete(dest):
|
||||
def onerror(func, path, exc_info):
|
||||
if not os.access(path, os.W_OK):
|
||||
os.chmod(path, S_IWUSR)
|
||||
func(path)
|
||||
else:
|
||||
raise
|
||||
|
||||
shutil.rmtree(ensure_text(str(dest)), ignore_errors=True, onerror=onerror)
|
||||
|
||||
|
||||
class _Debug(object):
|
||||
def __init__(self, src, dest):
|
||||
self.src = src
|
||||
self.dest = dest
|
||||
|
||||
def __str__(self):
|
||||
return "{}{} to {}".format(
|
||||
"directory " if self.src.is_dir() else "",
|
||||
ensure_text(str(self.src)),
|
||||
ensure_text(str(self.dest)),
|
||||
)
|
||||
|
||||
|
||||
__all__ = (
|
||||
"ensure_dir",
|
||||
"symlink",
|
||||
"copy",
|
||||
"symlink",
|
||||
"copytree",
|
||||
"safe_delete",
|
||||
)
|
||||
Reference in New Issue
Block a user