import_orig.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2006, 2007, 2009, 2011 Guido Guenther <agx@sigxcpu.org>
  4. # (C) 2012 Intel Corporation <markus.lehtonen@linux.intel.com>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, please see
  17. # <http://www.gnu.org/licenses/>
  18. #
  19. """Common functionality for import-orig scripts"""
  20. import contextlib
  21. import os
  22. import tempfile
  23. import gbp.command_wrappers as gbpc
  24. import gbp.log
  25. from gbp.pkg import UpstreamSource
  26. from gbp.errors import GbpError
  27. from gbp.deb.upstreamsource import DebianUpstreamSource
  28. # Try to import readline, since that will cause raw_input to get fancy
  29. # line editing and history capabilities. However, if readline is not
  30. # available, raw_input will still work.
  31. try:
  32. import readline
  33. except ImportError:
  34. pass
  35. def orig_needs_repack(upstream_source, options):
  36. """
  37. Determine if the upstream sources needs to be repacked
  38. We repack if
  39. 1. we want to filter out files and use pristine tar since we want
  40. to make a filtered tarball available to pristine-tar
  41. 2. when we don't have a suitable upstream tarball (e.g. zip archive or unpacked dir)
  42. and want to use filters
  43. 3. when we don't have a suitable upstream tarball (e.g. zip archive or unpacked dir)
  44. and want to use pristine-tar
  45. """
  46. if ((options.pristine_tar and options.filter_pristine_tar and len(options.filters) > 0)):
  47. return True
  48. elif not upstream_source.is_orig():
  49. if len(options.filters):
  50. return True
  51. elif options.pristine_tar:
  52. return True
  53. return False
  54. def cleanup_tmp_tree(tree):
  55. """remove a tree of temporary files"""
  56. try:
  57. gbpc.RemoveTree(tree)()
  58. except gbpc.CommandExecFailed:
  59. gbp.log.err("Removal of tmptree %s failed." % tree)
  60. def is_link_target(target, link):
  61. """does symlink link already point to target?"""
  62. if os.path.exists(link):
  63. if os.path.samefile(target, link):
  64. return True
  65. return False
  66. def ask_package_name(default, name_validator_func, err_msg):
  67. """
  68. Ask the user for the source package name.
  69. @param default: The default package name to suggest to the user.
  70. """
  71. while True:
  72. sourcepackage = raw_input("What will be the source package name? [%s] " % default)
  73. if not sourcepackage: # No input, use the default.
  74. sourcepackage = default
  75. # Valid package name, return it.
  76. if name_validator_func(sourcepackage):
  77. return sourcepackage
  78. # Not a valid package name. Print an extra
  79. # newline before the error to make the output a
  80. # bit clearer.
  81. gbp.log.warn("\nNot a valid package name: '%s'.\n%s" % (sourcepackage, err_msg))
  82. def ask_package_version(default, ver_validator_func, err_msg):
  83. """
  84. Ask the user for the upstream package version.
  85. @param default: The default package version to suggest to the user.
  86. """
  87. while True:
  88. version = raw_input("What is the upstream version? [%s] " % default)
  89. if not version: # No input, use the default.
  90. version = default
  91. # Valid version, return it.
  92. if ver_validator_func(version):
  93. return version
  94. # Not a valid upstream version. Print an extra
  95. # newline before the error to make the output a
  96. # bit clearer.
  97. gbp.log.warn("\nNot a valid upstream version: '%s'.\n%s" % (version, err_msg))
  98. def repacked_tarball_name(source, name, version):
  99. if source.is_orig():
  100. # Repacked orig tarball needs a different name since there's already
  101. # one with that name
  102. name = os.path.join(
  103. os.path.dirname(source.path),
  104. os.path.basename(source.path).replace(".tar", ".gbp.tar"))
  105. else:
  106. # Repacked sources or other archives get canonical name
  107. name = os.path.join(
  108. os.path.dirname(source.path),
  109. "%s_%s.orig.tar.bz2" % (name, version))
  110. return name
  111. def repack_source(source, name, version, tmpdir, filters):
  112. """Repack the source tree"""
  113. name = repacked_tarball_name(source, name, version)
  114. repacked = source.pack(name, filters)
  115. if source.is_orig(): # the tarball was filtered on unpack
  116. repacked.unpacked = source.unpacked
  117. else: # otherwise unpack the generated tarball get a filtered tree
  118. if tmpdir:
  119. cleanup_tmp_tree(tmpdir)
  120. tmpdir = tempfile.mkdtemp(dir='../')
  121. repacked.unpack(tmpdir, filters)
  122. return (repacked, tmpdir)
  123. def download_orig(url):
  124. """
  125. Download orig tarball from given URL
  126. @param url: the download URL
  127. @type url: C{str}
  128. @returns: The upstream source tarball
  129. @rtype: DebianUpstreamSource
  130. @raises GbpError: on all errors
  131. """
  132. CHUNK_SIZE=4096
  133. try:
  134. import requests
  135. except ImportError:
  136. requests = None
  137. if requests is None:
  138. raise GbpError("python-requests not installed")
  139. tarball = os.path.basename(url)
  140. target = os.path.join('..', tarball)
  141. if os.path.exists(target):
  142. raise GbpError("Failed to download %s: %s already exists" % (url, target))
  143. try:
  144. with contextlib.closing(requests.get(url, verify=True, stream=True)) as r:
  145. with contextlib.closing(open(target, 'w', CHUNK_SIZE)) as target_fd:
  146. for d in r.iter_content(CHUNK_SIZE):
  147. target_fd.write(d)
  148. except Exception as e:
  149. raise GbpError("Failed to download %s: %s" % (url, e))
  150. if os.path.exists(target):
  151. os.unlink(target)
  152. return DebianUpstreamSource(target)