Best Python code snippet using autotest_python
__init__.py
Source:__init__.py
1# uncompyle6 version 2.10.12# Python bytecode 2.7 (62211)3# Decompiled from: Python 3.6.8 (default, Apr 20 2019, 23:18:21) 4# [GCC 8.2.0]5# Embedded file name: google3/__init__.py6# Compiled at: 2019-06-18 16:41:387"""This is the root of the google3 tree.8Code in here is built by the Google3 build system.9"""10import os11import sys12import warnings13def _SetupPath(old_path, this_dir):14 """Setup package import path for Google3.15 16 old_path: List of directories.17 this_dir: Directory that this package is loaded from.18 19 Allow the google3 package to import subpackages and modules from two20 different directory trees: the VCS client (Piper, CitC, git5 or something21 else) and the READONLY directory (or symlink to srcfs).22 23 This code sets the top-level path, and relies on24 Google3MetaImportHook to propagate the path to subpackages.25 26 Returns a list of three things:27 28 The first is the new package path, consisting of the old package29 path reordered and with extra directories inserted.30 31 The second is a subset of the first, consisting of all the google332 directories that we might want to take third-party modules from.33 34 The third is a boolean saying whether a READONLY directory was35 found. If true, the 'directory merging' functionality will be36 activated. Note that this will never be true if running from a .par37 file, since 'client_root' will be '/somedir/somefile.par' and thus38 '/somedir/somefile.par/READONLY' will not be a directory.39 40 """41 this_dir = os.path.abspath(this_dir)42 parent_dir = os.path.dirname(this_dir)43 if os.path.basename(parent_dir) == 'READONLY':44 client_root = os.path.dirname(parent_dir)45 readwrite_dir = os.path.join(client_root, 'google3')46 readonly_dir = os.path.join(client_root, 'READONLY', 'google3')47 have_readonly_dir = 148 elif parent_dir.endswith('/READONLY/stateless-client') and parent_dir.startswith('/usr/local/google/'):49 client_root = parent_dir[len('/usr/local/google'):-len('/READONLY/stateless-client')]50 readwrite_dir = os.path.join(client_root, 'google3')51 readonly_dir = os.path.join(parent_dir, 'google3')52 have_readonly_dir = 153 else:54 client_root = parent_dir55 readwrite_dir = os.path.join(client_root, 'google3')56 readonly_dir = os.path.join(client_root, 'READONLY', 'google3')57 have_readonly_dir = os.path.isdir(readonly_dir)58 google3_path = [59 readwrite_dir]60 if have_readonly_dir:61 google3_path.append(readonly_dir)62 package_path = google3_path[:]63 for pathdir in old_path:64 if pathdir not in package_path:65 package_path.append(pathdir)66 return (package_path, google3_path, have_readonly_dir)67def _FixupParentPathByName(module_name):68 """Given a module name, find its parent package, and fix that package's path.69 70 module_name: Fully-qualified module name71 72 """73 lastdot = module_name.rfind('.')74 if lastdot == -1:75 return76 second_lastdot = module_name.rfind('.', 0, lastdot)77 if second_lastdot == -1:78 return79 parent_name = module_name[:lastdot]80 parent = sys.modules.get(parent_name)81 grandparent_name = module_name[:second_lastdot]82 grandparent = sys.modules.get(grandparent_name)83 if parent and grandparent:84 _MaybeInheritPath(parent_name, parent, grandparent)85def _MaybeInheritPath(package_name, package, package_parent):86 """Given a package, fixup its path if necessary.87 88 package_name: Fully-qualified module name89 package, package_parent_name: Module objects90 """91 if getattr(package, '_g_inherit_processed__', 0):92 return93 if not getattr(package, '_g_inherit_path__', 1):94 return95 if not getattr(package_parent, '_g_inherit_path__', 0):96 return97 _InheritPath(package_name, package, package_parent)98def _InheritPath(package_name, package, package_parent):99 """Compute a path for a package, based on the path of its parent.100 101 If package is named spam.eggs, then for each entry D in102 package_parent's path, add D/eggs to package's path.103 104 package_name: Fully qualified package name105 package, package_parent: Module objects106 107 """108 basename = package_name.split('.')[-1]109 assert basename, 'Contact build-help@google.com'110 orig_package_path = getattr(package, '__path__', [])111 new_path = []112 for pathdir in getattr(package_parent, '__path__', []):113 newdir = os.path.join(pathdir, basename)114 if newdir in orig_package_path or os.path.isdir(newdir):115 new_path.append(newdir)116 for pathdir in orig_package_path:117 if pathdir not in new_path:118 new_path.append(pathdir)119 package.__path__[:] = new_path120 package._g_inherit_path__ = 1121 package._g_inherit_processed__ = 1122class _Python23MergeImportsHook:123 """Propagate package search path to all subpackages of Google3.124 125 This class is a meta-import hook, as defined by Python 2.3 and126 above. Instead of actually importing anything, it works like a127 pre-import hook to fix up the __path__ in a package object that has128 already been imported.129 130 Consider packages A, A.B, and A.B.C. A's __path__ contains a list131 of directories. We want A.B's __path__ to be set to the same list132 of directories, except with '/B' appended to each one. We could133 update A.B's __path__ when A.B is first imported, but that is134 difficult to implement. Instead, we allow A.B's path to be135 incorrect until A.B.C is imported. When A.B.C is imported, this136 hook runs, looks at A's __path__, and copies it with modifications137 to A.B's __path__. The updated __path__ is then used by the normal138 import mechanism to find A.B.C.139 140 """141 def find_module(self, module_name, unused_path=None):142 """Called by standard Python import mechanism.143 144 module_name: Fully-qualified module name145 unused: List of directories to search, from parent package.146 147 We use this as a signal that a module is about to be imported, and148 fixup its parent's path if necessary.149 150 We then return a failure notification (via 'return None'), so that151 the normal import process continues.152 153 """154 _FixupParentPathByName(module_name)155 return None156_merge_imports_hook_installed = 0157def _SetupMergeImportsHook(have_readonly_dir):158 """Enable hook to merge directory trees for imports.159 160 have_readonly_dir: 1 if [p4 client]/READONLY exists161 """162 global _merge_imports_hook_installed163 if _merge_imports_hook_installed:164 return165 if not have_readonly_dir or os.environ.get('GOOGLE3_DISABLE_MERGE_IMPORTS'):166 return167 _merge_imports_hook_installed = 1168 meta_path = getattr(sys, 'meta_path', [])169 meta_path.append(_Python23MergeImportsHook())170def _SetupThirdParty(sys_path, google3_path):171 """Setup import path to code in google3/third_party/py.172 173 sys_path: Original sys.path174 google3_path: Google3 dirs being added to sys.path175 176 Returns nothing, modifies sys_path in place.177 """178 third_party_path = [ os.path.join(d, 'third_party', 'py') for d in google3_path179 ]180 found_site_packages = idx = 0181 for idx in range(len(sys_path)):182 dirname = sys_path[idx]183 if dirname.find('site-packages') != -1:184 found_site_packages = 1185 break186 if found_site_packages:187 sys_path[idx:idx] = third_party_path188 else:189 sys_path.extend(third_party_path)190 path_hooks = getattr(sys, 'path_hooks', [])191 _CheckThirdParty(third_party_path, path_hooks, sys.modules)192 return None193def _CheckThirdParty(third_party_path, path_hooks, sys_modules):194 """Check for erroneous imports from site-packages directory.195 196 third_party_path: List of path entries. Each is an absolute197 directory name, but may be a pseudo-path formed by198 concatenating a .par filename with a subdir.199 E.g. '/home/zog/src1/google3/third_party/py' or200 '/root/myprog.par/google3/third_party/py'.201 202 For each top-level module or package that was imported from Python's203 site-package directory, but should have been imported from204 [client]/google3/third_party/py instead, issue a warning message.205 206 We try to determine this with a minimum of I/O, and without fully207 reimplementing import(). So we use heuristics: We only look at top208 level modules or packages (no dots), and we assume that every file209 or directory in google3/third_party/py is a module or package name.210 Since we control google3/third_party/py, this is generally safe.211 212 Returns a list of problematic modules213 """214 path_data = _ExaminePath(third_party_path, path_hooks)215 problems = []216 for module_name, module in sys_modules.items():217 if module_name.find('.') == -1:218 fn = getattr(module, '__file__', None)219 if fn and fn.find('site-packages') != -1:220 third_party_fn = _FindInPath(module_name, path_data)221 if third_party_fn:222 msg = '%s is deprecated, use %s instead. To fix this, move "import google3" or "from google3... import ..." before "import %s" in your main source file.' % (223 fn, third_party_fn,224 module_name)225 warnings.warn(msg, DeprecationWarning, stacklevel=2)226 problems.append((fn, third_party_fn, module_name))227 return problems228def _ExaminePath(dirs, path_hooks):229 """Determine the type and contents of a list of import path entries.230 231 dirs: List of path entries as above.232 path_hooks: Contents of sys.path_hooks233 234 We categorize each directory as 1) real directory or 2) zipfile.235 There is no usable Python-level API to access the import internals,236 so we have to reimplement sys.path_hooks237 processing. [imp.find_module() doesn't work because it wasn't238 updated when new-style import hooks were added to Python 2.3]239 240 Returns a list of (path, [dir contents if real dir], loader if zipfile)241 """242 path_data = []243 for dirname in dirs:244 files = []245 try:246 files = []247 for f in os.listdir(dirname):248 base, ext = os.path.splitext(f)249 if not ext or ext.startswith('.py'):250 files.append(base)251 except EnvironmentError:252 pass253 loader = None254 for path_hook in path_hooks:255 try:256 loader = path_hook(dirname)257 break258 except ImportError:259 pass260 path_data.append([dirname, files, loader])261 return path_data262def _FindInPath(module_name, path_data):263 """Heuristic search for a module in a set of directories.264 265 module_name: top-level module name. E.g. 'MySQLdb'266 path_data: List of (path, [dir contents if real dir], loader if zipfile)267 268 Returns the filename to the module or package dir, or None if not found.269 """270 assert '.' not in module_name, 'Contact build-help@google.com'271 for path, files, loader in path_data:272 if module_name in files:273 package_fn = os.path.join(path, module_name)274 init_fn = os.path.join(package_fn, '__init__.py')275 if os.path.exists(init_fn):276 return package_fn277 if loader and loader.find_module(module_name):278 return os.path.join(path, module_name)279 return None280def _SetupSwig():281 """Setup environment for Blaze built extension modules."""282 if sys.platform in ('win32', 'darwin'):283 return284 else:285 launcher_info = sys.modules.get('_launcher_info')286 if launcher_info is not None and launcher_info.is_google3_python_launcher:287 return288 native_code_deps_dso = os.environ.get('GOOGLE3_NATIVE_CODE_DEPS_DSO')289 native_code_deps_needed = os.environ.get('GOOGLE3_NATIVE_CODE_DEPS_NEEDED')290 ld_preload = os.environ.get('LD_PRELOAD')291 if native_code_deps_dso and ld_preload:292 parts = ld_preload.split()293 other_parts = [ part for part in parts if part != native_code_deps_dso ]294 if parts and not other_parts:295 del os.environ['LD_PRELOAD']296 return297 if len(other_parts) != len(parts):298 os.environ['LD_PRELOAD'] = ' '.join(other_parts)299 return300 if native_code_deps_dso:301 try:302 import ctypes303 except ImportError:304 if native_code_deps_needed:305 raise306 return307 try:308 ctypes.CDLL(native_code_deps_dso, ctypes.RTLD_GLOBAL)309 except OSError:310 if native_code_deps_needed:311 raise312 if native_code_deps_needed:313 del os.environ['GOOGLE3_NATIVE_CODE_DEPS_NEEDED']314 return315def _SetupHookModule():316 """Import an early startup hook if one has been specified in the environment.317 318 The GOOGLE3_PY_HOOK_MODULE environment variable, if set and not empty, must319 be a string of the form "google3.path.to.my.module". This module must be in320 the runtime dependencies of the py_binary or py_test or an ImportError will321 result. It will be imported upon the first google3 import - normally before322 most code runs - well before main(), flag parsing, or InitGoogle.323 324 Users may use it to implement a non-standard debugger to make the py_binary325 or py_test load their debugger by setting GOOGLE3_PY_HOOK_MODULE in their326 environment.327 """328 hook_module = os.environ.get('GOOGLE3_PY_HOOK_MODULE', '')329 if hook_module:330 try:331 import importlib332 importlib.import_module(hook_module)333 except ImportError:334 pass335if sys.version_info[:2] not in ((2, 6), (2, 7)):336 _msg = 'Python %d.%d is unsupported; use 2.7' % sys.version_info[:2]337 warnings.warn(_msg, DeprecationWarning, stacklevel=1)338basedir = os.path.dirname(os.path.abspath(__file__))339TYPE_CHECKING = False340_g_inherit_path__ = 1341_g_inherit_processed__ = 1342__path__ = globals().get('__path__', [])343__path__, _google3_path, _have_readonly_dir = _SetupPath(__path__, basedir)344_SetupMergeImportsHook(_have_readonly_dir)345_SetupThirdParty(sys.path, _google3_path)346_SetupSwig()347_SetupHookModule()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!