Best Python code snippet using avocado_python
py_pip_deb_checks.py
Source:py_pip_deb_checks.py
1# Software License Agreement (BSD License)2#3# Copyright (c) 2012, Willow Garage, Inc.4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9#10# * Redistributions of source code must retain the above copyright11# notice, this list of conditions and the following disclaimer.12# * Redistributions in binary form must reproduce the above13# copyright notice, this list of conditions and the following14# disclaimer in the documentation and/or other materials provided15# with the distribution.16# * Neither the name of Willow Garage, Inc. nor the names of its17# contributors may be used to endorse or promote products derived18# from this software without specific prior written permission.19#20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS23# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE24# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,25# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,26# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;27# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER28# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN30# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE31# POSSIBILITY OF SUCH DAMAGE.32"""33Checks to see if core Python scripts have:341) Been installed352) Have been installed via Debians on Ubuntu363) Have not been installed via pip on Ubuntu37"""38from __future__ import print_function39import subprocess40import importlib41import os42#A dictionary of core ROS python packages and their corresponding .deb packages43py_to_deb_core_packages = {44 'catkin_pkg': 'python-catkin-pkg',45 'rospkg': 'python-rospkg',46 'rosinstall': 'python-rosinstall',47 'rosdep2': 'python-rosdep',48}49#A dictionary of release ROS python packages and their corresponding .deb packages50py_to_deb_release_packages = {51 'bloom': 'python-bloom',52 'rosrelease': 'python-rosrelease',53}54def get_host_os():55 """Determines the name of the host operating system"""56 import rospkg.os_detect57 os_detector = rospkg.os_detect.OsDetect()58 return (os_detector.detect_os())[0]59def is_host_os_ubuntu():60 """Indicates if the host operating system is Ubuntu"""61 return (get_host_os() == 'ubuntu')62def is_debian_package_installed(deb_pkg):63 """Uses dpkg to determine if a package has been installed"""64 return (subprocess.call(65 'dpkg -l ' + deb_pkg,66 shell=True,67 stdout=subprocess.PIPE,68 stderr=subprocess.PIPE) == 0)69def is_a_pip_path_on_ubuntu(path):70 """Indicates if a path (either directory or file) is in the same place71 pip installs Python code"""72 return ('/usr/local' in path)73def is_python_package_installed(python_pkg):74 """Indicates if a Python package is importable in the current75 environment."""76 try:77 importlib.import_module(python_pkg)78 return True79 except ImportError:80 return False81def is_python_package_installed_via_pip_on_ubuntu(python_pkg):82 """Indicates if am importable package has been installed through pip on83 Ubuntu"""84 try:85 pkg_handle = importlib.import_module(python_pkg)86 return is_a_pip_path_on_ubuntu(pkg_handle.__file__)87 except ImportError:88 return False89# Error/Warning Rules90def python_module_install_check(ctx):91 """Make sure core Python modules are installed"""92 warn_str = ''93 for py_pkg in py_to_deb_core_packages:94 if not is_python_package_installed(py_pkg):95 warn_str = warn_str + py_pkg + ' -- '96 if (warn_str != ''):97 return warn_str98def deb_install_check_on_ubuntu(ctx):99 """Make sure on Debian python packages are installed"""100 if (is_host_os_ubuntu()):101 warn_str = ''102 for py_pkg in py_to_deb_core_packages:103 deb_pkg = py_to_deb_core_packages[py_pkg]104 if not is_debian_package_installed(deb_pkg):105 warn_str = warn_str + py_pkg + ' (' + deb_pkg + ') -- '106 if (warn_str != ''):107 return warn_str108def pip_install_check_on_ubuntu(ctx):109 """Make sure on Ubuntu, Python packages are install with apt and not pip"""110 if (is_host_os_ubuntu()):111 warn_str = ''112 for py_pkg in dict(py_to_deb_core_packages.items() + py_to_deb_core_packages.items()):113 if is_python_package_installed_via_pip_on_ubuntu(py_pkg):114 warn_str = warn_str + py_pkg + ' -- '115 if (warn_str != ''):116 return warn_str117warnings = [118 (python_module_install_check,119 "You are missing core ROS Python modules: "),120 (pip_install_check_on_ubuntu,121 "You have pip installed packages on Ubuntu, "122 "remove and install using Debian packages: "),123 (deb_install_check_on_ubuntu,124 "You are missing Debian packages for core ROS Python modules: "),125 ]126errors = []127def wtf_check(ctx):128 """Check implementation function for roswtf"""129 from roswtf.rules import warning_rule, error_rule130 for r in warnings:131 warning_rule(r, r[0](ctx), ctx)132 for r in errors:...
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!!