def test_detect_worktree_failing_git(self): with self.executable_git() as git: with open(git, "w") as fp: fp.write("#!/bin/sh\n") fp.write("exit 1") self.assertIsNone(Git.detect_worktree()) self.assertIsNone(Git.detect_worktree(git))
def test_detect_worktree_failing_git(self): with self.executable_git() as git: with open(git, 'w') as fp: fp.write('#!/bin/sh\n') fp.write('exit 1') self.assertIsNone(Git.detect_worktree()) self.assertIsNone(Git.detect_worktree(git))
def test_detect_worktree_working_git(self): expected_worktree_dir = '/a/fake/worktree/dir' with self.executable_git() as git: with open(git, 'w') as fp: fp.write('#!/bin/sh\n') fp.write('echo ' + expected_worktree_dir) self.assertEqual(expected_worktree_dir, Git.detect_worktree()) self.assertEqual(expected_worktree_dir, Git.detect_worktree(binary=git))
def test_detect_worktree_somewhere_else(self): with temporary_dir() as somewhere_else: with pushd(somewhere_else): loc = Git.detect_worktree(dir=somewhere_else) self.assertEquals(None, loc) subprocess.check_call(['git', 'init']) loc = Git.detect_worktree(dir=somewhere_else) self.assertEquals(os.path.realpath(somewhere_else), loc)
def test_detect_worktree_working_git(self): expected_worktree_dir = "/a/fake/worktree/dir" with self.executable_git() as git: with open(git, "w") as fp: fp.write("#!/bin/sh\n") fp.write("echo " + expected_worktree_dir) self.assertEqual(expected_worktree_dir, Git.detect_worktree()) self.assertEqual(expected_worktree_dir, Git.detect_worktree(binary=git))
def worktree_relative_to(some_dir, expected): # Given a directory relative to the worktree, tests that the worktree is detected as 'expected'. subdir = os.path.join(clone, some_dir) if not os.path.isdir(subdir): os.mkdir(subdir) actual = Git.detect_worktree(subdir=subdir) self.assertEqual(expected, actual)
def project_template(self): target_levels = {Revision.lenient(platform['target_level']) for platform in self.blob['jvm_platforms']['platforms'].values()} lang_level = max(target_levels) if target_levels else Revision(1, 8) configured_project = TemplateData( root_dir=get_buildroot(), outdir=self.output_directory, git_root=Git.detect_worktree(), modules=self.module_templates_by_filename.values(), java=TemplateData( encoding=self.java_encoding, maximum_heap_size=self.java_maximum_heap_size, jdk='{0}.{1}'.format(*lang_level.components[:2]), language_level='JDK_{0}_{1}'.format(*lang_level.components[:2]), ), resource_extensions=[], scala=None, checkstyle_classpath=';'.join([]), debug_port=self.debug_port, annotation_processing=self.annotation_processing_template, extra_components=[], junit_tests=self._junit_tests_config(), global_junit_vm_parameters=' '.join(self.global_junit_jvm_options), ) return configured_project
def __call__(self, manifest_entries=None): """Returns a dict suitable for passing to 'manifest_entries' in a 'jvm_binary() definition""" manifest_entries = manifest_entries or {} buildroot = get_buildroot() worktree = Git.detect_worktree( subdir=os.path.join(buildroot, self._parse_context.rel_path)) if worktree: git = Git(worktree=worktree) manifest_entries['Implementation-Version'] = git.commit_id manifest_entries['Built-By'] = pwd.getpwuid(os.getuid()).pw_name return manifest_entries
def __call__(self, manifest_entries=None): """Returns a dict suitable for passing to 'manifest_entries' in a 'jvm_binary() definition""" manifest_entries = manifest_entries or {} buildroot = get_buildroot() worktree = Git.detect_worktree(subdir=os.path.join(buildroot, self._parse_context.rel_path)) if worktree: git = Git(worktree=worktree) manifest_entries['Implementation-Version'] = git.commit_id manifest_entries['Built-By'] = pwd.getpwuid(os.getuid()).pw_name return manifest_entries
def worktree_relative_to(cwd, expected): """Given a cwd relative to the worktree, tests that the worktree is detected as 'expected'.""" orig_cwd = os.getcwd() try: abs_cwd = os.path.join(clone, cwd) if not os.path.isdir(abs_cwd): os.mkdir(abs_cwd) os.chdir(abs_cwd) actual = Git.detect_worktree() self.assertEqual(expected, actual) finally: os.chdir(orig_cwd)
def worktree_relative_to(cwd, expected): # Given a cwd relative to the worktree, tests that the worktree is detected as 'expected'. orig_cwd = os.getcwd() try: abs_cwd = os.path.join(clone, cwd) if not os.path.isdir(abs_cwd): os.mkdir(abs_cwd) os.chdir(abs_cwd) actual = Git.detect_worktree() self.assertEqual(expected, actual) finally: os.chdir(orig_cwd)
def get_scm(): """Returns the pants Scm if any.""" # TODO(John Sirois): Extract a module/class to carry the bootstrap logic. global _SCM if not _SCM: from pants.scm.git import Git # We know about git, so attempt an auto-configure worktree = Git.detect_worktree() if worktree and os.path.isdir(worktree): git = Git(worktree=worktree) try: logger.info('Detected git repository at {} on branch {}'.format(worktree, git.branch_name)) set_scm(git) except git.LocalException as e: logger.info('Failed to load git repository at {}: {}'.format(worktree, e)) return _SCM
def get_scm(): """Returns the pants Scm if any.""" # TODO(John Sirois): Extract a module/class to carry the bootstrap logic. global _SCM if not _SCM: from pants.scm.git import Git # We know about git, so attempt an auto-configure worktree = Git.detect_worktree() if worktree and os.path.isdir(worktree): git = Git(worktree=worktree) try: log.info('Detected git repository at %s on branch %s' % (worktree, git.branch_name)) set_scm(git) except git.LocalException as e: log.info('Failed to load git repository at %s: %s' % (worktree, e)) return _SCM
def get_scm() -> Optional[Scm]: """Returns the pants Scm if any. :API: public """ # TODO(John Sirois): Extract a module/class to carry the bootstrap logic. global _SCM if _SCM: return _SCM from pants.scm.git import Git # We know about git, so attempt an auto-configure worktree = Git.detect_worktree() if worktree and os.path.isdir(worktree): git = Git(worktree=worktree) try: logger.debug(f'Detected git repository at {worktree} on branch {git.branch_name}') set_scm(git) except git.LocalException as e: logger.info(f'Failed to load git repository at {worktree}: {e!r}') return _SCM
def test_detect_worktree_invalid_executable_git(self): with self.executable_git() as git: self.assertIsNone(Git.detect_worktree()) self.assertIsNone(Git.detect_worktree(binary=git))
def generate_project(self, project): def create_content_root(source_set): root_relative_path = os.path.join(source_set.source_base, source_set.path) \ if source_set.path else source_set.source_base if self.get_options().infer_test_from_siblings: is_test = IdeaGen._sibling_is_test(source_set) else: is_test = source_set.is_test if source_set.resources_only: if source_set.is_test: content_type = 'java-test-resource' else: content_type = 'java-resource' else: content_type = '' sources = TemplateData( path=root_relative_path, package_prefix=source_set.path.replace('/', '.') if source_set.path else None, is_test=is_test, content_type=content_type ) return TemplateData( path=root_relative_path, sources=[sources], exclude_paths=[os.path.join(source_set.source_base, x) for x in source_set.excludes], ) content_roots = [create_content_root(source_set) for source_set in project.sources] if project.has_python: content_roots.extend(create_content_root(source_set) for source_set in project.py_sources) scala = None if project.has_scala: scala = TemplateData( language_level=self.scala_language_level, maximum_heap_size=self.scala_maximum_heap_size, fsc=self.fsc, compiler_classpath=project.scala_compiler_classpath ) exclude_folders = [] if self.get_options().exclude_maven_target: exclude_folders += IdeaGen._maven_targets_excludes(get_buildroot()) exclude_folders += self.get_options().exclude_folders java_language_level = None for target in project.targets: if isinstance(target, JvmTarget): if java_language_level is None or java_language_level < target.platform.source_level: java_language_level = target.platform.source_level if java_language_level is not None: java_language_level = 'JDK_{0}_{1}'.format(*java_language_level.components[:2]) configured_module = TemplateData( root_dir=get_buildroot(), path=self.module_filename, content_roots=content_roots, bash=self.bash, python=project.has_python, scala=scala, internal_jars=[cp_entry.jar for cp_entry in project.internal_jars], internal_source_jars=[cp_entry.source_jar for cp_entry in project.internal_jars if cp_entry.source_jar], external_jars=[cp_entry.jar for cp_entry in project.external_jars], external_javadoc_jars=[cp_entry.javadoc_jar for cp_entry in project.external_jars if cp_entry.javadoc_jar], external_source_jars=[cp_entry.source_jar for cp_entry in project.external_jars if cp_entry.source_jar], annotation_processing=self.annotation_processing_template, extra_components=[], exclude_folders=exclude_folders, java_language_level=java_language_level, ) outdir = os.path.abspath(self.intellij_output_dir) if not os.path.exists(outdir): os.makedirs(outdir) configured_project = TemplateData( root_dir=get_buildroot(), outdir=outdir, git_root=Git.detect_worktree(), modules=[configured_module], java=TemplateData( encoding=self.java_encoding, maximum_heap_size=self.java_maximum_heap_size, jdk=self.java_jdk, language_level='JDK_1_{}'.format(self.java_language_level) ), resource_extensions=list(project.resource_extensions), scala=scala, checkstyle_classpath=';'.join(project.checkstyle_classpath), debug_port=project.debug_port, annotation_processing=self.annotation_processing_template, extra_components=[], ) existing_project_components = None existing_module_components = None if not self.nomerge: # Grab the existing components, which may include customized ones. existing_project_components = self._parse_xml_component_elements(self.project_filename) existing_module_components = self._parse_xml_component_elements(self.module_filename) # Generate (without merging in any extra components). safe_mkdir(os.path.abspath(self.intellij_output_dir)) ipr = self._generate_to_tempfile( Generator(pkgutil.get_data(__name__, self.project_template), project=configured_project)) iml = self._generate_to_tempfile( Generator(pkgutil.get_data(__name__, self.module_template), module=configured_module)) if not self.nomerge: # Get the names of the components we generated, and then delete the # generated files. Clunky, but performance is not an issue, and this # is an easy way to get those component names from the templates. extra_project_components = self._get_components_to_merge(existing_project_components, ipr) extra_module_components = self._get_components_to_merge(existing_module_components, iml) os.remove(ipr) os.remove(iml) # Generate again, with the extra components. ipr = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.project_template), project=configured_project.extend(extra_components=extra_project_components))) iml = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.module_template), module=configured_module.extend(extra_components=extra_module_components))) self.context.log.info('Generated IntelliJ project in {directory}' .format(directory=self.gen_project_workdir)) shutil.move(ipr, self.project_filename) shutil.move(iml, self.module_filename) return self.project_filename if self.open else None
def generate_project(self, project): def create_content_root(source_set): root_relative_path = os.path.join(source_set.source_base, source_set.path) \ if source_set.path else source_set.source_base if self.get_options().infer_test_from_siblings: is_test = IdeaGen._sibling_is_test(source_set) else: is_test = source_set.is_test sources = TemplateData( path=root_relative_path, package_prefix=source_set.path.replace('/', '.') if source_set.path else None, is_test=is_test ) return TemplateData( path=root_relative_path, sources=[sources], exclude_paths=[os.path.join(source_set.source_base, x) for x in source_set.excludes], ) content_roots = [create_content_root(source_set) for source_set in project.sources] if project.has_python: content_roots.extend(create_content_root(source_set) for source_set in project.py_sources) scala = None if project.has_scala: scala = TemplateData( language_level=self.scala_language_level, maximum_heap_size=self.scala_maximum_heap_size, fsc=self.fsc, compiler_classpath=project.scala_compiler_classpath ) exclude_folders = [] if self.get_options().exclude_maven_target: exclude_folders += IdeaGen._maven_targets_excludes(get_buildroot()) exclude_folders += self.get_options().exclude_folders configured_module = TemplateData( root_dir=get_buildroot(), path=self.module_filename, content_roots=content_roots, bash=self.bash, python=project.has_python, scala=scala, internal_jars=[cp_entry.jar for cp_entry in project.internal_jars], internal_source_jars=[cp_entry.source_jar for cp_entry in project.internal_jars if cp_entry.source_jar], external_jars=[cp_entry.jar for cp_entry in project.external_jars], external_javadoc_jars=[cp_entry.javadoc_jar for cp_entry in project.external_jars if cp_entry.javadoc_jar], external_source_jars=[cp_entry.source_jar for cp_entry in project.external_jars if cp_entry.source_jar], extra_components=[], exclude_folders=exclude_folders, ) outdir = os.path.abspath(self.intellij_output_dir) if not os.path.exists(outdir): os.makedirs(outdir) configured_project = TemplateData( root_dir=get_buildroot(), outdir=outdir, git_root=Git.detect_worktree(), modules=[ configured_module ], java=TemplateData( encoding=self.java_encoding, maximum_heap_size=self.java_maximum_heap_size, jdk=self.java_jdk, language_level = 'JDK_1_%d' % self.java_language_level ), resource_extensions=list(project.resource_extensions), scala=scala, checkstyle_classpath=';'.join(project.checkstyle_classpath), debug_port=project.debug_port, extra_components=[], ) existing_project_components = None existing_module_components = None if not self.nomerge: # Grab the existing components, which may include customized ones. existing_project_components = self._parse_xml_component_elements(self.project_filename) existing_module_components = self._parse_xml_component_elements(self.module_filename) # Generate (without merging in any extra components). safe_mkdir(os.path.abspath(self.intellij_output_dir)) ipr = self._generate_to_tempfile( Generator(pkgutil.get_data(__name__, self.project_template), project = configured_project)) iml = self._generate_to_tempfile( Generator(pkgutil.get_data(__name__, self.module_template), module = configured_module)) if not self.nomerge: # Get the names of the components we generated, and then delete the # generated files. Clunky, but performance is not an issue, and this # is an easy way to get those component names from the templates. extra_project_components = self._get_components_to_merge(existing_project_components, ipr) extra_module_components = self._get_components_to_merge(existing_module_components, iml) os.remove(ipr) os.remove(iml) # Generate again, with the extra components. ipr = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.project_template), project = configured_project.extend(extra_components = extra_project_components))) iml = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.module_template), module = configured_module.extend(extra_components = extra_module_components))) self.context.log.info('Generated IntelliJ project in {directory}' .format(directory=self.gen_project_workdir)) shutil.move(ipr, self.project_filename) shutil.move(iml, self.module_filename) return self.project_filename if self.open else None
def test_detect_worktree_no_git(self): with self.empty_path(): self.assertIsNone(Git.detect_worktree())