windows.py
476 lines
| 14.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / windows.py
Martin Geisler
|
r8226 | # windows.py - Windows utility function implementations for Mercurial | ||
# | ||||
# Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others | ||||
# | ||||
# 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 | |||
Gregory Szorc
|
r27360 | from __future__ import absolute_import | ||
Sune Foldager
|
r8421 | |||
Gregory Szorc
|
r27360 | import errno | ||
import msvcrt | ||||
import os | ||||
import re | ||||
import stat | ||||
import sys | ||||
from .i18n import _ | ||||
from . import ( | ||||
encoding, | ||||
Yuya Nishihara
|
r32367 | policy, | ||
Pulkit Goyal
|
r30612 | pycompat, | ||
Matt Harbison
|
r27436 | win32, | ||
Gregory Szorc
|
r27360 | ) | ||
Pulkit Goyal
|
r29760 | try: | ||
import _winreg as winreg | ||||
winreg.CloseKey | ||||
except ImportError: | ||||
import winreg | ||||
Yuya Nishihara
|
r32367 | osutil = policy.importmod(r'osutil') | ||
Matt Mackall
|
r15016 | executablepath = win32.executablepath | ||
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 | |||
Gregory Szorc
|
r25658 | umask = 0o022 | ||
Matt Mackall
|
r7890 | |||
Gregory Szorc
|
r26375 | class mixedfilemodewrapper(object): | ||
"""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. | ||||
""" | ||||
OPNONE = 0 | ||||
OPREAD = 1 | ||||
OPWRITE = 2 | ||||
def __init__(self, fp): | ||||
Yuya Nishihara
|
r31644 | object.__setattr__(self, r'_fp', fp) | ||
object.__setattr__(self, r'_lastop', 0) | ||||
Gregory Szorc
|
r26375 | |||
Matt Harbison
|
r31891 | def __enter__(self): | ||
return self._fp.__enter__() | ||||
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): | ||||
Yuya Nishihara
|
r31644 | object.__setattr__(self, r'_lastop', self.OPNONE) | ||
Gregory Szorc
|
r26375 | return self._fp.seek(*args, **kwargs) | ||
def write(self, d): | ||||
if self._lastop == self.OPREAD: | ||||
self._noopseek() | ||||
Yuya Nishihara
|
r31644 | object.__setattr__(self, r'_lastop', self.OPWRITE) | ||
Gregory Szorc
|
r26375 | return self._fp.write(d) | ||
def writelines(self, *args, **kwargs): | ||||
if self._lastop == self.OPREAD: | ||||
self._noopeseek() | ||||
Yuya Nishihara
|
r31644 | object.__setattr__(self, r'_lastop', self.OPWRITE) | ||
Gregory Szorc
|
r26375 | return self._fp.writelines(*args, **kwargs) | ||
def read(self, *args, **kwargs): | ||||
if self._lastop == self.OPWRITE: | ||||
self._noopseek() | ||||
Yuya Nishihara
|
r31644 | object.__setattr__(self, r'_lastop', self.OPREAD) | ||
Gregory Szorc
|
r26375 | return self._fp.read(*args, **kwargs) | ||
def readline(self, *args, **kwargs): | ||||
if self._lastop == self.OPWRITE: | ||||
self._noopseek() | ||||
Yuya Nishihara
|
r31644 | object.__setattr__(self, r'_lastop', self.OPREAD) | ||
Gregory Szorc
|
r26375 | return self._fp.readline(*args, **kwargs) | ||
def readlines(self, *args, **kwargs): | ||||
if self._lastop == self.OPWRITE: | ||||
self._noopseek() | ||||
Yuya Nishihara
|
r31644 | object.__setattr__(self, r'_lastop', self.OPREAD) | ||
Gregory Szorc
|
r26375 | return self._fp.readlines(*args, **kwargs) | ||
Sune Foldager
|
r8421 | def posixfile(name, mode='r', buffering=-1): | ||
Adrian Buehlmann
|
r24069 | '''Open a file with even more POSIX-like semantics''' | ||
Sune Foldager
|
r8421 | try: | ||
Adrian Buehlmann
|
r24069 | fp = osutil.posixfile(name, mode, buffering) # may raise WindowsError | ||
Matt Harbison
|
r24051 | |||
# The position when opening in append mode is implementation defined, so | ||||
# make it consistent with other platforms, which position at EOF. | ||||
if 'a' in mode: | ||||
Adrian Buehlmann
|
r25462 | fp.seek(0, os.SEEK_END) | ||
Matt Harbison
|
r24051 | |||
Gregory Szorc
|
r26375 | if '+' in mode: | ||
return mixedfilemodewrapper(fp) | ||||
Matt Harbison
|
r24051 | return fp | ||
Gregory Szorc
|
r25660 | except WindowsError as err: | ||
Adrian Buehlmann
|
r24069 | # convert to a friendlier exception | ||
Steve Borho
|
r9448 | raise IOError(err.errno, '%s: %s' % (name, err.strerror)) | ||
Matt Mackall
|
r7890 | |||
Yuya Nishihara
|
r32203 | # may be wrapped by win32mbcs extension | ||
listdir = osutil.listdir | ||||
Benoit Boissinot
|
r8778 | class winstdout(object): | ||
Matt Mackall
|
r7890 | '''stdout on windows misbehaves if sent through a pipe''' | ||
def __init__(self, fp): | ||||
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: | ||||
# This is workaround for "Not enough space" error on | ||||
# writing large size of data to console. | ||||
limit = 16000 | ||||
l = len(s) | ||||
start = 0 | ||||
Benoit Boissinot
|
r10394 | self.softspace = 0 | ||
Matt Mackall
|
r7890 | while start < l: | ||
end = start + limit | ||||
self.fp.write(s[start:end]) | ||||
start = end | ||||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Matt Mackall
|
r10282 | if inst.errno != 0: | ||
raise | ||||
Matt Mackall
|
r7890 | self.close() | ||
raise IOError(errno.EPIPE, 'Broken pipe') | ||||
def flush(self): | ||||
try: | ||||
return self.fp.flush() | ||||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Matt Mackall
|
r10282 | if inst.errno != errno.EINVAL: | ||
raise | ||||
Matt Mackall
|
r7890 | raise IOError(errno.EPIPE, 'Broken pipe') | ||
def _is_win_9x(): | ||||
'''return true if run on windows 95, 98 or me.''' | ||||
try: | ||||
return sys.getwindowsversion()[3] == 1 | ||||
except AttributeError: | ||||
Pulkit Goyal
|
r30636 | return 'command' in encoding.environ.get('comspec', '') | ||
Matt Mackall
|
r7890 | |||
def openhardlinks(): | ||||
Adrian Buehlmann
|
r13375 | return not _is_win_9x() | ||
Matt Mackall
|
r7890 | |||
Adrian Buehlmann
|
r14231 | def parsepatchoutput(output_line): | ||
timeless
|
r8761 | """parses the output produced by patch and returns the filename""" | ||
Matt Mackall
|
r7890 | pf = output_line[14:] | ||
if pf[0] == '`': | ||||
pf = pf[1:-1] # Remove the quotes | ||||
return pf | ||||
def sshargs(sshcmd, host, user, port): | ||||
'''Build argument list for ssh or Plink''' | ||||
pflag = 'plink' in sshcmd.lower() and '-P' or '-p' | ||||
args = user and ("%s@%s" % (user, host)) or host | ||||
return port and ("%s %s %s" % (args, pflag, port)) or args | ||||
Adrian Buehlmann
|
r14232 | def setflags(f, l, x): | ||
Matt Mackall
|
r7890 | pass | ||
Adrian Buehlmann
|
r15011 | def copymode(src, dst, mode=None): | ||
pass | ||||
Adrian Buehlmann
|
r13879 | def checkexec(path): | ||
return False | ||||
Adrian Buehlmann
|
r13890 | def checklink(path): | ||
return False | ||||
Adrian Buehlmann
|
r14233 | def setbinary(fd): | ||
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: | ||||
msvcrt.setmode(fno(), os.O_BINARY) | ||||
Matt Mackall
|
r7890 | |||
def pconvert(path): | ||||
Pulkit Goyal
|
r30615 | return path.replace(pycompat.ossep, '/') | ||
Matt Mackall
|
r7890 | |||
def localpath(path): | ||||
return path.replace('/', '\\') | ||||
def normpath(path): | ||||
return pconvert(os.path.normpath(path)) | ||||
FUJIWARA Katsunori
|
r15671 | def normcase(path): | ||
Adrian Buehlmann
|
r25071 | return encoding.upper(path) # NTFS compares via upper() | ||
Matt Mackall
|
r15488 | |||
Siddharth Agarwal
|
r24598 | # see posix.py for definitions | ||
normcasespec = encoding.normcasespecs.upper | ||||
normcasefallback = encoding.upperfallback | ||||
Matt Mackall
|
r7890 | def samestat(s1, s2): | ||
return False | ||||
# 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) | ||||
_quotere = None | ||||
FUJIWARA Katsunori
|
r23682 | _needsshellquote = None | ||
Matt Mackall
|
r7890 | def shellquote(s): | ||
Matt Harbison
|
r24908 | r""" | ||
>>> shellquote(r'C:\Users\xyz') | ||||
'"C:\\Users\\xyz"' | ||||
>>> shellquote(r'C:\Users\xyz/mixed') | ||||
'"C:\\Users\\xyz/mixed"' | ||||
>>> # Would be safe not to quote too, since it is all double backslashes | ||||
>>> shellquote(r'C:\\Users\\xyz') | ||||
'"C:\\\\Users\\\\xyz"' | ||||
>>> # But this must be quoted | ||||
>>> shellquote(r'C:\\Users\\xyz/abc') | ||||
'"C:\\\\Users\\\\xyz/abc"' | ||||
""" | ||||
Matt Mackall
|
r7890 | global _quotere | ||
if _quotere is None: | ||||
_quotere = re.compile(r'(\\*)("|\\$)') | ||||
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 | ||||
# "\". | ||||
_needsshellquote = re.compile(r'[^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 Mackall
|
r7890 | return '"%s"' % _quotere.sub(r'\1\1\\\2', s) | ||
def quotecommand(cmd): | ||||
"""Build a command string suitable for os.popen* calls.""" | ||||
Steve Borho
|
r13188 | if sys.version_info < (2, 7, 1): | ||
# Python versions since 2.7.1 do this extra quoting themselves | ||||
return '"' + cmd + '"' | ||||
return cmd | ||||
Matt Mackall
|
r7890 | |||
def popen(command, mode='r'): | ||||
# Work around "popen spawned process may not write to stdout | ||||
# under windows" | ||||
# http://bugs.python.org/issue1366 | ||||
Ross Lagerwall
|
r17391 | command += " 2> %s" % os.devnull | ||
Matt Mackall
|
r7890 | return os.popen(quotecommand(command), mode) | ||
Adrian Buehlmann
|
r14234 | def explainexit(code): | ||
Matt Mackall
|
r7890 | return _("exited with status %d") % code, code | ||
# if you change this stub into a real check, please try to implement the | ||||
# username and groupname functions above, too. | ||||
Martin Geisler
|
r8657 | def isowner(st): | ||
Matt Mackall
|
r7890 | return True | ||
Adrian Buehlmann
|
r14271 | def findexe(command): | ||
Matt Mackall
|
r7890 | '''Find executable for command searching like cmd.exe does. | ||
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. | ||||
If command isn't found None is returned.''' | ||||
Pulkit Goyal
|
r30636 | pathext = encoding.environ.get('PATHEXT', '.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: | ||
pathexts = [''] | ||||
def findexisting(pathcommand): | ||||
'Will append extension (if needed) and return existing file' | ||||
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) | ||
Pulkit Goyal
|
r30636 | for path in encoding.environ.get('PATH', '').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 | |||
Martin von Zweigbergk
|
r32291 | _wantedkinds = {stat.S_IFREG, stat.S_IFLNK} | ||
Bryan O'Sullivan
|
r18017 | |||
Matt Mackall
|
r7890 | def statfiles(files): | ||
Bryan O'Sullivan
|
r18017 | '''Stat each file in files. Yield each stat, or None if a file | ||
does not exist or has a type we don't care about. | ||||
Matt Mackall
|
r7890 | Cluster and cache stat per directory to minimize number of OS stat calls.''' | ||
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: | ||
FUJIWARA Katsunori
|
r15637 | nf = normcase(nf) | ||
Shun-ichi GOTO
|
r9099 | dir, base = os.path.split(nf) | ||
if not dir: | ||||
dir = '.' | ||||
Matt Mackall
|
r7890 | cache = dircache.get(dir, None) | ||
if cache is None: | ||||
try: | ||||
FUJIWARA Katsunori
|
r15637 | dmap = dict([(normcase(n), s) | ||
Yuya Nishihara
|
r32203 | for n, k, s in listdir(dir, True) | ||
Matt Mackall
|
r18041 | if getkind(s.st_mode) in _wantedkinds]) | ||
Gregory Szorc
|
r25660 | except OSError as err: | ||
Matt Mackall
|
r7890 | # Python >= 2.5 returns ENOENT and adds winerror field | ||
# EINVAL is raised if dir is not a directory. | ||||
Pierre-Yves David
|
r25204 | if err.errno not in (errno.ENOENT, errno.EINVAL, | ||
Matt Mackall
|
r7890 | errno.ENOTDIR): | ||
raise | ||||
dmap = {} | ||||
cache = dircache.setdefault(dir, dmap) | ||||
yield cache.get(base, None) | ||||
def username(uid=None): | ||||
"""Return the name of the user with the given uid. | ||||
If uid is None, return the name of the current user.""" | ||||
return None | ||||
def groupname(gid=None): | ||||
"""Return the name of the group with the given gid. | ||||
If gid is None, return the name of the current group.""" | ||||
return None | ||||
FUJIWARA Katsunori
|
r24692 | def removedirs(name): | ||
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) | ||||
Adrian Buehlmann
|
r9549 | def rename(src, dst): | ||
'''atomically rename file src to dst, replacing dst if it exists''' | ||||
try: | ||||
os.rename(src, dst) | ||||
Gregory Szorc
|
r25660 | except OSError as e: | ||
Adrian Buehlmann
|
r13278 | if e.errno != errno.EEXIST: | ||
raise | ||||
Adrian Buehlmann
|
r13280 | unlink(dst) | ||
Adrian Buehlmann
|
r9549 | os.rename(src, dst) | ||
Patrick Mezard
|
r10239 | def gethgcmd(): | ||
return [sys.executable] + sys.argv[:1] | ||||
Patrick Mezard
|
r11138 | def groupmembers(name): | ||
# Don't support groups on Windows for now | ||||
Brodie Rao
|
r16687 | raise KeyError | ||
Patrick Mezard
|
r11138 | |||
Adrian Buehlmann
|
r14926 | def isexec(f): | ||
return False | ||||
Idan Kamara
|
r14927 | class cachestat(object): | ||
def __init__(self, path): | ||||
pass | ||||
def cacheable(self): | ||||
return False | ||||
Adrian Buehlmann
|
r16807 | def lookupreg(key, valname=None, scope=None): | ||
''' Look up a key/value name in the Windows registry. | ||||
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). | ||||
''' | ||||
if scope is None: | ||||
Pulkit Goyal
|
r29760 | scope = (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE) | ||
Adrian Buehlmann
|
r16807 | elif not isinstance(scope, (list, tuple)): | ||
scope = (scope,) | ||||
for s in scope: | ||||
try: | ||||
Pulkit Goyal
|
r29760 | val = winreg.QueryValueEx(winreg.OpenKey(s, key), valname)[0] | ||
Adrian Buehlmann
|
r16807 | # never let a Unicode string escape into the wild | ||
Yuya Nishihara
|
r31447 | return encoding.unitolocal(val) | ||
Adrian Buehlmann
|
r16807 | except EnvironmentError: | ||
pass | ||||
Matt Mackall
|
r8614 | expandglobs = True | ||
Bryan O'Sullivan
|
r18868 | |||
def statislink(st): | ||||
'''check whether a stat result is a symlink''' | ||||
return False | ||||
def statisexec(st): | ||||
'''check whether a stat result is an executable file''' | ||||
return False | ||||
Gregory Szorc
|
r22245 | |||
Pierre-Yves David
|
r25420 | def poll(fds): | ||
# see posix.py for description | ||||
raise NotImplementedError() | ||||
Gregory Szorc
|
r22245 | def readpipe(pipe): | ||
"""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) | ||||
return ''.join(chunks) | ||||
Yuya Nishihara
|
r29530 | |||
def bindunixsocket(sock, path): | ||||
raise NotImplementedError('unsupported platform') | ||||