|
| 1 | +# vim: set fileencoding=utf-8 : |
| 2 | +# |
| 3 | +# (C) 2018 Shengjing Zhu <i@zhsj.me> |
| 4 | +# This program is free software; you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation; either version 2 of the License, or |
| 7 | +# (at your option) any later version. |
| 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 |
| 15 | +# along with this program; if not, please see |
| 16 | +# <http://www.gnu.org/licenses/> |
| 17 | +"""A Debian Copyright file""" |
| 18 | +import email |
| 19 | +import os |
| 20 | + |
| 21 | + |
| 22 | +class NoCopyrightError(Exception): |
| 23 | + """No copyright found""" |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +class ParseCopyrightError(Exception): |
| 28 | + """Problem parsing copyright""" |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +class Copyright(object): |
| 33 | + """A Debian copyright""" |
| 34 | + def __init__(self, contents=None, filename="debian/copyright"): |
| 35 | + """ |
| 36 | + Parse an existing copyright file. |
| 37 | +
|
| 38 | + @param contents: content of a control file |
| 39 | + @type contents: C{str} |
| 40 | + @param filename: name of the control file |
| 41 | + @type filename: C{str} |
| 42 | + @return: Copyright object |
| 43 | + @rtype: C{gbp.deb.conrol.Copyright} object |
| 44 | + """ |
| 45 | + if contents: |
| 46 | + copyright = email.message_from_string(contents) |
| 47 | + else: |
| 48 | + if not os.access(filename, os.F_OK): |
| 49 | + raise NoCopyrightError("Copyright file %s doesn't exist" % filename) |
| 50 | + with open(filename) as f: |
| 51 | + copyright = email.message_from_file(f) |
| 52 | + |
| 53 | + if not copyright.items(): |
| 54 | + raise ParseCopyrightError("Empty or invalid copyright file or contents") |
| 55 | + |
| 56 | + self._copyright = copyright |
| 57 | + self.filename = filename |
| 58 | + |
| 59 | + @property |
| 60 | + def files_excluded(self): |
| 61 | + """The file list to be excluded""" |
| 62 | + return self._copyright['Files-Excluded'].split() |
0 commit comments