def get_branch_name(self) -> str: # pylint: disable=broad-except reponame = "waiting..." with raise_context(): repo = self.get_repo() with raise_context(): reponame = repo.head.object.hexsha repo_branch = self.target_branch or 'ERROR' if repo.head.is_detached and repo_branch.replace('tags/', '') in repo.tags: return repo_branch reponame = repo.active_branch.name return reponame
def _make_operations(self, operation: Callable) -> Any: ''' Handle VCS operations and sync data from project. :param operation: function that should be hdandled. :return: tuple with repo-object and fetch-results ''' self._set_status("SYNC") try: with transaction.atomic(): result = self._operate(operation) self.proj.status = "OK" self._update_tasks(self._get_playbook_path(result[0])) self._set_project_modules() self._handle_yaml(self._load_yaml() or dict()) self._update_slave_inventories(self.proj.slave_inventory.all()) self.proj.save() except Exception as err: logger.debug(traceback.format_exc()) self.message('Sync error: {}'.format(err), 'error') self._set_status("ERROR") raise else: with raise_context(verbose=True): self.handler_class(self, result).trigger_execution() return result
def prefetch(self, queryset_func_name, view, queryset): with raise_context(): queryset = self.filter_by_func( queryset, f'{queryset_func_name}_related', self.filter_model_fields( view, self.fields_fetch_map[queryset_func_name])) return queryset
def make_update(self, env: ENV_VARS_TYPE) -> Tuple[git.Repo, Any]: try: repo = self._get_or_create_repo(env) except git.InvalidGitRepositoryError: logger.info('Convert project [{}] to GIT.'.format(self.proj.id)) repo = git.Repo.init(self.path) repo.create_remote('origin', self.proj.repository) with repo.git.custom_environment(**env): kwargs = self.options.get("FETCH_KWARGS", dict()) origin = repo.remote('origin') logger.debug('Fetch remote brances for project [{}].'.format(self.proj.id)) origin.fetch(**kwargs) if not list(origin.refs): config_writer = repo.config_writer() config_writer.set_value("user", "email", self.proj.owner.email).release() user_name = self.proj.owner.username if self.proj.owner.last_name and self.proj.owner.first_name: # nocv user_name = '{u.fist_name} {u.last_name}'.format(u=self.proj.owner) config_writer.set_value("user", "name", user_name).release() repo.git.add(A=True) repo.git.commit(m='Create project from Polemarch.') logger.debug('Push project [{}] as master.'.format(self.proj.id)) repo.git.push('--set-upstream', 'origin', 'master') results = repo, self.vcs_update(repo, env) with raise_context(): repo.git.checkout(self.target_branch) return results
def test_executors(self): dir_name = os.path.dirname(__file__) cmd = utils.UnhandledExecutor(stderr=utils.UnhandledExecutor.DEVNULL) self.assertEqual('yes', cmd.execute('echo yes'.split(' '), dir_name)) cmd = utils.Executor(stderr=utils.Executor.DEVNULL) self.assertEqual('yes', cmd.execute('echo yes'.split(' '), dir_name)) cmd = utils.Executor() with utils.raise_context(): cmd.execute('bash -c "python0.0 --version"'.split(' '), dir_name)
def execute(self, cmd: Iterable[Text], cwd: Text): pm_ansible_path = ' '.join(self.pm_ansible()) new_cmd = list() for one_cmd in cmd: if isinstance(one_cmd, six.string_types): with raise_context(): one_cmd = one_cmd.decode('utf-8') new_cmd.append(one_cmd) self.history.raw_args = " ".join(new_cmd).replace(pm_ansible_path, '').lstrip() return super(Executor, self).execute(new_cmd, cwd)
def make_clone(self, env: ENV_VARS_TYPE) -> Tuple[git.Repo, None]: kw = dict(**self.options.get("CLONE_KWARGS", dict())) if self.target_branch: kw['branch'] = self.target_branch.replace('tags/', '') repo = self.vsc_clone(self.proj.repository, self.path, env=env, **kw) with raise_context(): self.proj.variables.update_or_create( key='repo_branch', defaults=dict(value=repo.active_branch.name) ) return repo, None
def __call__(self, value, serializer_field=None): if not self.has_pillow: warnings.warn(self.warning_msg, ImportWarning) return super().__call__(value) should_resize_image = False with raise_context(): should_resize_image = serializer_field.context[ 'request'].headers.get('Auto-Resize-Image', None) == 'true' self.validate(value, should_resize_image)
def _update_submodules(self, repo): for sm in repo.submodules: # Calling git directly for own submodules # since using relative path is not working in gitpython # see https://github.com/gitpython-developers/GitPython/issues/730 with raise_context(): if sm.url[0:3] == '../': sm_path = sm.name repo.git.submodule('init', sm_path) repo.git.submodule('update', sm_path) else: sm.update(init=True)
def _get_or_create_repo(self, env: ENV_VARS_TYPE) -> git.Repo: try: repo = self.get_repo() branch = self.target_branch with raise_context(): repo.git.checkout(branch) is_not_detached = not repo.head.is_detached if branch and is_not_detached and repo.active_branch.name != branch: self.delete() raise git.NoSuchPathError except git.NoSuchPathError: repo = self.make_clone(env)[0] return repo
def _get_playbook_path(self, repo: Any = None) -> Iterable[pathlib.Path]: path_list_additional = [] additional_pb_pattern = None with raise_context(): additional_pb_pattern = self.proj.variables.get( key='playbook_path').value if additional_pb_pattern and (pathlib.Path(self.path) / additional_pb_pattern).exists(): path_list_additional = self.search_files( repo, additional_pb_pattern + '/*.yml') return chain(self.search_files(repo, '*.yml'), path_list_additional)
def _update_submodules(self, repo: git.Repo): logger.debug('Update GIT submodules in project [{}]'.format(self.proj.id)) for sm in repo.submodules: # Calling git directly for own submodules # since using relative path is not working in gitpython # see https://github.com/gitpython-developers/GitPython/issues/730 with raise_context(): logger.debug('Update module "{}" in project [{}].'.format(sm.name, self.proj.id)) if sm.url[0:3] == '../': sm_path = sm.name repo.git.submodule('init', sm_path) repo.git.submodule('update', sm_path) else: sm.update(init=True)
def dir_prepare_git(self, src: Text, work_dir: Text, revision: Text): # pylint: disable=no-member import git repo = git.Repo.clone_from( url=src + "/.git", to_path=work_dir, **self.project.repo_handlers.opts(self.project.type).get('PREP_KWARGS', {}) ) repo.git.checkout(revision or self.project.branch) for sm in repo.submodules: # Calling git directly for own submodules # since using relative path is not working in gitpython # see https://github.com/gitpython-developers/GitPython/issues/730 with raise_context(): if sm.url[0:3] == '../': repo_parent_url, _ = os.path.split(repo.remotes.origin.url) actual_url = os.path.join(repo_parent_url, sm.name) with sm.config_writer() as writer: writer.set('url', actual_url) sm.update(init=True)
def test_raise_context(self): class SomeEx(KeyError): pass @utils.exception_with_traceback() def ex_method(ex=Exception('Valid ex')): raise ex with self.assertRaises(SomeEx) as exc: ex_method(SomeEx()) self.assertTrue(getattr(exc.exception, 'traceback', False)) with utils.raise_context(TypeError): ex_method(SomeEx()) @utils.raise_context(SomeEx, exclude=Exception) def ex_method(ex=Exception('Valid ex')): raise ex ex_method(SomeEx()) with self.assertRaises(Exception): ex_method()
def get_branch_name(self): # pylint: disable=broad-except with raise_context(): return self.get_repo().active_branch.name return "waiting..."
def tearDown(self): super(HooksTestCase, self).tearDown() for script in self.scripts: with raise_context(): os.remove(script)
def _read(self, file): data = file.read() with raise_context(): data = data.decode('utf-8') return data
def execute(self, when, message): for hook in self.when(when): with raise_context(): hook.run(when, message)
def make_update(self, env): repo = self._get_or_create_repo(env) resutls = repo, self.vcs_update(repo, env) with raise_context(): repo.git.checkout(self.target_branch) return resutls
def line_handler(self, proc, line): # pylint: disable=unused-argument if line is not None: with raise_context(): self.write_output(line)
def execute(self, when: Text, message: Any) -> NoReturn: for hook in self.when(when): with raise_context(): hook.run(when, message)
def generate_file(self, name: Text): with open(os.path.join(self.path, name), 'w', encoding='utf-8') as fd: with raise_context(): fd.write(get_render('polemarch/{}'.format(name), settings.MANUAL_PROJECT_VARS))