Source

timestamp / makewritable.py

# makewritable.py - check files to be changed for read-only flag and
#                   interactively remove it.
#
# Copyright 2009 Friedrich Kastner-Masilko <face@snoopie.at>
#
# 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'''
    
    self.auditor(path)
    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:
                    frames=inspect.stack()
                    for frame in frames:
                        l = frame[0].f_locals
                        if l.has_key('ui'):
                            self.currentUi=l['ui']
                            break
                doit = self.makewritable
                if not doit:
                    while True:
                        choices = (_('&Yes, make this file writable'),
                                   _('&No, leave this file read-only'),
                                   _('&All, make all remaining files writable without further notice'),
                                   _('&?'))
                        r = self.currentUi.promptchoice(_('make \'%s\' writable?') % f + _(' (Yna?) '), choices)
                        if r == 0:
                            doit = True
                            break
                        elif r == 1:
                            break
                        elif r == 2:
                            self.makewritable = True;
                            doit = True;
                            break
                        elif r == 3:
                            c = opener.__doc__.find('y - make the file writable')
                            for l in opener.__doc__[c:].splitlines():
                                if l: self.currentUi.write(_(l.strip()), '\n')
                            continue
                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