Passed
Push — develop ( 401873...6653d0 )
by Christophe
03:41
created

setup._latest()   A

Complexity

Conditions 5

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nop 3
dl 0
loc 10
rs 9.3333
c 0
b 0
f 0
1
# pylint: disable=duplicate-code
2
"""A setuptools based setup module.
3
4
See:
5
https://packaging.python.org/en/latest/distributing.html
6
https://github.com/chdemko/pandoc-latex-tip
7
"""
8
9
# To use a consistent encoding
10
11
# pylint: disable=no-name-in-module,import-error
12
from distutils.version import LooseVersion
13
14
import os
15
import configparser
16
import re
17
import urllib.request
18
import urllib.error
19
import shutil
20
import sys
21
22
import pkg_resources
23
from pkg_resources import get_distribution
24
25
# Always prefer setuptools over distutils
26
from setuptools import setup
27
from setuptools.command.build_ext import build_ext
28
from setuptools.command.build_py import build_py
29
30
HERE = os.path.abspath(os.path.dirname(__file__))
31
32
# Get the long description from the README file
33
with open("README.md", encoding="utf-8") as lg:
34
    LONG_DESCRIPTION = lg.read()
35
36
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
37
def _post():
38
    _post_fontawesome_47()
39
    _post_fontawesome_5x()
40
    _post_glyphicons_33()
41
    _post_material_design_3x()
42
43
44
def _post_fontawesome_47():
45
    # fontawesome 4.7
46
    folder = _folder("fontawesome", "4.7")
47
    _download(
48
        "https://raw.githubusercontent.com/FortAwesome/"
49
        "Font-Awesome/v4.7.0/css/font-awesome.css",
50
        folder,
51
        "font-awesome.css",
52
    )
53
    _download(
54
        "https://github.com/FortAwesome/Font-Awesome/"
55
        "blob/v4.7.0/fonts/fontawesome-webfont.ttf?raw=true",
56
        folder,
57
        "fontawesome-webfont.ttf",
58
    )
59
60
61
def _post_fontawesome_5x():
62
    # fontawesome 5.x
63
    folder = _folder("fontawesome", "5.x")
64
65
    versions = _versions(
66
        "https://api.github.com/repos/FortAwesome/Font-Awesome/tags",
67
        "Unable to get the last version number of the Font-Awesome package on github\n",
68
    )
69
70
    latest = _latest("^5.", versions, "5.14.0")
71
72
    _download(
73
        "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/"
74
        + latest
75
        + "/css/fontawesome.css",
76
        folder,
77
        "fontawesome.css",
78
    )
79
    for ttf in ["fa-brands-400", "fa-regular-400", "fa-solid-900"]:
80
        _download(
81
            "https://github.com/FortAwesome/Font-Awesome/blob/"
82
            + latest
83
            + "/webfonts/"
84
            + ttf
85
            + ".ttf?raw=true",
86
            folder,
87
            ttf + ".ttf",
88
        )
89
90
91
def _post_glyphicons_33():
92
    # glyphicons 3.3
93
    folder = _folder("glyphicons", "3.3")
94
95
    _download(
96
        "https://github.com/twbs/bootstrap/raw/v3.3.7/dist/css/bootstrap.css",
97
        folder,
98
        "bootstrap.css",
99
    )
100
101
    _download(
102
        "https://github.com/twbs/bootstrap/"
103
        "blob/v3.3.7/dist/fonts/glyphicons-halflings-regular.ttf?raw=true",
104
        folder,
105
        "glyphicons-halflings-regular.ttf",
106
    )
107
108
    with open(
109
        os.path.join(folder, "bootstrap.css"), "rt", encoding="utf-8"
110
    ) as original, open(
111
        os.path.join(folder, "bootstrap-modified.css"), "w", encoding="utf-8"
112
    ) as modified:
113
        index = 0
114
        for line in original:
115
            if index >= 1067:
116
                break
117
            if index >= 280:
118
                modified.write(line)
119
            index = index + 1
120
        original.close()
121
        modified.close()
122
123
124
def _post_material_design_3x():
125
    # material design 3.x
126
    folder = _folder("materialdesign", "3.x")
127
128
    versions = _versions(
129
        "https://api.github.com/repos/Templarian/MaterialDesign-Webfont/tags",
130
        "Unable to get the last version number of the MaterialDesign-Webfont package on github\n",
131
    )
132
133
    latest = _latest("^v3.", versions, "v5.9.55")
134
135
    _download(
136
        "https://github.com/Templarian/MaterialDesign-Webfont/blob/"
137
        + latest
138
        + "/css/materialdesignicons.css",
139
        folder,
140
        "materialdesignicons.css",
141
    )
142
143
    _download(
144
        "https://github.com/Templarian/MaterialDesign-Webfont/blob/"
145
        + latest
146
        + "/fonts/materialdesignicons-webfont.ttf?raw=true",
147
        folder,
148
        "materialdesignicons-webfont.ttf",
149
    )
150
151
152
def _download(url, folder, filename):
153
    try:
154
        with urllib.request.urlopen(url) as response, open(
155
            os.path.join(folder, filename), "wb"
156
        ) as out_file:
157
            shutil.copyfileobj(response, out_file)
158
    except urllib.error.HTTPError as exception:
159
        sys.stderr.write(str(exception))
160
161
162
def _latest(match, versions, latest):
163
    try:
164
        for version in versions:
165
            if re.match(match, version["name"]) and LooseVersion(
166
                version["name"]
167
            ) > LooseVersion(latest):
