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.

311 lines
10 KiB

6 years ago
  1. """SCons.Tool.Packaging
  2. SCons Packaging Tool.
  3. """
  4. #
  5. # Copyright (c) 2001 - 2017 The SCons Foundation
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files (the
  9. # "Software"), to deal in the Software without restriction, including
  10. # without limitation the rights to use, copy, modify, merge, publish,
  11. # distribute, sublicense, and/or sell copies of the Software, and to
  12. # permit persons to whom the Software is furnished to do so, subject to
  13. # the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included
  16. # in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  19. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. __revision__ = "src/engine/SCons/Tool/packaging/__init__.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  26. import SCons.Environment
  27. from SCons.Variables import *
  28. from SCons.Errors import *
  29. from SCons.Util import is_List, make_path_relative
  30. from SCons.Warnings import warn, Warning
  31. import os, imp
  32. import SCons.Defaults
  33. __all__ = [ 'src_targz', 'src_tarbz2', 'src_zip', 'tarbz2', 'targz', 'zip', 'rpm', 'msi', 'ipk' ]
  34. #
  35. # Utility and Builder function
  36. #
  37. def Tag(env, target, source, *more_tags, **kw_tags):
  38. """ Tag a file with the given arguments, just sets the accordingly named
  39. attribute on the file object.
  40. TODO: FIXME
  41. """
  42. if not target:
  43. target=source
  44. first_tag=None
  45. else:
  46. first_tag=source
  47. if first_tag:
  48. kw_tags[first_tag[0]] = ''
  49. if len(kw_tags) == 0 and len(more_tags) == 0:
  50. raise UserError("No tags given.")
  51. # XXX: sanity checks
  52. for x in more_tags:
  53. kw_tags[x] = ''
  54. if not SCons.Util.is_List(target):
  55. target=[target]
  56. else:
  57. # hmm, sometimes the target list, is a list of a list
  58. # make sure it is flattened prior to processing.
  59. # TODO: perhaps some bug ?!?
  60. target=env.Flatten(target)
  61. for t in target:
  62. for (k,v) in kw_tags.items():
  63. # all file tags have to start with PACKAGING_, so we can later
  64. # differentiate between "normal" object attributes and the
  65. # packaging attributes. As the user should not be bothered with
  66. # that, the prefix will be added here if missing.
  67. if k[:10] != 'PACKAGING_':
  68. k='PACKAGING_'+k
  69. t.Tag(k, v)
  70. def Package(env, target=None, source=None, **kw):
  71. """ Entry point for the package tool.
  72. """
  73. # check if we need to find the source files ourself
  74. if not source:
  75. source = env.FindInstalledFiles()
  76. if len(source)==0:
  77. raise UserError("No source for Package() given")
  78. # decide which types of packages shall be built. Can be defined through
  79. # four mechanisms: command line argument, keyword argument,
  80. # environment argument and default selection( zip or tar.gz ) in that
  81. # order.
  82. try: kw['PACKAGETYPE']=env['PACKAGETYPE']
  83. except KeyError: pass
  84. if not kw.get('PACKAGETYPE'):
  85. from SCons.Script import GetOption
  86. kw['PACKAGETYPE'] = GetOption('package_type')
  87. if kw['PACKAGETYPE'] == None:
  88. if 'Tar' in env['BUILDERS']:
  89. kw['PACKAGETYPE']='targz'
  90. elif 'Zip' in env['BUILDERS']:
  91. kw['PACKAGETYPE']='zip'
  92. else:
  93. raise UserError("No type for Package() given")
  94. PACKAGETYPE=kw['PACKAGETYPE']
  95. if not is_List(PACKAGETYPE):
  96. PACKAGETYPE=PACKAGETYPE.split(',')
  97. # load the needed packagers.
  98. def load_packager(type):
  99. try:
  100. file,path,desc=imp.find_module(type, __path__)
  101. return imp.load_module(type, file, path, desc)
  102. except ImportError as e:
  103. raise EnvironmentError("packager %s not available: %s"%(type,str(e)))
  104. packagers=list(map(load_packager, PACKAGETYPE))
  105. # set up targets and the PACKAGEROOT
  106. try:
  107. # fill up the target list with a default target name until the PACKAGETYPE
  108. # list is of the same size as the target list.
  109. if not target: target = []
  110. size_diff = len(PACKAGETYPE)-len(target)
  111. default_name = "%(NAME)s-%(VERSION)s"
  112. if size_diff>0:
  113. default_target = default_name%kw
  114. target.extend( [default_target]*size_diff )
  115. if 'PACKAGEROOT' not in kw:
  116. kw['PACKAGEROOT'] = default_name%kw
  117. except KeyError as e:
  118. raise SCons.Errors.UserError( "Missing Packagetag '%s'"%e.args[0] )
  119. # setup the source files
  120. source=env.arg2nodes(source, env.fs.Entry)
  121. # call the packager to setup the dependencies.
  122. targets=[]
  123. try:
  124. for packager in packagers:
  125. t=[target.pop(0)]
  126. t=packager.package(env,t,source, **kw)
  127. targets.extend(t)
  128. assert( len(target) == 0 )
  129. except KeyError as e:
  130. raise SCons.Errors.UserError( "Missing Packagetag '%s' for %s packager"\
  131. % (e.args[0],packager.__name__) )
  132. except TypeError as e:
  133. # this exception means that a needed argument for the packager is
  134. # missing. As our packagers get their "tags" as named function
  135. # arguments we need to find out which one is missing.
  136. from inspect import getargspec
  137. args,varargs,varkw,defaults=getargspec(packager.package)
  138. if defaults!=None:
  139. args=args[:-len(defaults)] # throw away arguments with default values
  140. args.remove('env')
  141. args.remove('target')
  142. args.remove('source')
  143. # now remove any args for which we have a value in kw.
  144. args=[x for x in args if x not in kw]
  145. if len(args)==0:
  146. raise # must be a different error, so re-raise
  147. elif len(args)==1:
  148. raise SCons.Errors.UserError( "Missing Packagetag '%s' for %s packager"\
  149. % (args[0],packager.__name__) )
  150. else:
  151. raise SCons.Errors.UserError( "Missing Packagetags '%s' for %s packager"\
  152. % (", ".join(args),packager.__name__) )
  153. target=env.arg2nodes(target, env.fs.Entry)
  154. targets.extend(env.Alias( 'package', targets ))
  155. return targets
  156. #
  157. # SCons tool initialization functions
  158. #
  159. added = None
  160. def generate(env):
  161. from SCons.Script import AddOption
  162. global added
  163. if not added:
  164. added = 1
  165. AddOption('--package-type',
  166. dest='package_type',
  167. default=None,
  168. type="string",
  169. action="store",
  170. help='The type of package to create.')
  171. try:
  172. env['BUILDERS']['Package']
  173. env['BUILDERS']['Tag']
  174. except KeyError:
  175. env['BUILDERS']['Package'] = Package
  176. env['BUILDERS']['Tag'] = Tag
  177. def exists(env):
  178. return 1
  179. # XXX
  180. def options(opts):
  181. opts.AddVariables(
  182. EnumVariable( 'PACKAGETYPE',
  183. 'the type of package to create.',
  184. None, allowed_values=list(map( str, __all__ )),
  185. ignorecase=2
  186. )
  187. )
  188. #
  189. # Internal utility functions
  190. #
  191. def copy_attr(f1, f2):
  192. """ copies the special packaging file attributes from f1 to f2.
  193. """
  194. copyit = lambda x: not hasattr(f2, x) and x[:10] == 'PACKAGING_'
  195. if f1._tags:
  196. pattrs = [tag for tag in f1._tags if copyit(tag)]
  197. for attr in pattrs:
  198. f2.Tag(attr, f1.GetTag(attr))
  199. def putintopackageroot(target, source, env, pkgroot, honor_install_location=1):
  200. """ Uses the CopyAs builder to copy all source files to the directory given
  201. in pkgroot.
  202. If honor_install_location is set and the copied source file has an
  203. PACKAGING_INSTALL_LOCATION attribute, the PACKAGING_INSTALL_LOCATION is
  204. used as the new name of the source file under pkgroot.
  205. The source file will not be copied if it is already under the the pkgroot
  206. directory.
  207. All attributes of the source file will be copied to the new file.
  208. """
  209. # make sure the packageroot is a Dir object.
  210. if SCons.Util.is_String(pkgroot): pkgroot=env.Dir(pkgroot)
  211. if not SCons.Util.is_List(source): source=[source]
  212. new_source = []
  213. for file in source:
  214. if SCons.Util.is_String(file): file = env.File(file)
  215. if file.is_under(pkgroot):
  216. new_source.append(file)
  217. else:
  218. if file.GetTag('PACKAGING_INSTALL_LOCATION') and\
  219. honor_install_location:
  220. new_name=make_path_relative(file.GetTag('PACKAGING_INSTALL_LOCATION'))
  221. else:
  222. new_name=make_path_relative(file.get_path())
  223. new_file=pkgroot.File(new_name)
  224. new_file=env.CopyAs(new_file, file)[0]
  225. copy_attr(file, new_file)
  226. new_source.append(new_file)
  227. return (target, new_source)
  228. def stripinstallbuilder(target, source, env):
  229. """ Strips the install builder action from the source list and stores
  230. the final installation location as the "PACKAGING_INSTALL_LOCATION" of
  231. the source of the source file. This effectively removes the final installed
  232. files from the source list while remembering the installation location.
  233. It also warns about files which have no install builder attached.
  234. """
  235. def has_no_install_location(file):
  236. return not (file.has_builder() and\
  237. hasattr(file.builder, 'name') and\
  238. (file.builder.name=="InstallBuilder" or\
  239. file.builder.name=="InstallAsBuilder"))
  240. if len([src for src in source if has_no_install_location(src)]):
  241. warn(Warning, "there are files to package which have no\
  242. InstallBuilder attached, this might lead to irreproducible packages")
  243. n_source=[]
  244. for s in source:
  245. if has_no_install_location(s):
  246. n_source.append(s)
  247. else:
  248. for ss in s.sources:
  249. n_source.append(ss)
  250. copy_attr(s, ss)
  251. ss.Tag('PACKAGING_INSTALL_LOCATION', s.get_path())
  252. return (target, n_source)
  253. # Local Variables:
  254. # tab-width:4
  255. # indent-tabs-mode:nil
  256. # End:
  257. # vim: set expandtab tabstop=4 shiftwidth=4: