summaryrefslogtreecommitdiff
path: root/setup.py
blob: e488b1cd14cadc350bcd53edb3287c10dd5681e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# Copyright 2016  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


from distutils.core import setup, Extension
from distutils.cmd import Command
from distutils.command.build import build
from distutils.command.clean import clean
import glob
import os
import re
import shutil
import subprocess
import sys
import tempfile

import cliapp

import pgpwordlist


class Check(Command):

    user_options = [
        ('unit-tests', 'u', 'run unit tests?'),
        ('nitpick', 'n', 'check copyright statements, line lengths, etc'),
    ]

    def set_all_options(self, new_value):
        self.unit_tests = new_value
        self.nitpick = new_value

    def initialize_options(self):
        self.set_all_options(False)

    def finalize_options(self):
        any_set = (
            self.unit_tests or
            self.nitpick)
        if not any_set:
            self.set_all_options(True)

    def run(self):
        if self.unit_tests:
            self.run_unit_tests()

        if self.nitpick:
            self.run_nitpick_checks()

        print "setup.py check done"

    def run_unit_tests(self):
        print "run unit tests"
        cliapp.runcmd(['python', '-m', 'CoverageTestRunner',
                       '--ignore-missing-from=without-tests', 'pgpwordlist'])
        if os.path.exists('.coverage'):
            os.remove('.coverage')

    def run_nitpick_checks(self):
        self.check_with_pep8()
        self.check_with_pylint()
        if os.path.exists('.git'):
            sources = self.find_all_source_files()
            self.check_copyright_statements(sources)
        else:
            print "no .git, no nitpick for you"

    def check_with_pep8(self):
        output = cliapp.runcmd(['pep8', '--version'])
        parts = output.strip().split('.')
        # pep8 version 1.5.7 is in Debian jessie. Previous versions
        # give bad warnings about whitespace around some operators,
        # and later versions don't. So we only run pep8 if it's new
        # enough.
        if parts >= ['1', '5', '7']:
            print 'running pep8'
            cliapp.runcmd(['pep8', 'pgpwordlist'], stdout=None, stderr=None)
        else:
            print 'not running pep8'

    def check_with_pylint(self):
        output = cliapp.runcmd(['pylint', '--version'])
        parts = output.splitlines()[0].split('.')
        # pylint version 1.3.1 is in Debian jessie. Previous versions
        # do not know all the things in the pylint.conf file we use,
        # so we only run pylint if it's new enough.
        if parts >= ['pylint 1', '3', '1,']:
            print 'running pylint'
            cliapp.runcmd(
                ['pylint', '--rcfile=pylint.conf', 'obnamlib'],
                stdout=None, stderr=None)
        else:
            print 'not running pylint', parts

    def check_copyright_statements(self, sources):
        if self.copylint_is_available():
            print 'check copyright statements in source files'
            cliapp.runcmd(['copyright-statement-lint'] + sources)
        else:
            print 'no copyright-statement-lint: no copyright checks'

    def copylint_is_available(self):
        returncode, stdout, stderr = cliapp.runcmd_unchecked(
            ['sh', '-c', 'command -v copyright-statement-lint'])
        return returncode == 0

    def find_all_source_files(self):
        exclude = [
            r'^debian/',
            r'^README\.',
            r'^NEWS$',
            r'^COPYING$',
            r'^pylint\.conf$',
            r'^without-tests$',
            r'^pgpwordlist/wordlist\.py$',
            r'^pgpwordlist/version\.py$',
            ]

        pats = [re.compile(x) for x in exclude]

        output = cliapp.runcmd(['git', 'ls-files'])
        result = []
        for line in output.splitlines():
            for pat in pats:
                if pat.search(line):
                    break
            else:
                result.append(line)
        return result


setup(
    name='py_pgpwordlist',
    version=pgpwordlist.__version__,
    description='Convert hex strings to words from PGP word list, and back',
    author='Lars Wirzenius',
    author_email='liw@liw.fi',
    url='http://liw.fi/py_pgpwordlist/',
    packages=[
        'pgpwordlist',
    ],
    cmdclass={
        'check': Check,
    },
)