2009-12-15 14:26:01 +00:00
|
|
|
#
|
2010-02-23 13:20:05 +00:00
|
|
|
# sysutils.py
|
2010-01-12 11:45:54 +00:00
|
|
|
#
|
2019-07-11 00:30:10 +00:00
|
|
|
# Copyright (C) 2009-2019 Red Hat, Inc.
|
2010-01-12 11:45:54 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# Red Hat Author(s): Martin Gracik <mgracik@redhat.com>
|
2009-12-15 14:26:01 +00:00
|
|
|
#
|
|
|
|
|
2011-05-09 23:04:35 +00:00
|
|
|
__all__ = ["joinpaths", "touch", "replace", "chown_", "chmod_", "remove",
|
|
|
|
"linktree"]
|
2010-02-23 13:20:05 +00:00
|
|
|
|
2009-12-15 14:26:01 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
2010-10-12 16:23:29 +00:00
|
|
|
import fileinput
|
2010-11-16 08:36:07 +00:00
|
|
|
import pwd
|
|
|
|
import grp
|
|
|
|
import glob
|
2011-03-07 16:15:27 +00:00
|
|
|
import shutil
|
2018-08-30 18:31:55 +00:00
|
|
|
import shlex
|
|
|
|
from configparser import ConfigParser
|
2009-12-15 14:26:01 +00:00
|
|
|
|
2012-08-22 22:24:49 +00:00
|
|
|
from pylorax.executils import runcmd
|
2009-12-15 14:26:01 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
def joinpaths(*args, **kwargs):
|
|
|
|
path = os.path.sep.join(args)
|
2009-12-15 14:26:01 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
if kwargs.get("follow_symlinks"):
|
|
|
|
return os.path.realpath(path)
|
2009-12-15 14:26:01 +00:00
|
|
|
else:
|
2010-10-12 16:23:29 +00:00
|
|
|
return path
|
2009-12-15 14:26:01 +00:00
|
|
|
|
|
|
|
|
2010-10-19 15:35:50 +00:00
|
|
|
def touch(fname):
|
2020-10-06 17:55:00 +00:00
|
|
|
with open(fname, "w") as f:
|
|
|
|
f.write("")
|
2010-10-19 15:35:50 +00:00
|
|
|
|
|
|
|
|
2014-05-09 00:21:34 +00:00
|
|
|
def replace(fname, find, sub):
|
2010-02-23 13:20:05 +00:00
|
|
|
fin = fileinput.input(fname, inplace=1)
|
|
|
|
pattern = re.compile(find)
|
2009-12-15 14:26:01 +00:00
|
|
|
|
|
|
|
for line in fin:
|
2014-05-09 00:21:34 +00:00
|
|
|
line = pattern.sub(sub, line)
|
2009-12-15 14:26:01 +00:00
|
|
|
sys.stdout.write(line)
|
|
|
|
|
|
|
|
fin.close()
|
|
|
|
|
|
|
|
|
2010-11-16 08:36:07 +00:00
|
|
|
def chown_(path, user=None, group=None, recursive=False):
|
|
|
|
uid = gid = -1
|
|
|
|
|
|
|
|
if user is not None:
|
|
|
|
uid = pwd.getpwnam(user)[2]
|
|
|
|
if group is not None:
|
|
|
|
gid = grp.getgrnam(group)[2]
|
|
|
|
|
|
|
|
for fname in glob.iglob(path):
|
|
|
|
os.chown(fname, uid, gid)
|
|
|
|
|
|
|
|
if recursive and os.path.isdir(fname):
|
|
|
|
for nested in os.listdir(fname):
|
|
|
|
nested = joinpaths(fname, nested)
|
|
|
|
chown_(nested, user, group, recursive)
|
|
|
|
|
|
|
|
|
|
|
|
def chmod_(path, mode, recursive=False):
|
|
|
|
for fname in glob.iglob(path):
|
|
|
|
os.chmod(fname, mode)
|
|
|
|
|
|
|
|
if recursive and os.path.isdir(fname):
|
|
|
|
for nested in os.listdir(fname):
|
|
|
|
nested = joinpaths(fname, nested)
|
|
|
|
chmod_(nested, mode, recursive)
|
|
|
|
|
|
|
|
|
2011-03-07 16:15:27 +00:00
|
|
|
def cpfile(src, dst):
|
|
|
|
shutil.copy2(src, dst)
|
|
|
|
if os.path.isdir(dst):
|
|
|
|
dst = joinpaths(dst, os.path.basename(src))
|
|
|
|
|
|
|
|
return dst
|
2011-05-09 14:24:15 +00:00
|
|
|
|
2011-05-26 22:14:10 +00:00
|
|
|
def mvfile(src, dst):
|
|
|
|
if os.path.isdir(dst):
|
|
|
|
dst = joinpaths(dst, os.path.basename(src))
|
|
|
|
os.rename(src, dst)
|
|
|
|
return dst
|
|
|
|
|
2011-05-09 14:24:15 +00:00
|
|
|
def remove(target):
|
2011-05-31 15:31:21 +00:00
|
|
|
if os.path.isdir(target) and not os.path.islink(target):
|
2011-05-09 14:24:15 +00:00
|
|
|
shutil.rmtree(target)
|
|
|
|
else:
|
|
|
|
os.unlink(target)
|
2011-05-09 23:04:35 +00:00
|
|
|
|
|
|
|
def linktree(src, dst):
|
2013-03-18 15:58:35 +00:00
|
|
|
runcmd(["/bin/cp", "-alx", src, dst])
|
2018-08-30 18:31:55 +00:00
|
|
|
|
|
|
|
def unquote(s):
|
|
|
|
return ' '.join(shlex.split(s))
|
|
|
|
|
|
|
|
class UnquotingConfigParser(ConfigParser):
|
|
|
|
"""A ConfigParser, only with unquoting of the values."""
|
2018-09-04 18:11:54 +00:00
|
|
|
# pylint: disable=arguments-differ
|
2018-08-30 18:31:55 +00:00
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
ret = super().get(*args, **kwargs)
|
|
|
|
if ret:
|
|
|
|
ret = unquote(ret)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def flatconfig(filename):
|
|
|
|
"""Use UnquotingConfigParser to read a flat config file (without
|
|
|
|
section headers) by adding a section header.
|
|
|
|
"""
|
|
|
|
with open (filename, 'r') as conffh:
|
|
|
|
conftext = "[main]\n" + conffh.read()
|
|
|
|
config = UnquotingConfigParser()
|
|
|
|
config.read_string(conftext)
|
|
|
|
return config['main']
|
2019-06-21 20:22:29 +00:00
|
|
|
|
|
|
|
def read_tail(path, size):
|
|
|
|
"""Read up to `size` kibibytes from the end of a file"""
|
2019-07-11 00:30:10 +00:00
|
|
|
|
|
|
|
# NOTE: In py3 text files are unicode, not bytes so we have to open it as bytes
|
|
|
|
with open(path, "rb") as f:
|
|
|
|
return _read_file_end(f, size)
|
|
|
|
|
|
|
|
def _read_file_end(f, size):
|
|
|
|
"""Read the end of a file
|
|
|
|
|
|
|
|
This skips to the next line to avoid starting in the middle of a unicode character.
|
|
|
|
And returns "" in the case of a UnicodeDecodeError
|
|
|
|
"""
|
|
|
|
f.seek(0, 2)
|
|
|
|
end = f.tell()
|
|
|
|
if end < 1024 * size:
|
|
|
|
f.seek(0, 0)
|
|
|
|
else:
|
|
|
|
f.seek(end - (1024 * size))
|
|
|
|
data = f.read()
|
|
|
|
try:
|
|
|
|
# Find the first newline in the block
|
|
|
|
newline = min(1+data.find(b'\n'), len(data))
|
|
|
|
text = data[newline:].decode("UTF-8")
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
return ""
|
|
|
|
return text
|