export.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Development tool - export command plugin
  2. #
  3. # Copyright (C) 2014-2017 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. """Devtool export plugin"""
  18. import os
  19. import argparse
  20. import tarfile
  21. import logging
  22. import datetime
  23. import json
  24. logger = logging.getLogger('devtool')
  25. # output files
  26. default_arcname_prefix = "workspace-export"
  27. metadata = '.export_metadata'
  28. def export(args, config, basepath, workspace):
  29. """Entry point for the devtool 'export' subcommand"""
  30. def add_metadata(tar):
  31. """Archive the workspace object"""
  32. # finally store the workspace metadata
  33. with open(metadata, 'w') as fd:
  34. fd.write(json.dumps((config.workspace_path, workspace)))
  35. tar.add(metadata)
  36. os.unlink(metadata)
  37. def add_recipe(tar, recipe, data):
  38. """Archive recipe with proper arcname"""
  39. # Create a map of name/arcnames
  40. arcnames = []
  41. for key, name in data.items():
  42. if name:
  43. if key == 'srctree':
  44. # all sources, no matter where are located, goes into the sources directory
  45. arcname = 'sources/%s' % recipe
  46. else:
  47. arcname = name.replace(config.workspace_path, '')
  48. arcnames.append((name, arcname))
  49. for name, arcname in arcnames:
  50. tar.add(name, arcname=arcname)
  51. # Make sure workspace is non-empty and possible listed include/excluded recipes are in workspace
  52. if not workspace:
  53. logger.info('Workspace contains no recipes, nothing to export')
  54. return 0
  55. else:
  56. for param, recipes in {'include':args.include,'exclude':args.exclude}.items():
  57. for recipe in recipes:
  58. if recipe not in workspace:
  59. logger.error('Recipe (%s) on %s argument not in the current workspace' % (recipe, param))
  60. return 1
  61. name = args.file
  62. default_name = "%s-%s.tar.gz" % (default_arcname_prefix, datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
  63. if not name:
  64. name = default_name
  65. else:
  66. # if name is a directory, append the default name
  67. if os.path.isdir(name):
  68. name = os.path.join(name, default_name)
  69. if os.path.exists(name) and not args.overwrite:
  70. logger.error('Tar archive %s exists. Use --overwrite/-o to overwrite it')
  71. return 1
  72. # if all workspace is excluded, quit
  73. if not len(set(workspace.keys()).difference(set(args.exclude))):
  74. logger.warning('All recipes in workspace excluded, nothing to export')
  75. return 0
  76. exported = []
  77. with tarfile.open(name, 'w:gz') as tar:
  78. if args.include:
  79. for recipe in args.include:
  80. add_recipe(tar, recipe, workspace[recipe])
  81. exported.append(recipe)
  82. else:
  83. for recipe, data in workspace.items():
  84. if recipe not in args.exclude:
  85. add_recipe(tar, recipe, data)
  86. exported.append(recipe)
  87. add_metadata(tar)
  88. logger.info('Tar archive created at %s with the following recipes: %s' % (name, ', '.join(exported)))
  89. return 0
  90. def register_commands(subparsers, context):
  91. """Register devtool export subcommands"""
  92. parser = subparsers.add_parser('export',
  93. help='Export workspace into a tar archive',
  94. description='Export one or more recipes from current workspace into a tar archive',
  95. group='advanced')
  96. parser.add_argument('--file', '-f', help='Output archive file name')
  97. parser.add_argument('--overwrite', '-o', action="store_true", help='Overwrite previous export tar archive')
  98. group = parser.add_mutually_exclusive_group()
  99. group.add_argument('--include', '-i', nargs='+', default=[], help='Include recipes into the tar archive')
  100. group.add_argument('--exclude', '-e', nargs='+', default=[], help='Exclude recipes into the tar archive')
  101. parser.set_defaults(func=export)