def testListPackage(self): with files.TemporaryDirectory() as t: self.assertEqual(([], []), pkg_resources.ListPackage(t)) Touch(os.path.join(t, 'foo.py'), '"""Foo module."""') self.assertEqual(([], ['foo']), pkg_resources.ListPackage(t)) Touch(os.path.join(t, '__init__.py'), '"""Package marker."""') self.assertEqual(([], ['foo']), pkg_resources.ListPackage(t)) os.makedirs(os.path.join(t, 'pkg')) self.assertEqual(([], ['foo']), pkg_resources.ListPackage(t)) Touch(os.path.join(t, 'pkg', '__init__.py'), '"""Package marker."""') self.assertEqual((['pkg'], ['foo']), pkg_resources.ListPackage(t)) Touch(os.path.join(t, 'bar.py'), '"""Bar module."""') self.assertEqual((['pkg'], ['bar', 'foo']), pkg_resources.ListPackage(t)) # Check support for additional extensions. self.assertEqual( (['pkg'], ['bar', 'foo']), pkg_resources.ListPackage(t, extra_extensions=['.yaml', '.junk'])) Touch(os.path.join(t, 'baz.yaml'), '') Touch(os.path.join(t, 'baz.junk'), '') self.assertEqual( (['pkg'], ['bar', 'baz.yaml', 'foo']), pkg_resources.ListPackage(t, extra_extensions=['.yaml'])) self.assertEqual( (['pkg'], ['bar', 'baz.junk', 'baz.yaml', 'foo']), pkg_resources.ListPackage(t, extra_extensions=['.yaml', '.junk']))
def FindSubElements(impl_paths, path): """Find all the sub groups and commands under this group. Args: impl_paths: [str], A list of file paths to the command implementation for this group. path: [str], A list of group names that got us down to this command group with respect to the CLI itself. This path should be used for things like error reporting when a specific element in the tree needs to be referenced. Raises: CommandLoadFailure: If the command is invalid and cannot be loaded. LayoutException: if there is a command or group with an illegal name. Returns: ({str: [str]}, {str: [str]), A tuple of groups and commands found where each item is a mapping from name to a list of paths that implement that command or group. There can be multiple paths because a command or group could be implemented in both python and yaml (for different release tracks). """ if len(impl_paths) > 1: raise CommandLoadFailure( '.'.join(path), Exception('Command groups cannot be implemented in yaml')) impl_path = impl_paths[0] groups, commands = pkg_resources.ListPackage(impl_path, extra_extensions=['.yaml']) return (_GenerateElementInfo(impl_path, groups), _GenerateElementInfo(impl_path, commands))
def FindSubElements(module_dir, module_path, release_track): """Find all the sub groups and commands under this group. Args: module_dir: str, The path to the tools directory that this command or group lives within. module_path: [str], The command group names that brought us down to this command group or command from the top module directory. release_track: ReleaseTrack, The release track that we should load. Raises: LayoutException: if there is a command or group with an illegal name. Returns: ([groups_info], [commands_info]), The info needed to construct sub groups and sub commands. """ location = os.path.join(module_dir, *module_path) groups, commands = pkg_resources.ListPackage(location) for collection in [groups, commands]: for name in collection: if re.search('[A-Z]', name): raise LayoutException( 'Commands and groups cannot have capital letters: {0}.'. format(name)) return (_GenerateElementInfo(module_dir, module_path, release_track, groups), _GenerateElementInfo(module_dir, module_path, release_track, commands))
def testListPackageInZip(self): with files.TemporaryDirectory() as t: with files.TemporaryDirectory() as zip_tmp_dir: zip_pkg = MakeZip(t, zip_tmp_dir, 'pkg') self.assertEqual(([], []), pkg_resources.ListPackage(zip_pkg)) Touch(os.path.join(t, 'foo.py'), '"""Foo module."""') with files.TemporaryDirectory() as zip_tmp_dir: zip_pkg = MakeZip(t, zip_tmp_dir, 'pkg') self.assertEqual(([], ['foo']), pkg_resources.ListPackage(zip_pkg)) Touch(os.path.join(t, '__init__.py'), '"""Package marker."""') with files.TemporaryDirectory() as zip_tmp_dir: zip_pkg = MakeZip(t, zip_tmp_dir, 'pkg') self.assertEqual(([], ['foo']), pkg_resources.ListPackage(zip_pkg)) os.makedirs(os.path.join(t, 'pkg')) with files.TemporaryDirectory() as zip_tmp_dir: zip_pkg = MakeZip(t, zip_tmp_dir, 'pkg') self.assertEqual(([], ['foo']), pkg_resources.ListPackage(zip_pkg)) Touch(os.path.join(t, 'pkg', '__init__.py'), '"""Package marker."""') with files.TemporaryDirectory() as zip_tmp_dir: zip_pkg = MakeZip(t, zip_tmp_dir, 'pkg') self.assertEqual((['pkg'], ['foo']), pkg_resources.ListPackage(zip_pkg)) Touch(os.path.join(t, 'bar.py'), '"""Bar module."""') with files.TemporaryDirectory() as zip_tmp_dir: zip_pkg = MakeZip(t, zip_tmp_dir, 'pkg') self.assertEqual((['pkg'], ['bar', 'foo']), pkg_resources.ListPackage(zip_pkg))
def _FindSubElements(self): """Final all the sub groups and commands under this group. Raises: LayoutException: if there is a command or group with an illegal name. """ location = os.path.join(self._module_dir, *self._module_path) groups, commands = pkg_resources.ListPackage(location) for collection in [groups, commands]: for name in collection: if re.search('[A-Z]', name): raise LayoutException('Commands and groups cannot have capital ' 'letters: %s.' % name) for group_info in self._GetSubPathForNames(groups): self.AddSubGroup(group_info) for command_info in self._GetSubPathForNames(commands): self.AddSubCommand(command_info)