Skip to content

Instantly share code, notes, and snippets.

@oerpli
Last active August 17, 2020 10:57
Show Gist options
  • Save oerpli/6b88df07c0ef0635bcf1cf456da69fd9 to your computer and use it in GitHub Desktop.
Save oerpli/6b88df07c0ef0635bcf1cf456da69fd9 to your computer and use it in GitHub Desktop.
"""
MIT License
Copyright (c) 2019 Abraham Hinteregger
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import re
import subprocess
from collections import defaultdict
from itertools import chain
def get_versions(lines):
ddict = defaultdict(set)
for line in lines.splitlines()[2:]:
pkg, version = re.match(r"^(\S+)\s+(\S+)", line, re.MULTILINE).groups()
ddict[pkg].add(version)
return ddict
def semantic_cmp(version_string):
def mysplit(string):
version_substrs = lambda x: re.findall(r"([A-z]+|\d+)", x)
return list(chain(map(version_substrs, string.split("."))))
def str_ord(string):
num = 0
for char in string:
num *= 255
num += ord(char)
return num
def try_int(version_str):
try:
return int(version_str)
except ValueError:
return str_ord(version_str)
mss = list(chain(*mysplit(version_string)))
return tuple(map(try_int, mss))
def main():
sp_i = subprocess.run(["conda", "list"], stdout=subprocess.PIPE)
sp_v = subprocess.run(["conda", "search", "--outdated"], stdout=subprocess.PIPE)
installed = get_versions(sp_i.stdout.decode("utf-8"))
available = get_versions(sp_v.stdout.decode("utf-8"))
for pkg, inst_vs in installed.items():
avail_vs = sorted(list(available[pkg]), key=semantic_cmp)
if not avail_vs:
continue
current = min(inst_vs, key=semantic_cmp)
newest = avail_vs[-1]
if avail_vs and current != newest:
if semantic_cmp(current) < semantic_cmp(newest):
print(f"{pkg}:\n\tInstalled:\t{current}\n\tNewest:\t\t{newest}")
new = [x for x in avail_vs if semantic_cmp(current) < semantic_cmp(x)]
print("\tNewer:\t\t" + f", ".join(new))
if __name__ == "__main__":
main()
marshmallow:
Installed: 2.19.5
Newest: 3.0.0b8
Newer: 3.0.0b8
matplotlib:
Installed: 3.0.3
Newest: 3.1.0
Newer: 3.1.0
sip:
Installed: 4.19.8
Newest: 4.19.13
Newer: 4.19.12, 4.19.13
urllib3:
Installed: 1.24.3
Newest: 1.25.3
Newer: 1.25, 1.25.1, 1.25.2, 1.25.3
@GiantMolecularCloud
Copy link

Indeed, this happened on Linux and macOS. Apparently, you have to run with shell=True when specifying the command as a string or give the command as a list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment