| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import locale |
| 25 |
|
| 26 |
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') |
| 27 |
|
| 28 |
|
| 29 |
import argparse |
| 30 |
import string |
| 31 |
import os.path |
| 32 |
import shutil |
| 33 |
import datetime |
| 34 |
import tempfile |
| 35 |
import subprocess |
| 36 |
import sys |
| 37 |
|
| 38 |
class CannotContinueError(Exception): |
| 39 |
"""Exception class for cases where processing cannot continue.""" |
| 40 |
|
| 41 |
pass |
| 42 |
|
| 43 |
if subprocess.call(["rpmbuild", "--version"], stdout = sys.stdout, stderr = sys.stderr) != 0: |
| 44 |
sys.stderr.write("Could not execute `rpmbuild --version`. " |
| 45 |
"Please make sure it's installed (often packaged as 'rpm-build').\n") |
| 46 |
sys.exit(1) |
| 47 |
|
| 48 |
def get_rpmbuild_paths(): |
| 49 |
sources = subprocess.check_output(["rpm", "--eval", "%{_sourcedir}"]).strip().decode('utf-8') |
| 50 |
|
| 51 |
if not os.path.exists(sources): |
| 52 |
if subprocess.call(["rpmdev-setuptree"], stdout = sys.stdout, stderr = sys.stderr) != 0: |
| 53 |
raise CannotContinueError("Failed to setup rpmbuild tree. Please make sure you have rpmdev-setuptree " |
| 54 |
"installed, or set it up manually. The SOURCES directory (%%{_sourcedir}) was expected at " |
| 55 |
"'%s'" % (sources)) |
| 56 |
|
| 57 |
rpm = subprocess.check_output(["rpm", "--eval", "%{_rpmdir}"]).strip().decode('utf-8') |
| 58 |
srpm = subprocess.check_output(["rpm", "--eval", "%{_srcrpmdir}"]).strip().decode('utf-8') |
| 59 |
|
| 60 |
if not os.path.exists(rpm) or not os.path.exists(srpm): |
| 61 |
sys.stderr.write("The SOURCES rpmbuild directory exists but RPM or SRPM do not. Please make " |
| 62 |
"sure your rpmbuild tree is setup correctly. Delete it and it will be created automatically.\n") |
| 63 |
|
| 64 |
return sources, rpm, srpm |
| 65 |
|
| 66 |
def copy_sources_to_rpmbuild(rpmbuild_sources_path, files): |
| 67 |
for f in files: |
| 68 |
shutil.copyfile(f.name, "%s/%s" % (rpmbuild_sources_path, os.path.basename(f.name))) |
| 69 |
|
| 70 |
def make_sources_list(files): |
| 71 |
ret = "" |
| 72 |
|
| 73 |
for (i, f) in enumerate(files): |
| 74 |
ret += "Source%i: %s\n" % (i, os.path.basename(f.name)) |
| 75 |
|
| 76 |
return ret |
| 77 |
|
| 78 |
def make_installer(scap_location, files): |
| 79 |
prepper = "" |
| 80 |
installer = "" |
| 81 |
installed_files = "" |
| 82 |
|
| 83 |
for (i, f) in enumerate(files): |
| 84 |
prepper += "cp %%SOURCE%i .\n" % (i) |
| 85 |
|
| 86 |
installer += "cp %s $RPM_BUILD_ROOT/%s/%%{name}/\n" % (os.path.basename(f.name), scap_location) |
| 87 |
installed_files += "%s/%%{name}/%s\n" % (scap_location, os.path.basename(f.name)) |
| 88 |
|
| 89 |
return prepper, installer, installed_files |
| 90 |
|
| 91 |
def create_spec(template_path, name, |
| 92 |
version, release, summary, license, |
| 93 |
scap_location, files, |
| 94 |
target_file): |
| 95 |
|
| 96 |
spec_template = """ |
| 97 |
# __ prefixed varibles in the form of ${__*} will get replaced by Python's string.Template |
| 98 |
|
| 99 |
Name: ${__package_name} |
| 100 |
Version: ${__package_version} |
| 101 |
Release: ${__package_release} |
| 102 |
Summary: ${__package_summary} |
| 103 |
License: ${__package_license} |
| 104 |
|
| 105 |
${__package_sources} |
| 106 |
|
| 107 |
BuildArch: noarch |
| 108 |
|
| 109 |
#BuildRequires: openscap-utils >= ${__package_openscap_version} |
| 110 |
#Requires: openscap-utils >= ${__package_openscap_version} |
| 111 |
|
| 112 |
%description |
| 113 |
This package was generated by scap-as-rpm. |
| 114 |
|
| 115 |
%prep |
| 116 |
${__package_prepper} |
| 117 |
|
| 118 |
%build |
| 119 |
|
| 120 |
%install |
| 121 |
mkdir -p $RPM_BUILD_ROOT/${__package_scap_location}/%{name} |
| 122 |
${__package_installer} |
| 123 |
|
| 124 |
%files |
| 125 |
${__package_installed_files} |
| 126 |
|
| 127 |
%changelog |
| 128 |
* ${__changelog_date} save-as-rpm - ${__package_version}-${__package_release} |
| 129 |
- Autogenerated |
| 130 |
""" |
| 131 |
|
| 132 |
template = string.Template(spec_template) |
| 133 |
|
| 134 |
sources_list = make_sources_list(files) |
| 135 |
prepper, installer, installed_files = make_installer(scap_location, files) |
| 136 |
|
| 137 |
spec_source = template.safe_substitute( |
| 138 |
__package_name = name, |
| 139 |
|
| 140 |
__package_version = version, |
| 141 |
__package_release = release, |
| 142 |
__package_summary = summary, |
| 143 |
__package_license = license, |
| 144 |
|
| 145 |
__package_scap_location = scap_location, |
| 146 |
|
| 147 |
__package_sources = sources_list, |
| 148 |
__package_openscap_version = "0.9.12", |
| 149 |
__package_prepper = prepper, |
| 150 |
__package_installer = installer, |
| 151 |
__package_installed_files = installed_files, |
| 152 |
__changelog_date = datetime.date.today().strftime("%a %b %d %Y") |
| 153 |
) |
| 154 |
|
| 155 |
target_file.write(spec_source) |
| 156 |
|
| 157 |
def main(): |
| 158 |
parser = argparse.ArgumentParser( |
| 159 |
description = "Takes given SCAP input(s) and makes an RPM package that contains them. " |
| 160 |
"The result RPM can be installed using # yum install ./package-name-1-1.rpm " |
| 161 |
"which will put the contents into /usr/share/xml/scap. No dependency on OpenSCAP " |
| 162 |
"is enforced in the output package, so you can use any SCAP-capable scanner to " |
| 163 |
"evaluate the content.") |
| 164 |
|
| 165 |
|
| 166 |
parser.add_argument("--pkg-name", dest = "pkg_name", default = None, |
| 167 |
help = "Name of the RPM package, if none is provided the basename of the first SCAP input is used. Ex.: xyz-security-guide") |
| 168 |
parser.add_argument("--pkg-version", dest = "pkg_version", default = "1") |
| 169 |
parser.add_argument("--pkg-release", dest = "pkg_release", default = "1") |
| 170 |
parser.add_argument("--pkg-summary", dest = "pkg_summary", default = "stub", |
| 171 |
help = "Optional short description of the package.") |
| 172 |
parser.add_argument("--pkg-license", dest = "pkg_license", default = "Unknown", |
| 173 |
help = "Short name of the license that you want to publish the package under. Ex.: GPLv2+, BSD, ...") |
| 174 |
|
| 175 |
parser.add_argument("--pkg-scap-location", dest = "pkg_scap_location", default = "%{_datadir}/xml/scap", |
| 176 |
help = "Folder where SCAP files are supposed to be installed. Each package will have its own folder " |
| 177 |
"inside this folder. RPM variables can be used and will be expanded as usual. It is " |
| 178 |
"recommended to keep the default settings.") |
| 179 |
|
| 180 |
parser.add_argument("--rpm-destination", dest = "rpm_destination", default = ".", |
| 181 |
help = "The folder (absolute or relative to CWD) where the result RPM shall be saved.") |
| 182 |
parser.add_argument("--srpm-destination", dest = "srpm_destination", default = None, |
| 183 |
help = "The folder (absolute or relative to CWD) where the result SRPM shall be saved.") |
| 184 |
parser.add_argument("files", metavar = "FILE", nargs = "+", |
| 185 |
help = "List of files that should be put into the result package. " |
| 186 |
"These should be SCAP XML files but such requirement is not enforced.") |
| 187 |
|
| 188 |
args = parser.parse_args() |
| 189 |
|
| 190 |
if not args.rpm_destination: |
| 191 |
print("--rpm-destination has to be specified") |
| 192 |
parser.print_help() |
| 193 |
return 1 |
| 194 |
|
| 195 |
if not os.path.isdir(args.rpm_destination): |
| 196 |
print("'%s' does not seem like a directory or isn't accessible!" % args.rpm_destination) |
| 197 |
parser.print_help() |
| 198 |
return 1 |
| 199 |
|
| 200 |
if args.srpm_destination is not None and not os.path.isdir(args.srpm_destination): |
| 201 |
print("'%s' does not seem like a directory or isn't accessible!" % args.srpm_destination) |
| 202 |
parser.print_help() |
| 203 |
return 1 |
| 204 |
|
| 205 |
for fpath in args.files: |
| 206 |
if not os.path.exists(fpath): |
| 207 |
print("File '%s' does not exist or isn't accessible!" % fpath) |
| 208 |
parser.print_help() |
| 209 |
return 1 |
| 210 |
|
| 211 |
args.files = [open(fpath, "r") for fpath in args.files] |
| 212 |
|
| 213 |
rpmbuild_sources_path, rpmbuild_rpm_path, rpmbuild_srpm_path = get_rpmbuild_paths() |
| 214 |
|
| 215 |
copy_sources_to_rpmbuild(rpmbuild_sources_path, args.files) |
| 216 |
|
| 217 |
name = args.pkg_name |
| 218 |
if name is None: |
| 219 |
name, _ = os.path.splitext(os.path.basename(args.files[0].name)) |
| 220 |
|
| 221 |
temp_dir = tempfile.mkdtemp() |
| 222 |
spec_file = open("%s/%s.spec" % (temp_dir, name), "w") |
| 223 |
try: |
| 224 |
create_spec("templates/package.spec", |
| 225 |
name, |
| 226 |
args.pkg_version, args.pkg_release, args.pkg_summary, args.pkg_license, |
| 227 |
args.pkg_scap_location, args.files, |
| 228 |
spec_file) |
| 229 |
spec_file_path = spec_file.name |
| 230 |
spec_file.close() |
| 231 |
|
| 232 |
ret = subprocess.call(["rpmbuild", "-ba", spec_file_path], stdout = sys.stdout) |
| 233 |
if ret != 0: |
| 234 |
raise CannotContinueError("Failed to build RPM and SRPM for %s" % spec_file_path) |
| 235 |
|
| 236 |
finally: |
| 237 |
shutil.rmtree(temp_dir) |
| 238 |
|
| 239 |
rpm_basename = "%s-%s-%s.noarch.rpm" % (name, args.pkg_version, args.pkg_release) |
| 240 |
srpm_basename = "%s-%s-%s.src.rpm" % (name, args.pkg_version, args.pkg_release) |
| 241 |
|
| 242 |
shutil.copy(os.path.join(rpmbuild_rpm_path, "noarch", rpm_basename), os.path.join(args.rpm_destination, rpm_basename)) |
| 243 |
if args.srpm_destination is not None: |
| 244 |
shutil.copy(os.path.join(rpmbuild_srpm_path, srpm_basename), os.path.join(args.srpm_destination, srpm_basename)) |
| 245 |
|
| 246 |
print("") |
| 247 |
print("Resulting RPM:\t'%s'" % (os.path.join(args.rpm_destination, rpm_basename))) |
| 248 |
if args.srpm_destination is not None: |
| 249 |
print("Resulting SRPM:\t'%s'" % (os.path.join(args.srpm_destination, srpm_basename))) |
| 250 |
print("") |
| 251 |
print("Finished!") |
| 252 |
|
| 253 |
return 0 |
| 254 |
|
| 255 |
if __name__ == "__main__": |
| 256 |
try: |
| 257 |
ret = main() |
| 258 |
sys.exit(ret) |
| 259 |
except CannotContinueError as e: |
| 260 |
sys.stderr.write("%s\n" % e.message) |
| 261 |
sys.exit(1) |