File: tasks.py

package info (click to toggle)
python-rjsmin 1.2.5%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,256 kB
  • sloc: javascript: 8,503; python: 5,315; ansic: 821; sh: 100; makefile: 19
file content (262 lines) | stat: -rw-r--r-- 6,197 bytes parent folder | download
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
# -*- coding: ascii -*-
#
# Copyright 2018 - 2025
# Andr\xe9 Malo or his licensors, as applicable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Dependency Management Tasks
~~~~~~~~~~~~~~~~~~~~~~~~~~~

"""
import json as _json
import os as _os
import sys as _sys

import invoke as _invoke

from .. import pypi as _pypi
from .._inv import tasks as _tasks
from .._inv import util as _util

# pylint: disable = import-outside-toplevel

NAMESPACE = "deps"


def _default_config(ctx):
    """
    Set default config

    Returns:
      adict: ctx.deps after defaults being applied
    """
    if getattr(_default_config, "applied", None):
        return ctx.deps

    ctx.deps = _util.dictmerge(
        _util.adict(
            toplevel=[
                "development.txt",
                "tests/requirements.txt",
            ],
            upgrade=[
                "checkout-requirements.txt",
            ],
            keep_as_is=[
                "compat-requirements.txt",
            ],
            no_compat=["setuptools", "pip", "build"],
            no_upgrade=[],
            no_latest=[],
            boilerplate=["setuptools", "pip", "build"],
            compat=True,
            latest=_util.adict(
                file="development.txt",
                pattern=r"(?=^-e\s+\.)",
                prefix="# Latest dependencies",
                suffix="",
            ),
        ),
        ctx.get("deps", {}),
    )
    _default_config.applied = True
    return ctx.deps


@_invoke.task()
def old(ctx):
    """List outdated python packages"""
    with ctx.shell.root_dir():
        ctx.run(ctx.c("pip list -i %s -o", _pypi.index_url(ctx)), echo=True)


@_invoke.task()
def package(ctx, upgrade=False):
    """
    Update python dependencies, excluding development (``-e .``)

    Parameters:
      upgrade (bool):
        Run pip install with ``-U`` flag?
    """
    cmd = ["pip", "install", "-i", _pypi.index_url(ctx)]
    if upgrade:
        cmd += ["-U"]
    cmd += ["-e", "."]

    with ctx.shell.root_dir():
        ctx.run(ctx.c(cmd), echo=True)


@_invoke.task(default=True)
def dev(ctx, upgrade=False):
    """
    Update python dependencies, including development (``-r development.txt``)

    Parameters:
      upgrade (bool):
        Run pip install with ``-U`` flag?
    """
    cmd = ["pip", "install", "-i", _pypi.index_url(ctx)]
    if upgrade:
        cmd += ["-U"]
    cmd += ["-r", "development.txt"]

    with ctx.shell.root_dir():
        ctx.run(ctx.c(cmd), echo=True)


@_invoke.task()
def reset(ctx, upgrade=False):
    """
    Reset your virtual env

    This command uninstalls everything except editable installs and reinstalls
    from scratch (``-r development.txt``)

    Parameters:
      upgrade (bool):
        Run pip install with ``-U`` flag?
    """
    cmd = [ctx.which("bash"), "-il", "%s/reset.sh"]
    if upgrade:
        cmd += ["-u"]
    cmd += ["."]
    with ctx.shell.root_dir():
        ctx.run(
            ctx.c(cmd, ctx.shell.native(_os.path.dirname(__file__))),
            pty=True,
        )


@_invoke.task()
def inspect(ctx, deps=False, verbose=False, debug=False):
    """
    Inspect current dependencies and print to stdout

    Parameters:
      deps (bool):
        run ``inv deps`` before?

      verbose (bool):
        Verbose mode? Default: true

      debug (bool):
        Debug mode? Default: false
    """
    if deps:
        _tasks.execute(ctx, "deps.dev")

    from . import _inspect

    _sys.stdout.write(
        _json.dumps(
            _inspect.inspect_dependencies(
                ctx,
                _default_config(ctx),
                verbose=verbose,
                debug=debug,
            ),
            indent=4,
            default=str,
        )
        + "\n"
    )


@_invoke.task()
def check(ctx, upgrade=False, deps=False, verbose=True, debug=False):
    """
    Suggest dependency changes (basically dry run for `inv deps.patch`)

    Parameters:
      deps (bool):
        run ``inv deps`` before?

      upgrade (bool):
        Consider upgrades? They might be incompatible with your code.
        Default: false

      verbose (bool):
        Verbose mode? Default: true

      debug (bool):
        Debug mode? Default: false
    """
    if deps:
        _tasks.execute(ctx, "deps.dev")

    from . import _suggest

    result = _suggest.suggest_updates(
        ctx,
        _default_config(ctx),
        upgrade=upgrade,
        verbose=verbose,
        debug=debug,
    )

    result["replace"] = {
        file.name: dict(type=file.type, **info)
        for file, info in result["replace"].items()
    }
    result["latest"] = result["latest"] and result["latest"].as_patch_info()
    _sys.stdout.write(
        _json.dumps(
            result,
            indent=4,
            default=str,
        )
        + "\n"
    )


@_invoke.task()
def patch(ctx, deps=False, upgrade=False, verbose=True, debug=False):
    """
    Find dependency updates and patch the files

    Parameters:
      deps (bool):
        run ``inv.deps`` before?

      upgrade (bool):
        Consider upgrades? They might be incompatible with your code.
        Default: false

      verbose (bool):
        Verbose mode? Default: true

      debug (bool):
        Debug mode? Default: false
    """
    if deps:
        _tasks.execute(ctx, "deps.dev")

    from . import _patch

    _sys.stdout.write(
        _json.dumps(
            _patch.patch_updates(
                ctx,
                _default_config(ctx),
                upgrade=upgrade,
                verbose=verbose,
                debug=debug,
            ),
            indent=4,
            default=str,
        )
        + "\n"
    )