windows.py
786 lines
| 23.8 KiB
| text/x-python
|
PythonLexer
/ mercurial / windows.py
Martin Geisler
|
r8226 | # windows.py - Windows utility function implementations for Mercurial | ||
# | ||||
Raphaël Gomès
|
r47575 | # Copyright 2005-2009 Olivia Mackall <olivia@selenic.com> and others | ||
Martin Geisler
|
r8226 | # | ||
# This software may be used and distributed according to the terms of the | ||||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Matt Mackall
|
r7890 | |||
Matt Harbison
|
r52756 | from __future__ import annotations | ||
Sune Foldager
|
r8421 | |||
Gregory Szorc
|
r27360 | import errno | ||
Augie Fackler
|
r44356 | import getpass | ||
Matt Harbison
|
r50214 | import msvcrt # pytype: disable=import-error | ||
Gregory Szorc
|
r27360 | import os | ||
import re | ||||
import stat | ||||
Matt Harbison
|
r38502 | import string | ||
Gregory Szorc
|
r27360 | import sys | ||
Matt Harbison
|
r50713 | import typing | ||
Matt Harbison
|
r50213 | import winreg # pytype: disable=import-error | ||
Gregory Szorc
|
r27360 | |||
Matt Harbison
|
r50688 | from typing import ( | ||
Matt Harbison
|
r52917 | Any, | ||
Matt Harbison
|
r50712 | AnyStr, | ||
Matt Harbison
|
r50688 | BinaryIO, | ||
Matt Harbison
|
r50707 | Iterable, | ||
Iterator, | ||||
List, | ||||
Matt Harbison
|
r50712 | Mapping, | ||
Matt Harbison
|
r50707 | NoReturn, | ||
Optional, | ||||
Matt Harbison
|
r50712 | Pattern, | ||
Matt Harbison
|
r50707 | Sequence, | ||
Matt Harbison
|
r50713 | Tuple, | ||
Matt Harbison
|
r50707 | Union, | ||
Matt Harbison
|
r50688 | ) | ||
Gregory Szorc
|
r27360 | from .i18n import _ | ||
from . import ( | ||||
encoding, | ||||
Augie Fackler
|
r33724 | error, | ||
Yuya Nishihara
|
r32367 | policy, | ||
Pulkit Goyal
|
r30612 | pycompat, | ||
Matt Harbison
|
r50688 | typelib, | ||
Matt Harbison
|
r27436 | win32, | ||
Gregory Szorc
|
r27360 | ) | ||
Pulkit Goyal
|
r29760 | |||
Augie Fackler
|
r43906 | osutil = policy.importmod('osutil') | ||
Yuya Nishihara
|
r32367 | |||
Matt Harbison
|
r35531 | getfsmountpoint = win32.getvolumename | ||
Matt Harbison
|
r35528 | getfstype = win32.getfstype | ||
Matt Mackall
|
r15016 | getuser = win32.getuser | ||
hidewindow = win32.hidewindow | ||||
makedir = win32.makedir | ||||
nlinks = win32.nlinks | ||||
oslink = win32.oslink | ||||
samedevice = win32.samedevice | ||||
samefile = win32.samefile | ||||
setsignalhandler = win32.setsignalhandler | ||||
spawndetached = win32.spawndetached | ||||
Bryan O'Sullivan
|
r17560 | split = os.path.split | ||
Matt Mackall
|
r15016 | testpid = win32.testpid | ||
unlink = win32.unlink | ||||
Adrian Buehlmann
|
r14985 | |||
Matt Harbison
|
r50713 | if typing.TYPE_CHECKING: | ||
r52178 | ||||
Matt Harbison
|
r50713 | def split(p: bytes) -> Tuple[bytes, bytes]: | ||
raise NotImplementedError | ||||
Matt Harbison
|
r50712 | umask: int = 0o022 | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r49801 | class mixedfilemodewrapper: | ||
Gregory Szorc
|
r26375 | """Wraps a file handle when it is opened in read/write mode. | ||
fopen() and fdopen() on Windows have a specific-to-Windows requirement | ||||
that files opened with mode r+, w+, or a+ make a call to a file positioning | ||||
function when switching between reads and writes. Without this extra call, | ||||
Python will raise a not very intuitive "IOError: [Errno 0] Error." | ||||
This class wraps posixfile instances when the file is opened in read/write | ||||
mode and automatically adds checks or inserts appropriate file positioning | ||||
calls when necessary. | ||||
""" | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r26375 | OPNONE = 0 | ||
OPREAD = 1 | ||||
OPWRITE = 2 | ||||
def __init__(self, fp): | ||||
Augie Fackler
|
r43906 | object.__setattr__(self, '_fp', fp) | ||
object.__setattr__(self, '_lastop', 0) | ||||
Gregory Szorc
|
r26375 | |||
Matt Harbison
|
r31891 | def __enter__(self): | ||
Matt Harbison
|
r40974 | self._fp.__enter__() | ||
return self | ||||
Matt Harbison
|
r31891 | |||
def __exit__(self, exc_type, exc_val, exc_tb): | ||||
self._fp.__exit__(exc_type, exc_val, exc_tb) | ||||
Gregory Szorc
|
r26375 | def __getattr__(self, name): | ||
return getattr(self._fp, name) | ||||
def __setattr__(self, name, value): | ||||
return self._fp.__setattr__(name, value) | ||||
def _noopseek(self): | ||||
self._fp.seek(0, os.SEEK_CUR) | ||||
def seek(self, *args, **kwargs): | ||||
Augie Fackler
|
r43906 | object.__setattr__(self, '_lastop', self.OPNONE) | ||
Gregory Szorc
|
r26375 | return self._fp.seek(*args, **kwargs) | ||
def write(self, d): | ||||
if self._lastop == self.OPREAD: | ||||
self._noopseek() | ||||
Augie Fackler
|
r43906 | object.__setattr__(self, '_lastop', self.OPWRITE) | ||
Gregory Szorc
|
r26375 | return self._fp.write(d) | ||
def writelines(self, *args, **kwargs): | ||||
if self._lastop == self.OPREAD: | ||||
self._noopeseek() | ||||
Augie Fackler
|
r43906 | object.__setattr__(self, '_lastop', self.OPWRITE) | ||
Gregory Szorc
|
r26375 | return self._fp.writelines(*args, **kwargs) | ||
def read(self, *args, **kwargs): | ||||
if self._lastop == self.OPWRITE: | ||||
self._noopseek() | ||||
Augie Fackler
|
r43906 | object.__setattr__(self, '_lastop', self.OPREAD) | ||
Gregory Szorc
|
r26375 | return self._fp.read(*args, **kwargs) | ||
def readline(self, *args, **kwargs): | ||||
if self._lastop == self.OPWRITE: | ||||
self._noopseek() | ||||
Augie Fackler
|
r43906 | object.__setattr__(self, '_lastop', self.OPREAD) | ||
Gregory Szorc
|
r26375 | return self._fp.readline(*args, **kwargs) | ||
def readlines(self, *args, **kwargs): | ||||
if self._lastop == self.OPWRITE: | ||||
self._noopseek() | ||||
Augie Fackler
|
r43906 | object.__setattr__(self, '_lastop', self.OPREAD) | ||
Gregory Szorc
|
r26375 | return self._fp.readlines(*args, **kwargs) | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r49801 | class fdproxy: | ||
Matt Harbison
|
r39845 | """Wraps osutil.posixfile() to override the name attribute to reflect the | ||
underlying file name. | ||||
""" | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r39845 | def __init__(self, name, fp): | ||
self.name = name | ||||
self._fp = fp | ||||
def __enter__(self): | ||||
Matt Harbison
|
r40973 | self._fp.__enter__() | ||
# Return this wrapper for the context manager so that the name is | ||||
# still available. | ||||
return self | ||||
Matt Harbison
|
r39845 | |||
def __exit__(self, exc_type, exc_value, traceback): | ||||
self._fp.__exit__(exc_type, exc_value, traceback) | ||||
def __iter__(self): | ||||
return iter(self._fp) | ||||
def __getattr__(self, name): | ||||
return getattr(self._fp, name) | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | def posixfile(name, mode=b'r', buffering=-1): | ||
Adrian Buehlmann
|
r24069 | '''Open a file with even more POSIX-like semantics''' | ||
Sune Foldager
|
r8421 | try: | ||
Augie Fackler
|
r43346 | fp = osutil.posixfile(name, mode, buffering) # may raise WindowsError | ||
Matt Harbison
|
r24051 | |||
Matt Harbison
|
r39845 | # PyFile_FromFd() ignores the name, and seems to report fp.name as the | ||
# underlying file descriptor. | ||||
Gregory Szorc
|
r49753 | fp = fdproxy(name, fp) | ||
Matt Harbison
|
r39845 | |||
Matt Harbison
|
r24051 | # The position when opening in append mode is implementation defined, so | ||
# make it consistent with other platforms, which position at EOF. | ||||
Augie Fackler
|
r43347 | if b'a' in mode: | ||
Adrian Buehlmann
|
r25462 | fp.seek(0, os.SEEK_END) | ||
Matt Harbison
|
r24051 | |||
Augie Fackler
|
r43347 | if b'+' in mode: | ||
Gregory Szorc
|
r26375 | return mixedfilemodewrapper(fp) | ||
Matt Harbison
|
r24051 | return fp | ||
Matt Harbison
|
r48822 | except WindowsError as err: # pytype: disable=name-error | ||
Adrian Buehlmann
|
r24069 | # convert to a friendlier exception | ||
Augie Fackler
|
r43346 | raise IOError( | ||
Augie Fackler
|
r43906 | err.errno, '%s: %s' % (encoding.strfromlocal(name), err.strerror) | ||
Augie Fackler
|
r43346 | ) | ||
Matt Mackall
|
r7890 | |||
Yuya Nishihara
|
r32203 | # may be wrapped by win32mbcs extension | ||
listdir = osutil.listdir | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def get_password() -> bytes: | ||
Matt Harbison
|
r47949 | """Prompt for password with echo off, using Windows getch(). | ||
This shouldn't be called directly- use ``ui.getpass()`` instead, which | ||||
checks if the session is interactive first. | ||||
""" | ||||
Matt Harbison
|
r48593 | pw = u"" | ||
Matt Harbison
|
r47949 | while True: | ||
Matt Harbison
|
r48217 | c = msvcrt.getwch() # pytype: disable=module-attr | ||
Matt Harbison
|
r48593 | if c == u'\r' or c == u'\n': | ||
Matt Harbison
|
r47949 | break | ||
Matt Harbison
|
r48593 | if c == u'\003': | ||
Matt Harbison
|
r47949 | raise KeyboardInterrupt | ||
Matt Harbison
|
r48593 | if c == u'\b': | ||
Matt Harbison
|
r47949 | pw = pw[:-1] | ||
else: | ||||
pw = pw + c | ||||
Matt Harbison
|
r48593 | msvcrt.putwch(u'\r') # pytype: disable=module-attr | ||
msvcrt.putwch(u'\n') # pytype: disable=module-attr | ||||
return encoding.unitolocal(pw) | ||||
Matt Harbison
|
r47949 | |||
Matt Harbison
|
r50688 | class winstdout(typelib.BinaryIO_Proxy): | ||
Augie Fackler
|
r46554 | """Some files on Windows misbehave. | ||
Manuel Jacob
|
r45705 | |||
When writing to a broken pipe, EINVAL instead of EPIPE may be raised. | ||||
When writing too many bytes to a console at the same, a "Not enough space" | ||||
error may happen. Python 3 already works around that. | ||||
Augie Fackler
|
r46554 | """ | ||
Matt Mackall
|
r7890 | |||
Matt Harbison
|
r50688 | def __init__(self, fp: BinaryIO): | ||
Matt Mackall
|
r7890 | self.fp = fp | ||
def __getattr__(self, key): | ||||
return getattr(self.fp, key) | ||||
def close(self): | ||||
try: | ||||
self.fp.close() | ||||
Idan Kamara
|
r14004 | except IOError: | ||
pass | ||||
Matt Mackall
|
r7890 | |||
def write(self, s): | ||||
try: | ||||
Gregory Szorc
|
r49754 | return self.fp.write(s) | ||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Sune Foldager
|
r38575 | if inst.errno != 0 and not win32.lasterrorwaspipeerror(inst): | ||
Matt Mackall
|
r10282 | raise | ||
Matt Mackall
|
r7890 | self.close() | ||
Augie Fackler
|
r43906 | raise IOError(errno.EPIPE, 'Broken pipe') | ||
Matt Mackall
|
r7890 | |||
def flush(self): | ||||
try: | ||||
return self.fp.flush() | ||||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Sune Foldager
|
r38575 | if not win32.lasterrorwaspipeerror(inst): | ||
Matt Mackall
|
r10282 | raise | ||
Augie Fackler
|
r43906 | raise IOError(errno.EPIPE, 'Broken pipe') | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def openhardlinks() -> bool: | ||
Matt Harbison
|
r44374 | return True | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def parsepatchoutput(output_line: bytes) -> bytes: | ||
timeless
|
r8761 | """parses the output produced by patch and returns the filename""" | ||
Matt Mackall
|
r7890 | pf = output_line[14:] | ||
Augie Fackler
|
r43347 | if pf[0] == b'`': | ||
Augie Fackler
|
r43346 | pf = pf[1:-1] # Remove the quotes | ||
Matt Mackall
|
r7890 | return pf | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def sshargs( | ||
sshcmd: bytes, host: bytes, user: Optional[bytes], port: Optional[bytes] | ||||
) -> bytes: | ||||
Matt Mackall
|
r7890 | '''Build argument list for ssh or Plink''' | ||
Augie Fackler
|
r43347 | pflag = b'plink' in sshcmd.lower() and b'-P' or b'-p' | ||
args = user and (b"%s@%s" % (user, host)) or host | ||||
if args.startswith(b'-') or args.startswith(b'/'): | ||||
Augie Fackler
|
r33724 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b'illegal ssh hostname or username starting with - or /: %s') | ||
Augie Fackler
|
r43346 | % args | ||
) | ||||
Jun Wu
|
r33732 | args = shellquote(args) | ||
if port: | ||||
Augie Fackler
|
r43347 | args = b'%s %s %s' % (pflag, shellquote(port), args) | ||
Jun Wu
|
r33732 | return args | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def setflags(f: bytes, l: bool, x: bool) -> None: | ||
Adrian Buehlmann
|
r15011 | pass | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def copymode( | ||
src: bytes, | ||||
dst: bytes, | ||||
Matt Harbison
|
r52692 | mode: Optional[int] = None, | ||
Matt Harbison
|
r50707 | enforcewritable: bool = False, | ||
) -> None: | ||||
pass | ||||
def checkexec(path: bytes) -> bool: | ||||
Adrian Buehlmann
|
r13879 | return False | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def checklink(path: bytes) -> bool: | ||
Adrian Buehlmann
|
r13890 | return False | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def setbinary(fd) -> None: | ||
Matt Mackall
|
r7890 | # When run without console, pipes may expose invalid | ||
# fileno(), usually set to -1. | ||||
Augie Fackler
|
r14969 | fno = getattr(fd, 'fileno', None) | ||
if fno is not None and fno() >= 0: | ||||
Matt Harbison
|
r44207 | msvcrt.setmode(fno(), os.O_BINARY) # pytype: disable=module-attr | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def pconvert(path: bytes) -> bytes: | ||
Augie Fackler
|
r43347 | return path.replace(pycompat.ossep, b'/') | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def localpath(path: bytes) -> bytes: | ||
Augie Fackler
|
r43347 | return path.replace(b'/', b'\\') | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | def normpath(path: bytes) -> bytes: | ||
Matt Mackall
|
r7890 | return pconvert(os.path.normpath(path)) | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def normcase(path: bytes) -> bytes: | ||
Augie Fackler
|
r43346 | return encoding.upper(path) # NTFS compares via upper() | ||
Matt Mackall
|
r15488 | |||
Matt Harbison
|
r50712 | DRIVE_RE_B: Pattern[bytes] = re.compile(b'^[a-z]:') | ||
DRIVE_RE_S: Pattern[str] = re.compile('^[a-z]:') | ||||
r48422 | ||||
Matt Harbison
|
r50712 | # TODO: why is this accepting str? | ||
def abspath(path: AnyStr) -> AnyStr: | ||||
r48422 | abs_path = os.path.abspath(path) # re-exports | |||
# Python on Windows is inconsistent regarding the capitalization of drive | ||||
# letter and this cause issue with various path comparison along the way. | ||||
# So we normalize the drive later to upper case here. | ||||
# | ||||
# See https://bugs.python.org/issue40368 for and example of this hell. | ||||
if isinstance(abs_path, bytes): | ||||
if DRIVE_RE_B.match(abs_path): | ||||
abs_path = abs_path[0:1].upper() + abs_path[1:] | ||||
elif DRIVE_RE_S.match(abs_path): | ||||
abs_path = abs_path[0:1].upper() + abs_path[1:] | ||||
return abs_path | ||||
Siddharth Agarwal
|
r24598 | # see posix.py for definitions | ||
Matt Harbison
|
r50712 | normcasespec: int = encoding.normcasespecs.upper | ||
Siddharth Agarwal
|
r24598 | normcasefallback = encoding.upperfallback | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | def samestat(s1: os.stat_result, s2: os.stat_result) -> bool: | ||
Matt Mackall
|
r7890 | return False | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | def shelltocmdexe(path: bytes, env: Mapping[bytes, bytes]) -> bytes: | ||
Matt Harbison
|
r38502 | r"""Convert shell variables in the form $var and ${var} inside ``path`` | ||
to %var% form. Existing Windows style variables are left unchanged. | ||||
The variables are limited to the given environment. Unknown variables are | ||||
left unchanged. | ||||
>>> e = {b'var1': b'v1', b'var2': b'v2', b'var3': b'v3'} | ||||
>>> # Only valid values are expanded | ||||
>>> shelltocmdexe(b'cmd $var1 ${var2} %var3% $missing ${missing} %missing%', | ||||
... e) | ||||
'cmd %var1% %var2% %var3% $missing ${missing} %missing%' | ||||
>>> # Single quote prevents expansion, as does \$ escaping | ||||
>>> shelltocmdexe(b"cmd '$var1 ${var2} %var3%' \$var1 \${var2} \\", e) | ||||
Matt Harbison
|
r38747 | 'cmd "$var1 ${var2} %var3%" $var1 ${var2} \\' | ||
Matt Harbison
|
r38646 | >>> # $$ is not special. %% is not special either, but can be the end and | ||
>>> # start of consecutive variables | ||||
Matt Harbison
|
r38502 | >>> shelltocmdexe(b"cmd $$ %% %var1%%var2%", e) | ||
Matt Harbison
|
r38646 | 'cmd $$ %% %var1%%var2%' | ||
Matt Harbison
|
r38502 | >>> # No double substitution | ||
>>> shelltocmdexe(b"$var1 %var1%", {b'var1': b'%var2%', b'var2': b'boom'}) | ||||
'%var1% %var1%' | ||||
Matt Harbison
|
r38748 | >>> # Tilde expansion | ||
>>> shelltocmdexe(b"~/dir ~\dir2 ~tmpfile \~/", {}) | ||||
'%USERPROFILE%/dir %USERPROFILE%\\dir2 ~tmpfile ~/' | ||||
Matt Harbison
|
r38502 | """ | ||
Matt Harbison
|
r38748 | if not any(c in path for c in b"$'~"): | ||
Matt Harbison
|
r38502 | return path | ||
varchars = pycompat.sysbytes(string.ascii_letters + string.digits) + b'_-' | ||||
res = b'' | ||||
index = 0 | ||||
pathlen = len(path) | ||||
while index < pathlen: | ||||
Augie Fackler
|
r43346 | c = path[index : index + 1] | ||
if c == b'\'': # no expansion within single quotes | ||||
path = path[index + 1 :] | ||||
Matt Harbison
|
r38502 | pathlen = len(path) | ||
try: | ||||
index = path.index(b'\'') | ||||
Matt Harbison
|
r38747 | res += b'"' + path[:index] + b'"' | ||
Matt Harbison
|
r38502 | except ValueError: | ||
res += c + path | ||||
index = pathlen - 1 | ||||
elif c == b'%': # variable | ||||
Augie Fackler
|
r43346 | path = path[index + 1 :] | ||
Matt Harbison
|
r38502 | pathlen = len(path) | ||
try: | ||||
index = path.index(b'%') | ||||
except ValueError: | ||||
res += b'%' + path | ||||
index = pathlen - 1 | ||||
else: | ||||
var = path[:index] | ||||
res += b'%' + var + b'%' | ||||
Matt Harbison
|
r38646 | elif c == b'$': # variable | ||
Augie Fackler
|
r43346 | if path[index + 1 : index + 2] == b'{': | ||
path = path[index + 2 :] | ||||
Matt Harbison
|
r38502 | pathlen = len(path) | ||
try: | ||||
index = path.index(b'}') | ||||
var = path[:index] | ||||
# See below for why empty variables are handled specially | ||||
Matt Harbison
|
r39946 | if env.get(var, b'') != b'': | ||
Matt Harbison
|
r38502 | res += b'%' + var + b'%' | ||
else: | ||||
res += b'${' + var + b'}' | ||||
except ValueError: | ||||
res += b'${' + path | ||||
index = pathlen - 1 | ||||
else: | ||||
var = b'' | ||||
index += 1 | ||||
Augie Fackler
|
r43346 | c = path[index : index + 1] | ||
Matt Harbison
|
r38502 | while c != b'' and c in varchars: | ||
var += c | ||||
index += 1 | ||||
Augie Fackler
|
r43346 | c = path[index : index + 1] | ||
Matt Harbison
|
r38502 | # Some variables (like HG_OLDNODE) may be defined, but have an | ||
# empty value. Those need to be skipped because when spawning | ||||
# cmd.exe to run the hook, it doesn't replace %VAR% for an empty | ||||
# VAR, and that really confuses things like revset expressions. | ||||
# OTOH, if it's left in Unix format and the hook runs sh.exe, it | ||||
# will substitute to an empty string, and everything is happy. | ||||
Matt Harbison
|
r39946 | if env.get(var, b'') != b'': | ||
Matt Harbison
|
r38502 | res += b'%' + var + b'%' | ||
else: | ||||
res += b'$' + var | ||||
Matt Harbison
|
r39946 | if c != b'': | ||
Matt Harbison
|
r38502 | index -= 1 | ||
Augie Fackler
|
r43346 | elif ( | ||
c == b'~' | ||||
and index + 1 < pathlen | ||||
and path[index + 1 : index + 2] in (b'\\', b'/') | ||||
): | ||||
Augie Fackler
|
r43347 | res += b"%USERPROFILE%" | ||
Augie Fackler
|
r43346 | elif ( | ||
c == b'\\' | ||||
and index + 1 < pathlen | ||||
and path[index + 1 : index + 2] in (b'$', b'~') | ||||
): | ||||
Matt Harbison
|
r38748 | # Skip '\', but only if it is escaping $ or ~ | ||
Augie Fackler
|
r43346 | res += path[index + 1 : index + 2] | ||
Matt Harbison
|
r38502 | index += 1 | ||
else: | ||||
res += c | ||||
index += 1 | ||||
return res | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r7890 | # A sequence of backslashes is special iff it precedes a double quote: | ||
# - if there's an even number of backslashes, the double quote is not | ||||
# quoted (i.e. it ends the quoted region) | ||||
# - if there's an odd number of backslashes, the double quote is quoted | ||||
# - in both cases, every pair of backslashes is unquoted into a single | ||||
# backslash | ||||
# (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx ) | ||||
# So, to quote a string, we must surround it in double quotes, double | ||||
timeless@mozdev.org
|
r17505 | # the number of backslashes that precede double quotes and add another | ||
Matt Mackall
|
r7890 | # backslash before every double quote (being careful with the double | ||
# quote we've appended to the end) | ||||
Matt Harbison
|
r50712 | _quotere: Optional[Pattern[bytes]] = None | ||
FUJIWARA Katsunori
|
r23682 | _needsshellquote = None | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def shellquote(s: bytes) -> bytes: | ||
Matt Harbison
|
r24908 | r""" | ||
Yuya Nishihara
|
r34133 | >>> shellquote(br'C:\Users\xyz') | ||
Matt Harbison
|
r24908 | '"C:\\Users\\xyz"' | ||
Yuya Nishihara
|
r34133 | >>> shellquote(br'C:\Users\xyz/mixed') | ||
Matt Harbison
|
r24908 | '"C:\\Users\\xyz/mixed"' | ||
>>> # Would be safe not to quote too, since it is all double backslashes | ||||
Yuya Nishihara
|
r34133 | >>> shellquote(br'C:\\Users\\xyz') | ||
Matt Harbison
|
r24908 | '"C:\\\\Users\\\\xyz"' | ||
>>> # But this must be quoted | ||||
Yuya Nishihara
|
r34133 | >>> shellquote(br'C:\\Users\\xyz/abc') | ||
Matt Harbison
|
r24908 | '"C:\\\\Users\\\\xyz/abc"' | ||
""" | ||||
Matt Mackall
|
r7890 | global _quotere | ||
if _quotere is None: | ||||
Matt Harbison
|
r39680 | _quotere = re.compile(br'(\\*)("|\\$)') | ||
FUJIWARA Katsunori
|
r23682 | global _needsshellquote | ||
if _needsshellquote is None: | ||||
Matt Harbison
|
r24885 | # ":" is also treated as "safe character", because it is used as a part | ||
# of path name on Windows. "\" is also part of a path name, but isn't | ||||
# safe because shlex.split() (kind of) treats it as an escape char and | ||||
# drops it. It will leave the next character, even if it is another | ||||
# "\". | ||||
Matt Harbison
|
r39680 | _needsshellquote = re.compile(br'[^a-zA-Z0-9._:/-]').search | ||
Yuya Nishihara
|
r24108 | if s and not _needsshellquote(s) and not _quotere.search(s): | ||
FUJIWARA Katsunori
|
r23682 | # "s" shouldn't have to be quoted | ||
return s | ||||
Matt Harbison
|
r39680 | return b'"%s"' % _quotere.sub(br'\1\1\\\2', s) | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | def _unquote(s: bytes) -> bytes: | ||
Yuya Nishihara
|
r36433 | if s.startswith(b'"') and s.endswith(b'"'): | ||
return s[1:-1] | ||||
return s | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def shellsplit(s: bytes) -> List[bytes]: | ||
Yuya Nishihara
|
r36433 | """Parse a command string in cmd.exe way (best-effort)""" | ||
return pycompat.maplist(_unquote, pycompat.shlexsplit(s, posix=False)) | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r7890 | # if you change this stub into a real check, please try to implement the | ||
# username and groupname functions above, too. | ||||
Matt Harbison
|
r50707 | def isowner(st: os.stat_result) -> bool: | ||
Matt Mackall
|
r7890 | return True | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def findexe(command: bytes) -> Optional[bytes]: | ||
Augie Fackler
|
r46554 | """Find executable for command searching like cmd.exe does. | ||
Matt Mackall
|
r7890 | If command is a basename then PATH is searched for command. | ||
PATH isn't searched if command is an absolute or relative path. | ||||
An extension from PATHEXT is found and added if not present. | ||||
Augie Fackler
|
r46554 | If command isn't found None is returned.""" | ||
Augie Fackler
|
r43347 | pathext = encoding.environ.get(b'PATHEXT', b'.COM;.EXE;.BAT;.CMD') | ||
Pulkit Goyal
|
r30612 | pathexts = [ext for ext in pathext.lower().split(pycompat.ospathsep)] | ||
Matt Mackall
|
r7890 | if os.path.splitext(command)[1].lower() in pathexts: | ||
Augie Fackler
|
r43347 | pathexts = [b''] | ||
Matt Mackall
|
r7890 | |||
Matt Harbison
|
r50707 | def findexisting(pathcommand: bytes) -> Optional[bytes]: | ||
Matt Harbison
|
r44226 | """Will append extension (if needed) and return existing file""" | ||
Matt Mackall
|
r7890 | for ext in pathexts: | ||
executable = pathcommand + ext | ||||
if os.path.exists(executable): | ||||
return executable | ||||
return None | ||||
Pulkit Goyal
|
r30615 | if pycompat.ossep in command: | ||
Matt Mackall
|
r7890 | return findexisting(command) | ||
Augie Fackler
|
r43347 | for path in encoding.environ.get(b'PATH', b'').split(pycompat.ospathsep): | ||
Matt Mackall
|
r7890 | executable = findexisting(os.path.join(path, command)) | ||
if executable is not None: | ||||
return executable | ||||
Steve Borho
|
r10156 | return findexisting(os.path.expanduser(os.path.expandvars(command))) | ||
Matt Mackall
|
r7890 | |||
Augie Fackler
|
r43346 | |||
Martin von Zweigbergk
|
r32291 | _wantedkinds = {stat.S_IFREG, stat.S_IFLNK} | ||
Bryan O'Sullivan
|
r18017 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def statfiles(files: Sequence[bytes]) -> Iterator[Optional[os.stat_result]]: | ||
Augie Fackler
|
r46554 | """Stat each file in files. Yield each stat, or None if a file | ||
Bryan O'Sullivan
|
r18017 | does not exist or has a type we don't care about. | ||
Augie Fackler
|
r46554 | Cluster and cache stat per directory to minimize number of OS stat calls.""" | ||
Augie Fackler
|
r43346 | dircache = {} # dirname -> filename -> status | None if file does not exist | ||
Bryan O'Sullivan
|
r18017 | getkind = stat.S_IFMT | ||
Matt Mackall
|
r7890 | for nf in files: | ||
Augie Fackler
|
r43346 | nf = normcase(nf) | ||
Shun-ichi GOTO
|
r9099 | dir, base = os.path.split(nf) | ||
if not dir: | ||||
Augie Fackler
|
r43347 | dir = b'.' | ||
Matt Mackall
|
r7890 | cache = dircache.get(dir, None) | ||
if cache is None: | ||||
try: | ||||
Augie Fackler
|
r44937 | dmap = { | ||
normcase(n): s | ||||
for n, k, s in listdir(dir, True) | ||||
if getkind(s.st_mode) in _wantedkinds | ||||
} | ||||
Manuel Jacob
|
r50206 | except (FileNotFoundError, NotADirectoryError): | ||
Matt Mackall
|
r7890 | dmap = {} | ||
cache = dircache.setdefault(dir, dmap) | ||||
yield cache.get(base, None) | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def username(uid: Optional[int] = None) -> Optional[bytes]: | ||
Matt Mackall
|
r7890 | """Return the name of the user with the given uid. | ||
If uid is None, return the name of the current user.""" | ||||
Augie Fackler
|
r44356 | if not uid: | ||
Matt Harbison
|
r50414 | try: | ||
return pycompat.fsencode(getpass.getuser()) | ||||
except ModuleNotFoundError: | ||||
# getpass.getuser() checks for a few environment variables first, | ||||
# but if those aren't set, imports pwd and calls getpwuid(), none of | ||||
# which exists on Windows. | ||||
pass | ||||
Matt Mackall
|
r7890 | return None | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def groupname(gid: Optional[int] = None) -> Optional[bytes]: | ||
Matt Mackall
|
r7890 | """Return the name of the group with the given gid. | ||
If gid is None, return the name of the current group.""" | ||||
return None | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r52613 | def readlink(path: bytes) -> bytes: | ||
path_str = pycompat.fsdecode(path) | ||||
Matt Harbison
|
r48680 | try: | ||
Matt Harbison
|
r52613 | link = os.readlink(path_str) | ||
Matt Harbison
|
r48680 | except ValueError as e: | ||
# On py2, os.readlink() raises an AttributeError since it is | ||||
# unsupported. On py3, reading a non-link raises a ValueError. Simply | ||||
# treat this as the error the locking code has been expecting up to now | ||||
# until an effort can be made to enable symlink support on Windows. | ||||
raise AttributeError(e) | ||||
return pycompat.fsencode(link) | ||||
Matt Harbison
|
r39940 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | def removedirs(name: bytes) -> None: | ||
Henrik Stuart
|
r8364 | """special version of os.removedirs that does not remove symlinked | ||
directories or junction points if they actually contain files""" | ||||
Yuya Nishihara
|
r32203 | if listdir(name): | ||
Henrik Stuart
|
r8364 | return | ||
os.rmdir(name) | ||||
head, tail = os.path.split(name) | ||||
if not tail: | ||||
head, tail = os.path.split(head) | ||||
while head and tail: | ||||
try: | ||||
Yuya Nishihara
|
r32203 | if listdir(head): | ||
Henrik Stuart
|
r8364 | return | ||
os.rmdir(head) | ||||
Idan Kamara
|
r14004 | except (ValueError, OSError): | ||
Henrik Stuart
|
r8364 | break | ||
head, tail = os.path.split(head) | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | def rename(src: bytes, dst: bytes) -> None: | ||
Adrian Buehlmann
|
r9549 | '''atomically rename file src to dst, replacing dst if it exists''' | ||
try: | ||||
os.rename(src, dst) | ||||
Manuel Jacob
|
r50200 | except FileExistsError: | ||
Adrian Buehlmann
|
r13280 | unlink(dst) | ||
Adrian Buehlmann
|
r9549 | os.rename(src, dst) | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | def gethgcmd() -> List[bytes]: | ||
Matt Harbison
|
r39755 | return [encoding.strtolocal(arg) for arg in [sys.executable] + sys.argv[:1]] | ||
Patrick Mezard
|
r10239 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def groupmembers(name: bytes) -> List[bytes]: | ||
Patrick Mezard
|
r11138 | # Don't support groups on Windows for now | ||
Brodie Rao
|
r16687 | raise KeyError | ||
Patrick Mezard
|
r11138 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def isexec(f: bytes) -> bool: | ||
Adrian Buehlmann
|
r14926 | return False | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r49801 | class cachestat: | ||
Matt Harbison
|
r52917 | stat: os.stat_result | ||
Matt Harbison
|
r50710 | def __init__(self, path: bytes) -> None: | ||
Matt Harbison
|
r52917 | self.stat = os.stat(path) | ||
Idan Kamara
|
r14927 | |||
Matt Harbison
|
r50710 | def cacheable(self) -> bool: | ||
Matt Harbison
|
r52917 | return bool(self.stat.st_ino) | ||
__hash__ = object.__hash__ | ||||
def __eq__(self, other: Any) -> bool: | ||||
try: | ||||
# Only dev, ino, size, mtime and atime are likely to change. Out | ||||
# of these, we shouldn't compare atime but should compare the | ||||
# rest. However, one of the other fields changing indicates | ||||
# something fishy going on, so return False if anything but atime | ||||
# changes. | ||||
return ( | ||||
self.stat.st_ino == other.stat.st_ino | ||||
and self.stat.st_dev == other.stat.st_dev | ||||
and self.stat.st_nlink == other.stat.st_nlink | ||||
and self.stat.st_uid == other.stat.st_uid | ||||
and self.stat.st_gid == other.stat.st_gid | ||||
and self.stat.st_size == other.stat.st_size | ||||
and self.stat[stat.ST_MTIME] == other.stat[stat.ST_MTIME] | ||||
and self.stat[stat.ST_CTIME] == other.stat[stat.ST_CTIME] | ||||
) | ||||
except AttributeError: | ||||
return False | ||||
def __ne__(self, other: Any) -> bool: | ||||
return not self == other | ||||
Idan Kamara
|
r14927 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def lookupreg( | ||
key: bytes, | ||||
valname: Optional[bytes] = None, | ||||
scope: Optional[Union[int, Iterable[int]]] = None, | ||||
) -> Optional[bytes]: | ||||
Augie Fackler
|
r46554 | """Look up a key/value name in the Windows registry. | ||
Adrian Buehlmann
|
r16807 | |||
valname: value name. If unspecified, the default value for the key | ||||
is used. | ||||
scope: optionally specify scope for registry lookup, this can be | ||||
a sequence of scopes to look up in order. Default (CURRENT_USER, | ||||
LOCAL_MACHINE). | ||||
Augie Fackler
|
r46554 | """ | ||
Adrian Buehlmann
|
r16807 | if scope is None: | ||
Matt Harbison
|
r49843 | # pytype: disable=module-attr | ||
Pulkit Goyal
|
r29760 | scope = (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE) | ||
Matt Harbison
|
r49843 | # pytype: enable=module-attr | ||
Adrian Buehlmann
|
r16807 | elif not isinstance(scope, (list, tuple)): | ||
scope = (scope,) | ||||
for s in scope: | ||||
try: | ||||
Matt Harbison
|
r49843 | # pytype: disable=module-attr | ||
Matt Harbison
|
r39679 | with winreg.OpenKey(s, encoding.strfromlocal(key)) as hkey: | ||
Matt Harbison
|
r49843 | # pytype: enable=module-attr | ||
Matt Harbison
|
r50212 | name = None | ||
if valname is not None: | ||||
name = encoding.strfromlocal(valname) | ||||
Matt Harbison
|
r49843 | # pytype: disable=module-attr | ||
Matt Harbison
|
r40302 | val = winreg.QueryValueEx(hkey, name)[0] | ||
Matt Harbison
|
r49843 | # pytype: enable=module-attr | ||
Matt Harbison
|
r39679 | # never let a Unicode string escape into the wild | ||
return encoding.unitolocal(val) | ||||
Adrian Buehlmann
|
r16807 | except EnvironmentError: | ||
pass | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | expandglobs: bool = True | ||
Bryan O'Sullivan
|
r18868 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def statislink(st: Optional[os.stat_result]) -> bool: | ||
Bryan O'Sullivan
|
r18868 | '''check whether a stat result is a symlink''' | ||
return False | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def statisexec(st: Optional[os.stat_result]) -> bool: | ||
Bryan O'Sullivan
|
r18868 | '''check whether a stat result is an executable file''' | ||
return False | ||||
Gregory Szorc
|
r22245 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50712 | def poll(fds) -> List: | ||
Pierre-Yves David
|
r25420 | # see posix.py for description | ||
raise NotImplementedError() | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def readpipe(pipe) -> bytes: | ||
Gregory Szorc
|
r22245 | """Read all available data from a pipe.""" | ||
chunks = [] | ||||
while True: | ||||
Matt Harbison
|
r24653 | size = win32.peekpipe(pipe) | ||
Gregory Szorc
|
r22245 | if not size: | ||
break | ||||
s = pipe.read(size) | ||||
if not s: | ||||
break | ||||
chunks.append(s) | ||||
Augie Fackler
|
r43347 | return b''.join(chunks) | ||
Yuya Nishihara
|
r29530 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r50707 | def bindunixsocket(sock, path: bytes) -> NoReturn: | ||
Augie Fackler
|
r43906 | raise NotImplementedError('unsupported platform') | ||