Skip to content

Instantly share code, notes, and snippets.

@hanepjiv hanepjiv/checkuse.py
Last active Dec 24, 2017

Embed
What would you like to do?
Checks for Gentoo Linux USE flags.
#! /bin/env python3
# -*-mode:python;coding:utf-8;-*-
"""
checkuse.py
@author hanepjiv <hanepjiv@gmail.com>
@since 2015/04/06
@date 2017/12/07
"""
# #############################################################################
# The MIT License (MIT)
#
# Copyright (c) <2015> hanepjiv <hanepjiv@gmail.com>
#
# 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.
# #############################################################################
# =============================================================================
CURRENT = 1
AGE = 0
REVISION = 0
# =============================================================================
MAJOR = CURRENT - AGE
VERSION = '{}.{}.{}'.format(MAJOR, AGE, REVISION)
# =============================================================================
PATH_MAKE_CONF = "/etc/portage/make.conf"
PATH_USE_DESC = "/usr/portage/profiles/use.desc"
PATH_USE_LOCAL_DESC = "/usr/portage/profiles/use.local.desc"
# =============================================================================
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# #############################################################################
# =============================================================================
import sys
import os
import subprocess
import re
# #############################################################################
# =============================================================================
class CheckUSE:
"""
"""
# =========================================================================
def __init__(self, *a_args, **a_kwds):
"""
"""
self.__dict__.update(a_kwds)
# =========================================================================
def check(self, args):
"""
"""
# ---------------------------------------------------------------------
use_desc = {}
for l in open(PATH_USE_DESC):
d = re.split(" - ", l)
if 2 is len(d):
use_desc[d[0].strip()] = d[1].strip()
# ---------------------------------------------------------------------
use_local_desc = {}
for l in open(PATH_USE_LOCAL_DESC):
d = re.split(" - ", l)
if 2 is len(d):
k = re.split(":", d[0].strip())
use_local_desc[k[1].strip()] = (d[1].strip(), k[0].strip())
# ---------------------------------------------------------------------
print("emerge --info")
info = str(subprocess.check_output(['emerge', '--info']))
use = re.search('USE=\"([^\"]*)', info).group(1)
uses = re.split("\s+", use.strip().replace(os.linesep, " "))
# ---------------------------------------------------------------------
global_use = []
local_use = []
unknown_use = []
for u in uses:
if u in use_desc.keys():
global_use.append(u)
elif u in use_local_desc.keys():
local_use.append(u)
else:
unknown_use.append(u)
# ---------------------------------------------------------------------
for u in global_use:
print("{0}global {2}{1} {3}".format(
bcolors.OKGREEN,
bcolors.ENDC,
u,
use_desc[u]))
for u in local_use:
print("{0}local {2}{1} {4}{5}{1} {3}".format(
bcolors.WARNING,
bcolors.ENDC,
u,
use_local_desc[u][0],
bcolors.OKBLUE,
use_local_desc[u][1]))
for u in unknown_use:
print("{0}unknown {2}{1}".format(
bcolors.FAIL,
bcolors.ENDC,
u))
return 0
# #############################################################################
# =============================================================================
def _parse_args_():
# -------------------------------------------------------------------------
import argparse
# -------------------------------------------------------------------------
parser = argparse.ArgumentParser()
parser.add_argument('-V', '--version', action='version', version=VERSION)
return parser.parse_args()
# =============================================================================
if __name__ == '__main__':
exit(CheckUSE().check(_parse_args_()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.