本文整理汇总了Python中shutil.chown函数的典型用法代码示例。如果您正苦于以下问题:Python chown函数的具体用法?Python chown怎么用?Python chown使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了chown函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_directory
def add_directory():
add_group()
if not os.path.isdir(WEBPLEASE_PATH):
os.mkdir(WEBPLEASE_PATH)
shutil.chown(WEBPLEASE_PATH, user=WEBPLEASE_OWNER, group=WEBPLEASE_GROUP)
os.chmod(WEBPLEASE_PATH, mode=(stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
| stat.S_IRGRP | stat.S_IXGRP | stat.S_ISVTX))
示例2: upgrade_charm
def upgrade_charm():
apt_install(filter_installed_packages(determine_packages()),
fatal=True)
# NOTE: ensure psutil install for hugepages configuration
status_set('maintenance', 'Installing apt packages')
apt_install(filter_installed_packages(['python-psutil']))
packages_removed = remove_old_packages()
if packages_removed and not is_unit_paused_set():
log("Package purge detected, restarting services", "INFO")
for s in services():
service_restart(s)
for r_id in relation_ids('amqp'):
amqp_joined(relation_id=r_id)
if is_relation_made('nrpe-external-master'):
update_nrpe_config()
# Fix previously wrongly created path permissions
# LP: https://bugs.launchpad.net/charm-cinder-ceph/+bug/1779676
asok_path = '/var/run/ceph/'
gid = grp.getgrnam("kvm").gr_gid
if gid and os.path.isdir(asok_path) and gid != os.stat(asok_path).st_gid:
log("{} not owned by group 'kvm', fixing permissions."
.format(asok_path))
shutil.chown(asok_path, group='kvm')
示例3: __init__
def __init__(self, logger, config):
self.logger = logger
self.config = config
self._shutdown = threading.Event()
config.event_manager = EventManager(logger, config)
sm = SensorManager(logger, config)
sm.start()
self.threads.append(sm)
#dbweb = DispatchBuddyWebUI(logger, config)
#th = threading.Thread(target=dbweb.run_app, name='DB WebUI')
#th.start()
#self.threads.append((th,dbweb))
for p in ('/var/db/DispatchBuddy',
'/var/db/DispatchBuddy/celery.results',
'/var/db/DispatchBuddy/evdata',
'/var/db/DispatchBuddy/pcap',
'/var/db/DispatchBuddy/tmp',
):
try:
os.stat(p)
except FileNotFoundError:
os.mkdir(p, 0o700)
shutil.chown(p,
user=config.get('main', 'run as user'),
group=config.get('main', 'run as group')
)
except:
traceback.print_exc()
示例4: set_mode
def set_mode(path, mode):
if mode is None:
# Keep mode unchanged
return
if (mode.perms_s or mode.owner or mode.group) is None:
# Nothing to set
return
# No chown() on Windows, and must set one of owner/group
if not is_windows() and (mode.owner or mode.group) is not None:
try:
shutil.chown(path, mode.owner, mode.group)
except PermissionError as e:
msg = '{!r}: Unable to set owner {!r} and group {!r}: {}, ignoring...'
print(msg.format(path, mode.owner, mode.group, e.strerror))
except LookupError:
msg = '{!r}: Non-existent owner {!r} or group {!r}: ignoring...'
print(msg.format(path, mode.owner, mode.group))
except OSError as e:
if e.errno == errno.EINVAL:
msg = '{!r}: Non-existent numeric owner {!r} or group {!r}: ignoring...'
print(msg.format(path, mode.owner, mode.group))
else:
raise
# Must set permissions *after* setting owner/group otherwise the
# setuid/setgid bits will get wiped by chmod
# NOTE: On Windows you can set read/write perms; the rest are ignored
if mode.perms_s is not None:
try:
os.chmod(path, mode.perms)
except PermissionError as e:
msg = '{!r}: Unable to set permissions {!r}: {}, ignoring...'
print(msg.format(path, mode.perms_s, e.strerror))
示例5: chown_dir
def chown_dir(directory, username):
"""Set owner and group of directory to username."""
shutil.chown(directory, username, username)
for root, dirs, files in os.walk(directory):
for child in dirs + files:
shutil.chown(os.path.join(root, child), username, username)
logger.info("{} chown'd to {}".format(directory, username))
示例6: handle_manual
def handle_manual(torrent):
auto_processed = False
def handle_media(path, move):
nonlocal auto_processed
guess = guessit.guessit(path)
if guess['type'] == 'episode':
move_episode(path, guess, move)
auto_processed = True
elif guess['type'] == 'movie':
move_movie(path, guess, move)
auto_processed = True
part_regex = re.compile('.*part(\d+).rar', re.IGNORECASE)
for index, file in torrent.files().items():
file_path = os.path.join(torrent.downloadDir, file['name'])
if check_extension(file_path) and 'sample' not in file_path.lower():
# Log and ignore mkv files of less than ~92MiB
try:
if os.path.getsize(file_path) >= 96811278:
handle_media(file_path, False)
else:
syslog.syslog(
syslog.LOG_ERR, 'Detected false media file, skipping'
)
except FileNotFoundError:
syslog.syslog(syslog.LOG_ERR, 'Torrent file missing, skipping')
elif file_path.endswith('rar'):
# Ignore parts beyond the first in a rar series
match = part_regex.match(file_path)
if match and int(match.group(1)) > 1:
continue
with tempfile.TemporaryDirectory() as temp_dir:
paths = extract(file_path, temp_dir)
if paths:
for path in paths:
shutil.chown(path, group=plex_group)
os.chmod(path, 0o664)
handle_media(path, True)
if auto_processed:
pb_notify(
textwrap.dedent(
'''
Manually added torrent {0} finished downloading
and was auto-processed
'''.format(torrent.name)
).strip()
)
else:
pb_notify(
'Manually added torrent {0} finished downloading'.format(
torrent.name
)
)
示例7: post
def post():
username = request.forms.get('username')
pubkey = request.forms.get('pubkey')
authorized_keys = "/etc/ssh/authorized_keys/" + username
print(username)
# TODO: Actual input validation and error handling
if len(username) < 2:
return "Username must be at least two characters long."
if not len(pubkey):
return "You should really include a public key."
if username == "root":
return "Nice try."
useradd = run(["useradd", "--shell", shell, "-g", groupname,
"-d", home, username])
if useradd.returncode == 9:
return HTTPResponse(status=409,
body="Sorry, but that user already exists.")
with open(authorized_keys, 'a+') as f:
f.write(pubkey)
chown(authorized_keys, username, groupname)
chmod(authorized_keys, 0o600)
return "Registered " + username
示例8: save_data
def save_data(self, data):
# Age data
if hasattr(data, '__len__') and len(data) > self.GROOM_THRESHOLD:
for i, e in enumerate(data):
data[i] = ZEntry(e.path, int(e.rank * self.GROOM_LEVEL), e.time)
# Use a temporary file to minimize time the file is open and minimize clobbering
# Use delete=False so the file can be closed without removing it. On Windows
# you can not copy an open file.
from tempfile import NamedTemporaryFile
with NamedTemporaryFile('wt', encoding=sys.getfilesystemencoding(),
delete=False) as f:
for e in data:
f.write("{}|{}|{}\n".format(e.path, int(e.rank), int(e.time.timestamp())))
f.flush()
if self.Z_OWNER:
shutil.chown(f.name, user=self.Z_OWNER)
# On POSIX, rename() is atomic and will clobber
# On Windows, neither of these is true, so remove first.
from xonsh.platform import ON_WINDOWS
if ON_WINDOWS and os.path.exists(self.Z_DATA):
os.remove(self.Z_DATA)
shutil.copy(f.name, self.Z_DATA)
os.remove(f.name)
示例9: add_user
def add_user(username, home_base='/home', clean_user=True):
user_folder=home_base+"/"+username
try:
user_check=pwd.getpwnam(username)
if clean_user==True:
return False
else:
return True
except KeyError:
if not os.path.isdir(home_base):
os.mkdir(home_base, 0o755)
if not os.path.isdir(user_folder):
os.mkdir(user_folder, 0o755)
if subprocess.call("sudo useradd -M -d "+user_folder+" -s /usr/sbin/nologin "+username, shell=True) > 0:
return False
else:
shutil.chown(user_folder, username, username)
return True
示例10: merge_overlay
def merge_overlay(self,path,overlay_path):
for f in os.listdir(overlay_path):
original_file = os.path.join(path,f)
final_file = os.path.join(overlay_path,f)
status = os.lstat(final_file)
# if it is a character device with 0 as major number, the original file/folder must be deleted
if (stat.S_ISCHR(status.st_mode) is True) and (os.major(status.st_rdev) == 0):
self.full_delete(original_file)
continue
# if it is a newly created file or folder, we just move it. That way it is faster and everything is preserved
if os.path.exists(original_file) is False:
self.run_external_program('mv "{:s}" "{:s}"'.format(final_file,original_file), False)
continue
ostatus = os.lstat(original_file)
# if it is a file, just copy it and overwrite
if (stat.S_ISDIR(status.st_mode) is False):
self.full_delete(original_file)
self.run_external_program('cp -a "{:s}" "{:s}"'.format(final_file,original_file), False)
continue
# if the new element is a folder, but the old is a file, delete the file and move the folder
if (stat.S_ISDIR(ostatus.st_mode) is False):
self.full_delete(original_file)
self.run_external_program('mv "{:s}" "{:s}"'.format(final_file,original_file), False)
continue
# if we reach here, both elements are folders, so let's check them recursively
shutil.copystat(final_file,original_file) # set permission bits
if (status.st_uid != ostatus.st_uid) or (status.st_gid != ostatus.st_gid):
shutil.chown(original_file,status.st_uid,status.st_gid)
self.merge_overlay(original_file,final_file)
示例11: _create_conf_dir
def _create_conf_dir(username):
conf_dir = '/home/{}/.openvpn_tunneler'.format(username)
# Use the following mode: u=rwx, g=rx, o-rwx
mode = stat.S_IRWXU | stat.S_IRGRP + stat.S_IXGRP | 00
if not os.path.exists(conf_dir):
os.mkdir(conf_dir, mode)
shutil.chown(conf_dir, user=username, group=username)
示例12: create
def create(self, *args, **kwargs):
""" create the virtualenv if it doesn't exist """
if not os.path.exists(self.venv_path):
os.makedirs(self.venv_path, mode=0o755, exist_ok=True)
shutil.chown(self.venv_path, self.usr, self.usr)
return subprocess.call(self.init_cmd)
else:
return 0
示例13: makedirs
def makedirs(path, mode=0o750, user='root', group='root'):
if os.path.exists(path):
assert os.path.isdir(path), '{} is not a directory'
else:
# Don't specify mode here, to ensure parent dirs are traversable.
os.makedirs(path)
shutil.chown(path, user, group)
os.chmod(path, mode)
示例14: fixperms
def fixperms(dirpath, user=None, group=None, umask=22):
for dirpath, dirnames, filenames in os.walk(dirpath):
shutil.chown(dirpath, user, group)
os.chmod(dirpath, 0o777 - umask)
for filename in filenames:
filepath = join(dirpath, filename)
shutil.chown(filepath, user, group)
os.chmod(filepath, 0o666 - umask)
示例15: create_folder
def create_folder(folder):
if not os.path.isdir(folder):
os.makedirs(folder, mode=0o770)
os.chdir(folder)
os.system('git init --bare --shared')
for root, dirs, files in os.walk(folder):
for entry in files + dirs:
shutil.chown(os.path.join(root, entry), group=DAEMONCGI_GROUP)