Ejemplo n.º 1
0
def getOptions():
  if sys.version_info >= (2,7):
    from argparse import ArgumentParser
    parser = ArgumentParser(description='Turn SBML model into URDME 1.2 model files.')
    parser.add_argument('-f','--file', action='store',help="Input file name (SBML format)", default = "model.xml", dest="input")
    parser.add_argument('-o','--output',action='store', help="Output destination (directory)", default=".", dest="output")
    return parser.parse_args()
  else:
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-f","--file", help="Input file name (SBML format)", type="string", default = "model.xml", dest="input")
    parser.add_option("-o","--output", help="Output destination (directory)", type="string", default=".", dest="output")
    (opts, args) = parser.parse_args()
    return opts
Ejemplo n.º 2
0
def create_option_parser():
    from argparse import ArgumentParser
    p = ArgumentParser()

    p.add_option(
        '-s', '--src',
        type=str,
        dest='srcdir',
        default='mozilla',
        help='The directory containing the Mozilla l10n sources.'
    )
    p.add_argument(
        '-d', '--dest',
        type=str,
        dest='destdir',
        default='l10n',
        help='The destination directory to copy the en-US locale files to.'
    )
    p.add_argument(
        '-p', '--mozproduct',
        type=str,
        dest='mozproduct',
        default='browser',
        help='The Mozilla product name.'
    )
    p.add_argument(
        '--delete-dest',
        type=str,
        dest='deletedest',
        default=False,
        action='store_true',
        help='Delete the destination directory (if it exists).'
    )

    p.add_argument(
        '-v', '--verbose',
        type=str,
        dest='verbose',
        action='store_true',
        default=False,
        help='Be more noisy'
    )

    return p
Ejemplo n.º 3
0
from keras.models import Sequential
from keras.layers import Embedding, Merge
from keras.layers.core import Activation
from keras.preprocessing import sequence
from keras.optimizers import SGD


from stream.streamer import SymmetricContextStreamer, SessionContextStreamer, stream_generator, Batch2BatchStreamer, SkipGramStreamer
from tabtools.tabutils import RecordMeta
from preprocess.preprocessing import read_sampling_table, read_feature_map


if __name__ == '__main__':

    parser = ArgumentParser()
    parser.add_option('-i', dest="input", default=None, type=str, help="input file")
    parser.add_option('-d', '--dim', dest="dim", default=300, type=int, help="embeddings dimensionality")
    parser.add_option('-b', '--batch_size', dest="batch_size", default=50, type=int, help="training batch size")
    parser.add_option('--window', dest="window", default=5, type=int, help="window size")
    parser.add_option('--neg', dest="neg_pairs", default=1.0, type=float, help="neg samples proportion")
#    parser.add_option('-i', dest="input", default=None, type=str, help="input file")

    args = parser.parse_args()

    embedding_dim = args.dim
    num_features = ...

    word_layer = Sequential()
    word_layer.add(Embedding(num_features, embedding_dim, ini t='uniform'))
    context_layer = Sequential()
    context_layer.add(Embedding(num_features, embedding_dim, init='uniform'))
    'full_name':"Wizard of ID", 'first_date':date(2000, 1, 1)}}

#path_prefix = ''.join(["/Users/aaronmeurer/Documents/Comics/", ACME_name, "/",
#    ACME_name, "-"])
path_prefix = ''.join(["/Users/aaronmeurer/Documents/Comics_test/", ACME_name,
    "/", ACME_name, "-"]) # Testing path
# The remainder of the path with the year should be created later

# Get the command line arguments
usage = "usage: comics_downloader.py [-s|--strips strip names] [-t|--times dates (yyyymmdd)]"
parser = ArgumentParser(description=usage)

parser.add_option(
    '-s', '--strips',
    dest='strips',
    action='store',
    default=None,
    help="Download just the strips listed.  Use the ACMEReader short name " +
    "(the name of the comic's folder in ~/Documents/Comics/).")

parser.add_option(
    '-t', '--t',
    dest='time',
    action='store',
    default=None,
    help="Download strips starting from the given date, given in yyyymmdd " +
    "format. It goes back until it finds an already downloaded strip, or " +
    "until it reaches the start date for that strip.")

if len(sys.argv) > 1:
    if '-s' in sys.argv:
