bdist_rpm.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. """distutils.command.bdist_rpm
  2. Implements the Distutils 'bdist_rpm' command (create RPM source and binary
  3. distributions)."""
  4. import subprocess, sys, os
  5. from distutils.core import Command
  6. from distutils.debug import DEBUG
  7. from distutils.util import get_platform
  8. from distutils.file_util import write_file
  9. from distutils.errors import *
  10. from distutils.sysconfig import get_python_version
  11. from distutils import log
  12. class bdist_rpm(Command):
  13. description = "create an RPM distribution"
  14. user_options = [
  15. ('bdist-base=', None,
  16. "base directory for creating built distributions"),
  17. ('rpm-base=', None,
  18. "base directory for creating RPMs (defaults to \"rpm\" under "
  19. "--bdist-base; must be specified for RPM 2)"),
  20. ('dist-dir=', 'd',
  21. "directory to put final RPM files in "
  22. "(and .spec files if --spec-only)"),
  23. ('python=', None,
  24. "path to Python interpreter to hard-code in the .spec file "
  25. "(default: \"python\")"),
  26. ('fix-python', None,
  27. "hard-code the exact path to the current Python interpreter in "
  28. "the .spec file"),
  29. ('spec-only', None,
  30. "only regenerate spec file"),
  31. ('source-only', None,
  32. "only generate source RPM"),
  33. ('binary-only', None,
  34. "only generate binary RPM"),
  35. ('use-bzip2', None,
  36. "use bzip2 instead of gzip to create source distribution"),
  37. # More meta-data: too RPM-specific to put in the setup script,
  38. # but needs to go in the .spec file -- so we make these options
  39. # to "bdist_rpm". The idea is that packagers would put this
  40. # info in setup.cfg, although they are of course free to
  41. # supply it on the command line.
  42. ('distribution-name=', None,
  43. "name of the (Linux) distribution to which this "
  44. "RPM applies (*not* the name of the module distribution!)"),
  45. ('group=', None,
  46. "package classification [default: \"Development/Libraries\"]"),
  47. ('release=', None,
  48. "RPM release number"),
  49. ('serial=', None,
  50. "RPM serial number"),
  51. ('vendor=', None,
  52. "RPM \"vendor\" (eg. \"Joe Blow <joe@example.com>\") "
  53. "[default: maintainer or author from setup script]"),
  54. ('packager=', None,
  55. "RPM packager (eg. \"Jane Doe <jane@example.net>\")"
  56. "[default: vendor]"),
  57. ('doc-files=', None,
  58. "list of documentation files (space or comma-separated)"),
  59. ('changelog=', None,
  60. "RPM changelog"),
  61. ('icon=', None,
  62. "name of icon file"),
  63. ('provides=', None,
  64. "capabilities provided by this package"),
  65. ('requires=', None,
  66. "capabilities required by this package"),
  67. ('conflicts=', None,
  68. "capabilities which conflict with this package"),
  69. ('build-requires=', None,
  70. "capabilities required to build this package"),
  71. ('obsoletes=', None,
  72. "capabilities made obsolete by this package"),
  73. ('no-autoreq', None,
  74. "do not automatically calculate dependencies"),
  75. # Actions to take when building RPM
  76. ('keep-temp', 'k',
  77. "don't clean up RPM build directory"),
  78. ('no-keep-temp', None,
  79. "clean up RPM build directory [default]"),
  80. ('use-rpm-opt-flags', None,
  81. "compile with RPM_OPT_FLAGS when building from source RPM"),
  82. ('no-rpm-opt-flags', None,
  83. "do not pass any RPM CFLAGS to compiler"),
  84. ('rpm3-mode', None,
  85. "RPM 3 compatibility mode (default)"),
  86. ('rpm2-mode', None,
  87. "RPM 2 compatibility mode"),
  88. # Add the hooks necessary for specifying custom scripts
  89. ('prep-script=', None,
  90. "Specify a script for the PREP phase of RPM building"),
  91. ('build-script=', None,
  92. "Specify a script for the BUILD phase of RPM building"),
  93. ('pre-install=', None,
  94. "Specify a script for the pre-INSTALL phase of RPM building"),
  95. ('install-script=', None,
  96. "Specify a script for the INSTALL phase of RPM building"),
  97. ('post-install=', None,
  98. "Specify a script for the post-INSTALL phase of RPM building"),
  99. ('pre-uninstall=', None,
  100. "Specify a script for the pre-UNINSTALL phase of RPM building"),
  101. ('post-uninstall=', None,
  102. "Specify a script for the post-UNINSTALL phase of RPM building"),
  103. ('clean-script=', None,
  104. "Specify a script for the CLEAN phase of RPM building"),
  105. ('verify-script=', None,
  106. "Specify a script for the VERIFY phase of the RPM build"),
  107. # Allow a packager to explicitly force an architecture
  108. ('force-arch=', None,
  109. "Force an architecture onto the RPM build process"),
  110. ('quiet', 'q',
  111. "Run the INSTALL phase of RPM building in quiet mode"),
  112. ]
  113. boolean_options = ['keep-temp', 'use-rpm-opt-flags', 'rpm3-mode',
  114. 'no-autoreq', 'quiet']
  115. negative_opt = {'no-keep-temp': 'keep-temp',
  116. 'no-rpm-opt-flags': 'use-rpm-opt-flags',
  117. 'rpm2-mode': 'rpm3-mode'}
  118. def initialize_options(self):
  119. self.bdist_base = None
  120. self.rpm_base = None
  121. self.dist_dir = None
  122. self.python = None
  123. self.fix_python = None
  124. self.spec_only = None
  125. self.binary_only = None
  126. self.source_only = None
  127. self.use_bzip2 = None
  128. self.distribution_name = None
  129. self.group = None
  130. self.release = None
  131. self.serial = None
  132. self.vendor = None
  133. self.packager = None
  134. self.doc_files = None
  135. self.changelog = None
  136. self.icon = None
  137. self.prep_script = None
  138. self.build_script = None
  139. self.install_script = None
  140. self.clean_script = None
  141. self.verify_script = None
  142. self.pre_install = None
  143. self.post_install = None
  144. self.pre_uninstall = None
  145. self.post_uninstall = None
  146. self.prep = None
  147. self.provides = None
  148. self.requires = None
  149. self.conflicts = None
  150. self.build_requires = None
  151. self.obsoletes = None
  152. self.keep_temp = 0
  153. self.use_rpm_opt_flags = 1
  154. self.rpm3_mode = 1
  155. self.no_autoreq = 0
  156. self.force_arch = None
  157. self.quiet = 0
  158. def finalize_options(self):
  159. self.set_undefined_options('bdist', ('bdist_base', 'bdist_base'))
  160. if self.rpm_base is None:
  161. if not self.rpm3_mode:
  162. raise DistutilsOptionError(
  163. "you must specify --rpm-base in RPM 2 mode")
  164. self.rpm_base = os.path.join(self.bdist_base, "rpm")
  165. if self.python is None:
  166. if self.fix_python:
  167. self.python = sys.executable
  168. else:
  169. self.python = "python3"
  170. elif self.fix_python:
  171. raise DistutilsOptionError(
  172. "--python and --fix-python are mutually exclusive options")
  173. if os.name != 'posix':
  174. raise DistutilsPlatformError("don't know how to create RPM "
  175. "distributions on platform %s" % os.name)
  176. if self.binary_only and self.source_only:
  177. raise DistutilsOptionError(
  178. "cannot supply both '--source-only' and '--binary-only'")
  179. # don't pass CFLAGS to pure python distributions
  180. if not self.distribution.has_ext_modules():
  181. self.use_rpm_opt_flags = 0
  182. self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
  183. self.finalize_package_data()
  184. def finalize_package_data(self):
  185. self.ensure_string('group', "Development/Libraries")
  186. self.ensure_string('vendor',
  187. "%s <%s>" % (self.distribution.get_contact(),
  188. self.distribution.get_contact_email()))
  189. self.ensure_string('packager')
  190. self.ensure_string_list('doc_files')
  191. if isinstance(self.doc_files, list):
  192. for readme in ('README', 'README.txt'):
  193. if os.path.exists(readme) and readme not in self.doc_files:
  194. self.doc_files.append(readme)
  195. self.ensure_string('release', "1")
  196. self.ensure_string('serial') # should it be an int?
  197. self.ensure_string('distribution_name')
  198. self.ensure_string('changelog')
  199. # Format changelog correctly
  200. self.changelog = self._format_changelog(self.changelog)
  201. self.ensure_filename('icon')
  202. self.ensure_filename('prep_script')
  203. self.ensure_filename('build_script')
  204. self.ensure_filename('install_script')
  205. self.ensure_filename('clean_script')
  206. self.ensure_filename('verify_script')
  207. self.ensure_filename('pre_install')
  208. self.ensure_filename('post_install')
  209. self.ensure_filename('pre_uninstall')
  210. self.ensure_filename('post_uninstall')
  211. # XXX don't forget we punted on summaries and descriptions -- they
  212. # should be handled here eventually!
  213. # Now *this* is some meta-data that belongs in the setup script...
  214. self.ensure_string_list('provides')
  215. self.ensure_string_list('requires')
  216. self.ensure_string_list('conflicts')
  217. self.ensure_string_list('build_requires')
  218. self.ensure_string_list('obsoletes')
  219. self.ensure_string('force_arch')
  220. def run(self):
  221. if DEBUG:
  222. print("before _get_package_data():")
  223. print("vendor =", self.vendor)
  224. print("packager =", self.packager)
  225. print("doc_files =", self.doc_files)
  226. print("changelog =", self.changelog)
  227. # make directories
  228. if self.spec_only:
  229. spec_dir = self.dist_dir
  230. self.mkpath(spec_dir)
  231. else:
  232. rpm_dir = {}
  233. for d in ('SOURCES', 'SPECS', 'BUILD', 'RPMS', 'SRPMS'):
  234. rpm_dir[d] = os.path.join(self.rpm_base, d)
  235. self.mkpath(rpm_dir[d])
  236. spec_dir = rpm_dir['SPECS']
  237. # Spec file goes into 'dist_dir' if '--spec-only specified',
  238. # build/rpm.<plat> otherwise.
  239. spec_path = os.path.join(spec_dir,
  240. "%s.spec" % self.distribution.get_name())
  241. self.execute(write_file,
  242. (spec_path,
  243. self._make_spec_file()),
  244. "writing '%s'" % spec_path)
  245. if self.spec_only: # stop if requested
  246. return
  247. # Make a source distribution and copy to SOURCES directory with
  248. # optional icon.
  249. saved_dist_files = self.distribution.dist_files[:]
  250. sdist = self.reinitialize_command('sdist')
  251. if self.use_bzip2:
  252. sdist.formats = ['bztar']
  253. else:
  254. sdist.formats = ['gztar']
  255. self.run_command('sdist')
  256. self.distribution.dist_files = saved_dist_files
  257. source = sdist.get_archive_files()[0]
  258. source_dir = rpm_dir['SOURCES']
  259. self.copy_file(source, source_dir)
  260. if self.icon:
  261. if os.path.exists(self.icon):
  262. self.copy_file(self.icon, source_dir)
  263. else:
  264. raise DistutilsFileError(
  265. "icon file '%s' does not exist" % self.icon)
  266. # build package
  267. log.info("building RPMs")
  268. rpm_cmd = ['rpm']
  269. if os.path.exists('/usr/bin/rpmbuild') or \
  270. os.path.exists('/bin/rpmbuild'):
  271. rpm_cmd = ['rpmbuild']
  272. if self.source_only: # what kind of RPMs?
  273. rpm_cmd.append('-bs')
  274. elif self.binary_only:
  275. rpm_cmd.append('-bb')
  276. else:
  277. rpm_cmd.append('-ba')
  278. rpm_cmd.extend(['--define', '__python %s' % self.python])
  279. if self.rpm3_mode:
  280. rpm_cmd.extend(['--define',
  281. '_topdir %s' % os.path.abspath(self.rpm_base)])
  282. if not self.keep_temp:
  283. rpm_cmd.append('--clean')
  284. if self.quiet:
  285. rpm_cmd.append('--quiet')
  286. rpm_cmd.append(spec_path)
  287. # Determine the binary rpm names that should be built out of this spec
  288. # file
  289. # Note that some of these may not be really built (if the file
  290. # list is empty)
  291. nvr_string = "%{name}-%{version}-%{release}"
  292. src_rpm = nvr_string + ".src.rpm"
  293. non_src_rpm = "%{arch}/" + nvr_string + ".%{arch}.rpm"
  294. q_cmd = r"rpm -q --qf '%s %s\n' --specfile '%s'" % (
  295. src_rpm, non_src_rpm, spec_path)
  296. out = os.popen(q_cmd)
  297. try:
  298. binary_rpms = []
  299. source_rpm = None
  300. while True:
  301. line = out.readline()
  302. if not line:
  303. break
  304. l = line.strip().split()
  305. assert(len(l) == 2)
  306. binary_rpms.append(l[1])
  307. # The source rpm is named after the first entry in the spec file
  308. if source_rpm is None:
  309. source_rpm = l[0]
  310. status = out.close()
  311. if status:
  312. raise DistutilsExecError("Failed to execute: %s" % repr(q_cmd))
  313. finally:
  314. out.close()
  315. self.spawn(rpm_cmd)
  316. if not self.dry_run:
  317. if self.distribution.has_ext_modules():
  318. pyversion = get_python_version()
  319. else:
  320. pyversion = 'any'
  321. if not self.binary_only:
  322. srpm = os.path.join(rpm_dir['SRPMS'], source_rpm)
  323. assert(os.path.exists(srpm))
  324. self.move_file(srpm, self.dist_dir)
  325. filename = os.path.join(self.dist_dir, source_rpm)
  326. self.distribution.dist_files.append(
  327. ('bdist_rpm', pyversion, filename))
  328. if not self.source_only:
  329. for rpm in binary_rpms:
  330. rpm = os.path.join(rpm_dir['RPMS'], rpm)
  331. if os.path.exists(rpm):
  332. self.move_file(rpm, self.dist_dir)
  333. filename = os.path.join(self.dist_dir,
  334. os.path.basename(rpm))
  335. self.distribution.dist_files.append(
  336. ('bdist_rpm', pyversion, filename))
  337. def _dist_path(self, path):
  338. return os.path.join(self.dist_dir, os.path.basename(path))
  339. def _make_spec_file(self):
  340. """Generate the text of an RPM spec file and return it as a
  341. list of strings (one per line).
  342. """
  343. # definitions and headers
  344. spec_file = [
  345. '%define name ' + self.distribution.get_name(),
  346. '%define version ' + self.distribution.get_version().replace('-','_'),
  347. '%define unmangled_version ' + self.distribution.get_version(),
  348. '%define release ' + self.release.replace('-','_'),
  349. '',
  350. 'Summary: ' + self.distribution.get_description(),
  351. ]
  352. # Workaround for #14443 which affects some RPM based systems such as
  353. # RHEL6 (and probably derivatives)
  354. vendor_hook = subprocess.getoutput('rpm --eval %{__os_install_post}')
  355. # Generate a potential replacement value for __os_install_post (whilst
  356. # normalizing the whitespace to simplify the test for whether the
  357. # invocation of brp-python-bytecompile passes in __python):
  358. vendor_hook = '\n'.join([' %s \\' % line.strip()
  359. for line in vendor_hook.splitlines()])
  360. problem = "brp-python-bytecompile \\\n"
  361. fixed = "brp-python-bytecompile %{__python} \\\n"
  362. fixed_hook = vendor_hook.replace(problem, fixed)
  363. if fixed_hook != vendor_hook:
  364. spec_file.append('# Workaround for http://bugs.python.org/issue14443')
  365. spec_file.append('%define __os_install_post ' + fixed_hook + '\n')
  366. # put locale summaries into spec file
  367. # XXX not supported for now (hard to put a dictionary
  368. # in a config file -- arg!)
  369. #for locale in self.summaries.keys():
  370. # spec_file.append('Summary(%s): %s' % (locale,
  371. # self.summaries[locale]))
  372. spec_file.extend([
  373. 'Name: %{name}',
  374. 'Version: %{version}',
  375. 'Release: %{release}',])
  376. # XXX yuck! this filename is available from the "sdist" command,
  377. # but only after it has run: and we create the spec file before
  378. # running "sdist", in case of --spec-only.
  379. if self.use_bzip2:
  380. spec_file.append('Source0: %{name}-%{unmangled_version}.tar.bz2')
  381. else:
  382. spec_file.append('Source0: %{name}-%{unmangled_version}.tar.gz')
  383. spec_file.extend([
  384. 'License: ' + self.distribution.get_license(),
  385. 'Group: ' + self.group,
  386. 'BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot',
  387. 'Prefix: %{_prefix}', ])
  388. if not self.force_arch:
  389. # noarch if no extension modules
  390. if not self.distribution.has_ext_modules():
  391. spec_file.append('BuildArch: noarch')
  392. else:
  393. spec_file.append( 'BuildArch: %s' % self.force_arch )
  394. for field in ('Vendor',
  395. 'Packager',
  396. 'Provides',
  397. 'Requires',
  398. 'Conflicts',
  399. 'Obsoletes',
  400. ):
  401. val = getattr(self, field.lower())
  402. if isinstance(val, list):
  403. spec_file.append('%s: %s' % (field, ' '.join(val)))
  404. elif val is not None:
  405. spec_file.append('%s: %s' % (field, val))
  406. if self.distribution.get_url() != 'UNKNOWN':
  407. spec_file.append('Url: ' + self.distribution.get_url())
  408. if self.distribution_name:
  409. spec_file.append('Distribution: ' + self.distribution_name)
  410. if self.build_requires:
  411. spec_file.append('BuildRequires: ' +
  412. ' '.join(self.build_requires))
  413. if self.icon:
  414. spec_file.append('Icon: ' + os.path.basename(self.icon))
  415. if self.no_autoreq:
  416. spec_file.append('AutoReq: 0')
  417. spec_file.extend([
  418. '',
  419. '%description',
  420. self.distribution.get_long_description()
  421. ])
  422. # put locale descriptions into spec file
  423. # XXX again, suppressed because config file syntax doesn't
  424. # easily support this ;-(
  425. #for locale in self.descriptions.keys():
  426. # spec_file.extend([
  427. # '',
  428. # '%description -l ' + locale,
  429. # self.descriptions[locale],
  430. # ])
  431. # rpm scripts
  432. # figure out default build script
  433. def_setup_call = "%s %s" % (self.python,os.path.basename(sys.argv[0]))
  434. def_build = "%s build" % def_setup_call
  435. if self.use_rpm_opt_flags:
  436. def_build = 'env CFLAGS="$RPM_OPT_FLAGS" ' + def_build
  437. # insert contents of files
  438. # XXX this is kind of misleading: user-supplied options are files
  439. # that we open and interpolate into the spec file, but the defaults
  440. # are just text that we drop in as-is. Hmmm.
  441. install_cmd = ('%s install -O1 --root=$RPM_BUILD_ROOT '
  442. '--record=INSTALLED_FILES') % def_setup_call
  443. script_options = [
  444. ('prep', 'prep_script', "%setup -n %{name}-%{unmangled_version}"),
  445. ('build', 'build_script', def_build),
  446. ('install', 'install_script', install_cmd),
  447. ('clean', 'clean_script', "rm -rf $RPM_BUILD_ROOT"),
  448. ('verifyscript', 'verify_script', None),
  449. ('pre', 'pre_install', None),
  450. ('post', 'post_install', None),
  451. ('preun', 'pre_uninstall', None),
  452. ('postun', 'post_uninstall', None),
  453. ]
  454. for (rpm_opt, attr, default) in script_options:
  455. # Insert contents of file referred to, if no file is referred to
  456. # use 'default' as contents of script
  457. val = getattr(self, attr)
  458. if val or default:
  459. spec_file.extend([
  460. '',
  461. '%' + rpm_opt,])
  462. if val:
  463. spec_file.extend(open(val, 'r').read().split('\n'))
  464. else:
  465. spec_file.append(default)
  466. # files section
  467. spec_file.extend([
  468. '',
  469. '%files -f INSTALLED_FILES',
  470. '%defattr(-,root,root)',
  471. ])
  472. if self.doc_files:
  473. spec_file.append('%doc ' + ' '.join(self.doc_files))
  474. if self.changelog:
  475. spec_file.extend([
  476. '',
  477. '%changelog',])
  478. spec_file.extend(self.changelog)
  479. return spec_file
  480. def _format_changelog(self, changelog):
  481. """Format the changelog correctly and convert it to a list of strings
  482. """
  483. if not changelog:
  484. return changelog
  485. new_changelog = []
  486. for line in changelog.strip().split('\n'):
  487. line = line.strip()
  488. if line[0] == '*':
  489. new_changelog.extend(['', line])
  490. elif line[0] == '-':
  491. new_changelog.append(line)
  492. else:
  493. new_changelog.append(' ' + line)
  494. # strip trailing newline inserted by first changelog entry
  495. if not new_changelog[0]:
  496. del new_changelog[0]
  497. return new_changelog