|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +''' |
| 4 | +Template by pypi-mobans |
| 5 | +''' |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import codecs |
| 10 | +import locale |
| 11 | +import platform |
| 12 | +from shutil import rmtree |
| 13 | + |
| 14 | +from setuptools import Command, setup, find_packages |
| 15 | + |
| 16 | +PY2 = sys.version_info[0] == 2 |
| 17 | +PY26 = PY2 and sys.version_info[1] < 7 |
| 18 | +PY33 = sys.version_info < (3, 4) |
| 19 | + |
| 20 | +# Work around mbcs bug in distutils. |
| 21 | +# http://bugs.python.org/issue10945 |
| 22 | +# This work around is only if a project supports Python < 3.4 |
| 23 | + |
| 24 | +# Work around for locale not being set |
| 25 | +try: |
| 26 | + lc = locale.getlocale() |
| 27 | + pf = platform.system() |
| 28 | + if pf != "Windows" and lc == (None, None): |
| 29 | + locale.setlocale(locale.LC_ALL, "C.UTF-8") |
| 30 | +except (ValueError, UnicodeError, locale.Error): |
| 31 | + locale.setlocale(locale.LC_ALL, "en_US.UTF-8") |
| 32 | + |
| 33 | +NAME = "pypi-mobans" |
| 34 | +AUTHOR = "C.W." |
| 35 | +VERSION = "" |
| 36 | + |
| 37 | +LICENSE = "NEW BSD" |
| 38 | +DESCRIPTION = ( |
| 39 | + "Scaffolding templates for your Python project." |
| 40 | +) |
| 41 | +URL = "https://github.com/moremoban/pypi-mobans" |
| 42 | +DOWNLOAD_URL = "%s/archive/0.0.0.tar.gz" % URL |
| 43 | +FILES = ["README.rst", "CHANGELOG.rst"] |
| 44 | +KEYWORDS = [ |
| 45 | + "python", |
| 46 | +] |
| 47 | + |
| 48 | +CLASSIFIERS = [ |
| 49 | + "Topic :: Software Development :: Libraries", |
| 50 | + "Programming Language :: Python", |
| 51 | + "Intended Audience :: Developers", |
| 52 | + "Programming Language :: Python :: 2.6", |
| 53 | + "Programming Language :: Python :: 2.7", |
| 54 | + "Programming Language :: Python :: 3.3", |
| 55 | + "Programming Language :: Python :: 3.4", |
| 56 | + "Programming Language :: Python :: 3.5", |
| 57 | + "Programming Language :: Python :: 3.6", |
| 58 | + |
| 59 | + "Programming Language :: Python :: 3.7", |
| 60 | + |
| 61 | + "Programming Language :: Python :: 3.8", |
| 62 | + |
| 63 | +] |
| 64 | + |
| 65 | +INSTALL_REQUIRES = [ |
| 66 | +] |
| 67 | +SETUP_COMMANDS = {} |
| 68 | + |
| 69 | +PACKAGES = find_packages(exclude=["ez_setup", "examples", "tests", "tests.*"]) |
| 70 | +EXTRAS_REQUIRE = { |
| 71 | +} |
| 72 | +# You do not need to read beyond this line |
| 73 | +PUBLISH_COMMAND = "{0} setup.py sdist bdist_wheel upload -r pypi".format(sys.executable) |
| 74 | +GS_COMMAND = ("gs pypi-mobans v0.0.0 " + |
| 75 | + "Find 0.0.0 in changelog for more details") |
| 76 | +NO_GS_MESSAGE = ("Automatic github release is disabled. " + |
| 77 | + "Please install gease to enable it.") |
| 78 | +UPLOAD_FAILED_MSG = ( |
| 79 | + 'Upload failed. please run "%s" yourself.' % PUBLISH_COMMAND) |
| 80 | +HERE = os.path.abspath(os.path.dirname(__file__)) |
| 81 | + |
| 82 | + |
| 83 | +class PublishCommand(Command): |
| 84 | + """Support setup.py upload.""" |
| 85 | + |
| 86 | + description = "Build and publish the package on github and pypi" |
| 87 | + user_options = [] |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def status(s): |
| 91 | + """Prints things in bold.""" |
| 92 | + print("\033[1m{0}\033[0m".format(s)) |
| 93 | + |
| 94 | + def initialize_options(self): |
| 95 | + pass |
| 96 | + |
| 97 | + def finalize_options(self): |
| 98 | + pass |
| 99 | + |
| 100 | + def run(self): |
| 101 | + try: |
| 102 | + self.status("Removing previous builds...") |
| 103 | + rmtree(os.path.join(HERE, "dist")) |
| 104 | + rmtree(os.path.join(HERE, "build")) |
| 105 | + rmtree(os.path.join(HERE, "pypi_mobans.egg-info")) |
| 106 | + except OSError: |
| 107 | + pass |
| 108 | + |
| 109 | + self.status("Building Source and Wheel (universal) distribution...") |
| 110 | + run_status = True |
| 111 | + if has_gease(): |
| 112 | + run_status = os.system(GS_COMMAND) == 0 |
| 113 | + else: |
| 114 | + self.status(NO_GS_MESSAGE) |
| 115 | + if run_status: |
| 116 | + if os.system(PUBLISH_COMMAND) != 0: |
| 117 | + self.status(UPLOAD_FAILED_MSG) |
| 118 | + |
| 119 | + sys.exit() |
| 120 | + |
| 121 | + |
| 122 | +SETUP_COMMANDS.update({ |
| 123 | + "publish": PublishCommand |
| 124 | +}) |
| 125 | + |
| 126 | + |
| 127 | +def has_gease(): |
| 128 | + """ |
| 129 | + test if github release command is installed |
| 130 | +
|
| 131 | + visit http://github.com/moremoban/gease for more info |
| 132 | + """ |
| 133 | + try: |
| 134 | + import gease # noqa |
| 135 | + return True |
| 136 | + except ImportError: |
| 137 | + return False |
| 138 | + |
| 139 | + |
| 140 | +def read_files(*files): |
| 141 | + """Read files into setup""" |
| 142 | + text = "" |
| 143 | + for single_file in files: |
| 144 | + content = read(single_file) |
| 145 | + text = text + content + "\n" |
| 146 | + return text |
| 147 | + |
| 148 | + |
| 149 | +def read(afile): |
| 150 | + """Read a file into setup""" |
| 151 | + the_relative_file = os.path.join(HERE, afile) |
| 152 | + with codecs.open(the_relative_file, "r", "utf-8") as opened_file: |
| 153 | + content = filter_out_test_code(opened_file) |
| 154 | + content = "".join(list(content)) |
| 155 | + return content |
| 156 | + |
| 157 | + |
| 158 | +def filter_out_test_code(file_handle): |
| 159 | + found_test_code = False |
| 160 | + for line in file_handle.readlines(): |
| 161 | + if line.startswith(".. testcode:"): |
| 162 | + found_test_code = True |
| 163 | + continue |
| 164 | + if found_test_code is True: |
| 165 | + if line.startswith(" "): |
| 166 | + continue |
| 167 | + else: |
| 168 | + empty_line = line.strip() |
| 169 | + if len(empty_line) == 0: |
| 170 | + continue |
| 171 | + else: |
| 172 | + found_test_code = False |
| 173 | + yield line |
| 174 | + else: |
| 175 | + for keyword in ["|version|", "|today|"]: |
| 176 | + if keyword in line: |
| 177 | + break |
| 178 | + else: |
| 179 | + yield line |
| 180 | + |
| 181 | + |
| 182 | +if __name__ == "__main__": |
| 183 | + setup( |
| 184 | + test_suite="tests", |
| 185 | + name=NAME, |
| 186 | + author=AUTHOR, |
| 187 | + version=VERSION, |
| 188 | + author_email=EMAIL, |
| 189 | + description=DESCRIPTION, |
| 190 | + url=URL, |
| 191 | + download_url=DOWNLOAD_URL, |
| 192 | + long_description=read_files(*FILES), |
| 193 | + license=LICENSE, |
| 194 | + keywords=KEYWORDS, |
| 195 | + extras_require=EXTRAS_REQUIRE, |
| 196 | + tests_require=["nose"], |
| 197 | + install_requires=INSTALL_REQUIRES, |
| 198 | + packages=PACKAGES, |
| 199 | + include_package_data=True, |
| 200 | + zip_safe=False, |
| 201 | + classifiers=CLASSIFIERS, |
| 202 | + cmdclass=SETUP_COMMANDS |
| 203 | + ) |
0 commit comments