#!/usr/bin/env python
# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

from __future__ import unicode_literals

import io
import os.path
import sys

import setuptools


MODULE_NAME = "pytablereader"
REPOSITORY_URL = "https://github.com/thombashi/{:s}".format(MODULE_NAME)
REQUIREMENT_DIR = "requirements"
ENCODING = "utf8"

pkg_info = {}


def need_pytest():
    return set(["pytest", "test", "ptr"]).intersection(sys.argv)


class ReleaseCommand(setuptools.Command):
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        tag = "v{}".format(pkg_info["__version__"])

        print("Pushing git tags: {}".format(tag))

        os.system("git tag {}".format(tag))
        os.system("git push --tags")


with open(os.path.join(MODULE_NAME, "__version__.py")) as f:
    exec(f.read(), pkg_info)

with io.open("README.rst", encoding=ENCODING) as fp:
    long_description = fp.read()

with io.open(os.path.join("docs", "pages", "introduction", "summary.txt"), encoding=ENCODING) as f:
    summary = f.read().strip()

with open(os.path.join(REQUIREMENT_DIR, "requirements.txt")) as f:
    install_requires = [line.strip() for line in f if line.strip()]

with open(os.path.join(REQUIREMENT_DIR, "test_requirements.txt")) as f:
    tests_requires = [line.strip() for line in f if line.strip()]

with open(os.path.join(REQUIREMENT_DIR, "docs_requirements.txt")) as f:
    docs_requires = [line.strip() for line in f if line.strip()]

setuptools_require = ["setuptools>=38.3.0"]
pytest_runner_require = ["pytest-runner"] if need_pytest() else []

setuptools.setup(
    name=MODULE_NAME,
    version=pkg_info["__version__"],
    url=REPOSITORY_URL,

    author=pkg_info["__author__"],
    author_email=pkg_info["__email__"],
    description=summary,
    include_package_data=True,
    keywords=[
        "table", "reader", "pandas",
        "CSV", "Excel", "HTML", "JSON", "LTSV", "Markdown", "MediaWiki", "TSV",
        "SQLite",
    ],
    license=pkg_info["__license__"],
    long_description=long_description,
    packages=setuptools.find_packages(exclude=["test*"]),
    project_urls={
        "Documentation": "http://{:s}.rtfd.io/".format(MODULE_NAME),
        "Tracker": "{:s}/issues".format(REPOSITORY_URL),
    },
    python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*',

    install_requires=setuptools_require + install_requires,
    setup_requires=setuptools_require + pytest_runner_require,
    tests_require=tests_requires,
    extras_require={
        "build": "wheel",
        "test": tests_requires,
        "docs": docs_requires,
    },

    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "Intended Audience :: Information Technology",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.4",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6",
        "Topic :: Database",
        "Topic :: Software Development :: Libraries",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],
    cmdclass={
        "release": ReleaseCommand,
    })

@@ -466,370 +466,177 @@
 )%0A%0A%0A
-class R
+def get_r
 elease
-C
+_c
 ommand
-(setuptools.Command):%0A    user_options = %5B%5D%0A%0A    def initialize_options(self):%0A        pass%0A%0A    def finalize_options(self):%0A        pass%0A%0A    def run(self):%0A        tag = %22v%7B%7D%22.format(pkg_info%5B%22__version__%22%5D)%0A%0A        print(%22Pushing git tags: %7B%7D%22.format(tag))%0A%0A        os.system(%22git tag %7B%7D%22.format(tag))%0A        os.system(%22git push --tags%22)
+_class():%0A    try:%0A        from releasecmd import ReleaseCommand%0A    except ImportError:%0A        return %7B%7D%0A%0A    return %7B%22release%22: ReleaseCommand%7D
 %0A%0A%0Aw
@@ -2416,19 +2416,18 @@
    %22
-test%22: test
+docs%22: doc
 s_re
@@ -2439,34 +2439,75 @@
 s,%0A        %22
-docs%22: doc
+release%22: %22releasecmd%3E=0.0.9%22,%0A        %22test%22: test
 s_requires,%0A
@@ -3238,48 +3238,33 @@
 ass=
-%7B%0A        %22release%22: ReleaseCommand,%0A    %7D
+get_release_command_class()
 )%0A

Update release command