Debian Codegraph @

tasks/deps/_patch.py

raw symbols SCIP
1 # -*- coding: ascii -*-
2 #
3 # Copyright 2018 - 2025
4 # Andr\xe9 Malo or his licensors, as applicable
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 """
18 Propose updates to current dependencies
19 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20
21 """
22
23 import collections as _collections
24 import logging as _logging
25
26 from . import _parse
27 from . import _suggest
28
29 # pylint: disable = import-outside-toplevel
30
31 logger = _logging.getLogger("deps.patch")
32
33
34 def patch_updates(ctx, config, upgrade=False, verbose=True, debug=False):
35 """
36 Patch requirement files for dependency updates
37
38 Parameters:
39 config (adict):
40 The deps config
41
42 upgrade (bool):
43 Consider upgrades?
44
45 verbose (bool):
46 Verbose mode? Default: true
47
48 debug (bool):
49 Debug mode? Default: false
50
51 Returns:
52 dict: Changelog (``{bucket -> {name -> new version}}``)
53 """
54 logger.setLevel("DEBUG" if debug else ("INFO" if verbose else "WARN"))
55
56 changes = _suggest.suggest_updates(
57 ctx, config, upgrade=upgrade, verbose=verbose, debug=debug
58 )
59 replace, latest = changes["replace"], changes["latest"]
60 if latest and not latest.as_patch_info():
61 latest = None
62 if not replace and not latest:
63 logger.info("Nothing to do.")
64 return {}
65
66 changelog = _collections.defaultdict(
67 lambda: _collections.defaultdict(dict)
68 )
69 boilerplate = set(_parse.normalize(item) for item in config.boilerplate)
70
71 for file, change in sorted(replace.items()):
72 logger.info("Patching %s...", file.name)
73 file.patch(change).write()
74
75 for name, update in change["changes"].items():
76 bucket = (
77 "boilerplate"
78 if name in boilerplate or file.type == "text"
79 else "update"
80 )
81 changelog[bucket][name] = sorted(
82 set(item["new_version"] for item in update)
83 )
84
85 if latest:
86 # The file might have been modified, so reload
87 error = latest.load()
88 if error:
89 ctx.fail(error)
90
91 latest.write()
92 changelog["support"] = latest.as_changed()
93 for name in changelog["support"]:
94 changelog["update"].pop(name, None)
95
96 return {key: dict(value) for key, value in changelog.items() if value}