def _get_win_id(self): """Get the window ID based on the characteristic string.""" result = common_util.simple_system_output(self.get_id_cmd) for line in result.splitlines(): if self.geometry_str in line: return line.split()[0].strip() return None
def xinput_helper(cmd): """A helper of xinput.sh to execute a command. This is a method copied from factory/py/test/utils.py """ dummy_script = '. /opt/google/input/xinput.sh\n%s' with tempfile.NamedTemporaryFile(prefix='cros_touch_xinput.') as f: f.write(dummy_script % cmd) f.flush() return common_util.simple_system_output('sh %s' % f.name)
def _get_robot_script_dir(self): """Get the directory of the robot control scripts.""" for lib_path in [conf.robot_lib_path, conf.robot_lib_path_local]: cmd = ('find %s -maxdepth 1 -type d -name %s' % (lib_path, conf.python_package)) path = common_util.simple_system_output(cmd) if path: robot_script_dir = os.path.join(path, conf.gestures_sub_path) if os.path.isdir(robot_script_dir): return robot_script_dir return ''
def _run_git_cmd(cmd): """Run git command. When a server test is invoked, the present working directory may look like "/tmp/test_that.xxxx/..." To be able to run git commands against the autotest repo, it is required to pushd to autotest project temporarily. """ new_cmd = ('pushd %s > /dev/null; %s; popd > /dev/null' % (common.client_dir, cmd)) return simple_system_output(new_cmd)
def get_screen_size(): """Get the screen size using xwininfo.""" cmd = 'DISPLAY=:0 xwininfo -root' wininfo = common_util.simple_system_output(cmd) # the geometry string looks like: # " -geometry 3840x1200+0+0" geometry_pattern = re.compile('\s*-geometry\s*(\d+)x(\d+)+.*', re.I) for line in wininfo.splitlines(): result = geometry_pattern.search(line) if result: width = int(result.group(1)) height = int(result.group(2)) return (width, height) return None
def _get_device_description(self, device_description_file): """Get the device description either from the specified device description file or from the system device node. """ if device_description_file: # Get the device description from the device description file. try: with open(device_description_file) as dd: return dd.read() except Exception as e: msg = 'Error: %s in opening the device description file: %s' print msg % (e, device_description_file) elif self.device_node: # Get the device description from the device node. cmd = 'evemu-describe %s' % self.device_node try: return common_util.simple_system_output(cmd) except Exception as e: msg = 'Error: %s in getting the device description from %s' print msg % (e, self.device_node) return None
def get_cpu(): """Get the processor of the machine.""" return common_util.simple_system_output('uname -m')
def _dimensions(self, device_spec): device_script = os.path.join(self._robot_script_dir, 'device_size.py') cmd = 'python %s %s' % (device_script, device_spec) results = common_util.simple_system_output(cmd) dimensions = results.split() return float(dimensions[0]), float(dimensions[1])
def _setup_gtk_environment(self): """Set up the gtk environment correctly.""" def _warning(msg=None): print 'Warning: fail to setup gtk environment.' if msg: print '\t' + msg print '\tImage files would not be shown properly.' print '\tIt does not affect the test results though.' def _make_symlink(path, symlink): """Remove the symlink if exists. Create a new symlink to point to the given path. """ if os.path.islink(symlink): os.remove(symlink) os.symlink(real_gtk_dir, self.gtk_symlink) self.new_symlink = True self.gtk_symlink = None self.tmp = tempfile.mkdtemp() self.moved_flag = False self.original_gtk_realpath = None self.new_symlink = False # Get LoaderDir: # The output of gdk-pixbuf-query-loaders looks like: # # GdkPixbuf Image Loader Modules file # Automatically generated file, do not edit # Created by gdk-pixbuf-query-loaders from gtk+-2.20.1 # # LoaderDir = /usr/lib64/gtk-2.0/2.10.0/loaders loader_dir_str = common_util.simple_system_output( 'gdk-pixbuf-query-loaders | grep LoaderDir') result = re.search('(/.*?)/(gtk-.*?)/', loader_dir_str) if result: prefix = result.group(1) self.gtk_version = result.group(2) else: _warning('Cannot derive gtk version from LoaderDir.') return # Verify the existence of the loaders file. gdk_pixbuf_loaders = ('/usr/local/etc/%s/gdk-pixbuf.loaders' % self.gtk_version) if not os.path.isfile(gdk_pixbuf_loaders): msg = 'The loaders file "%s" does not exist.' % gdk_pixbuf_loaders _warning(msg) return # Setup the environment variable for GdkPixbuf Image Loader Modules file # so that gtk library could find it. os.environ['GDK_PIXBUF_MODULE_FILE'] = gdk_pixbuf_loaders # In the loaders file, it specifies the paths of various # sharable objects (.so) which are used to load images of corresponding # image formats. For example, for png loader, the path looks like # # "/usr/lib64/gtk-2.0/2.10.0/loaders/libpixbufloader-png.so" # "png" 5 "gtk20" "The PNG image format" "LGPL" # "image/png" "" # "png" "" # "\211PNG\r\n\032\n" "" 100 # # However, the real path for the .so file is under # "/usr/local/lib64/..." # Hence, we would like to make a temporary symlink so that # gtk library could find the .so file correctly. self.gtk_symlink = os.path.join(prefix, self.gtk_version) prefix_list = prefix.split('/') prefix_list.insert(prefix_list.index('usr') + 1, 'local') real_gtk_dir = os.path.join('/', *(prefix_list + [self.gtk_version])) # Make sure that the directory of .so files does exist. if not os.path.isdir(real_gtk_dir): msg = 'The directory of gtk image loaders "%s" does not exist.' _warning(msg % real_gtk_dir) return # Take care of an existing symlink. if os.path.islink(self.gtk_symlink): # If the symlink does not point to the correct path, # save the real path of the symlink and re-create the symlink. if not os.path.samefile(self.gtk_symlink, real_gtk_dir): self.original_gtk_realpath = os.path.realpath(self.gtk_symlink) _make_symlink(real_gtk_dir, self.gtk_symlink) # Take care of an existing directory. elif os.path.isdir(self.gtk_symlink): # Move the directory only if it is not what we expect. if not os.path.samefile(self.gtk_symlink, real_gtk_dir): shutil.move(self.gtk_symlink, self.tmp) self.moved_flag = True _make_symlink(real_gtk_dir, self.gtk_symlink) # Take care of an existing file. # Such a file is not supposed to exist here. Move it anyway. elif os.path.isfile(self.gtk_symlink): shutil.move(self.gtk_symlink, self.tmp) self.moved_flag = True _make_symlink(real_gtk_dir, self.gtk_symlink) # Just create the temporary symlink since there is nothing here. else: _make_symlink(real_gtk_dir, self.gtk_symlink)