#!/usr/bin/python

"""
Copyright (c) 2013, Juniper Networks, Inc.
All rights reserved.
Author : Michael Ganley

manipulate paths to xml files.
"""
import argparse
from operator import index
import os
import sys
import shlex
import subprocess
import shutil



class manifestDir (object):
    def __init__(self, options):
        self.opt = options

    def parse (self):
        foo = os.path.split(self.opt.path_name)

        if DEBUG:
            print "manifest filename: ", foo[1]
        foo = os.path.split(foo[0])
        self.opt.sku = foo[1]
        if DEBUG:
            print "sku: ", self.opt.sku

        foo = os.path.split(foo[0])
        self.opt.product = foo[1]
        if DEBUG:
            print "product: ", self.opt.product

        """
        Because when the find runs it pass the '.'
        we have to skip over it.
        """
        foo = os.path.split(foo[0])
        foo = os.path.split(foo[0])
        self.opt.buildid=foo[1]
        if DEBUG:
            print "Build ID: ", self.opt.buildid

        foo = os.path.split(foo[0])
        self.opt.branch = foo[1]
        if DEBUG:
            print "Branch: ", self.opt.branch

    def fireProcedure (self):

        if DEBUG:
            print "Going to execute procedure"
            command = "ectool runProcedure Contrail-CI-CD --procedureName Foo " \
                      "--actualParameter branch=" + self.opt.branch + \
                      " Platform=" + self.opt.product + \
                      " sku=" + self.opt.sku + \
                      " BuildId=" + self.opt.buildid

        else:
            command = "ectool runProcedure Contrail-CI-CD --procedureName PipeLine " \
                      "--actualParameter branch=" + self.opt.branch + \
                      " Platform=" + self.opt.product + \
                      " sku=" + self.opt.sku + \
                      " BuildId=" + self.opt.buildid

        if DEBUG:
            print "executing following command:\n", command

        execute(command, ignore_errors=False)


def execute(command, ignore_errors=False):
    """ Function to execute shell command and return the output """

    if DEBUG:
        print 'DEBUG: %s' % (command)
    pipe = subprocess.Popen(shlex.split(command),
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            close_fds=True)

    data = pipe.stdout.read()
    rc = pipe.wait()
    cwd = os.getcwd()
    if rc and not ignore_errors:
        print 'Error : Working directory : %s' % (cwd)
        print 'Error : Failed to execute command: %s\n%s' % (command, data)
        sys.exit(1)
    return data.strip()

def cmd_exists(cmd):
    return subprocess.call("type " + cmd, shell=True,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0


def parse_options(args):
    """ Parse command line arguments """
    parser = argparse.ArgumentParser(description='Contrail/EC procedure wrapper script ')

    parser.add_argument('-v', '--verbose', dest='debug', action='store_true',
                        help='Enable verbose mode')

    parser.add_argument('-p', '--path', dest='path_name',
                        help="Full path to the manifest file")

    subparsers = parser.add_subparsers(title='Fire off EC jobs based on the path to manifest file',
                                       description='Select one command',
                                       dest='command')

    parser_parse = subparsers.add_parser('parse',
                                           description='parse and execute build for manifest')
    opt = parser.parse_args(args)
    return opt


if __name__ == '__main__':
    options = parse_options(sys.argv[1:])
    DEBUG = options.debug
    manifest = manifestDir(options)

    """ Test to make sure all our commands exist """



    if manifest.opt.command == 'parse':
        manifest.parse()
        manifest.fireProcedure()
        sys.exit(0)

    print "Unknown command: ", manifest.opt.command
    sys.exit(1)




93bab88b8d546b6fe2e33860ef123e0e08dafab7
#!/usr/bin/python

"""
Copyright (c) 2013, Juniper Networks, Inc.
All rights reserved.
Author : Michael Ganley

manipulate paths to xml files.
"""
import argparse
from operator import index
import os
import sys
import shlex
import subprocess
import shutil



class manifestDir (object):
    def __init__(self, options):
        self.opt = options

    def parse (self):
        foo = os.path.split(self.opt.path_name)

        if DEBUG:
            print "manifest filename: ", foo[1]
        foo = os.path.split(foo[0])
        self.opt.sku = foo[1]
        if DEBUG:
            print "sku: ", self.opt.sku

        foo = os.path.split(foo[0])
        self.opt.product = foo[1]
        if DEBUG:
            print "product: ", self.opt.product

        """
        Because when the find runs it pass the '.'
        we have to skip over it.
        """
        foo = os.path.split(foo[0])
        foo = os.path.split(foo[0])
        self.opt.buildid=foo[1]
        if DEBUG:
            print "Build ID: ", self.opt.buildid

        foo = os.path.split(foo[0])
        self.opt.branch = foo[1]
        if DEBUG:
            print "Branch: ", self.opt.branch

    def fireProcedure (self):

        if DEBUG:
            print "Going to execute procedure"
            command = "ectool runProcedure Contrail-CI-CD --procedureName Foo " \
                      "--actualParameter branch=" + self.opt.branch + \
                      " Platform=" + self.opt.product + \
                      " sku=" + self.opt.sku + \
                      " BuildId=" + self.opt.buildid

        else:
            command = "ectool runProcedure Contrail-CI-CD --procedureName PipeLine " \
                      "--actualParameter branch=" + self.opt.branch + \
                      " Platform=" + self.opt.product + \
                      " sku=" + self.opt.sku + \
                      " BuildId=" + self.opt.buildid

        if DEBUG:
            print "executing following command:\n", command

        execute(command, ignore_errors=False)


def execute(command, ignore_errors=False):
    """ Function to execute shell command and return the output """

    if DEBUG:
        print 'DEBUG: %s' % (command)
    pipe = subprocess.Popen(shlex.split(command),
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            close_fds=True)

    data = pipe.stdout.read()
    rc = pipe.wait()
    cwd = os.getcwd()
    if rc and not ignore_errors:
        print 'Error : Working directory : %s' % (cwd)
        print 'Error : Failed to execute command: %s\n%s' % (command, data)
        sys.exit(1)
    return data.strip()

def cmd_exists(cmd):
    return subprocess.call("type " + cmd, shell=True,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0


def parse_options(args):
    """ Parse command line arguments """
    parser = argparse.ArgumentParser(description='Contrail/EC procedure wrapper script ')

    parser.add_argument('-v', '--verbose', dest='debug', action='store_true',
                        help='Enable verbose mode')

    parser.add_argument('-p', '--path', dest='path_name',
                        help="Full path to the manifest file")

    subparsers = parser.add_subparsers(title='Fire off EC jobs based on the path to manifest file',
                                       description='Select one command',
                                       dest='command')

    parser_parse = subparsers.add_parser('parse',
                                           description='parse and execute build for manifest')
    opt = parser.parse_args(args)
    return opt


if __name__ == '__main__':
    options = parse_options(sys.argv[1:])
    DEBUG = options.debug
    manifest = manifestDir(options)

    """ Test to make sure all our commands exist """



    if manifest.opt.command == 'parse':
        manifest.parse()
        manifest.fireProcedure()
        sys.exit(0)

    print "Unknown command: ", manifest.opt.command
    sys.exit(1)



