#!/usr/bin/env python3# Copyright (C) 2013 by Yu-Jie Lin## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.from__future__importprint_functionfromdistutils.coreimportCommand,setupfromunittestimportTestLoader,TextTestRunnerimportsys# scripts to be exculded from checkingEXCLUDE_SCRIPTS=()library_name='lnkckr'script_name='linkcheck'CHECK_LIST=(script_name,'.')# ============================================================================classcmd_test(Command):description='run tests'user_options=[]definitialize_options(self):passdeffinalize_options(self):passdefrun(self):loader=TestLoader()tests=loader.discover(start_dir='tests')runner=TextTestRunner(verbosity=2)runner.run(tests)classcmd_pep8(Command):description='run pep8'user_options=[]definitialize_options(self):passdeffinalize_options(self):passdefrun(self):try:importpep8exceptImportError:print('Cannot import pep8, you forgot to install?\n''run `pip install pep8` to install.',file=sys.stderr)sys.exit(1)p8=pep8.StyleGuide()# do not include code not written in b.pyp8.options.exclude+=EXCLUDE_SCRIPTS# ignore four-space indentation errorp8.options.ignore+=('E111','E121')print()print('Options')print('=======')print()print('Exclude:',p8.options.exclude)print('Ignore :',p8.options.ignore)print()print('Results')print('=======')print()report=p8.check_files(CHECK_LIST)print()print('Statistics')print('==========')print()report.print_statistics()print('%-7d Total errors and warnings'%report.get_count())classcmd_pyflakes(Command):description='run Pyflakes'user_options=[]definitialize_options(self):passdeffinalize_options(self):passdefrun(self):try:frompyflakesimportapifrompyflakesimportreporterasmodReporterexceptImportError:print('Cannot import pyflakes, you forgot to install?\n''run `pip install pyflakes` to install.',file=sys.stderr)sys.exit(1)fromos.pathimportbasenamereporter=modReporter._makeDefaultReporter()# monkey patch for exclusion of pathesapi_iterSourceCode=api.iterSourceCodedef_iterSourceCode(paths):forpathinapi_iterSourceCode(paths):ifbasename(path)notinEXCLUDE_SCRIPTS:yieldpathapi.iterSourceCode=_iterSourceCodeprint()print('Options')print('=======')print()print('Exclude:',EXCLUDE_SCRIPTS)print()print('Results')print('=======')print()warnings=api.checkRecursive(CHECK_LIST,reporter)print()print('Total warnings: %d'%warnings)classcmd_pylint(Command):description='run Pylint'user_options=[]definitialize_options(self):passdeffinalize_options(self):passdefrun(self):fromglobimportglobtry:frompylintimportlintexceptImportError:print('Cannot import pylint, you forgot to install?\n''run `pip install pylint` to install.',file=sys.stderr)sys.exit(1)print()print('Options')print('=======')print()print('Exclude:',EXCLUDE_SCRIPTS)files=list(CHECK_LIST)+['setup.py','lnkckr']+glob('tests/*.py')args=['--ignore=%s'%','.join(EXCLUDE_SCRIPTS),'--output-format=colorized','--include-ids=y','--indent-string=" "',]+filesprint()lint.Run(args)# ============================================================================withopen(library_name+'/__init__.py')asf:meta=dict((k.strip(' _'),eval(v))fork,vin# There will be a '\n', with eval(), it's safe to ignore(line.split('=')forlineinfifline.startswith('__')))# renaming meta-data keysmeta_renames=[('library','name'),('website','url'),('email','author_email'),]forold,newinmeta_renames:ifoldinmeta:meta[new]=meta[old]delmeta[old]# keep thesemeta_keys=['name','description','version','license','url','author','author_email']meta=dict(filter(lambdam:m[0]inmeta_keys,meta.items()))classifiers=['Development Status :: 3 - Alpha','Environment :: Console','Intended Audience :: End Users/Desktop','License :: OSI Approved :: MIT License','Natural Language :: English','Operating System :: POSIX :: Linux','Programming Language :: Python :: 2.7','Programming Language :: Python :: 3','Topic :: Internet :: WWW/HTTP :: Site Management :: Link Checking',]packages=['lnkckr','lnkckr.checkers',]setup_d=dict(cmdclass={'pep8':cmd_pep8,'pyflakes':cmd_pyflakes,'pylint':cmd_pylint,'test':cmd_test,},classifiers=classifiers,scripts=[script_name],packages=packages,**meta)if__name__=='__main__':setup(**setup_d)