summaryrefslogtreecommitdiff
path: root/creole/setup_utils.py
blob: 24b1b1a1169e0d843b9fca4da9f17dcb5561fd37 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
    utils for distutils setup
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    Get README.creole as ReStructuredText on-the-fly for setup.long_description

    More information:
        https://code.google.com/p/python-creole/wiki/UseInSetup

    usage in setup.py e.g.:
    ---------------------------------------------------------------------------
    #!/usr/bin/env python
    # coding: utf-8

    import os
    import sys
    from setuptools import setup, find_packages

    PACKAGE_ROOT = os.path.dirname(os.path.abspath(__file__))

    try:
        from creole.setup_utils import get_long_description
    except ImportError:
        if 'register' in sys.argv or 'sdist' in sys.argv or '--long-description' in sys.argv:
            etype, evalue, etb = sys.exc_info()
            evalue = etype('%s - Please install python-creole >= v0.8 -  e.g.: pip install python-creole' % evalue)
            raise etype, evalue, etb
        long_description = None
    else:
        long_description = get_long_description(PACKAGE_ROOT)

    setup(
        ...
        long_description = long_description,
        ...
    )
    ---------------------------------------------------------------------------

    :copyleft: 2011-2020 by the python-creole team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""

import codecs
import datetime
import os
import shutil
import subprocess
import sys
import warnings
from pathlib import Path

from creole import __version__, creole2html, html2rest
from creole.shared.unknown_tags import raise_unknown_node, transparent_unknown_nodes

RAISE_ERRORS_ARGS = (
    'check', 'register', 'sdist', 'bdist', 'upload',
    '--long-description', '--restructuredtext',
)


def should_raise_errors():
    """
    Raise only errors, if one of RAISE_ERRORS_ARGS is in sys.argv
    or if no argument presents.
    """
    if len(sys.argv) == 1:
        return True

    for arg in RAISE_ERRORS_ARGS:
        if arg in sys.argv:
            return True
    return False


def get_long_description(package_root, filename='README.creole', raise_errors=None):
    """ read README file and convert it on-the-fly to ReStructuredText. """

    warnings.warn('get_long_description() will be removed in the future', DeprecationWarning)

    if raise_errors is None:
        raise_errors = should_raise_errors()

    if raise_errors:
        sys.stderr.write('Test creole2rest and raise an error, if rendering failed...\n')
        # raise a error if a unknown node found
        unknown_emit = raise_unknown_node
    else:
        # ignore unknown nodes
        unknown_emit = transparent_unknown_nodes

    filepath = os.path.join(package_root, filename)
    long_description_origin = ''
    try:
        # Read creole README
        f = codecs.open(filepath, 'r', encoding='utf-8')
        long_description_origin = f.read().strip()
        f.close()

        # convert creole into html
        long_description_html = creole2html(long_description_origin)

        # convert html to ReSt
        long_description_rest = html2rest(
            long_description_html,
            emitter_kwargs={'unknown_emit': unknown_emit}
        )
    except Exception:
        if raise_errors:
            raise
        # Don't raise the error e.g. in ./setup install process
        evalue = sys.exc_info()[1]
        long_description_rest = f'[Error: {evalue}]\n{long_description_origin}'
    else:
        if raise_errors:
            # Test created ReSt code like PyPi does it.
            from creole.rest_tools.pypi_rest2html import pypi_rest2html
            try:
                pypi_rest2html(long_description_rest)
            except SystemExit as e:
                msg = f'Error creole2rest self test failed: rest2html() exist with status code: {e.args[0]}\n'
                sys.stderr.write(msg)
                sys.exit(msg)
            except Exception as e:
                sys.exit(f'ReSt2html error: {e}')
            else:
                if 'check' in sys.argv:
                    print('Generating creole to ReSt to html, ok.')

    return long_description_rest


def update_rst_readme(package_root, filename='README.creole'):
    """
    Generate README.rst from README.creole
    """
    assert isinstance(package_root, Path)
    assert package_root.is_dir(), f'Directory not found: {package_root}'
    creole_readme_path = Path(package_root, filename)
    assert creole_readme_path.is_file(), f'File not found: {creole_readme_path}'

    rest_readme_path = creole_readme_path.with_suffix('.rst')
    print(
        f'Generate {rest_readme_path.name} from {creole_readme_path.name}',
        end='...', flush=True
    )

    with creole_readme_path.open('r') as f:
        creole_readme = f.read().strip()

    # convert creole into html
    html_readme = creole2html(creole_readme)

    # convert html to ReSt
    rest_readme = html2rest(
        html_readme,
        emitter_kwargs={
            'unknown_emit': raise_unknown_node  # raise a error if a unknown node found
        }
    )

    with rest_readme_path.open('w') as f:
        f.write(rest_readme)
        f.write('\n\n------------\n\n')
        dt = datetime.datetime.utcnow()
        dt = dt.replace(microsecond=0)
        dt = dt.isoformat(sep=' ')
        f.write(f'``Note: this file is generated from {filename} with "python-creole" at {dt}``')

    print('done.')


