Example #1
0
def add_ignore_file(location):
    '''
    Add EB config files and log files to git ignore file
    '''
    log.info('Adding ignore files to "{0}".'.format(location))
    # Compile ignore file dict and regular expressions
    namelist = dict()
    relist = dict()
    for item in GitIgnoreFile.Files:
        namelist[item.Name] = True
        relist[item.Name] = re.compile(item.NameRe, re.UNICODE)
    log.debug('Files needs to present in git ignore list: {0}'.\
              format(misc.collection_to_string(list(namelist.keys()))))

    with open(location, 'a+') as f:
        # Search for filenames
        f.seek(0, os.SEEK_SET)
        lines = f.readlines()
        for line in lines:
            for name, regex in list(relist.items()):
                if regex.match(line):
                    namelist[name] = False
        
        # Add filenames if not present in ignore file
        f.seek(0, os.SEEK_END)
        if len(lines) > 0 and not os.linesep in lines[-1]: # Add EOL if last line doesn't have it
            f.write(os.linesep)
        for name, add in list(namelist.items()):
            if add:
                log.debug('Adding file "{0}" to git ignore list.'.format(name))
                f.write('{0}{1}'.format(name, os.linesep))
def call(command, quiet = True):
    '''
    Call external process. command is a list of command line arguments including name
    of external process and arguments.
    '''
    if isinstance(command, str):
        command_line = shlex.split(command)
    elif isinstance(command, list):
        command_line = command
    else:
        raise EBSCliException('Parameter must be instance of list or string.')
    
    log.debug('Running external commands "{0}".'.\
              format(misc.collection_to_string(command_line)))
    # Using OS native code page 
    command_line = [x.encode(locale.getpreferredencoding()) for x in command_line]
    args = {'args':command_line}
    if misc.is_os_windows():
        # TODO: set shell to True will allow Windows translate "git" to "git.cmd", 
        # but might introduce other issues.
        args['shell'] = True
    if quiet:
        args['stderr'] = subprocess.STDOUT        
        
    return misc.to_unicode(subprocess.check_output(**args), False, locale.getpreferredencoding())
Example #3
0
def save_env_option_setting_file(location, option_settings):
    log.info(u'Try loading option settings file from {0}'.format(location))
