You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.5 KiB

6 years ago
  1. """SCons.Tool.rpm
  2. Tool-specific initialization for rpm.
  3. There normally shouldn't be any need to import this module directly.
  4. It will usually be imported through the generic SCons.Tool.Tool()
  5. selection method.
  6. The rpm tool calls the rpmbuild command. The first and only argument should a
  7. tar.gz consisting of the source file and a specfile.
  8. """
  9. #
  10. # Copyright (c) 2001 - 2017 The SCons Foundation
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining
  13. # a copy of this software and associated documentation files (the
  14. # "Software"), to deal in the Software without restriction, including
  15. # without limitation the rights to use, copy, modify, merge, publish,
  16. # distribute, sublicense, and/or sell copies of the Software, and to
  17. # permit persons to whom the Software is furnished to do so, subject to
  18. # the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included
  21. # in all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  24. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  25. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. #
  31. __revision__ = "src/engine/SCons/Tool/rpm.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  32. import os
  33. import re
  34. import shutil
  35. import subprocess
  36. import SCons.Builder
  37. import SCons.Node.FS
  38. import SCons.Util
  39. import SCons.Action
  40. import SCons.Defaults
  41. def get_cmd(source, env):
  42. tar_file_with_included_specfile = source
  43. if SCons.Util.is_List(source):
  44. tar_file_with_included_specfile = source[0]
  45. return "%s %s %s"%(env['RPM'], env['RPMFLAGS'],
  46. tar_file_with_included_specfile.get_abspath() )
  47. def build_rpm(target, source, env):
  48. # create a temporary rpm build root.
  49. tmpdir = os.path.join( os.path.dirname( target[0].get_abspath() ), 'rpmtemp' )
  50. if os.path.exists(tmpdir):
  51. shutil.rmtree(tmpdir)
  52. # now create the mandatory rpm directory structure.
  53. for d in ['RPMS', 'SRPMS', 'SPECS', 'BUILD']:
  54. os.makedirs( os.path.join( tmpdir, d ) )
  55. # set the topdir as an rpmflag.
  56. env.Prepend( RPMFLAGS = '--define \'_topdir %s\'' % tmpdir )
  57. # now call rpmbuild to create the rpm package.
  58. handle = subprocess.Popen(get_cmd(source, env),
  59. stdout=subprocess.PIPE,
  60. stderr=subprocess.STDOUT,
  61. shell=True)
  62. output = SCons.Util.to_str(handle.stdout.read())
  63. status = handle.wait()
  64. if status:
  65. raise SCons.Errors.BuildError( node=target[0],
  66. errstr=output,
  67. filename=str(target[0]) )
  68. else:
  69. # XXX: assume that LC_ALL=C is set while running rpmbuild
  70. output_files = re.compile( 'Wrote: (.*)' ).findall( output )
  71. for output, input in zip( output_files, target ):
  72. rpm_output = os.path.basename(output)
  73. expected = os.path.basename(input.get_path())
  74. assert expected == rpm_output, "got %s but expected %s" % (rpm_output, expected)
  75. shutil.copy( output, input.get_abspath() )
  76. # cleanup before leaving.
  77. shutil.rmtree(tmpdir)
  78. return status
  79. def string_rpm(target, source, env):
  80. try:
  81. return env['RPMCOMSTR']
  82. except KeyError:
  83. return get_cmd(source, env)
  84. rpmAction = SCons.Action.Action(build_rpm, string_rpm)
  85. RpmBuilder = SCons.Builder.Builder(action = SCons.Action.Action('$RPMCOM', '$RPMCOMSTR'),
  86. source_scanner = SCons.Defaults.DirScanner,
  87. suffix = '$RPMSUFFIX')
  88. def generate(env):
  89. """Add Builders and construction variables for rpm to an Environment."""
  90. try:
  91. bld = env['BUILDERS']['Rpm']
  92. except KeyError:
  93. bld = RpmBuilder
  94. env['BUILDERS']['Rpm'] = bld
  95. env.SetDefault(RPM = 'LC_ALL=C rpmbuild')
  96. env.SetDefault(RPMFLAGS = SCons.Util.CLVar('-ta'))
  97. env.SetDefault(RPMCOM = rpmAction)
  98. env.SetDefault(RPMSUFFIX = '.rpm')
  99. def exists(env):
  100. return env.Detect('rpmbuild')
  101. # Local Variables:
  102. # tab-width:4
  103. # indent-tabs-mode:nil
  104. # End:
  105. # vim: set expandtab tabstop=4 shiftwidth=4: