Source code for meta_package_manager.bitbar

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# <bitbar.title>Meta Package Manager</bitbar.title>
# <bitbar.version>v2.7.1</bitbar.version>
# <>Kevin Deldycke</>
# <>kdeldycke</>
# <bitbar.desc>List outdated packages and manage upgrades.</bitbar.desc>
# <bitbar.dependencies>python,mpm</bitbar.dependencies>
# <bitbar.image></bitbar.image>
# <bitbar.abouturl></bitbar.abouturl>

Bitbar plugin for Meta Package Manager (a.k.a. the :command:`mpm` CLI).

Default update cycle is set to 7 hours so we have a chance to get user's
attention once a day. Higher frequency might ruin the system as all checks are
quite resource intensive, and Homebrew might hit GitHub's API calls quota.

from __future__ import print_function, unicode_literals

import json
import os
import sys
from operator import itemgetter
from subprocess import PIPE, Popen

PY2 = sys.version_info[0] == 2

""" Define the rendering mode of outdated packages list.

Set this constant to ``False`` to replace the default flat layout with an
alternative structure where all upgrade actions are put into submenus, one for
each manager.

# Make it easier to change font, sizes and colors of the output
# See for details
# An alternate "good looking" font is "font=NotoMono size=13" (not installed
# on MacOS by default though) that matches the system font quite well.
    'normal':  '',                              # Use default system font
    'summary': '',                              # Package summary
    'package': '',                              # Indiviual packages
    'error':   'color=red font=Menlo size=12',  # Errors
# Use a monospaced font when using submenus
    FONTS['summary'] = 'font=Menlo size=12'

[docs]def fix_environment(): """Tweak environment variable to find non-default system-wide binaries. macOS does not put ``/usr/local/bin`` or ``/opt/local/bin`` in the ``PATH`` for GUI apps. For some package managers this is a problem. Additioanlly Homebrew and Macports are using different pathes. So, to make sure we can always get to the necessary binaries, we overload the path. Current preference order would equate to Homebrew, Macports, then system. """ os.environ['PATH'] = ':'.join([ '/usr/local/bin', '/usr/local/sbin', '/opt/local/bin', '/opt/local/sbin', os.environ.get('PATH', '')]) # Python 3 Surrogate Handling. See: # os.environ['LC_ALL'] = os.environ['LANG'] = 'en_US.UTF-8'
[docs]def run(*args): """Run a shell command, return error code, output and error message.""" assert isinstance(args, tuple) try: process = Popen(args, stdout=PIPE, stderr=PIPE) except OSError: return None, None, "`{}` executable not found.".format(args[0]) output, error = process.communicate() return ( process.returncode, output.decode('utf-8') if output else None, error.decode('utf-8') if error else None)
[docs]def echo(message): """Print message to the output. Not unlike ``click.echo()``, this method is required to support discrepencies in the way strings are handled in different Python versions and platforms. """ if PY2: message = message.encode('utf-8') print(message)
if __name__ == '__main__': fix_environment() print_menu()