Google+ Tools
Make Google+ profile picture
Make Google plus banners for profile
Create and share your Google Plus profile banners.

Profile image for mrk studios alejandrombernardis on October 14, 2011
Language
Python
Tags

Py | Handler


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2011 The Octopus Apps Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# Author: Alejandro M. Bernardis
# Email: alejandro.m.bernardis at gmail.com
# Created: Apr 5, 2011 12:27:14 AM
#

import os
import re
import sys
from servertools import get_version
from servertools.common import DB, REPO, USER, services_list

# Variables
# ------------------------------------------------------------------------------
path_root = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
path_lib = os.path.join(path_root, 'lib')
path_package = os.path.join(path_root, 'servertools')
path_extras = [path_root, path_lib]

# Methods
# ------------------------------------------------------------------------------
usage_string = """Usage:  
  option: %prog [-h|-s|-v] [--help|--services|--version]
      or: %prog <service> ... args
      or: %prog <service> [-h|--help]"""
      
version_string = """The Octopus Apps - - Server Tools
  version %s"""
      
error_string = """Error:
  The action "%s" does not available
"""

def usage():
    print usage_string.replace('%prog', os.path.basename(__file__))
    sys.exit(1)
    
def usage_version():
    print version_string % get_version()
    sys.exit(1)
    
def usage_service():
    print """Services:
  Database:
    - dbcreate      create a new database
    - dbremove      permanently remove a database
    - dbadd         associated with a user repository
    - dbdel         a database detaches a repository
    - dblist        displays a list of all databases

  Repository:
    - repocreate    create a new repository
    - reporemove    permanently remove a repository
    - repoadd       enabled to access the repository
    - repodel       disable access to the repository
    - repolist      displays a list of all repositories

  User:
    - usercreate    create a new user
    - userremove    permanently remove a user
    - useradd       associated with a user repository
    - userdel       a user detaches a repository
    - userlist      displays a list of all users
"""

# Methods
# ------------------------------------------------------------------------------
def fix_sys_paths(paths=[]):
    """Fixed system paths"""
    sys.path = paths + sys.path

def run(fname, gargs):
    """Execute program."""
    fix_sys_paths(path_extras)
    execfile(fname, gargs)
    
def validate(args, pdir=path_package):
    """Validate the action to run."""
    largs = len(args)
    
    if largs == 2: 
        argument = args[1].lower()

        if argument in ['-v', '--version']:
            usage_version()
    
        elif argument in ['-s', '--services']:
            usage_service()
        
    elif largs >= 2:
        action = args[1].lower()
        pattern = r'^(%s|%s|%s)' % (DB, REPO, USER)
        
        if action in services_list:
            fname = os.path.join(pdir, os.path.basename(
                '%s.py' % re.search(pattern, action).group()))
            
            if os.path.exists(fname):
                run(fname, globals())
                return
            
        if action not in ['-h', '--help']:
            print error_string % action
        
    usage() 

# Main
# ------------------------------------------------------------------------------
if __name__ == '__main__':
    validate(sys.argv)

Comments

blog comments powered by Disqus