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())
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())
def execute(self, pool): # Test if git local repo exists if not os.path.isdir(os.path.join(os.getcwdu(), DevToolsConfigFile.Path)): prompt.error(DevToolsMessage.GitRepoNotExist.format('')) return error = False current_path = os.getcwdu() try: log.info(u'Running Dev Tools initialization script.') if misc.is_os_windows(): path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.WindowsClimbUpDepth) #TODO: replace current workaround for WindowsModuleScript current_path = os.getcwdu() script_path = os.path.join(path, OSSpecific.WindowsModuleScriptPath) log.debug(u'Changing path to {0}.'.format(script_path)) os.chdir(script_path) log.info(u'Running script "{0}".'.format(OSSpecific.WindowsModuleScriptName)) shell_utils.call([OSSpecific.WindowsModuleScriptName]) log.debug(u'Changing path to {0}.'.format(current_path)) os.chdir(current_path) log.info(u'Running script "{0}".'.format(OSSpecific.WindowsRepoScript)) fullpath = os.path.join(path, OSSpecific.WindowsRepoScript) prompt.error(shell_utils.call([fullpath])) else: path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.LinuxClimbUpDepth) log.info(u'Running script "{0}" at {1}.'.format(OSSpecific.LinuxRepoScript, path)) fullpath = os.path.join(path, OSSpecific.LinuxRepoScript) prompt.error(shell_utils.call([fullpath])) location = DevToolsConfigFile.Path + os.path.sep + DevToolsConfigFile.Name config_file.set_access_permission(location, True) except subprocess.CalledProcessError as ex: # Git returned with an error code log.error(u'Dev Tools initialization script report an error, because "{0}".'.format(ex)) error = True prompt.error(DevToolsMessage.InitError.format(ex.message)) except (OSError, IOError) as ex: log.error(u'Failed to call Dev Tools initialization script, because "{0}".'.format(ex)) # Cannot find or run script error = True if ex.errno == FileErrorConstant.FileNotFoundErrorCode: if fullpath: prompt.error(DevToolsMessage.FileMissingError.format(fullpath)) else: prompt.error(ex) finally: if error: prompt.error(DevToolsMessage.ExecutionError.format(DevToolsConfigFile.InitHelpUrl)) ret_result = OperationResult(self, None, None, None) return ret_result
def run_dev_tools_script(self): log.info('Running Dev Tools initialization script.') current_path = os.getcwd() try: if misc.is_os_windows(): path = shell_utils.climb_dir_tree( shell_utils.ori_path(), OSSpecific.WindowsClimbUpDepth) #TODO: replace current workaround for WindowsModuleScript current_path = os.getcwd() script_path = os.path.join(path, OSSpecific.WindowsModuleScriptPath) log.debug('Changing path to {0}.'.format(script_path)) os.chdir(script_path) log.info('Running script "{0}".'.format( OSSpecific.WindowsModuleScriptName)) shell_utils.call([OSSpecific.WindowsModuleScriptName]) log.debug('Changing path to {0}.'.format(current_path)) os.chdir(current_path) log.info('Running script "{0}".'.format( OSSpecific.WindowsRepoScript)) fullpath = os.path.join(path, OSSpecific.WindowsRepoScript) shell_utils.call([fullpath]) else: path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.LinuxClimbUpDepth) log.info('Running script "{0}" at {1}.'.format( OSSpecific.LinuxRepoScript, path)) fullpath = os.path.join(path, OSSpecific.LinuxRepoScript) shell_utils.call([fullpath]) except subprocess.CalledProcessError as ex: # Git returned with an error code log.error( 'Dev Tools initialiation script report an error, because "{0}".' .format(ex)) prompt.error(DevToolsMessage.InitError) raise except (OSError, IOError) as ex: log.error( 'Failed to call Dev Tools initialiation script, because "{0}".' .format(ex)) # Cannot find or run script if ex.errno == FileErrorConstant.FileNotFoundErrorCode: prompt.error(DevToolsMessage.FileMissingError.format(fullpath)) raise
def set_access_permission(location, quiet = False): log.info(u'Setting file access permission at "{0}".'.format(location)) if misc.is_os_windows(): log.debug(u'Skipped setting file access permission for Windows platform.') return False try: os.chmod(location, stat.S_IRUSR | stat.S_IWUSR) return True except BaseException as ex: log.error(u'Encountered error when setting access permission for file "{0}", because "{1}".'.\ format(location, ex)) if quiet: return False else: raise
def check_access_permission(location, quiet = False): log.info(u'Checking file access permission at "{0}".'.format(location)) if misc.is_os_windows(): log.debug(u'Skipped checking file access permission for Windows platform.') return None try: file_mode = os.stat(location).st_mode if 0 != stat.S_IMODE(file_mode) & stat.S_IRWXG or\ 0 != stat.S_IMODE(file_mode) & stat.S_IRWXO : return False return True except BaseException as ex: log.error(u'Encountered error when checking access permission for file "{0}", because "{1}".'.\ format(location, ex)) if quiet: return None else: raise
def run_dev_tools_script(self): log.info('Running Dev Tools initialization script.') current_path = os.getcwd() try: if misc.is_os_windows(): path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.WindowsClimbUpDepth) #TODO: replace current workaround for WindowsModuleScript current_path = os.getcwd() script_path = os.path.join(path, OSSpecific.WindowsModuleScriptPath) log.debug('Changing path to {0}.'.format(script_path)) os.chdir(script_path) log.info('Running script "{0}".'.format(OSSpecific.WindowsModuleScriptName)) shell_utils.call([OSSpecific.WindowsModuleScriptName]) log.debug('Changing path to {0}.'.format(current_path)) os.chdir(current_path) log.info('Running script "{0}".'.format(OSSpecific.WindowsRepoScript)) fullpath = os.path.join(path, OSSpecific.WindowsRepoScript) shell_utils.call([fullpath]) else: path = shell_utils.climb_dir_tree(shell_utils.ori_path(), OSSpecific.LinuxClimbUpDepth) log.info('Running script "{0}" at {1}.'.format(OSSpecific.LinuxRepoScript, path)) fullpath = os.path.join(path, OSSpecific.LinuxRepoScript) shell_utils.call([fullpath]) except subprocess.CalledProcessError as ex: # Git returned with an error code log.error('Dev Tools initialiation script report an error, because "{0}".'.format(ex)) prompt.error(DevToolsMessage.InitError) raise except (OSError, IOError) as ex: log.error('Failed to call Dev Tools initialiation script, because "{0}".'.format(ex)) # Cannot find or run script if ex.errno == FileErrorConstant.FileNotFoundErrorCode: prompt.error(DevToolsMessage.FileMissingError.format(fullpath)) raise
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)
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)