bdist_rpm.py 21 KB

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