def hook(hook_api): """ SQLAlchemy 0.9 introduced the decorator 'util.dependencies'. This decorator does imports. eg: @util.dependencies("sqlalchemy.sql.schema") This hook scans for included SQLAlchemy modules and then scans those modules for any util.dependencies and marks those modules as hidden imports. """ if not is_module_satisfies('sqlalchemy >= 0.9'): return # this parser is very simplistic but seems to catch all cases as of V1.1 depend_regex = re.compile(r'@util.dependencies\([\'"](.*?)[\'"]\)') hidden_imports_set = set() known_imports = set() for node in hook_api.module_graph.flatten(start=hook_api.module): if isinstance(node, SourceModule) and \ node.identifier.startswith('sqlalchemy.'): known_imports.add(node.identifier) # Determine the encoding of the source file. with open_file(node.filename, 'rb') as f: encoding = guess_encoding(f) # Use that to open the file. with open_file(node.filename, text_read_mode, encoding=encoding) as f: for match in depend_regex.findall(f.read()): hidden_imports_set.add(match) hidden_imports_set -= known_imports if len(hidden_imports_set): logger.info(" Found %d sqlalchemy hidden imports", len(hidden_imports_set)) hook_api.add_imports(*list(hidden_imports_set))
def hook(hook_api): """ SQLAlchemy 0.9 introduced the decorator 'util.dependencies'. This decorator does imports. E.g.: @util.dependencies("sqlalchemy.sql.schema") This hook scans for included SQLAlchemy modules and then scans those modules for any util.dependencies and marks those modules as hidden imports. """ if not is_module_satisfies('sqlalchemy >= 0.9'): return # this parser is very simplistic but seems to catch all cases as of V1.1 depend_regex = re.compile(r'@util.dependencies\([\'"](.*?)[\'"]\)') hidden_imports_set = set() known_imports = set() for node in hook_api.module_graph.iter_graph(start=hook_api.module): if isinstance(node, SourceModule) and \ node.identifier.startswith('sqlalchemy.'): known_imports.add(node.identifier) # Determine the encoding of the source file. with open(node.filename, 'rb') as f: encoding = guess_encoding(f) # Use that to open the file. with open(node.filename, 'r', encoding=encoding) as f: for match in depend_regex.findall(f.read()): hidden_imports_set.add(match) hidden_imports_set -= known_imports if len(hidden_imports_set): logger.info(" Found %d sqlalchemy hidden imports", len(hidden_imports_set)) hook_api.add_imports(*list(hidden_imports_set))
def test_guess_encoding(self): fp = BytesIO(b"# coding: utf-8") self.assertEqual(util.guess_encoding(fp), "utf-8") fp = BytesIO(b"\n# coding: utf-8") self.assertEqual(util.guess_encoding(fp), "utf-8") fp = BytesIO(b"# coding: latin-1") self.assertEqual(util.guess_encoding(fp), "latin-1") fp = BytesIO(b"\n# coding: latin-1") self.assertEqual(util.guess_encoding(fp), "latin-1") fp = BytesIO( b"#!/usr/bin/env/python\n# vim: set fileencoding=latin-1 :") self.assertEqual(util.guess_encoding(fp), "latin-1") fp = BytesIO(b"\n\n\n# coding: latin-1") if sys.version_info[0] == 2: self.assertEqual(util.guess_encoding(fp), "ascii") else: self.assertEqual(util.guess_encoding(fp), "utf-8") del fp