Files
rpmbuild/helpers.py
T
2026-08-19 12:12:47 +02:00

219 lines
6.8 KiB
Python
Executable File

#!/usr/bin/python
# -*- coding: utf-8 -*-
import getpass
import os
import re
import shutil
import stat
import subprocess
import sys
def verbose():
verb = os.getenv("EMERGE_VERBOSE")
if not verb == None and verb.isdigit() and int(verb) > 0:
return int(verb)
else:
return 0
def debug(message):
if verbose() >= 0:
print("debug: %s" % message)
return True
def info(message):
if verbose() >= 0:
print("info: %s" % message)
return True
def warning(message):
print("warning: %s" % message)
return True
def error(message):
die(message)
def die(message):
print("emerge fatal error: %s" % message, file=sys.stderr)
sys.exit(1)
def system(cmdstring, file=None):
debug("Executing '%s'" % cmdstring)
out = subprocess.PIPE
if file != None:
out = file
p = subprocess.Popen(cmdstring, shell=True, stdout=out, stderr=sys.stderr)
ret = p.wait()
if ret != 0:
die("Error executing %s (%s)" % (cmdstring, ret))
ret = ""
if file == None:
ret = p.stdout.read().strip()
return ret
def getOS_Name():
if os.path.exists("/etc/SuSE-release"):
f = open("/etc/SuSE-release", "r")
rx = re.compile(r"^VERSION = (.*)$")
for line in f.readlines():
m = rx.match(line)
if not m:
line = re.sub(r"\s", "", line)
line = re.sub(r"\([xX]86[-_]64\)", "", line)
line = line.replace("SUSE", "")
line = line.replace("LinuxEnterpriseServer", "SLE")
line = line.replace("LinuxEnterpriseDesktop", "SLE")
return line.strip()
return "unknown"
else:
f = open("/etc/os-release", "r")
rx = re.compile(r'^PRETTY_NAME="(.*)"$')
for line in f.readlines():
m = rx.match(line)
if m:
return m.group(1).replace(" ", "_")
return "unknown"
def removeDirectory(dest_dir):
info("--------------------------------")
info("Removing %s" % (dest_dir))
info("--------------------------------")
if os.path.exists(dest_dir) and os.path.isdir(dest_dir):
for root, dirs, files in os.walk(dest_dir, topdown=False):
for name in files:
try:
os.remove(os.path.join(root, name))
except:
die(
"couldn't delete file %s\n ( %s )"
% (name, os.path.join(root, name))
)
for name in dirs:
try:
if os.path.islink(os.path.join(root, name)):
os.remove(os.path.join(root, name))
else:
os.rmdir(os.path.join(root, name))
except:
die(
"couldn't delete directory %s\n( %s )"
% (name, os.path.join(root, name))
)
os.rmdir(dest_dir)
def copySrcDirToDestDir(srcdir, dstdir, perm=None, with_subdirs=True):
info("--------------------------------")
info("Copying %s to %s" % (srcdir, dstdir))
info("--------------------------------")
while srcdir[-1] == "/":
srcdir = srcdir[:-1]
if with_subdirs:
for root, dirs, files in os.walk(srcdir):
tmpdir = root.replace(srcdir, dstdir)
debug("dstdir = '%s'" % tmpdir)
if not os.path.exists(tmpdir):
debug("Creating dir '%s'" % tmpdir)
os.makedirs(tmpdir)
if perm:
os.chmod(tmpdir, perm | stat.S_IREAD | stat.S_IRGRP | stat.S_IROTH)
for file in files:
if os.path.isfile(tmpdir) and os.path.exists(tmpdir):
os.remove(tmpdir)
shutil.copy(os.path.join(root, file), tmpdir)
if perm:
f = os.path.join(tmpdir, file)
add_perm = 0
if os.path.isdir(f):
add_perm = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
else:
mode = os.stat(f).st_mode
if (stat.S_IXUSR & mode) == stat.S_IXUSR:
add_perm = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
os.chmod(f, perm | add_perm)
else:
for f in os.listdir(srcdir):
if os.path.isdir(f):
continue
shutil.copy(os.path.join(srcdir, f), os.path.join(dstdir, f))
def moveFilesToDir(srcdir, dstdir, rx_filter=None, with_subdirs=True):
info("--------------------------------")
info("Moving files from %s to %s" % (srcdir, dstdir))
info("--------------------------------")
moved_files = []
if not os.path.exists(dstdir):
os.makedirs(dstdir)
if with_subdirs:
for root, dirs, files in os.walk(srcdir):
for file in files:
if rx_filter and not rx_filter.match(file):
continue
src = os.path.join(root, file)
dst = os.path.join(dstdir, file)
debug("Move %s to %s" % (src, dst))
if os.path.exists(dst):
os.remove(dst)
moved_files.append(dst)
shutil.move(src, dstdir)
else:
for f in os.listdir(srcdir):
if os.path.isdir(f):
continue
if rx_filter and not rx_filter.match(f):
continue
src = os.path.join(srcdir, file)
dst = os.path.join(dstdir, file)
debug("Move %s to %s" % (src, dst))
moved_files.append(dst)
shutil.move(src, dstdir)
return moved_files
def adjustPermissions(srcdir):
info("--------------------------------")
info("Adjusting permissions in %s" % srcdir)
info("--------------------------------")
stat_file = stat.S_IREAD | stat.S_IWRITE | stat.S_IRUSR | stat.S_IROTH
stat_executable = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
stat_dir = stat_file | stat_executable
while srcdir[-1] == "/":
srcdir = srcdir[:-1]
for root, dirs, files in os.walk(srcdir):
os.chmod(
root,
)
for file in files:
f = os.path.join(root, file)
perm = 0
if os.path.isdir(f):
perm = stat_dir
else:
perm = stat_file
mode = os.stat(f).st_mode
if (stat.S_IXUSR & mode) == stat.S_IXUSR:
perm |= stat_executable
os.chmod(f, perm)
def exec_rpmbuild(spec_dir, spec_filename, buildroot=None):
spec = os.path.join(spec_dir, spec_filename)
cmd = "rpmbuild -ba %s" % spec
if buildroot:
cmd += " --buildroot=%s" % buildroot
system(cmd, sys.stdout) and die("during rpmbuild. cmd: %s" % command)