|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (c) the purl authors |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +# Visit https://github.com/package-url/packageurl-python for support and |
| 24 | +# download. |
| 25 | + |
| 26 | + |
| 27 | +from __future__ import absolute_import |
| 28 | +from __future__ import print_function |
| 29 | +from __future__ import unicode_literals |
| 30 | + |
| 31 | +from django.db import models |
| 32 | + |
| 33 | +from packageurl import PackageURL |
| 34 | + |
| 35 | + |
| 36 | +class PackageURLMixin(models.Model): |
| 37 | + """ |
| 38 | + Abstract Model for Package URL "purl" fields support. |
| 39 | + """ |
| 40 | + type = models.CharField( |
| 41 | + max_length=16, |
| 42 | + blank=True, |
| 43 | + null=True, |
| 44 | + help_text=_( |
| 45 | + 'A short code to identify the type of this package. ' |
| 46 | + 'For example: gem for a Rubygem, docker for a container, ' |
| 47 | + 'pypi for a Python Wheel or Egg, maven for a Maven Jar, ' |
| 48 | + 'deb for a Debian package, etc.' |
| 49 | + ) |
| 50 | + ) |
| 51 | + |
| 52 | + namespace = models.CharField( |
| 53 | + max_length=255, |
| 54 | + blank=True, |
| 55 | + null=True, |
| 56 | + help_text=_( |
| 57 | + 'Package name prefix, such as Maven groupid, Docker image owner, ' |
| 58 | + 'GitHub user or organization, etc.' |
| 59 | + ), |
| 60 | + ) |
| 61 | + |
| 62 | + name = models.CharField( |
| 63 | + max_length=100, |
| 64 | + blank=True, |
| 65 | + null=True, |
| 66 | + help_text=_('Name of the package.'), |
| 67 | + ) |
| 68 | + |
| 69 | + version = models.CharField( |
| 70 | + max_length=50, |
| 71 | + blank=True, |
| 72 | + null=True, |
| 73 | + help_text=_('Version of the package.'), |
| 74 | + ) |
| 75 | + |
| 76 | + qualifiers = models.CharField( |
| 77 | + max_length=1024, |
| 78 | + blank=True, |
| 79 | + null=True, |
| 80 | + help_text=_( |
| 81 | + 'Extra qualifying data for a package such as the name of an OS, ' |
| 82 | + 'architecture, distro, etc.' |
| 83 | + ), |
| 84 | + ) |
| 85 | + |
| 86 | + subpath = models.CharField( |
| 87 | + max_length=200, |
| 88 | + blank=True, |
| 89 | + null=True, |
| 90 | + help_text=_( |
| 91 | + 'Extra subpath within a package, relative to the package root.' |
| 92 | + ), |
| 93 | + ) |
| 94 | + |
| 95 | + class Meta: |
| 96 | + abstract = True |
| 97 | + |
| 98 | + @property |
| 99 | + def package_url(self): |
| 100 | + """ |
| 101 | + Return a compact Package URL "purl" string. |
| 102 | + """ |
| 103 | + try: |
| 104 | + purl = PackageURL( |
| 105 | + self.type, self.namespace, self.name, |
| 106 | + self.version, self.qualifiers, self.subpath |
| 107 | + ) |
| 108 | + except ValueError: |
| 109 | + return '' |
| 110 | + return str(purl) |
0 commit comments