parent
43dba6868c
commit
22cf59cd0b
101
mode_dir.py
101
mode_dir.py
|
@ -1,4 +1,4 @@
|
||||||
import grp, method, mode2, os.path, pwd, re
|
import commands, grp, method, mode2, os.path, pwd, re
|
||||||
from lex3 import Grammar, PatternRule, RegionRule, PatternGroupRule
|
from lex3 import Grammar, PatternRule, RegionRule, PatternGroupRule
|
||||||
from point2 import Point
|
from point2 import Point
|
||||||
from method import Method, Argument
|
from method import Method, Argument
|
||||||
|
@ -64,13 +64,12 @@ class Dir(mode2.Fundamental):
|
||||||
}
|
}
|
||||||
def __init__(self, w):
|
def __init__(self, w):
|
||||||
mode2.Fundamental.__init__(self, w)
|
mode2.Fundamental.__init__(self, w)
|
||||||
self.add_action(Chmod())
|
self.add_action_and_bindings(RefreshView(), ('C-c r',))
|
||||||
self.add_action(Chown())
|
|
||||||
self.add_action(Chgrp())
|
|
||||||
|
|
||||||
self.add_action(TouchPath())
|
|
||||||
self.add_action(RefreshView())
|
|
||||||
self.add_action_and_bindings(OpenPath(), ('RETURN',))
|
self.add_action_and_bindings(OpenPath(), ('RETURN',))
|
||||||
|
self.add_action_and_bindings(Chmod(), ('C-c m',))
|
||||||
|
self.add_action_and_bindings(Chown(), ('C-c o',))
|
||||||
|
self.add_action_and_bindings(Chgrp(), ('C-c g',))
|
||||||
|
self.add_action_and_bindings(TouchPath(), ('C-c t',))
|
||||||
self.add_action_and_bindings(RemovePath(), ('DELETE', 'BACKSPACE', 'C-d'))
|
self.add_action_and_bindings(RemovePath(), ('DELETE', 'BACKSPACE', 'C-d'))
|
||||||
def name(self):
|
def name(self):
|
||||||
return "Dir"
|
return "Dir"
|
||||||
|
@ -113,10 +112,25 @@ class OpenPath(Method):
|
||||||
w.set_error("opening %r" % path)
|
w.set_error("opening %r" % path)
|
||||||
w.application.methods['open-file'].execute(w, filename=path)
|
w.application.methods['open-file'].execute(w, filename=path)
|
||||||
|
|
||||||
class Chmod(Method):
|
class DirCmd(Method):
|
||||||
|
def _make_cmd(self, w, path, **vargs):
|
||||||
|
return ''
|
||||||
|
def _run(self, w, **vargs):
|
||||||
|
basename = _resolve_name(w)
|
||||||
|
path = os.path.join(w.buffer.path, basename)
|
||||||
|
cmd = self._make_cmd(w, path, **vargs)
|
||||||
|
(status, output) = commands.getstatusoutput(cmd)
|
||||||
|
if status != 0:
|
||||||
|
w.set_error("%s failed (exit %d)" % (self.name, status))
|
||||||
|
w.application.methods['refresh-view'].execute(w, filename=path)
|
||||||
|
_find_name(w, basename)
|
||||||
|
|
||||||
|
class Chmod(DirCmd):
|
||||||
args = [Argument('mode', type=type(''), prompt="New Mode: ")]
|
args = [Argument('mode', type=type(''), prompt="New Mode: ")]
|
||||||
octal_re = re.compile('^[0-7]{1,4}$')
|
octal_re = re.compile('^[0-7]{1,4}$')
|
||||||
symbolic_re = re.compile('(?:[ugoa]*(?:[-+=](?:[rwxXst]*|[ugo]))+ *,?)+')
|
symbolic_re = re.compile('(?:[ugoa]*(?:[-+=](?:[rwxXst]*|[ugo]))+ *,?)+')
|
||||||
|
def _make_cmd(self, w, path, **vargs):
|
||||||
|
return 'chmod %r %r' % (vargs['mode'], path)
|
||||||
def _execute(self, w, **vargs):
|
def _execute(self, w, **vargs):
|
||||||
if self.octal_re.match(vargs['mode']):
|
if self.octal_re.match(vargs['mode']):
|
||||||
pass
|
pass
|
||||||
|
@ -124,15 +138,33 @@ class Chmod(Method):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
w.set_error("Not a valid mode: %r" % vargs['mode'])
|
w.set_error("Not a valid mode: %r" % vargs['mode'])
|
||||||
basename = _resolve_name(w)
|
self._run(w, **vargs)
|
||||||
path = os.path.join(w.buffer.path, basename)
|
|
||||||
retval = os.system('chmod %r %r' % (vargs['mode'], path))
|
def _valid_owner(owner):
|
||||||
w.application.methods['refresh-view'].execute(w, filename=path)
|
if not owner:
|
||||||
_find_name(w, basename)
|
return False
|
||||||
if retval != 0:
|
elif owner.isdigit():
|
||||||
w.set_error("chmod %r failed (exit %d)" % (vargs['mode'], retval))
|
return True
|
||||||
class Chown(Method):
|
try:
|
||||||
|
pwd.getpwnam(owner)
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
def _valid_group(group):
|
||||||
|
if not group:
|
||||||
|
return False
|
||||||
|
elif group.isdigit():
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
grp.getgrnam(group)
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
class Chown(DirCmd):
|
||||||
args = [Argument('owner', type=type(''), prompt="New Owner: ")]
|
args = [Argument('owner', type=type(''), prompt="New Owner: ")]
|
||||||
|
def _make_cmd(self, w, path, **vargs):
|
||||||
|
return 'chown %r %r' % (vargs['owner'], path)
|
||||||
def _execute(self, w, **vargs):
|
def _execute(self, w, **vargs):
|
||||||
fields = vargs['owner'].split(':')
|
fields = vargs['owner'].split(':')
|
||||||
if len(fields) == 1:
|
if len(fields) == 1:
|
||||||
|
@ -142,31 +174,22 @@ class Chown(Method):
|
||||||
else:
|
else:
|
||||||
w.set_error("Malformed 'owner' argument: %r" % vargs['owner'])
|
w.set_error("Malformed 'owner' argument: %r" % vargs['owner'])
|
||||||
return
|
return
|
||||||
|
if not _valid_owner(owner):
|
||||||
if not owner.isdigit():
|
w.set_error('User %r does not exist' % owner)
|
||||||
try:
|
return
|
||||||
pwd.getpwnam(owner)
|
if group is not None and not _valid_group(group):
|
||||||
except:
|
w.set_error('Group %r does not exist' % group)
|
||||||
w.set_error("User %r does not exist" % owner)
|
return
|
||||||
return
|
self._run(w, **vargs)
|
||||||
if group is not None and not group.isdigit():
|
class Chgrp(DirCmd):
|
||||||
try:
|
|
||||||
grp.getgrnam(group)
|
|
||||||
except:
|
|
||||||
w.set_error("Group %r does not exist" % group)
|
|
||||||
return
|
|
||||||
|
|
||||||
basename = _resolve_name(w)
|
|
||||||
path = os.path.join(w.buffer.path, basename)
|
|
||||||
retval = os.system('chown %r %r' % (vargs['owner'], path))
|
|
||||||
w.application.methods['refresh-view'].execute(w, filename=path)
|
|
||||||
_find_name(w, basename)
|
|
||||||
if retval != 0:
|
|
||||||
w.set_error("chown %r failed (exit %d)" % (vargs['mode'], retval))
|
|
||||||
class Chgrp(Method):
|
|
||||||
args = [Argument('group', type=type(''), prompt="New Group: ")]
|
args = [Argument('group', type=type(''), prompt="New Group: ")]
|
||||||
|
def _make_cmd(self, w, path, **vargs):
|
||||||
|
return 'chgrp %r %r' % (vargs['group'], path)
|
||||||
def _execute(self, w, **vargs):
|
def _execute(self, w, **vargs):
|
||||||
w.set_error('chgrp not implemented')
|
if not _valid_group(vargs['group']):
|
||||||
|
w.set_error('Group %r does not exist' % group)
|
||||||
|
return
|
||||||
|
self._run(w, **vargs)
|
||||||
|
|
||||||
class TouchPath(Method):
|
class TouchPath(Method):
|
||||||
args = [Argument('filename', datatype="path", prompt="Touch File: ")]
|
args = [Argument('filename', datatype="path", prompt="Touch File: ")]
|
||||||
|
|
Loading…
Reference in New Issue