def update_creole_rst_readme():
    update_rst_readme(
        package_root=Path(__file__).parent.parent,
        filename='README.creole'
    )


def verbose_check_output(*args, log=None):
    """ 'verbose' version of subprocess.check_output() """
    call_info = 'Call: %r' % ' '.join(args)
    try:
        output = subprocess.check_output(
            args, universal_newlines=True, stderr=subprocess.STDOUT
        )
    except subprocess.CalledProcessError as err:
        print('\n***ERROR:')
        print(err.output)
        if log is not None:
            log.write(err.output)
        raise
    return call_info, output


def verbose_check_call(*args):
    """ 'verbose' version of subprocess.check_call() """
    print('\tCall: %r\n' % ' '.join(args))
    subprocess.check_call(args, universal_newlines=True)


def confirm(txt):
    print(f'\n{txt}')
    if input('\nPublish anyhow? (Y/N)').lower() not in ('y', 'j'):
        print('Bye.')
        sys.exit(-1)


def poetry_publish(package_root, version, filename='README.creole', log_filename='publish.log'):
    """
    Helper to build and upload to PyPi, with prechecks and update README.rst from README.creole

    Optional arguments are passed to `poetry publish` e.g.:

        $ poetry config repositories.testpypi https://test.pypi.org/simple
        $ poetry run publish --repository=testpypi

    Build and upload to PyPi, if...
        ... __version__ doesn't contains 'dev'
        ... we are on git "master" branch
        ... git repository is 'clean' (no changed files)

    Upload with 'poetry', git tag the current version and git push --tag

    The cli arguments will be pass to 'twine'. So this is possible:
     * Display 'twine' help page...: ./setup.py publish --help
     * use testpypi................: ./setup.py publish --repository=test

    add this to poetry pyproject.toml, e.g.:

        [tool.poetry.scripts]
        publish = 'foo.bar:publish'

    based on:
    https://github.com/jedie/python-code-snippets/blob/master/CodeSnippets/setup_publish.py
    """
    update_rst_readme(package_root=package_root, filename=filename)

    for key in ('dev', 'rc'):
        if key in version:
            confirm(f'WARNING: Version contains {key!r}: v{version}\n')
            break

    print('\nCheck if we are on "master" branch:')
    call_info, output = verbose_check_output('git', 'branch', '--no-color')
    print(f'\t{call_info}')
    if '* master' in output:
        print('OK')
    else:
        confirm(f'\nNOTE: It seems you are not on "master":\n{output}')

    print('\ncheck if if git repro is clean:')
    call_info, output = verbose_check_output('git', 'status', '--porcelain')
    print(f'\t{call_info}')
    if output == '':
        print('OK')
    else:
        print('\n *** ERROR: git repro not clean:')
        print(output)
        sys.exit(-1)

    print('\nRun "poetry check":')
    call_info, output = verbose_check_output('poetry', 'check')
    if 'All set!' not in output:
        print(output)
        confirm('Check failed!')
    else:
        print('OK')

    print('\ncheck if pull is needed')
    verbose_check_call('git', 'fetch', '--all')
    call_info, output = verbose_check_output('git', 'log', 'HEAD..origin/master', '--oneline')
    print(f'\t{call_info}')
    if output == '':
        print('OK')
    else:
        print('\n *** ERROR: git repro is not up-to-date:')
        print(output)
        sys.exit(-1)
    verbose_check_call('git', 'push')

    print('\nCleanup old builds:')

    def rmtree(path):
        path = os.path.abspath(path)
        if os.path.isdir(path):
            print('\tremove tree:', path)
            shutil.rmtree(path)
    rmtree('./dist')
    rmtree('./build')

    print(f'\nSet new version to: v{version}')
    verbose_check_call('poetry', 'version', version)

    print('\nbuild but do not upload...')

    with open(log_filename, 'a') as log:
        log.write('\n')
        log.write('-' * 100)
        log.write('\n')
        call_info, output = verbose_check_output('poetry', 'build', log=log)
        print(f'\t{call_info}')
        log.write(call_info)
        log.write(output)

    print(f'Build log file is here: {log_filename!r}')

    git_tag = f'v{version}'

    print('\ncheck git tag')
    call_info, output = verbose_check_output(
        'git', 'log', 'HEAD..origin/master', '--oneline',
    )
    if git_tag in output:
        print(f'\n *** ERROR: git tag {git_tag!r} already exists!')
        print(output)
        sys.exit(-1)
    else:
        print('OK')

    print('\nUpload to PyPi via poetry:')
    args = ['poetry', 'publish'] + sys.argv[1:]
    verbose_check_call(*args)

    print('\ngit tag version')
    verbose_check_call('git', 'tag', git_tag)

    print('\ngit push tag to server')
    verbose_check_call('git', 'push', '--tags')

    sys.exit(0)


def publish_python_creole():
    """
        Publish python-creole to PyPi
        Call this via:
            $ poetry run publish
    """
    poetry_publish(
        package_root=Path(__file__).parent.parent,
        version=__version__,
    )


if __name__ == '__main__':
    update_creole_rst_readme()