| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
Propose updates to current dependencies |
| 19 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
import collections as _collections |
| 24 |
import itertools as _it |
| 25 |
import logging as _logging |
| 26 |
import os as _os |
| 27 |
|
| 28 |
from .. import _inspect |
| 29 |
from .. import _parse |
| 30 |
from . import _dep_file |
| 31 |
from . import _latest_file |
| 32 |
from . import _requirement |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
logger = _logging.getLogger("deps.suggest") |
| 37 |
|
| 38 |
|
| 39 |
def suggest_updates(ctx, config, upgrade=False, verbose=True, debug=False): |
| 40 |
""" |
| 41 |
Inspect dependencies |
| 42 |
|
| 43 |
Parameters: |
| 44 |
config (adict): |
| 45 |
The deps config |
| 46 |
|
| 47 |
upgrade (bool): |
| 48 |
Consider upgrades? |
| 49 |
|
| 50 |
verbose (bool): |
| 51 |
Verbose mode? Default: true |
| 52 |
|
| 53 |
debug (bool): |
| 54 |
Debug mode? Default: false |
| 55 |
|
| 56 |
Returns: |
| 57 |
dict: {"latest": dict, "replace": dict} |
| 58 |
""" |
| 59 |
|
| 60 |
|
| 61 |
logger.setLevel("DEBUG" if debug else ("INFO" if verbose else "WARN")) |
| 62 |
|
| 63 |
changes = _collections.defaultdict(list) |
| 64 |
candidates = { |
| 65 |
key: val |
| 66 |
for key, val in _inspect.inspect_dependencies( |
| 67 |
ctx, |
| 68 |
config, |
| 69 |
verbose=verbose, |
| 70 |
debug=debug, |
| 71 |
).items() |
| 72 |
if val["updates"] |
| 73 |
} |
| 74 |
if not candidates: |
| 75 |
logger.debug("No dependencies found to update") |
| 76 |
return {"latest": {}, "replace": {}} |
| 77 |
|
| 78 |
latest = {} |
| 79 |
not_latest = set(_parse.normalize(item) for item in config.no_latest) |
| 80 |
for info in _with_locations(ctx, candidates).values(): |
| 81 |
for loc, want_upgrade, compat in _updatable( |
| 82 |
config, upgrade, info["locations"] |
| 83 |
): |
| 84 |
oreq = str(loc["requirement"]) |
| 85 |
req, version = _replace(loc, info, want_upgrade, compat) |
| 86 |
preq = _parse.requirement(req) |
| 87 |
|
| 88 |
if compat and _parse.normalize(preq.name) not in not_latest: |
| 89 |
lreq, lversion = _replace( |
| 90 |
loc, info, want_upgrade, False, latest=True |
| 91 |
) |
| 92 |
plreq = _parse.requirement(lreq) |
| 93 |
key = _parse.normalize(plreq.name) |
| 94 |
|
| 95 |
if str(plreq) == str(preq): |
| 96 |
not_latest.add(key) |
| 97 |
elif key not in not_latest: |
| 98 |
latest[key] = dict( |
| 99 |
new=lreq, |
| 100 |
new_version=lversion, |
| 101 |
) |
| 102 |
|
| 103 |
if str(preq) == oreq: |
| 104 |
logger.debug("Unchanged requirement: %r", req) |
| 105 |
continue |
| 106 |
|
| 107 |
changes[loc["file"]].append( |
| 108 |
dict( |
| 109 |
new_version=version, |
| 110 |
new=req, |
| 111 |
old=loc["content"], |
| 112 |
) |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
as_key = lambda x: _parse.normalize(_parse.requirement(x).name) |
| 117 |
group_key = lambda x: as_key(x["old"]) |
| 118 |
|
| 119 |
return dict( |
| 120 |
latest=_suggest_latest(config, latest, not_latest), |
| 121 |
replace={ |
| 122 |
file: dict( |
| 123 |
changes={ |
| 124 |
name: list(group) |
| 125 |
for name, group in _it.groupby( |
| 126 |
sorted(changelist, key=group_key), key=group_key |
| 127 |
) |
| 128 |
} |
| 129 |
) |
| 130 |
for file, changelist in changes.items() |
| 131 |
}, |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
def _suggest_latest(config, latest, not_latest): |
| 136 |
""" |
| 137 |
Suggest latest deps for packages |
| 138 |
|
| 139 |
Parameters: |
| 140 |
config (adict): |
| 141 |
The deps config |
| 142 |
|
| 143 |
latest (dict): |
| 144 |
Package -> version info |
| 145 |
|
| 146 |
not_latest (set): |
| 147 |
Set of packages not to be added |
| 148 |
|
| 149 |
Returns: |
| 150 |
dict: {add: [str], remove: [str]} |
| 151 |
""" |
| 152 |
return ( |
| 153 |
_latest_file.LatestFile( |
| 154 |
config.latest.file, |
| 155 |
config.latest.pattern, |
| 156 |
config.latest.prefix, |
| 157 |
config.latest.suffix, |
| 158 |
latest, |
| 159 |
not_latest, |
| 160 |
) |
| 161 |
if config.compat |
| 162 |
else None |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
def _replace(loc, info, upgrade, compat, latest=False): |
| 167 |
""" |
| 168 |
Find replacement for a single requirement |
| 169 |
|
| 170 |
Parameters: |
| 171 |
loc (dict): |
| 172 |
Location |
| 173 |
|
| 174 |
info (dict): |
| 175 |
Package update info |
| 176 |
|
| 177 |
upgrade (bool): |
| 178 |
Try upgrading to the latest version? |
| 179 |
|
| 180 |
compat (bool): |
| 181 |
Ensure compatible changes? (``~=``) |
| 182 |
|
| 183 |
latest (bool): |
| 184 |
The target is the list of "latest" requirements (e.g. in |
| 185 |
development.txt) |
| 186 |
|
| 187 |
Returns: |
| 188 |
tuple: new requirement string and new version (extracted) |
| 189 |
(``(str, str)``) |
| 190 |
""" |
| 191 |
req, version = _requirement.replace( |
| 192 |
loc["content"], |
| 193 |
info["updates"], |
| 194 |
upgrade=upgrade, |
| 195 |
compat=compat, |
| 196 |
latest=latest, |
| 197 |
) |
| 198 |
logger.debug( |
| 199 |
"upgrade: %r, compat: %r, latest: %r, req: %s, v: %r", |
| 200 |
upgrade, |
| 201 |
compat, |
| 202 |
latest, |
| 203 |
req, |
| 204 |
version, |
| 205 |
) |
| 206 |
return req, version |
| 207 |
|
| 208 |
|
| 209 |
def _updatable(config, want_upgrade, locations): |
| 210 |
""" |
| 211 |
Find and classify updatable locations |
| 212 |
|
| 213 |
Parameters: |
| 214 |
config (adict): |
| 215 |
The deps config |
| 216 |
|
| 217 |
want_upgrade (bool): |
| 218 |
Upgrade requested? |
| 219 |
|
| 220 |
locations (iterable): |
| 221 |
Locations to consider |
| 222 |
|
| 223 |
Returns: |
| 224 |
iterable: of (location, upgrade possible, compat required) |
| 225 |
""" |
| 226 |
no_compat = set(_parse.normalize(item) for item in config.no_compat) |
| 227 |
no_upgrade = set(_parse.normalize(item) for item in config.no_upgrade) |
| 228 |
|
| 229 |
for loc in locations: |
| 230 |
if ( |
| 231 |
loc["requirement"].marker |
| 232 |
and not loc["requirement"].marker.evaluate() |
| 233 |
): |
| 234 |
logger.debug("Skipping %r, because it doesn't evaluate", loc) |
| 235 |
continue |
| 236 |
|
| 237 |
if loc["file"].name in config.keep_as_is: |
| 238 |
logger.debug("Skipping %r (keeping as-is)", loc) |
| 239 |
continue |
| 240 |
|
| 241 |
want_no_upgrade = ( |
| 242 |
_parse.normalize(loc["requirement"].name) in no_upgrade |
| 243 |
) |
| 244 |
upgrade = False if want_no_upgrade else want_upgrade |
| 245 |
|
| 246 |
if not want_no_upgrade and loc["file"].name in config.upgrade: |
| 247 |
compat = False |
| 248 |
upgrade = True |
| 249 |
elif _parse.normalize(loc["requirement"].name) in no_compat: |
| 250 |
compat = False |
| 251 |
else: |
| 252 |
compat = loc["file"].type != "text" and config.compat |
| 253 |
|
| 254 |
yield loc, upgrade, compat |
| 255 |
|
| 256 |
|
| 257 |
def _with_locations(ctx, candidates): |
| 258 |
""" |
| 259 |
Setup candidate locations |
| 260 |
|
| 261 |
Parameters: |
| 262 |
candidates (dict): |
| 263 |
The candidates to prepare |
| 264 |
|
| 265 |
Returns: |
| 266 |
dict: candidates again |
| 267 |
""" |
| 268 |
extra = [] |
| 269 |
|
| 270 |
with ctx.shell.root_dir(): |
| 271 |
if _os.path.isfile("setup.py"): |
| 272 |
extra.append(_dep_file.DepFile.by_type("setup", "setup.py")) |
| 273 |
logger.debug("Extra location found: %r", extra[-1]) |
| 274 |
|
| 275 |
if _os.path.isfile("pyproject.toml"): |
| 276 |
extra.append(_dep_file.DepFile.by_type("toml", "pyproject.toml")) |
| 277 |
logger.debug("Extra location found: %r", extra[-1]) |
| 278 |
|
| 279 |
if _os.path.isdir("setups"): |
| 280 |
for name in sorted(_os.listdir("setups")): |
| 281 |
if name.startswith("."): |
| 282 |
continue |
| 283 |
|
| 284 |
if name.endswith(".py"): |
| 285 |
extra.append( |
| 286 |
_dep_file.DepFile.by_type( |
| 287 |
"setup", "setups/%s" % (name,) |
| 288 |
) |
| 289 |
) |
| 290 |
logger.debug("Extra location found: %r", extra[-1]) |
| 291 |
|
| 292 |
elif name.endswith(".toml"): |
| 293 |
extra.append( |
| 294 |
_dep_file.DepFile.by_type( |
| 295 |
"toml", "setups/%s" % (name,) |
| 296 |
) |
| 297 |
) |
| 298 |
logger.debug("Extra location found: %r", extra[-1]) |
| 299 |
|
| 300 |
for name, info in candidates.items(): |
| 301 |
info["locations"] = [ |
| 302 |
dict( |
| 303 |
file=_dep_file.DepFile.by_type("text", item["file"]["name"]), |
| 304 |
content=item["file"]["content"].strip(), |
| 305 |
requirement=item["requirement"], |
| 306 |
) |
| 307 |
for item in info["locations"] |
| 308 |
if item.get("file") |
| 309 |
] + [ |
| 310 |
dict( |
| 311 |
file=depfile, |
| 312 |
content=req, |
| 313 |
requirement=_parse.requirement(req), |
| 314 |
) |
| 315 |
for depfile in extra |
| 316 |
for req in depfile.find(name) |
| 317 |
] |
| 318 |
|
| 319 |
return candidates |