168
                latest = version["name"]
169
    except TypeError:
170
        pass
171
    return latest
172
173
174
def _folder(collection, icon_version):
175
    # pylint: disable=import-outside-toplevel
176
177
    try:
178
        folder = os.path.join(
179
            sys.prefix,
180
            "share",
181
            "pandoc_latex_tip",
182
            get_distribution("pandoc_latex_tip").version,
183
            collection,
184
            icon_version,
185
        )
186
    except pkg_resources.DistributionNotFound:
187
        config = configparser.RawConfigParser()
188
        config.read("setup.cfg")
189
        folder = os.path.join(
190
            sys.prefix,
191
            "share",
192
            "pandoc_latex_tip",
193
            config.get("metadata", "release"),
194
            collection,
195
            icon_version,
196
        )
197
198
    if not os.path.exists(folder):
199
        os.makedirs(folder)
200
201
    return folder
202
203
204
def _versions(url, message):
205
    # pylint: disable=import-outside-toplevel
206
    import requests
207
208
    try:
209
        return requests.get(url).json()
210
    except ValueError:
211
        sys.stderr.write(message)
212
        return []
213
214
215
# pylint: disable=too-many-ancestors
216
class BuildPy(build_py):
217
    """Builder for py extension"""
218
219
    def run(self):
220
        super().run()
221
        self.execute(_post, (), msg="Running post build task")
222
223
224
# pylint: disable=too-many-ancestors
225
class BuildExt(build_ext):
226
    """Builder for classical extension"""
227
228
    def run(self):
229
        super().run()
230
        self.execute(_post, (), msg="Running post build task")
231
232
233
setup(
234
    cmdclass={"build_py": BuildPy, "build_ext": BuildExt},
235
    name="pandoc-latex-tip",
236
    # Versions should comply with PEP440.  For a discussion on single-sourcing
237
    # the version across setup.py and the project code, see
238
    # https://packaging.python.org/en/latest/single_source_version.html
239
    # The project's description
240
    description="A pandoc filter for adding tip in LaTeX",
241
    long_description=LONG_DESCRIPTION,
242
    long_description_content_type="text/markdown",
243
    # The project's main homepage.
244
    url="https://github.com/chdemko/pandoc-latex-tip",
245
    # The project's download page
246
    download_url="https://github.com/chdemko/pandoc-latex-tip/archive/develop.zip",
247
    # Author details
248
    author="Christophe Demko",
249
    author_email="[email protected]",
250
    # Maintainer details
251
    maintainer="Christophe Demko",
252
    maintainer_email="[email protected]",
253
    # Choose your license
254
    license="BSD-3-Clause",
255
    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
256
    classifiers=[
257
        # How mature is this project? Common values are
258
        #   3 - Alpha
259
        #   4 - Beta
260
        #   5 - Production/Stable
261
        "Development Status :: 5 - Production/Stable",
262
        # Specify the OS
263
        "Operating System :: OS Independent",
264
        # Indicate who your project is intended for
265
        "Environment :: Console",
266
        "Intended Audience :: End Users/Desktop",
267
        "Intended Audience :: Developers",
268
        "Topic :: Software Development :: Build Tools",
269
        "Topic :: Software Development :: Documentation",
270
        "Topic :: Text Processing :: Filters",
271
        # Specify the Python versions you support here. In particular, ensure
272
        # that you indicate whether you support Python 2, Python 3 or both.
273
        "Programming Language :: Python :: 3.6",
274
        "Programming Language :: Python :: 3.7",
275
        "Programming Language :: Python :: 3.8",
276
        "Programming Language :: Python :: 3.9",
277
    ],
278
    # What does your project relate to?
279
    keywords="pandoc filters latex tip Font-Awesome icon",
280
    # Alternatively, if you want to distribute just a my_module.py, uncomment
281
    # this:
282
    py_modules=["pandoc_latex_tip"],
283
    # To provide executable scripts, use entry points in preference to the
284
    # "scripts" keyword. Entry points provide cross-platform support and allow
285
    # pip to create the appropriate form of executable for the target platform.
286
    entry_points={"console_scripts": ["pandoc-latex-tip = pandoc_latex_tip:main"]},
287
    # List run-time dependencies here.  These will be installed by pip when
288
    # your project is installed. For an analysis of "install_requires" vs pip's
289
    # requirements files see:
290
    # https://packaging.python.org/en/latest/requirements.html
291
    install_requires=[
292
        "panflute>=2.0",
293
        "icon_font_to_png>=0.4",
294
        "Pillow>=8.1",
295
        "appdirs>=1.4",
296
        "requests>=2",
297
    ],
298
    # List additional groups of dependencies here (e.g. development
299
    # dependencies). You can install these using the following syntax,
300
    # for example:
301
    # $ pip install -e .[dev,test]
302
    extras_require={
303
        "dev": ["check-manifest"],
304
        "test": [
305
            "black",
306
            "tox",
307
            "pytest-runner",
308
            "coverage",
309
            "pylint",
310
            "Pygments",
311
            "radon",
312
            "mypy",
313
            "pytest-cov",
314
            "types-setuptools",
315
        ],
316
        "docs": ["Sphinx>=3.5", "sphinx_rtd_theme>=0.5"],
317
    },
318
    # packages=find_packages(),
319
    # include_package_data = True,
320
    setup_requires=["icon_font_to_png>=0.4", "appdirs>=1.4"],
321
)
322