lorax/src/pylorax/sysutils.py

109 lines
2.6 KiB
Python
Raw Normal View History

#
2010-02-23 13:20:05 +00:00
# sysutils.py
2010-01-12 11:45:54 +00:00
#
# Copyright (C) 2009 Red Hat, Inc.
#
# 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>
#
2011-05-09 23:04:35 +00:00
__all__ = ["joinpaths", "touch", "replace", "chown_", "chmod_", "remove",
"linktree"]
2010-02-23 13:20:05 +00:00
import sys
import os
import re
2010-10-12 16:23:29 +00:00
import fileinput
import pwd
import grp
import glob
import shutil
2011-05-09 23:04:35 +00:00
import subprocess
2010-10-12 16:23:29 +00:00
def joinpaths(*args, **kwargs):
path = os.path.sep.join(args)
2010-10-12 16:23:29 +00:00
if kwargs.get("follow_symlinks"):
return os.path.realpath(path)
else:
2010-10-12 16:23:29 +00:00
return path
2010-10-19 15:35:50 +00:00
def touch(fname):
2010-12-02 12:29:55 +00:00
with open(fname, "w") as _:
2010-10-19 15:35:50 +00:00
pass
2010-10-12 16:23:29 +00:00
def replace(fname, find, replace):
2010-02-23 13:20:05 +00:00
fin = fileinput.input(fname, inplace=1)
pattern = re.compile(find)
for line in fin:
2010-02-23 13:20:05 +00:00
line = pattern.sub(replace, line)
sys.stdout.write(line)
fin.close()
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)
def cpfile(src, dst):
shutil.copy2(src, dst)
if os.path.isdir(dst):
dst = joinpaths(dst, os.path.basename(src))
return dst
def mvfile(src, dst):
if os.path.isdir(dst):
dst = joinpaths(dst, os.path.basename(src))
os.rename(src, dst)
return dst
def remove(target):
if os.path.isdir(target):
shutil.rmtree(target)
else:
os.unlink(target)
2011-05-09 23:04:35 +00:00
def linktree(src, dst):
subprocess.check_call(["/bin/cp", "-al", src, dst])