Commits

Friedrich Kastner-Masilko  committed abe92fc

Added makewritable - interactively make files writable on write access

  • Participants
  • Parent commits 3382bce

Comments (0)

Files changed (1)

File 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, ui
+import os, sys, 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.audit_path(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:
+                frame=sys._getframe(1)
+                this=ui.ui(frame.f_globals['ui'])
+
+                query = _('make \'%s\' writable?') % f                                
+                invalid = True
+                while invalid:
+                    r = (this.prompt(query + _(' [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: this.write(_(l.strip()), '\n')
+                        continue
+                    elif r == 'y':
+                        os.chmod(f, s.st_mode | stat.S_IWRITE)
+                        invalid = False
+                    elif r == 'n':
+                        invalid = False
+                    elif r == 'a':
+                        os.chmod(f, s.st_mode | stat.S_IWRITE)
+                        invalid = False
+                    else:
+                        this.write('%s not supported.\n' % r[0])
+                    
+                
+    return util.opener.makewritable_origopener(self, path, mode, text, atomictemp)
+
+util.opener.makewritable_origopener = util.opener.__call__
+util.opener.__call__ = opener