#!/usr/bin/env python # Copyright 1999-2009 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # Universal Select Tool # Profiling Tool # uprofile.py mephx.x@gmail.com import os import re import sys import stat import json import traceback from umodule import * from uio import filesystem from uio import printsystem verbose = False printsystem.set_type('profile') class Profile(Module): def __init__(self, path): self.actions = [] self.parameters = [] self.output = [] str = '' for line in filesystem.read_file('.uprofile/' + path): str += line profile = json.loads(str) self.profile = profile self.name = path[:-5] self.author = profile['profile']['author'] self.version = profile['profile']['version'] self.description = profile['profile']['description'] self.actions.append(Action(name = 'set', \ description = 'Set this profile for this folder.', \ type = 'profile')) self.actions.append(Action(name = 'default', \ description = 'Set this profile the default profile.', \ type = 'profile')) class UniversalProfileTool: def __init__(self): self.profiles = [] return def get_profile(self, name): profile = Profile(name + '.json') return profile def get_profiles(self): """ Returns the list of available uprofiles """ for profile in filesystem.list_dir('.uprofile/'): if re.match('.*.json$', profile): self.profiles.append(Profile(profile)) return def parse_argv(self, args): global verbose, version profile = None profiles = None action = None printsystem.use_colors(True) for arg in args: if arg == '-v': verbose = True printsystem.verbose() args = args[1:] elif arg == '-nc': printsystem.use_colors(False) args = args[1:] if len(args) < 1: self.get_profiles() profiles = self.profiles elif len(args) == 1: profile = self.get_profile(args[0]) elif len(args) == 2: profile = self.get_profile(args[0]) action = profile.get_action(args[1]) action.build() action.do_action(args[1:]) if len(args) == 2: args = None else: args = args[2:] return [profile, profiles, args, action] def main(): uprofile = UniversalProfileTool() try: list = uprofile.parse_argv(sys.argv[1:]) printsystem.print_ui(profile = list[0], \ profiles = list[1], action = list[3], args = list[2]) except UserWarning, warning: printsystem.print_exception(warning, True) except Exception, exception: printsystem.print_exception(exception) if verbose: traceback.print_exc() printsystem.print_line('') exit(1) exit(0) if __name__ == '__main__': main()