utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 The Distro Tracker Developers
  3. # See the COPYRIGHT file at the top-level directory of this distribution and
  4. # at http://deb.li/DTAuthors
  5. #
  6. # This file is part of Distro Tracker. It is subject to the license terms
  7. # in the LICENSE file found in the top-level directory of this
  8. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  9. # including this file, may be copied, modified, propagated, or distributed
  10. # except according to the terms contained in the LICENSE file.
  11. from distro_tracker.core.models import Architecture
  12. from distro_tracker.accounts.models import UserEmail
  13. from distro_tracker.core.models import ContributorName
  14. from distro_tracker.core.models import SourcePackage
  15. from distro_tracker.core.models import SourcePackageName
  16. from distro_tracker.core.models import BinaryPackageName
  17. import shutil
  18. import tempfile
  19. import contextlib
  20. @contextlib.contextmanager
  21. def make_temp_directory(suffix=''):
  22. """
  23. Helper context manager which creates a temporary directory on enter and
  24. cleans it up on exit.
  25. """
  26. temp_dir_name = tempfile.mkdtemp(suffix=suffix)
  27. try:
  28. yield temp_dir_name
  29. finally:
  30. shutil.rmtree(temp_dir_name)
  31. def create_source_package(arguments):
  32. """
  33. Creates and returns a new
  34. :class:`SourcePackage <distro_tracker.core.models.SourcePackage>`
  35. instance based on the parameters given in the arguments.
  36. It takes care to automatically create any missing maintainers, package
  37. names, etc.
  38. """
  39. kwargs = {}
  40. if 'maintainer' in arguments:
  41. maintainer = arguments['maintainer']
  42. maintainer_email = UserEmail.objects.get_or_create(
  43. email=maintainer['email'])[0]
  44. kwargs['maintainer'] = ContributorName.objects.get_or_create(
  45. contributor_email=maintainer_email,
  46. name=maintainer.get('name', ''))[0]
  47. if 'name' in arguments:
  48. name = arguments['name']
  49. kwargs['source_package_name'] = (
  50. SourcePackageName.objects.get_or_create(name=name)[0])
  51. for arg in ('version', 'directory', 'dsc_file_name'):
  52. if arg in arguments:
  53. kwargs[arg] = arguments[arg]
  54. src_pkg = SourcePackage.objects.create(**kwargs)
  55. # Now add m2m fields
  56. if 'architectures' in arguments:
  57. architectures = arguments['architectures']
  58. src_pkg.architectures = Architecture.objects.filter(
  59. name__in=architectures)
  60. if 'binary_packages' in arguments:
  61. binaries = []
  62. for binary in arguments['binary_packages']:
  63. binaries.append(
  64. BinaryPackageName.objects.get_or_create(name=binary)[0])
  65. src_pkg.binary_packages = binaries
  66. if 'uploaders' in arguments:
  67. for uploader in arguments['uploaders']:
  68. contributor = ContributorName.objects.get_or_create(
  69. contributor_email=UserEmail.objects.get_or_create(
  70. email=uploader)[0])[0]
  71. src_pkg.uploaders.add(contributor)
  72. src_pkg.save()
  73. return src_pkg
  74. def set_mock_response(mock_requests, text="", headers=None, status_code=200):
  75. """
  76. Helper method which sets a mock response to the given mock requests
  77. module.
  78. It takes care to correctly set the return value of all useful requests
  79. module functions.
  80. :param mock_requests: A mock requests module.
  81. :param text: The text of the response.
  82. :param headers: The headers of the response.
  83. :param status_code: The status code of the response.
  84. """
  85. if headers is None:
  86. headers = {}
  87. mock_response = mock_requests.models.Response()
  88. mock_response.headers = headers
  89. mock_response.status_code = status_code
  90. mock_response.ok = status_code < 400
  91. mock_response.text = text
  92. mock_response.content = text.encode('utf-8')
  93. mock_response.iter_lines.return_value = text.splitlines()
  94. mock_requests.get.return_value = mock_response