timestamp /

# - check files to be changed for read-only flag and
#                   interactively remove it.
# Copyright 2009 Friedrich Kastner-Masilko <>
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

from mercurial.i18n import _
from mercurial import util
import os, stat, inspect

def opener(self, path, mode="r", text=False, atomictemp=False):
    '''interactively removes read-only flag from files to be changed

    You will be prompted wether to make a file writable or not.
    For each query, the following responses are possible:

    y - make the file writable
    n - leave the file read-only, leads to aborting the write access

    a - make all remaining files writable without further notice

    ? - display help'''
    f = os.path.join(self.base, path)
    m = mode    
    if not text and "b" not in mode:
        m += "b"
    if m not in ("r", "rb"):
        if os.path.exists(f):
            s = os.stat(f)
            if (s.st_mode & stat.S_IWRITE) == 0:
                if not self.currentUi:
                    for frame in frames:
                        l = frame[0].f_locals
                        if l.has_key('ui'):
                doit = self.makewritable
                if not doit:
                    while True:
                        r = (self.currentUi.prompt(_('make \'%s\' writable?') % f + _(' [Yna?] '), '(?i)[Yna?]?$') or 'y').lower()
                        if r == '?':
                            c = opener.__doc__.find('y - make the file writable')
                            for l in opener.__doc__[c:].splitlines():
                                if l: self.currentUi.write(_(l.strip()), '\n')
                        elif r == 'y':
                            doit = True
                        elif r == 'n':
                        elif r == 'a':
                            self.makewritable = True;
                            doit = True;
                if doit:
                    os.chmod(f, s.st_mode | stat.S_IWRITE)
                    self.currentUi.status(_('made \'%s\' writable\n') % f)
    return util.opener.makewritable_origopener(self, path, mode, text, atomictemp)

util.opener.makewritable_origopener = util.opener.__call__
util.opener.__call__ = opener
util.opener.makewritable = False
util.opener.currentUi = False