#    old_option_settings = load_env_option_setting_file(location, quiet = True)
    
    log.info(u'Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.iteritems()):
                if not _is_whitelisted_option(namespace, option):
#                    and (namespace not in old_option_settings \
#                         or option not in old_option_settings[namespace]):
                    continue
                
                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                # Add option setting to new file
                if value is None:
                    value = u''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error(u'Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
Example #4
0
def parse(parameter_pool, line=None):
    """ Parse command arguments"""
    parser = ArgumentParser(description=EBSCliAttr.Name, usage=EBSCliAttr.Usage)
    _init_parser(parser)

    if line is not None:
        args = vars(parser.parse_args(line.split()))
    else:
        args = vars(parser.parse_args())

    # Post prcessing
    if args[ParameterName.SolutionStack] is not None:
        solution_stack = _word_join(args[ParameterName.SolutionStack], u" ")
        args[ParameterName.SolutionStack] = solution_stack

    if args[ParameterName.Region] is not None:
        region_id = args[ParameterName.Region]
        region = ServiceRegionId.keys()[ServiceRegionId.values().index(region_id)]
        args[ParameterName.Region] = region

    # Store command line arguments into parameter pool
    for arg, value in args.iteritems():
        if value is not None:
            arg = misc.to_unicode(arg)
            value = misc.to_unicode(value)
            if arg == CLISwitch[ParameterName.Command]:
                parameter_pool.put(Parameter(ParameterName.Command, value, ParameterSource.CliArgument))
            else:
                parameter_pool.put(Parameter(arg, value, ParameterSource.CliArgument))

    log.info(u"Finished parsing command line arguments")
    if log.isEnabledFor(logging.DEBUG):
        log.debug(u"Received arguments: {0}".format(misc.collection_to_string(parameter_pool.parameter_names)))

    return args
def save_env_option_setting_file(location, option_settings):
    log.info(u'Try loading option settings file from {0}'.format(location))
#    old_option_settings = load_env_option_setting_file(location, quiet = True)
    
    log.info(u'Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.iteritems()):
                if not _is_whitelisted_option(namespace, option):
#                    and (namespace not in old_option_settings \
#                         or option not in old_option_settings[namespace]):
                    continue
                
                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                # Add option setting to new file
                if value is None:
                    value = u''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error(u'Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
Example #6
0
def add_ignore_file(location):
    '''
    Add EB config files and log files to git ignore file
    '''
    log.info(u'Adding ignore files to "{0}".'.format(location))
    # Compile ignore file dict and regular expressions
    namelist = dict()
    relist = dict()
    for item in GitIgnoreFile.Files:
        namelist[item.Name] = True
        relist[item.Name] = re.compile(item.NameRe, re.UNICODE)
    log.debug(u'Files needs to present in git ignore list: {0}'.\
              format(misc.collection_to_string(namelist.keys())))

    with open(location, 'a+') as f:
        # Search for filenames
        f.seek(0, os.SEEK_SET)
        for line in f:
            for name, regex in relist.items():
                if regex.match(line):
                    namelist[name] = False
        
        # Add filenames if not present in ignore file
        f.seek(0, os.SEEK_END)
        for name, add in namelist.items():
            if add:
                log.debug(u'Adding file "{0}" to git ignore list.'.format(name))
                f.write(u'{0}'.format(name))
Example #7
0
def load_env_option_setting_file(location, option_settings = None, quiet = False):
    log.info(u'Reading environment option settings from file at "{0}".'.format(location))
    
    if option_settings is None:
        option_settings = []
    
    try:
        parser = SectionedConfigParser()
        parser.read(location)
        
        for section in parser.sections():
            for option, value in parser.items(section):
                cos = ConfigurationOptionSetting()
                cos._namespace = misc.to_unicode(section)
                cos._option_name = misc.to_unicode(option)
                cos._value = misc.to_unicode(value)
                option_settings.append(cos) 
        
        log.debug(u'Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
        
        check_access_permission(location, True)
        return option_settings
    
    except BaseException as ex:
        log.error(u'Failed to load environment option setting file, because: "{0}"'.format(ex))
        if quiet:
            return []
        else:
            prompt.error(OptionSettingFileErrorMessage.ReadError.format(location))        
            raise
Example #8
0
def add_ignore_file(location):
    '''
    Add EB config files and log files to git ignore file
    '''
    log.info('Adding ignore files to "{0}".'.format(location))
    # Compile ignore file dict and regular expressions
    namelist = dict()
    relist = dict()
    for item in GitIgnoreFile.Files:
        namelist[item.Name] = True
        relist[item.Name] = re.compile(item.NameRe, re.UNICODE)
    log.debug('Files needs to present in git ignore list: {0}'.\
              format(misc.collection_to_string(list(namelist.keys()))))

    with open(location, 'a+') as f:
        # Search for filenames
        f.seek(0, os.SEEK_SET)
        lines = f.readlines()
        for line in lines:
            for name, regex in list(relist.items()):
                if regex.match(line):
                    namelist[name] = False

        # Add filenames if not present in ignore file
        f.seek(0, os.SEEK_END)
        if len(lines) > 0 and not os.linesep in lines[
                -1]:  # Add EOL if last line doesn't have it
            f.write(os.linesep)
        for name, add in list(namelist.items()):
            if add:
                log.debug('Adding file "{0}" to git ignore list.'.format(name))
                f.write('{0}{1}'.format(name, os.linesep))
Example #9
0
def call(command, quiet=True):
    '''
    Call external process. command is a list of command line arguments including name
    of external process and arguments.
    '''
    if isinstance(command, basestring):
        command_line = shlex.split(command)
    elif isinstance(command, list):
        command_line = command
    else:
        raise EBSCliException(u'Parameter must be instance of list or string.')

    log.debug(u'Running external commands "{0}".'.\
              format(misc.collection_to_string(command_line)))
    # Using OS native code page
    command_line = map(lambda x: x.encode(locale.getpreferredencoding()),
                       command_line)
    args = {'args': command_line}
    if misc.is_os_windows():
        # TODO: set shell to True will allow Windows translate "git" to "git.cmd",
        # but might introduce other issues.
        args['shell'] = True
    if quiet:
        args['stderr'] = subprocess.STDOUT

    return misc.to_unicode(subprocess.check_output(**args), False,
                           locale.getpreferredencoding())
Example #10
0
def load_env_option_setting_file(location, option_settings = None, quiet = False):
    log.info('Reading environment option settings from file at "{0}".'.format(location))
    
    if option_settings is None:
        option_settings = dict()
    
    try:
        parser = SectionedConfigParser()
        parser.read(location)
        
        for section in parser.sections():
            for option, value in parser.items(section):
                section = misc.to_unicode(section)
                option = misc.to_unicode(option)
                value = misc.to_unicode(value) 
                if section not in option_settings:
                    option_settings[section] = dict()
                option_settings[section][option] = value 
        
        log.debug('Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
        
        check_access_permission(location, True)
        return option_settings
    
    except BaseException as ex:
        log.error('Failed to load environment option setting file, because: "{0}"'.format(ex))
        if quiet:
            return option_settings
        else:
            prompt.error(OptionSettingFileErrorMessage.ReadError.format(location))        
            raise
Example #11
0
def parse(parameter_pool, line=None):
    ''' Parse command arguments'''
    parser = ArgumentParser(description=EBSCliAttr.Name,
                            usage=EBSCliAttr.Usage)
    _init_parser(parser)

    if line is not None:
        args = vars(parser.parse_args(line.split()))
    else:
        args = vars(parser.parse_args())

    # Post prcessing
    if args[ParameterName.EnvironmentTier] is not None:
        tier_serialized = args[ParameterName.EnvironmentTier]
        args[ParameterName.
             EnvironmentTier] = EnvironmentTier.from_serialized_string(
                 tier_serialized)

    if args[ParameterName.SolutionStack] is not None:
        solution_stack = _word_join(args[ParameterName.SolutionStack], u' ')
        args[ParameterName.SolutionStack] = solution_stack

    if args[ParameterName.Region] is not None:
        region_id = args[ParameterName.Region]
        region = ServiceRegionId.keys()[ServiceRegionId.values().index(
            region_id)]
        args[ParameterName.Region] = region

    # Store command line arguments into parameter pool
    for arg, value in args.iteritems():
        arg = misc.to_unicode(arg, convert_none=False)

        # Try to convert string/list-of-string parameters to unicode
        if arg not in NON_STRING_PARAMETERS:
            if isinstance(value, list):
                value = [misc.to_unicode(item) for item in value]
            else:
                value = misc.to_unicode(value, convert_none=False)

        if arg == CLISwitch[ParameterName.Command]:
            parameter_pool.put(
                Parameter(ParameterName.Command, value,
                          ParameterSource.CliArgument))
        elif arg == CLISwitch[ParameterName.SubCommand]:
            parameter_pool.put(
                Parameter(ParameterName.SubCommand, value,
                          ParameterSource.CliArgument))
        elif value is not None:
            parameter_pool.put(
                Parameter(arg, value, ParameterSource.CliArgument))

    log.info(u'Finished parsing command line arguments')
    if log.isEnabledFor(logging.DEBUG):
        log.debug(u'Received arguments: {0}'.\
                  format(misc.collection_to_string(parameter_pool.parameter_names)))

    return args
def parse(parameter_pool, line = None):
    ''' Parse command arguments'''
    parser = ArgumentParser(description = EBSCliAttr.Name, 
                            usage = EBSCliAttr.Usage)
    _init_parser(parser)

    if line is not None:
        args = vars(parser.parse_args(line.split()))
    else:
        args = vars(parser.parse_args())
   
    # Post prcessing
    if args[ParameterName.EnvironmentTier] is not None:
        tier_serialized = args[ParameterName.EnvironmentTier]
        args[ParameterName.EnvironmentTier] = EnvironmentTier.from_serialized_string(tier_serialized)
    
    if args[ParameterName.SolutionStack] is not None:
        solution_stack = _word_join(args[ParameterName.SolutionStack], ' ')
        args[ParameterName.SolutionStack] = solution_stack

    if args[ParameterName.Region] is not None:
        region_id = args[ParameterName.Region]
        region = list(ServiceRegionId.keys())[list(ServiceRegionId.values()).index(region_id)]
        args[ParameterName.Region] = region


    # Store command line arguments into parameter pool     
    for arg, value in args.items():
        arg = misc.to_unicode(arg, convert_none=False)
        
        # Try to convert string/list-of-string parameters to unicode
        if arg not in NON_STRING_PARAMETERS:
            if isinstance(value, list):
                value = [misc.to_unicode(item) for item in value]
            else:
                value = misc.to_unicode(value, convert_none=False)
        
        if arg == CLISwitch[ParameterName.Command]:
            parameter_pool.put(Parameter(ParameterName.Command, 
                                         value, 
                                         ParameterSource.CliArgument))
        elif arg == CLISwitch[ParameterName.SubCommand]:
            parameter_pool.put(Parameter(ParameterName.SubCommand, 
                                         value, 
                                         ParameterSource.CliArgument))
        elif value is not None:
            parameter_pool.put(Parameter(arg, 
                                         value, 
                                         ParameterSource.CliArgument))
    
    log.info('Finished parsing command line arguments')
    if log.isEnabledFor(logging.DEBUG): 
        log.debug('Received arguments: {0}'.\
                  format(misc.collection_to_string(parameter_pool.parameter_names)))
 
    return args
Example #13
0
def save_env_option_setting_file(location, option_settings):
    log.info(u'Writing environment option settings to file at "{0}".'.format(
        location))
    try:
        parser = SectionedConfigParser()

        for namespace, options in sorted(option_settings.iteritems()):
            if len(options) < 1:
                continue

            for option, value in sorted(options.iteritems()):

                if namespace.startswith(OptionSettingContainerPrefix):
                    pass
                elif namespace.startswith(
                        OptionSettingApplicationEnvironment.Namespace):
                    if option in OptionSettingApplicationEnvironment.IgnoreOptionNames:
                        continue
                    else:
                        pass
                # Skip if option setting is on in local option setting list
                elif namespace not in LocalOptionSettings \
                    or option not in LocalOptionSettings[namespace]:
                    continue

                if not parser.has_section(namespace):
                    parser.add_section(namespace)

                if value is None:
                    value = u''
                parser.set(namespace, option, value)

        parser.write(location)
        log.debug(u'Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))

        set_access_permission(location, True)
    except BaseException as ex:
        log.error(
            u'Failed to save environment option setting file, because: "{0}"'.
            format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))
        raise
Example #14
0
def parse(parameter_pool, line = None):
    ''' Parse command arguments'''
    parser = ArgumentParser(description = EBSCliAttr.Name, 
                            usage = EBSCliAttr.Usage)
    _init_parser(parser)

    if line is not None:
        args = vars(parser.parse_args(line.split()))
    else:
        args = vars(parser.parse_args())
   
    # Post prcessing
    if args[ParameterName.SolutionStack] is not None:
        solution_stack = _word_join(args[ParameterName.SolutionStack], ' ')
        args[ParameterName.SolutionStack] = solution_stack

    if args[ParameterName.Region] is not None:
        region_id = args[ParameterName.Region]
        region = list(ServiceRegionId.keys())[list(ServiceRegionId.values()).index(region_id)]
        args[ParameterName.Region] = region


    # Store command line arguments into parameter pool     
    for arg, value in args.items():
        if value is not None:
            arg = misc.to_unicode(arg)
            value = misc.to_unicode(value)
            if arg == CLISwitch[ParameterName.Command]:
                parameter_pool.put(Parameter(ParameterName.Command, 
                                             value, 
                                             ParameterSource.CliArgument))
            else:
                parameter_pool.put(Parameter(arg, 
                                             value, 
                                             ParameterSource.CliArgument))
    
    log.info('Finished parsing command line arguments')
    if log.isEnabledFor(_logging.DEBUG): 
        log.debug('Received arguments: {0}'.\
                  format(misc.collection_to_string(parameter_pool.parameter_names)))
 
    return args
Example #15
0
 def _call(self, command, *params):
     '''
     Call external process. command is a list of command line arguments including name
     of external process. params will be appended at the tail of command.
     '''
     if not isinstance(command, list):
         raise EBSCliException('Parameter must be instance of list.')
     command_line = command
     for param in params:
         command_line.append(param)
     
     log.debug('Running external commands "{0}".'.\
               format(misc.collection_to_string(command_line)))    
     if misc.is_os_windows():
         # TODO: set shell to True will allow Windows translate "git" to "git.cmd", 
         # but might introduce other issues.
         # Using Windows native code page 
         command_line = [x.encode(locale.getpreferredencoding()) for x in command_line]        
         return _subprocess.check_output(command_line, shell=True)
     else:
         return _subprocess.check_output(command_line)
Example #16
0
def save_env_option_setting_file(location, option_settings):
    log.info('Writing environment option settings to file at "{0}".'.format(location))
    try:
        parser = SectionedConfigParser()
        
        for namespace, options in sorted(option_settings.items()):
            if len(options) < 1:
                continue
            
            for option, value in sorted(options.items()):

                if namespace.startswith(OptionSettingContainerPrefix):
                    pass
                elif namespace.startswith(OptionSettingApplicationEnvironment.Namespace):
                    if option in OptionSettingApplicationEnvironment.IgnoreOptionNames:
                        continue
                    else:
                        pass
                # Skip if option setting is on in local option setting list
                elif namespace not in LocalOptionSettings \
                    or option not in LocalOptionSettings[namespace]:
                    continue

                if not parser.has_section(namespace):
                    parser.add_section(namespace)
                
                if value is None:
                    value = ''
                parser.set(namespace, option, value)
        
        parser.write(location)
        log.debug('Option settings written to file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
       
        set_access_permission(location, True)
    except BaseException as ex:
        log.error('Failed to save environment option setting file, because: "{0}"'.format(ex))
        prompt.error(OptionSettingFileErrorMessage.WriteError.format(location))        
        raise    
        
Example #17
0
    def _call(self, command, *params):
        '''
        Call external process. command is a list of command line arguments including name
        of external process. params will be appended at the tail of command.
        '''
        if not isinstance(command, list):
            raise EBSCliException('Parameter must be instance of list.')
        command_line = command
        for param in params:
            command_line.append(param)

        log.debug('Running external commands "{0}".'.\
                  format(misc.collection_to_string(command_line)))
        if misc.is_os_windows():
            # TODO: set shell to True will allow Windows translate "git" to "git.cmd",
            # but might introduce other issues.
            # Using Windows native code page
            command_line = [
                x.encode(locale.getpreferredencoding()) for x in command_line
            ]
            return _subprocess.check_output(command_line, shell=True)
        else:
            return _subprocess.check_output(command_line)
Example #18
0
def load_env_option_setting_file(location, option_settings=None, quiet=False):
    log.info('Reading environment option settings from file at "{0}".'.format(
        location))

    if option_settings is None:
        option_settings = dict()

    try:
        parser = SectionedConfigParser()
        parser.read(location)

        for section in parser.sections():
            for option, value in parser.items(section):
                section = misc.to_unicode(section)
                option = misc.to_unicode(option)
                value = misc.to_unicode(value)
                if section not in option_settings:
                    option_settings[section] = dict()
                option_settings[section][option] = value

        log.debug('Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))

        check_access_permission(location, True)
        return option_settings

    except BaseException as ex:
        log.error(
            'Failed to load environment option setting file, because: "{0}"'.
            format(ex))
        if quiet:
            return option_settings
        else:
            prompt.error(
                OptionSettingFileErrorMessage.ReadError.format(location))
            raise
Example #19
0
 def _log_api_result(self, operation_name, api_name, result):
     if log.isEnabledFor(_logging.DEBUG):
         log.debug(u'{0} response: {1}'.\
                   format(operation_name, misc.collection_to_string(result)))
Example #20
0
def log_response(api_name, result):
    if log.isEnabledFor(logging.DEBUG):
        log.debug('{0} response: {1}'.\
                  format(api_name, misc.collection_to_string(result)))
Example #21
0
 def _log_api_result(self, operation_name, api_name, result):
     if log.isEnabledFor(_logging.DEBUG):
         log.debug('{0} response: {1}'.\
                   format(operation_name, misc.collection_to_string(result)))
Example #22
0
 def __repr__(self):
     return "API Response.\n Request ID: {0}\n Results: {1}".format(
         self.request_id, misc.collection_to_string(self._result)
     )
Example #23
0
 def __repr__(self):
     return u'API Response.\n Request ID: {0}\n Results: {1}'.\
         format(self.request_id, misc.collection_to_string(self._result))