def exit(self, status=0, message=None): if not message is None: Logger.error(message) if status == 0: raise ArgParserExitNoError() else: raise ArgParserError()
def _exec_with_lock(self, exec_method, *method_args): lock_file = _os.path.join(self._repo_buddy_dir, 'lock') if not _os.path.isdir(self._repo_buddy_dir): # Create the .repobuddy directory if it does not exist already try: _os.mkdir(self._repo_buddy_dir) except OSError as err: raise CommandHandlerError('Error: ' + str(err)) else: Logger.debug('Found an existing .repobuddy directory...') try: # Acquire the lock before doing anything else with FileLock(lock_file): Logger.debug('Lock \'' + lock_file + '\' acquired') exec_method(*method_args) except FileLockError as err: # If it is a timeout error, it could be one of the following: # *** another instance of repobuddy is running # *** repobuddy was killed earlier without releasing the lock file if err.isTimeOut: raise CommandHandlerError( 'Error: Lock file ' + lock_file + ' already exists\n' + 'Is another instance of repobuddy running ?') else: raise CommandHandlerError(str(err)) except GitWrapperError as err: raise CommandHandlerError('Error: Git said => ' + str(err)) return
def exec_command(cls, command, base_dir, debug_output=True): Logger.msg('>> ' + ' '.join(command)) try: if debug_output: proc = _subprocess.Popen(command, cwd=base_dir) else: proc = _subprocess.Popen(command, cwd=base_dir, stdout=open(_os.devnull, 'w'), stderr=_subprocess.STDOUT) proc.communicate() return_code = proc.wait() if return_code != 0: raise ShellError('Command \'%s\' failed!' % command) except (OSError, IOError) as err: raise ShellError(str(err)) return
def _exec_git(self, command, capture_std_out=False, capture_std_err=False, is_clone=False): if is_clone: git_command = _shlex.split('git ' + command) else: git_command = _shlex.split( 'git --work-tree=. --git-dir=.git ' + command) Logger.debug('Exec: git %s' % command) try: kwargs = {} if capture_std_out: kwargs['stdout'] = _subprocess.PIPE if capture_std_err: kwargs['stderr'] = _subprocess.PIPE proc = _subprocess.Popen( git_command, cwd=self._base_dir, **kwargs) (out_msg, err_msg) = proc.communicate() return_code = proc.wait() if return_code != 0: if capture_std_err: raise GitWrapperError( 'Command \'git %s\' failed' % command, is_git_error=True, git_error_msg=err_msg.rstrip()) else: raise GitWrapperError( 'Command \'git %s\' failed' % command, is_git_error=True) if capture_std_out and capture_std_err: return (out_msg.rstrip(), err_msg.rstrip()) elif capture_std_out: return out_msg.rstrip() elif capture_std_err: return err_msg.rstrip() except OSError as err: raise GitWrapperError(str(err), is_git_error=False) return
def run_repobuddy(): # Initialize the Command Handler core command_handler = CommandHandler() handlers = command_handler.get_handlers() # Parse the command line arguments and invoke the handler arg_parser = ArgParser(handlers) try: arg_parser.parse(_sys.argv[1:]) except (CommandHandlerError, ArgParserError) as err: err_msg = str(err) if not err_msg is "None": Logger.error(err_msg) _sys.exit(1) except ArgParserExitNoError: pass _sys.exit(0)
def run_repobuddy(): # Initialize the Command Handler core command_handler = CommandHandler() handlers = command_handler.get_handlers() # Parse the command line arguments and invoke the handler arg_parser = ArgParser(handlers) try: arg_parser.parse(_sys.argv[1:]) except (CommandHandlerError, ArgParserError) as err: err_msg = str(err) if not err_msg is 'None': Logger.error(err_msg) _sys.exit(1) except ArgParserExitNoError: pass _sys.exit(0)
def _exec_git(self, command, capture_std_out=False, capture_std_err=False, is_clone=False): if is_clone: git_command = _shlex.split('git ' + command) else: git_command = _shlex.split('git --work-tree=. --git-dir=.git ' + command) Logger.debug('Exec: git %s' % command) try: kwargs = {} if capture_std_out: kwargs['stdout'] = _subprocess.PIPE if capture_std_err: kwargs['stderr'] = _subprocess.PIPE proc = _subprocess.Popen(git_command, cwd=self._base_dir, **kwargs) (out_msg, err_msg) = proc.communicate() return_code = proc.wait() if return_code != 0: if capture_std_err: raise GitWrapperError('Command \'git %s\' failed' % command, is_git_error=True, git_error_msg=err_msg.rstrip()) else: raise GitWrapperError('Command \'git %s\' failed' % command, is_git_error=True) if capture_std_out and capture_std_err: return (out_msg.rstrip(), err_msg.rstrip()) elif capture_std_out: return out_msg.rstrip() elif capture_std_err: return err_msg.rstrip() except OSError as err: raise GitWrapperError(str(err), is_git_error=False) return
def exec_command(cls, command, base_dir, debug_output=True): Logger.msg('>> ' + ' '.join(command)) try: if debug_output: proc = _subprocess.Popen( command, cwd=base_dir) else: proc = _subprocess.Popen( command, cwd=base_dir, stdout=open(_os.devnull, 'w'), stderr=_subprocess.STDOUT) proc.communicate() return_code = proc.wait() if return_code != 0: raise ShellError('Command \'%s\' failed!' % command) except (OSError, IOError) as err: raise ShellError(str(err)) return
def run_tests(): test_dir = _os.path.join(_os.getcwd(), 'testing-ground') git_wrapper_tests = GitWrapperTestSuite(test_dir).get_test_suite() test_output = _cStringIO.StringIO() runner = _unittest.TextTestRunner( stream=test_output, verbosity=2) runner.run(git_wrapper_tests) Logger.msg('\n') Logger.msg('#' * 80) Logger.msg(test_output.getvalue()) Logger.msg('#' * 80)
def _display_help_status(self): Logger.msg(self._status_command_parser.format_help()) return
def _display_help_init(self): Logger.msg(self._init_command_parser.format_help()) return
def _print_message(self, message, file_handle=None): if message: if file_handle is None: Logger.error('Writing to stderr - file is None') Logger.msg(message) return
def _exec_status(self): if not self._is_client_initialized(): raise CommandHandlerError( 'Error: Uninitialized client, ' + 'please run init to initialize the client first') # Parse the manifest XML self._parse_manifest_xml() # Get the client spec name from client info client = self._get_client_spec(self._get_client_info()) # Process each repo in the Client Spec for repo in client.repo_list: git = GitWrapper(_os.path.join(self._current_dir, repo.destination)) Logger.msg('####################################################') Logger.msg('Repo: ' + repo.destination) Logger.msg('Remote URL: ' + repo.url) git.update_index() current_branch = git.get_current_branch() dirty = False if current_branch is None: current_branch = 'Detached HEAD' if current_branch != repo.branch: Logger.msg('Original Branch: ' + repo.branch) Logger.msg('Current Branch: ' + current_branch + '\n') else: Logger.msg('Branch: ' + repo.branch + '\n') untracked_files = git.get_untracked_files() if len(untracked_files) != 0: Logger.msg('Untracked Files: \n' + '\n'.join(untracked_files) + '\n') dirty = True unstaged_files = git.get_unstaged_files() if len(unstaged_files) != 0: Logger.msg('Unstaged Files: \n' + '\n'.join(unstaged_files) + '\n') dirty = True uncommitted_staged_files = git.get_uncommitted_staged_files() if len(uncommitted_staged_files) != 0: Logger.msg('Uncommitted Changes: \n' + '\n'.join(uncommitted_staged_files) + '\n') dirty = True if not dirty: Logger.msg('No uncommitted changes') Logger.msg('####################################################') return
def _exec_status(self): if not self._is_client_initialized(): raise CommandHandlerError( 'Error: Uninitialized client, ' + 'please run init to initialize the client first') # Parse the manifest XML self._parse_manifest_xml() # Get the client spec name from client info client = self._get_client_spec(self._get_client_info()) # Process each repo in the Client Spec for repo in client.repo_list: git = GitWrapper( _os.path.join(self._current_dir, repo.destination)) Logger.msg('####################################################') Logger.msg('Repo: ' + repo.destination) Logger.msg('Remote URL: ' + repo.url) git.update_index() current_branch = git.get_current_branch() dirty = False if current_branch is None: current_branch = 'Detached HEAD' if current_branch != repo.branch: Logger.msg('Original Branch: ' + repo.branch) Logger.msg('Current Branch: ' + current_branch + '\n') else: Logger.msg('Branch: ' + repo.branch + '\n') untracked_files = git.get_untracked_files() if len(untracked_files) != 0: Logger.msg( 'Untracked Files: \n' + '\n'.join(untracked_files) + '\n') dirty = True unstaged_files = git.get_unstaged_files() if len(unstaged_files) != 0: Logger.msg( 'Unstaged Files: \n' + '\n'.join(unstaged_files) + '\n') dirty = True uncommitted_staged_files = git.get_uncommitted_staged_files() if len(uncommitted_staged_files) != 0: Logger.msg('Uncommitted Changes: \n' + '\n'.join(uncommitted_staged_files) + '\n') dirty = True if not dirty: Logger.msg('No uncommitted changes') Logger.msg('####################################################') return