Skip to content

Commit

Permalink
chown() method, owner and group properties for local/remote Paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
njharman authored and tomerfiliba committed Oct 6, 2012
1 parent d0a8a2c commit 0577dd1
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 203 deletions.
48 changes: 45 additions & 3 deletions plumbum/local_machine.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from __future__ import with_statement
import sys
import os
import sys
import grp
import pwd
import glob
import shutil
import subprocess
import logging
import stat
import time
from types import ModuleType
from tempfile import mkdtemp
from subprocess import Popen, PIPE
from contextlib import contextmanager

from plumbum.path import Path
from plumbum.remote_path import RemotePath
from plumbum.commands import CommandNotFound, ConcreteCommand
from plumbum.session import ShellSession
from types import ModuleType
from tempfile import mkdtemp
from plumbum.lib import _setdoc

logger = logging.getLogger("plumbum.local")
Expand Down Expand Up @@ -55,6 +58,26 @@ def basename(self):
def dirname(self):
return self.__class__(os.path.dirname(str(self)))

@property
@_setdoc(Path)
def owner(self):
stat = os.stat(str(self))
return pwd.getpwuid(stat.st_uid)[0]

@owner.setter
def owner(self, owner):
self.chown(owner)

@property
@_setdoc(Path)
def group(self):
stat = os.stat(str(self))
return grp.getgrgid(stat.st_gid)[0]

@group.setter
def group(self, group):
self.chown(group=group)

@_setdoc(Path)
def join(self, other):
if isinstance(other, RemotePath):
Expand Down Expand Up @@ -133,6 +156,25 @@ def write(self, data):
with self.open("w") as f:
f.write(data)

@_setdoc(Path)
def chown(self, owner='', group='', uid='', gid='', recursive=False):
gid = str(gid) # str so uid 0 (int) isn't seen as False
uid = str(uid)
args = list()
if recursive:
args.append('-R')
if uid:
owner = uid
if gid:
group = gid
if group:
owner = '%s:%s' % (owner, group)
args.append(owner)
args.append(str(self))
# recursive is a pain using os.chown
local['chown'](*args)


class Workdir(LocalPath):
"""Working directory manipulator"""

Expand Down
29 changes: 17 additions & 12 deletions plumbum/path.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Path(object):
"""An abstraction over file system paths. This class is abstract, and the two implementations
are :class:`LocalPath <plumbum.local_machine.LocalPath>` and
are :class:`LocalPath <plumbum.local_machine.LocalPath>` and
:class:`RemotePath <plumbum.remote_machine.RemotePath>`.
"""

__slots__ = []
CASE_SENSITIVE = True

def __repr__(self):
return "<%s %s>" % (self.__class__.__name__, str(self))
def __div__(self, other):
Expand Down Expand Up @@ -44,7 +44,7 @@ def __hash__(self):
def __nonzero__(self):
return bool(str(self))
__bool__ = __nonzero__

def up(self, count = 1):
"""Go up in ``count`` directories (the default is 1)"""
return self.join("../" * count)
Expand All @@ -67,11 +67,19 @@ def basename(self):
def dirname(self):
"""The dirname component of this path"""
raise NotImplementedError()

@property
def owner(self):
"""The owner of leaf component of this path"""
raise NotImplementedError()
@property
def group(self):
"""The group of leaf component of this path"""
raise NotImplementedError()

def _get_info(self):
raise NotImplementedError()
def join(self, *parts):
"""Joins this path with any number of paths"""
"""Joins this path with any number of paths"""
raise NotImplementedError()
def list(self):
"""Returns the files in this directory"""
Expand Down Expand Up @@ -114,9 +122,6 @@ def read(self):
def write(self, data):
"""writes the given data to this file"""
raise NotImplementedError()






def chown(self, owner=None, group=None, uid=None, gid=None, recursive=False):
"""Change ownership of leaf component of this path"""
raise NotImplementedError()
37 changes: 37 additions & 0 deletions plumbum/remote_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ def dirname(self):
return str(self)
return self.__class__(self.remote, str(self).rsplit("/", 1)[0])

@property
@_setdoc(Path)
def owner(self):
files = self.remote._session.run("ls -a %s" % (self,))[1].splitlines()
stat = os.stat(str(self))
return pwd.getpwuid(stat.st_uid)[0]

@owner.setter
def owner(self, owner):
self.chown(owner)

@property
@_setdoc(Path)
def group(self):
stat = os.stat(str(self))
return grp.getgrgid(stat.st_gid)[0]

@group.setter
def group(self, group):
self.chown(group=group)

def _get_info(self):
return (self.remote, self._path)

Expand Down Expand Up @@ -160,3 +181,19 @@ def write(self, data):
f.flush()
f.seek(0)
self.remote.upload(f.name, self)

@_setdoc(Path)
def chown(self, owner='', group='', uid='', gid='', recursive=False):
gid = str(gid) # str so uid 0 (int) isn't seen as False
uid = str(uid)
args = list()
if recursive:
args.append('-R')
if uid:
owner = uid
if gid:
group = gid
if group:
owner = '%s:%s' % (owner, group)
args.append(owner)
self.remote._session.run('chown %s "%s"' % (' '.join(args), self))
Loading

0 comments on commit 0577dd1

Please sign in to comment.