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 scmutil
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 scmutil.opener.makewritable_origopener(self, path, mode, text, atomictemp)

scmutil.opener.makewritable_origopener = scmutil.opener.__call__
scmutil.opener.__call__ = opener
scmutil.opener.makewritable = False
scmutil.opener.currentUi = False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.