def main(argv):
    """StartDscConfiguration"""

    # Define method arguments and description
    description = 'Starts the specified DSC configuration.'

    parameters = {
        'configurationmof' : {
            'shortForm' : 'c',
            'helpText' : 'The path to the configuration mof to start.',
            'required' : True,
            'action' : 'store'
        },
        'force' : {
            'shortForm' : 'f',
            'helpText' : 'Specifies that any current pending configuration should be forcibly removed before starting the new configuration.',
            'required' : False,
            'action' : 'store_true'
        }
    }

    # Parse -configurationmof on its own for backwards compatibility
    configmofArgument = None
    if '-configurationmof' in argv:
        configmofIndex = argv.index('-configurationmof')

        try:
            configmofArgument = argv[configmofIndex + 1]
        except:
            print 'StartDscConfiguration.py: error: Please provide a valid path argument for -configurationmof'
            exit(1)

        # Set the configuration mof parameter to no longer be required so it doesn't error in the arugment parser
        parameters['configurationmof']['required'] = False 

        # Remove -configurationmof and its argument from the list so it doesn't error in the arugment parser
        argv.pop(configmofIndex)
        argv.pop(configmofIndex)

    # Parse arguments
    if (useArgParse):
        # Used by Python 2.7+
        parser = ArgumentParser(description = description)

        for parameter in parameters.keys():
            parameterInfo = parameters[parameter]
            parser.add_argument('-' + parameterInfo['shortForm'], '--' + parameter, required = parameterInfo['required'], help = parameterInfo['helpText'], action = parameterInfo['action'])

        parsedArguments = parser.parse_args(argv)
    else:
        # Used by Python 2.4-2.6
        parser = OptionParser(description = description)

        for parameter in parameters.keys():
            parameterInfo = parameters[parameter]
            parser.add_option('-' + parameterInfo['shortForm'], '--' + parameter, help = parameterInfo['helpText'], action = parameterInfo['action'])

        (parsedArguments, extraArguments) = parser.parse_args(argv)

        for parameter in parameters.keys():
            if parameters[parameter]['required']:
                if not getattr(parsedArguments, parameter):
                    print 'StartDscConfiguration.py: error: argument -' + parameters[parameter]['shortForm'] + '/--' + parameter + ' is required.'
                    exit(1)

    # Check that we don't have two configuration mofs defined
    if configmofArgument and parsedArguments.configurationmof:
        print 'StartDscConfiguration.py: error: Two configuration mof arguments were found. Please provide only one.'
        exit(1)
    
    if configmofArgument:
        parsedArguments.configurationmof = configmofArgument

    # Read the configuration mof
    try:
        configurationFile = open(parsedArguments.configurationmof, 'r')
    except:
        configurationFile = open(parsedArguments.configurationmof, 'r', encoding = 'utf-16')

    try:
        configurationFileContent = configurationFile.read()
    finally:
        configurationFile.close()

    # Convert the file content to strings of integers representing unicode
    configurationData = []
    for char in configurationFileContent:
        configurationData.append(str(ord(char)))

    # # OMI CLI location
    omiBinDir = "<CONFIG_BINDIR>"
    omiCliPath = omiBinDir + "/omicli"
    dsc_host_base_path = helperlib.DSC_HOST_BASE_PATH
    dsc_host_path = join(dsc_host_base_path, 'bin/dsc_host')
    dsc_host_output_path = join(dsc_host_base_path, 'output')
    dsc_host_lock_path = join(dsc_host_base_path, 'dsc_host_lock')
    dsc_host_switch_path = join(dsc_host_base_path, 'dsc_host_ready')

    if ("omsconfig" in helperlib.DSC_SCRIPT_PATH):
        write_omsconfig_host_switch_event(pathToCurrentScript, isfile(dsc_host_switch_path))

    if ("omsconfig" in helperlib.DSC_SCRIPT_PATH) and (isfile(dsc_host_switch_path)):
        use_omsconfig_host = True
    else:
        use_omsconfig_host = False

    # Assemble parameters to pass to OMI CLI
    host_parameters = []
    if use_omsconfig_host:
        host_parameters.append(dsc_host_path)
        host_parameters.append(dsc_host_output_path)
        host_parameters.append("SendConfigurationApply")
        host_parameters.append(args[2])
        # Insert force if specified
        if parsedArguments.force:
            host_parameters.append("force")
    else:
        host_parameters.append(omiCliPath)
        host_parameters.append("iv")
        host_parameters.append("<DSC_NAMESPACE>")
        host_parameters.append("{")
        host_parameters.append("MSFT_DSCLocalConfigurationManager")
        host_parameters.append("}")
        host_parameters.append("SendConfigurationApply")
        host_parameters.append("{")
        host_parameters.append("ConfigurationData")
        host_parameters.append("[")

        # Insert configurationmof data here
        for token in configurationData:
            host_parameters.append(token)
        
        host_parameters.append("]")

        # Insert force if specified
        if parsedArguments.force:
            host_parameters.append("force")
            host_parameters.append("true")

        host_parameters.append("}")

    stdout = ''
    stderr = ''

    if use_omsconfig_host:
        try:
            # Open the dsc host lock file. This also creates a file if it does not exist
            dschostlock_filehandle = open(dsc_host_lock_path, 'w')
            print("Opened the dsc host lock file at the path '" + dsc_host_lock_path + "'")
            
            dschostlock_acquired = False

            # Acquire dsc host file lock
            for retry in range(10):
                try:
                    flock(dschostlock_filehandle, LOCK_EX | LOCK_NB)
                    dschostlock_acquired = True
                    break
                except IOError:
                    write_omsconfig_host_log('dsc_host lock file not acquired. retry (#' + str(retry) + ') after 60 seconds...', pathToCurrentScript)
                    sleep(60)

            if dschostlock_acquired:
                p = subprocess.Popen(parameters, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                stdout, stderr = p.communicate()
                print(stdout)
            else:
                print("dsc host lock already acuired by a different process")
        finally:
            # Release dsc host file lock
            flock(dschostlock_filehandle, LOCK_UN)

            # Close dsc host lock file handle
            dschostlock_filehandle.close()
    else:
        p = subprocess.Popen(parameters, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()

    print(stdout)
    print(stderr)
Ejemplo n.º 6
0
        key = '--wireless'
        if key in sys_args:
            CTRexScenario.test_types['wireless_tests'].append('wireless_tests')
            sys_args.remove(key)
        # Run all of the tests or just the selected ones
        if not sum([len(x) for x in CTRexScenario.test_types.values()]):
            for key in CTRexScenario.test_types.keys():
                if key == 'wireless_tests' and sys.version_info[0] < 3:
                    continue
                CTRexScenario.test_types[key].append(key)

    nose_argv += sys_args

    cfg_plugin = CTRexTestConfiguringPlugin()
    parser = ArgumentParser(add_help = False)
    parser.add_option = parser.add_argument
    cfg_plugin.options(parser)
    options, _ = parser.parse_known_args(sys.argv)

    trex_tests = options.stateless or options.stateful or options.astf or options.bird
    if not CTRexScenario.is_test_list and (trex_tests or not (trex_tests or options.functional or options.wireless)):
        if CTRexScenario.setup_dir and options.config_path:
            fatal('Please either define --cfg or use env. variable SETUP_DIR, not both.')
        if not options.config_path and CTRexScenario.setup_dir:
            options.config_path = CTRexScenario.setup_dir
        if not options.config_path:
            fatal('Please specify path to config.yaml using --cfg parameter or env. variable SETUP_DIR')
        options.config_path = options.config_path.rstrip('/')
        CTRexScenario.setup_name = os.path.basename(options.config_path)
        CTRexScenario.configuration = misc_methods.load_complete_config_file(os.path.join(options.config_path, 'config.yaml'))
        CTRexScenario.config_dict = misc_methods.load_object_config_file(os.path.join(options.config_path, 'config.yaml'))
Ejemplo n.º 7
0
def main(argv):
    """StartDscConfiguration"""

    # Define method arguments and description
    description = 'Starts the specified DSC configuration.'

    parameters = {
        'configurationmof': {
            'shortForm': 'c',
            'helpText': 'The path to the configuration mof to start.',
            'required': True,
            'action': 'store'
        },
        'force': {
            'shortForm': 'f',
            'helpText':
            'Specifies that any current pending configuration should be forcibly removed before starting the new configuration.',
            'required': False,
            'action': 'store_true'
        }
    }

    # Parse -configurationmof on its own for backwards compatibility
    configmofArgument = None
    if '-configurationmof' in argv:
        configmofIndex = argv.index('-configurationmof')

        try:
            configmofArgument = argv[configmofIndex + 1]
        except:
            print 'StartDscConfiguration.py: error: Please provide a valid path argument for -configurationmof'
            exit(1)

        # Set the configuration mof parameter to no longer be required so it doesn't error in the arugment parser
        parameters['configurationmof']['required'] = False

        # Remove -configurationmof and its argument from the list so it doesn't error in the arugment parser
        argv.pop(configmofIndex)
        argv.pop(configmofIndex)

    # Parse arguments
    if (useArgParse):
        # Used by Python 2.7+
        parser = ArgumentParser(description=description)

        for parameter in parameters.keys():
            parameterInfo = parameters[parameter]
            parser.add_argument('-' + parameterInfo['shortForm'],
                                '--' + parameter,
                                required=parameterInfo['required'],
                                help=parameterInfo['helpText'],
                                action=parameterInfo['action'])

        parsedArguments = parser.parse_args(argv)
    else:
        # Used by Python 2.4-2.6
        parser = OptionParser(description=description)

        for parameter in parameters.keys():
            parameterInfo = parameters[parameter]
            parser.add_option('-' + parameterInfo['shortForm'],
                              '--' + parameter,
                              help=parameterInfo['helpText'],
                              action=parameterInfo['action'])

        (parsedArguments, extraArguments) = parser.parse_args(argv)

        for parameter in parameters.keys():
            if parameters[parameter]['required']:
                if not getattr(parsedArguments, parameter):
                    print 'StartDscConfiguration.py: error: argument -' + parameters[
                        parameter][
                            'shortForm'] + '/--' + parameter + ' is required.'
                    exit(1)

    # Check that we don't have two configuration mofs defined
    if configmofArgument and parsedArguments.configurationmof:
        print 'StartDscConfiguration.py: error: Two configuration mof arguments were found. Please provide only one.'
        exit(1)

    if configmofArgument:
        parsedArguments.configurationmof = configmofArgument

    # Read the configuration mof
    try:
        configurationFile = open(parsedArguments.configurationmof, 'r')
    except:
        configurationFile = open(parsedArguments.configurationmof,
                                 'r',
                                 encoding='utf-16')

    try:
        configurationFileContent = configurationFile.read()
    finally:
        configurationFile.close()

    # Convert the file content to strings of integers representing unicode
    configurationData = []
    for char in configurationFileContent:
        configurationData.append(str(ord(char)))

    # OMI CLI location
    omiBinDir = "<CONFIG_BINDIR>"
    omiCliPath = omiBinDir + "/omicli"

    # Assemble parameters to pass to OMI CLI
    omiCliParameters = []
    omiCliParameters.append(omiCliPath)
    omiCliParameters.append("iv")
    omiCliParameters.append("<DSC_NAMESPACE>")
    omiCliParameters.append("{")
    omiCliParameters.append("MSFT_DSCLocalConfigurationManager")
    omiCliParameters.append("}")
    omiCliParameters.append("SendConfigurationApply")
    omiCliParameters.append("{")
    omiCliParameters.append("ConfigurationData")
    omiCliParameters.append("[")

    # Insert configurationmof data here
    for token in configurationData:
        omiCliParameters.append(token)

    omiCliParameters.append("]")

    # Insert force if specified
    if parsedArguments.force:
        omiCliParameters.append("force")
        omiCliParameters.append("true")

    omiCliParameters.append("}")

    # Call OMI CLI in subprocess
    omiCliProcess = Popen(omiCliParameters, stdout=PIPE, stderr=PIPE)

    # Retrieve stdout and stderr from OMI CLI call
    stdout, stderr = omiCliProcess.communicate()

    # Print output from OMI CLI call
    print(stdout)
    print(stderr)
Ejemplo n.º 8
0
    # [http://www.doughellmann.com/PyMOTW/optparse/]

    # from optparse import OptionParser, OptionValueError

    from argparse import ArgumentParser
    parser = ArgumentParser(description="A very extensible IRC bot written in Python.")
    
    
    parser = OptionParser(usage='usage: %prog [options] [filename(s)] ',
                          version='%%prog %s' % __version__ )

    # --help ==>    Usage: get_files_values.py pathname [options] 
    # --version ==> get_files_values.py 0.6


    parser.add_option("-r", "--recursive",  dest="do_recursion",  action="store_const", const=True, 
        help="Recursively process subdirectories. Recursion can be limited by setting DEPTH." ,default=False )
    
                                                
    parser.add_option("-v", "--verbose", dest="verbose_level", 
        help="increment verbose count (verbosity) by one. "\
                "Normal operation is to output one status line per file. "\
                "One -v option will give you information on what files are being"\
                " skipped and slightly more information at the end.  default=%default", action="count" ) 

    parser.add_option("-q", "--quiet", 
        action="store_const", const=0, dest="verbose_level", default=1, 
           help="Normal operation is to output one status line per file, status being \"inserted\", \"existing\", etc."
           " This option will prevent any output to stdout, Significant errors are still output to stderr.") 

 # -v, --verbose               increase verbosity
 # -q, --quiet                 suppress non-error messages
Ejemplo n.º 9
0
	def setup_optparser(self):
		"""Parse command line arguments using optarg"""
		parser = OptionParser(usage=usage, formatter_class=Formatter)
		parser.add_option = parser.add_argument
		parser.add_argument('--version', action='version', version=version)
		parser.add_option('--db', dest='db_type', metavar='[postgres|sqlite]',
						  help='Type of database')
		parser.add_option('--dbname', dest='db_name', metavar='NAME',
						  help='Specify the database file or name.')
		parser.add_option('--dbuser', dest='db_user', metavar='DATABASE',
						  help='PostGres user')
		parser.add_option('--dbpassword', dest='db_password', metavar='DATABASE',
						  help='PostGres password')
		parser.add_option('--dbhost', dest='db_host', metavar='HOST',
						  help='Hostname of PostGres server')
		parser.add_option('--dbport', dest='db_port', metavar='PORT',
						  help='Port of PostGres server')

		parser.add_option('-c', '--configfile', dest='config', metavar='FILE',
				help='Location of config file')
		parser.add_option('-u', '--username', dest='username', metavar='USER',
					  help='Specify the quassel username.')
		parser.add_option('-N', '--network', dest='network', metavar='NETWORK',
					  help='Specify the network to search.')
		parser.add_option('-b', '--buffer', dest='buffer', metavar='BUFFER',
					  help='Specify the Quassel buffer (query nick or #channel) to search')
		parser.add_option('-n', '--nick', dest='sender', metavar='NICK',
					  help='Specify the nickname to search for')
		parser.add_option('-t', '--time', dest='timerange', metavar='RANGE',
					  help='Time range. See README for details.')
		parser.add_option('-I', '--ignorecase', dest='ignorecase', action='store_true',
        			  help='Ignore case')
		parser.add_option('-i', '--inclusive', dest='inclusive', action='store_true',
		              help='Also search for joins, parts, etc.')
		parser.add_option('-L', '--limit', dest='limit', metavar='NUM',
		              help='Return at most NUM results')

		parser.add_option('--server', dest='server', action='store_true')
		parser.add_option('-H', '--host', dest='hostname', help='Connect to quasselgrep server at HOSTNAME')
		parser.add_option('-p', '--password', dest='password', help='Password your quassel username')

		parser.add_option('-l', dest='whole_line', action='store_true',
				help='Return only results whose message matches the entire search string')
		parser.add_option('-C', '--context', dest='context', metavar='LINES',
						  help='Include this many lines of context with results.')

		parser.add_option('--debug', dest='debug', action='store_true',
				help='Display information about the query instead of running it')

		parser.add_argument('keywords', nargs='*', help='The text to search for')

		self.parser = parser
		self.all_options = []
		self.valid_options = []
		for option in parser._actions:
			if not option.dest:
				continue

			# In python 3 arguments are read in as unicode, but not in python 2. The following
			# fixes all the above arguments which take in strings to decode them to unicode even
			# in python 2.
			if sys.version_info.major == 2 and (option.type == str or option.type == None):
				option.type = self.arg_to_unicode

			self.all_options.append(option.dest)

			if option.dest[:2] == 'db':
				continue
			if option.dest in ['hostname', 'config', 'server']:
				continue
			self.valid_options.append(option.dest)
Ejemplo n.º 10
0
    pass


from .cim_operations import WBEMConnection

#
# Parse command line args
#

optparser = OptionParser(
    usage='%prog HOSTNAME [-u USER -p PASS] [-n NAMESPACE] [--no-ssl]')

# Username and password

optparser.add_option('-u', '--user', dest='user',
                     action='store', type='string',
                     help='user to connect as')

optparser.add_option('-p', '--password', dest='password',
                     action='store', type='string',
                     help='password to connect user as')

# Change the default namespace used

optparser.add_option('-n', '--namespace', dest='namespace',
                     action='store', type='string', default='root/cimv2',
                     help='default namespace to use')

# Don't use SSL for remote connections

optparser.add_option('--no-ssl', dest='no_ssl', action='store_true',
Ejemplo n.º 11
0
 def add_argument(self, *args, **kwargs):
     if HAS_ARGPARSE:
         return Parser.add_argument(self, *args, **kwargs)
     return Parser.add_option(self, *args, **kwargs)
Ejemplo n.º 12
0
    def _get_parserobj(self, option_list):
        """judged to parser type, return tp parser object

        :param option_list: parser option list
        :return: parser object, optparse.OptionParser or
                 argparse.ArgumentParser
        :rtype: parser object class
        """
        if '--version' in self.parselines[0]:
            if argparse and 'argparse' == self.parser_type:
                parser = ArgumentParser(version='dummy',
                                        formatter_class=RawDescriptionHelpFormatter)
            else:
                parser = OptionParser(version="dummy")
        else:
            if argparse and 'argparse' == self.parser_type:
                parser = ArgumentParser(
                    formatter_class=RawDescriptionHelpFormatter)
            else:
                parser = OptionParser()
        for opt in option_list:
            if opt['short'] and self.parser_type is 'optparse':
                if parser.has_option(opt['short']):
                    parser.remove_option(opt['short'])
                parser.add_option(opt['short'], opt['long'],
                                  metavar=opt['metavar'],
                                  help=opt['help'].strip())
            elif not opt['short'] and self.parser_type is 'optparse':
                if parser.has_option(opt['short']):
                    parser.remove_option(opt['short'])
                parser.add_option(opt['long'],
                                  metavar=opt['metavar'],
                                  help=opt['help'].strip())
            elif opt['long'] and opt['short'] and \
                    self.parser_type is 'argparse':
                if opt['metavar'] is None:
                    parser.add_argument(opt['short'], opt['long'],
                                        action='store_true',
                                        help=opt['help'].strip())
                else:
                    parser.add_argument(opt['short'], opt['long'],
                                        metavar=opt['metavar'],
                                        help=opt['help'].strip())
            elif opt['short'] and self.parser_type is 'argparse':
                if opt['metavar'] is None:
                    parser.add_argument(opt['short'],
                                        action='store_true',
                                        help=opt['help'].strip())
                else:
                    parser.add_argument(opt['short'],
                                        metavar=opt['metavar'],
                                        help=opt['help'].strip())
            elif not opt['short'] and self.parser_type is 'argparse':
                if opt['metavar'] is None:
                    parser.add_argument(opt['long'],
                                        action='store_true',
                                        help=opt['help'].strip())
                else:
                    parser.add_argument(opt['long'],
                                        metavar=opt['metavar'],
                                        help=opt['help'].strip())
            else:
                raise InvalidParserTypeError("Invalid paresr type.")
        return parser
Ejemplo n.º 13
0
    def _get_parserobj(self, option_list):
        """judged to parser type, return tp parser object

        :param option_list: parser option list
        :return: parser object, optparse.OptionParser or
                 argparse.ArgumentParser
        :rtype: parser object class
        """
        if '--version' in self.parselines[0]:
            if argparse and 'argparse' == self.parser_type:
                parser = ArgumentParser(
                    version='dummy',
                    formatter_class=RawDescriptionHelpFormatter)
            else:
                parser = OptionParser(version="dummy")
        else:
            if argparse and 'argparse' == self.parser_type:
                parser = ArgumentParser(
                    formatter_class=RawDescriptionHelpFormatter)
            else:
                parser = OptionParser()
        for opt in option_list:
            if opt['short'] and self.parser_type is 'optparse':
                if parser.has_option(opt['short']):
                    parser.remove_option(opt['short'])
                parser.add_option(opt['short'],
                                  opt['long'],
                                  metavar=opt['metavar'],
                                  help=opt['help'].strip())
            elif not opt['short'] and self.parser_type is 'optparse':
                if parser.has_option(opt['short']):
                    parser.remove_option(opt['short'])
                parser.add_option(opt['long'],
                                  metavar=opt['metavar'],
                                  help=opt['help'].strip())
            elif opt['long'] and opt['short'] and \
                    self.parser_type is 'argparse':
                if opt['metavar'] is None:
                    parser.add_argument(opt['short'],
                                        opt['long'],
                                        action='store_true',
                                        help=opt['help'].strip())
                else:
                    parser.add_argument(opt['short'],
                                        opt['long'],
                                        metavar=opt['metavar'],
                                        help=opt['help'].strip())
            elif opt['short'] and self.parser_type is 'argparse':
                if opt['metavar'] is None:
                    parser.add_argument(opt['short'],
                                        action='store_true',
                                        help=opt['help'].strip())
                else:
                    parser.add_argument(opt['short'],
                                        metavar=opt['metavar'],
                                        help=opt['help'].strip())
            elif not opt['short'] and self.parser_type is 'argparse':
                if opt['metavar'] is None:
                    parser.add_argument(opt['long'],
                                        action='store_true',
                                        help=opt['help'].strip())
                else:
                    parser.add_argument(opt['long'],
                                        metavar=opt['metavar'],
                                        help=opt['help'].strip())
            else:
                raise InvalidParserTypeError("Invalid paresr type.")
        return parser
Ejemplo n.º 14
0
Archivo: build.py Proyecto: sdinc/viz3d
def main():
    """
    main method that calls into all other methods
    """
    ret = 0
    runpythonunit = False
    runpythonunitfast = False
    bHelp = 'Full test'
    fHelp = 'Full tests, no network connection'
    des = "Command Line Options"
    phpunit_args = ""
    #builds up command line arguments
    try:
        from argparse import ArgumentParser
        parser = ArgumentParser(description = des)
        parser.add_argument('--unit', '-b', dest='u', action = 'store_true', help=bHelp)
        parser.add_argument('--unit-fast', '-f', dest='f', action = 'store_true', help=fHelp)
        args = parser.parse_args()
        
    except ImportError: #Must be python 2.6 or lower
        from optparse import OptionParser
        parser = OptionParser(description = des)
        parser.add_option('--unit', '-b', dest='u', action = 'store_true', help=bHelp)
        parser.add_option('--unit-fast', '-f', dest='f', action = 'store_true', help=fHelp)
        args, blank  = parser.parse_args()
       
    #parse command line options
    if args.u:
        runpythonunit = True
    if args.f:
        runpythonunitfast = True
    
    if len(sys.argv) < 2:
        parser.print_help()
    
    if runpythonunitfast == True:
        #get the location of current virtualenv
        local_python = get_python()
        if not os.path.exists( local_python ):
            log("Error: virtualenv at (%s) does not exists." %(local_python) )
            runpythonunit =  False
            ret += 1
        #install dependancies and return deps that could not be installed
        ret += install_modules(tmpdir = local_python, nose = True)
        remove_module(tmpdir = local_python, module = 'viz3d')
    
    if runpythonunit == True:
        log("\n\n%s\n\n" %( bHelp ) )
        checkvirtualenv()
        #creates python virtualenv and returns the path to it
        tmpdir = createvirtualenv()
        #install dependancies and return deps that could not be installed
        dep_return = install_deps(tmpdir = tmpdir)
        #installs the python modules and runs the unit tests
        ret = ret + install_modules(tmpdir = tmpdir, dep_return = dep_return, nose = True)
        #clear out the temporary virtualenv
        log("\n\ncleaning up by removing dir (%s)\n\n" %(tmpdir) )
        subprocess.call(shlex.split("rm -rf %s" %(tmpdir)))
        #make sure the return status is reported correctly back to buildbot or command line 
    
    if not ret == 0:
        log("Error: Process returned none zero exit status, ie some thing went wrong.  If you are confused then send email to [email protected]")
        sys.exit(ret)
Ejemplo n.º 15
0
        freq = float(count) / total_count
        pos_sampling_table[item] = 1 - sqrt(min_freq / freq)
        if count < min_shows:
            pos_sampling_table[item] = 0

        neg_probabilities.append(count**neg_power / neg_sum)
        neg_items.append(item)

    return pos_sampling_table, neg_items, neg_probabilities



if __name__ == '__main__':

    argparser = ArgumentParser()
    argparser.add_option('-i', dest='infile', type=str, default=None, help='input file')
    argparser.add_option('-o', dest='outfile', type=str, default=None, help='output file')
    argparser.add_option('--del', dest='min_shows', type=int, default=None,
        help='keep items with at least del counts')
    argparser.add_option('--min-freq', dest='sampling_min_freq', type=float, default=None,
        help='min_freq used in subsampling probabilities')
    argparser.add_option('--sub', dest='subsample', type=float, default=None,
        help="subsampling parameter in Mikolov's formula")
    argparser.add_option('--stats', dest='stats_table', type=str, default=None,
        help="file with items statistics")
    #argparser.add_option('--fmap', dest='feature_map', type=str, default=None,
    #    help="feature map file")
    argparser.add_option('--win', dest='window', type=int, default=None,
        help='window size')
    argparser.add_option('--weight', dest='win_weight', action='store_true',
        help='weigh contexts according to their distance from center word')
def main(argv):
    """StartDscConfiguration"""

    # Define method arguments and description
    description = 'Starts the specified DSC configuration.'

    parameters = {
        'configurationmof' : {
            'shortForm' : 'c',
            'helpText' : 'The path to the configuration mof to start.',
            'required' : True,
            'action' : 'store'
        },
        'force' : {
            'shortForm' : 'f',
            'helpText' : 'Specifies that any current pending configuration should be forcibly removed before starting the new configuration.',
            'required' : False,
            'action' : 'store_true'
        }
    }

    # Parse -configurationmof on its own for backwards compatibility
    configmofArgument = None
    if '-configurationmof' in argv:
        configmofIndex = argv.index('-configurationmof')

        try:
            configmofArgument = argv[configmofIndex + 1]
        except:
            print('StartDscConfiguration.py: error: Please provide a valid path argument for -configurationmof')
            exit(1)

        # Set the configuration mof parameter to no longer be required so it doesn't error in the arugment parser
        parameters['configurationmof']['required'] = False 

        # Remove -configurationmof and its argument from the list so it doesn't error in the arugment parser
        argv.pop(configmofIndex)
        argv.pop(configmofIndex)

    # Parse arguments
    if (useArgParse):
        # Used by Python 2.7+
        parser = ArgumentParser(description = description)

        for parameter in parameters.keys():
            parameterInfo = parameters[parameter]
            parser.add_argument('-' + parameterInfo['shortForm'], '--' + parameter, required = parameterInfo['required'], help = parameterInfo['helpText'], action = parameterInfo['action'])

        parsedArguments = parser.parse_args(argv)
    else:
        # Used by Python 2.4-2.6
        parser = OptionParser(description = description)

        for parameter in parameters.keys():
            parameterInfo = parameters[parameter]
            parser.add_option('-' + parameterInfo['shortForm'], '--' + parameter, help = parameterInfo['helpText'], action = parameterInfo['action'])

        (parsedArguments, extraArguments) = parser.parse_args(argv)

        for parameter in parameters.keys():
            if parameters[parameter]['required']:
                if not getattr(parsedArguments, parameter):
                    print ('StartDscConfiguration.py: error: argument -', parameters[parameter]['shortForm'], '/--', parameter, ' is required.')
                    exit(1)

    # Check that we don't have two configuration mofs defined
    if configmofArgument and parsedArguments.configurationmof:
        print('StartDscConfiguration.py: error: Two configuration mof arguments were found. Please provide only one.')
        exit(1)
    
    if configmofArgument:
        parsedArguments.configurationmof = configmofArgument

    # Read the configuration mof
    try:
        configurationFile = codecs.open(parsedArguments.configurationmof, 'r')
    except:
        configurationFile = codecs.open(parsedArguments.configurationmof, 'r', encoding = 'utf-16')

    try:
        configurationFileContent = configurationFile.read()
    finally:
        if (configurationFile):
            configurationFile.close()

    # Convert the file content to strings of integers representing unicode
    configurationData = []
    for char in configurationFileContent:
        configurationData.append(str(ord(char)))

    # # OMI CLI location
    omicli_path = join(helperlib.CONFIG_BINDIR, 'omicli')
    dsc_host_base_path = helperlib.DSC_HOST_BASE_PATH
    dsc_host_path = join(dsc_host_base_path, 'bin/dsc_host')
    dsc_host_output_path = join(dsc_host_base_path, 'output')
    dsc_host_lock_path = join(dsc_host_base_path, 'dsc_host_lock')
    dsc_host_switch_path = join(dsc_host_base_path, 'dsc_host_ready')

    if ("omsconfig" in helperlib.DSC_SCRIPT_PATH):
        write_omsconfig_host_switch_event(pathToCurrentScript, isfile(dsc_host_switch_path))

    if ("omsconfig" in helperlib.DSC_SCRIPT_PATH) and (isfile(dsc_host_switch_path)):
        use_omsconfig_host = True
    else:
        use_omsconfig_host = False

    # Assemble parameters to pass to OMI CLI
    host_parameters = []
    if use_omsconfig_host:
        host_parameters.append(dsc_host_path)
        host_parameters.append(dsc_host_output_path)
        host_parameters.append("SendConfigurationApply")
        host_parameters.append(args[2])
        # Insert force if specified
        if parsedArguments.force:
            host_parameters.append("force")
    else:
        host_parameters.append(omicli_path)
        host_parameters.append("iv")
        host_parameters.append("<DSC_NAMESPACE>")
        host_parameters.append("{")
        host_parameters.append("MSFT_DSCLocalConfigurationManager")
        host_parameters.append("}")
        host_parameters.append("SendConfigurationApply")
        host_parameters.append("{")
        host_parameters.append("ConfigurationData")
        host_parameters.append("[")

        # Insert configurationmof data here
        for token in configurationData:
            host_parameters.append(token)
        
        host_parameters.append("]")

        # Insert force if specified
        if parsedArguments.force:
            host_parameters.append("force")
            host_parameters.append("true")

        host_parameters.append("}")

    stdout = ''
    stderr = ''

    if use_omsconfig_host:
        try:
            stop_old_host_instances(dsc_host_lock_path)

            # Open the dsc host lock file. This also creates a file if it does not exist
            dschostlock_filehandle = open(dsc_host_lock_path, 'w')
            print("Opened the dsc host lock file at the path '" + dsc_host_lock_path + "'")
            
            dschostlock_acquired = False

            # Acquire dsc host file lock
            for retry in range(10):
                try:
                    flock(dschostlock_filehandle, LOCK_EX | LOCK_NB)
                    dschostlock_acquired = True
                    break
                except IOError:
                    write_omsconfig_host_log('dsc_host lock file not acquired. retry (#' + str(retry) + ') after 60 seconds...', pathToCurrentScript)
                    sleep(60)

            if dschostlock_acquired:
                p = subprocess.Popen(parameters, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                stdout, stderr = p.communicate()
                stdout = stdout.decode() if isinstance(stdout, bytes) else stdout
                print(stdout)
            else:
                print("dsc host lock already acuired by a different process")
        finally:
            if (dschostlock_filehandle):
                # Release dsc host file lock
                flock(dschostlock_filehandle, LOCK_UN)

                # Close dsc host lock file handle
                dschostlock_filehandle.close()
    else:
        p = subprocess.Popen(host_parameters, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()

    stdout = stdout.decode() if isinstance(stdout, bytes) else stdout
    stderr = stderr.decode() if isinstance(stderr, bytes) else stderr
    print(stdout)   
    print(stderr)
Ejemplo n.º 17
0
            "usage": round(collector.usage, 1),
        })
        if sys.version_info[0] >= 3:
            response = response.encode('utf8')
        self.send_response(200)
        self.send_header('Content-Type', 'application/json; charset=utf-8')
        self.send_header('Content-Length', len(response))
        self.end_headers()
        self.wfile.write(response)


if __name__ == "__main__":
    # Options
    try:
        from argparse import ArgumentParser
        parser = ArgumentParser()
        parser.add_argument("-p", "--port", type=int, default=PORT, help="Port Number")
        options = parser.parse_args()
    except:
        from optparse import OptionParser
        parser = OptionParser()
        parser.add_option("-p", "--port", type="int", default=PORT, help="Port Number")
        (options, args) = parser.parse_args()

    try:
        collector = Collector()
        collector.start()
        HTTPServer(('', options.port), AutoScalingUsageHandler).serve_forever()
    except KeyboardInterrupt:
        collector.event.set()