Skip to content

Commit

Permalink
define error codes at module scope rather than at class scope
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibelp committed May 8, 2019
1 parent 4d0aa42 commit f8fcdc2
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/spack/spack/cmd/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ def list_files(args):
print(os.path.join(spack.paths.spack_root, relpath))


class LicenseError(object):
OLD_LICENSE = 1
SPDX_MISMATCH = 2
GENERAL_MISMATCH = 3
# Error codes for license verification. All values are chosen such that
# bool(value) evaluates to True
OLD_LICENSE, SPDX_MISMATCH, GENERAL_MISMATCH = range(1, 4)


class LicenseError(object):
def __init__(self):
self.error_counts = defaultdict(int)

Expand All @@ -106,9 +107,9 @@ def has_errors(self):

def error_messages(self):
total = sum(self.error_counts.values())
missing = self.error_counts[LicenseError.GENERAL_MISMATCH]
spdx_mismatch = self.error_counts[LicenseError.SPDX_MISMATCH]
old_license = self.error_counts[LicenseError.OLD_LICENSE]
missing = self.error_counts[GENERAL_MISMATCH]
spdx_mismatch = self.error_counts[SPDX_MISMATCH]
old_license = self.error_counts[OLD_LICENSE]
return (
'%d improperly licensed files' % (total),
'files with wrong SPDX-License-Identifier: %d' % spdx_mismatch,
Expand Down Expand Up @@ -146,7 +147,7 @@ def _check_license(lines, path):
def old_license(line, path):
if re.search('This program is free software', line):
print('{0}: has old LGPL license header'.format(path))
return LicenseError.OLD_LICENSE
return OLD_LICENSE

# If the SPDX identifier is present, then there is a mismatch (since it
# did not match the above regex)
Expand All @@ -156,7 +157,7 @@ def wrong_spdx_identifier(line, path):
print('{0}: SPDX license identifier mismatch'
'(expecting {1}, found {2})'
.format(path, apache2_mit_spdx, m.group(1)))
return LicenseError.SPDX_MISMATCH
return SPDX_MISMATCH

checks = [old_license, wrong_spdx_identifier]

Expand All @@ -167,7 +168,7 @@ def wrong_spdx_identifier(line, path):
return error

print('{0}: the license does not match the expected format'.format(path))
return LicenseError.GENERAL_MISMATCH
return GENERAL_MISMATCH


def verify(args):
Expand Down

0 comments on commit f8fcdc2

Please sign in to comment.