Best Python code snippet using autotest_python
external_packages.py
Source:external_packages.py  
...168        directory is cleaned up.169        :return: True on success, False otherwise.170        :raise OSError If the expected extraction directory does not exist.171        """172        self._extract_compressed_package()173        if self.verified_package.endswith('.tar.gz'):174            extension = '.tar.gz'175        elif self.verified_package.endswith('.tar.bz2'):176            extension = '.tar.bz2'177        elif self.verified_package.endswith('.zip'):178            extension = '.zip'179        else:180            raise Error('Unexpected package file extension on %s' %181                        self.verified_package)182        os.chdir(os.path.dirname(self.verified_package))183        os.chdir(self.local_filename[:-len(extension)])184        extracted_dir = os.getcwd()185        try:186            return self._build_and_install_current_dir(install_dir)187        finally:188            os.chdir(os.path.join(extracted_dir, '..'))189            shutil.rmtree(extracted_dir)190    def _extract_compressed_package(self):191        """Extract the fetched compressed .tar or .zip within its directory."""192        if not self.verified_package:193            raise Error('Package must have been fetched first.')194        os.chdir(os.path.dirname(self.verified_package))195        if self.verified_package.endswith('gz'):196            status = system("tar -xzf '%s'" % self.verified_package)197        elif self.verified_package.endswith('bz2'):198            status = system("tar -xjf '%s'" % self.verified_package)199        elif self.verified_package.endswith('zip'):200            status = system("unzip '%s'" % self.verified_package)201        else:202            raise Error('Unknown compression suffix on %s.' %203                        self.verified_package)204        if status:205            raise Error('tar failed with %s' % (status,))206    def _build_using_setup_py(self, setup_py='setup.py'):207        """208        Assuming the cwd is the extracted python package, execute a simple209        python setup.py build.210        :param setup_py - The name of the setup.py file to execute.211        :return: True on success, False otherwise.212        """213        if not os.path.exists(setup_py):214            raise Error('%s does not exist in %s' % (setup_py, os.getcwd()))215        status = system("'%s' %s build" % (sys.executable, setup_py))216        if status:217            logging.error('%s build failed.' % self.name)218            return False219        return True220    def _build_egg_using_setup_py(self, setup_py='setup.py'):221        """222        Assuming the cwd is the extracted python package, execute a simple223        python setup.py bdist_egg.224        :param setup_py - The name of the setup.py file to execute.225        :return: The relative path to the resulting egg file or '' on failure.226        """227        if not os.path.exists(setup_py):228            raise Error('%s does not exist in %s' % (setup_py, os.getcwd()))229        egg_subdir = 'dist'230        if os.path.isdir(egg_subdir):231            shutil.rmtree(egg_subdir)232        status = system("'%s' %s bdist_egg" % (sys.executable, setup_py))233        if status:234            logging.error('bdist_egg of setuptools failed.')235            return ''236        # I've never seen a bdist_egg lay multiple .egg files.237        for filename in os.listdir(egg_subdir):238            if filename.endswith('.egg'):239                return os.path.join(egg_subdir, filename)240    def _install_from_egg(self, install_dir, egg_path):241        """242        Install a module from an egg file by unzipping the necessary parts243        into install_dir.244        :param install_dir - The installation directory.245        :param egg_path - The pathname of the egg file.246        """247        status = system("unzip -q -o -d '%s' '%s'" % (install_dir, egg_path))248        if status:249            logging.error('unzip of %s failed', egg_path)250            return False251        egg_info = os.path.join(install_dir, 'EGG-INFO')252        if os.path.isdir(egg_info):253            shutil.rmtree(egg_info)254        return True255    def _get_temp_dir(self):256        return tempfile.mkdtemp(dir='/var/tmp')257    def _site_packages_path(self, temp_dir):258        # This makes assumptions about what python setup.py install259        # does when given a prefix.  Is this always correct?260        python_xy = 'python%s' % sys.version[:3]261        return os.path.join(temp_dir, 'lib', python_xy, 'site-packages')262    def _install_using_setup_py_and_rsync(self, install_dir,263                                          setup_py='setup.py',264                                          temp_dir=None):265        """266        Assuming the cwd is the extracted python package, execute a simple:267          python setup.py install --prefix=BLA268        BLA will be a temporary directory that everything installed will269        be picked out of and rsynced to the appropriate place under270        install_dir afterwards.271        Afterwards, it deconstructs the extra lib/pythonX.Y/site-packages/272        directory tree that setuptools created and moves all installed273        site-packages directly up into install_dir itself.274        :param install_dir the directory for the install to happen under.275        :param setup_py - The name of the setup.py file to execute.276        :return: True on success, False otherwise.277        """278        if not os.path.exists(setup_py):279            raise Error('%s does not exist in %s' % (setup_py, os.getcwd()))280        if temp_dir is None:281            temp_dir = self._get_temp_dir()282        try:283            status = system("'%s' %s install --no-compile --prefix='%s'"284                            % (sys.executable, setup_py, temp_dir))285            if status:286                logging.error('%s install failed.' % self.name)287                return False288            if os.path.isdir(os.path.join(temp_dir, 'lib')):289                # NOTE: This ignores anything outside of the lib/ dir that290                # was installed.291                temp_site_dir = self._site_packages_path(temp_dir)292            else:293                temp_site_dir = temp_dir294            status = system("rsync -r '%s/' '%s/'" %295                            (temp_site_dir, install_dir))296            if status:297                logging.error('%s rsync to install_dir failed.' % self.name)298                return False299            return True300        finally:301            shutil.rmtree(temp_dir)302    def _build_using_make(self, install_dir):303        """Build the current package using configure/make.304        :return: True on success, False otherwise.305        """306        install_prefix = os.path.join(install_dir, 'usr', 'local')307        status = system('./configure --prefix=%s' % install_prefix)308        if status:309            logging.error('./configure failed for %s', self.name)310            return False311        status = system('make')312        if status:313            logging.error('make failed for %s', self.name)314            return False315        status = system('make check')316        if status:317            logging.error('make check failed for %s', self.name)318            return False319        return True320    def _install_using_make(self):321        """Install the current package using make install.322        Assumes the install path was set up while running ./configure (in323        _build_using_make()).324        :return: True on success, False otherwise.325        """326        status = system('make install')327        return status == 0328    def fetch(self, dest_dir):329        """330        Fetch the package from one its URLs and save it in dest_dir.331        If the the package already exists in dest_dir and the checksum332        matches this code will not fetch it again.333        Sets the 'verified_package' attribute with the destination pathname.334        :param dest_dir - The destination directory to save the local file.335            If it does not exist it will be created.336        :return: A boolean indicating if we the package is now in dest_dir.337        :raise FetchError - When something unexpected happens.338        """339        if not os.path.exists(dest_dir):340            os.makedirs(dest_dir)341        local_path = os.path.join(dest_dir, self.local_filename)342        # If the package exists, verify its checksum and be happy if it is good.343        if os.path.exists(local_path):344            actual_hex_sum = _checksum_file(local_path)345            if self.hex_sum == actual_hex_sum:346                logging.info('Good checksum for existing %s package.',347                             self.name)348                self.verified_package = local_path349                return True350            logging.warning('Bad checksum for existing %s package.  '351                            'Re-downloading', self.name)352            os.rename(local_path, local_path + '.wrong-checksum')353        # Download the package from one of its urls, rejecting any if the354        # checksum does not match.355        for url in self.urls:356            logging.info('Fetching %s', url)357            try:358                url_file = urllib2.urlopen(url)359            except (urllib2.URLError, EnvironmentError):360                logging.warning('Could not fetch %s package from %s.',361                                self.name, url)362                continue363            data_length = int(url_file.info().get('Content-Length',364                                                  _MAX_PACKAGE_SIZE))365            if data_length <= 0 or data_length > _MAX_PACKAGE_SIZE:366                raise FetchError('%s from %s fails Content-Length %d '367                                 'sanity check.' % (self.name, url,368                                                    data_length))369            checksum = utils.hash('sha1')370            total_read = 0371            output = open(local_path, 'wb')372            try:373                while total_read < data_length:374                    data = url_file.read(_READ_SIZE)375                    if not data:376                        break377                    output.write(data)378                    checksum.update(data)379                    total_read += len(data)380            finally:381                output.close()382            if self.hex_sum != checksum.hexdigest():383                logging.warning('Bad checksum for %s fetched from %s.',384                                self.name, url)385                logging.warning('Got %s', checksum.hexdigest())386                logging.warning('Expected %s', self.hex_sum)387                os.unlink(local_path)388                continue389            logging.info('Good checksum.')390            self.verified_package = local_path391            return True392        else:393            return False394# NOTE: This class definition must come -before- all other ExternalPackage395# classes that need to use this version of setuptools so that is is inserted396# into the ExternalPackage.subclasses list before them.397class SetuptoolsPackage(ExternalPackage):398    # For all known setuptools releases a string compare works for the399    # version string.  Hopefully they never release a 0.10.  (Their own400    # version comparison code would break if they did.)401    # Any system with setuptools > 0.6 is fine. If none installed, then402    # try to install the latest found on the upstream.403    minimum_version = '0.6'404    version = '0.6c11'405    urls = ('http://pypi.python.org/packages/source/s/setuptools/'406            'setuptools-%s.tar.gz' % (version,),)407    local_filename = 'setuptools-%s.tar.gz' % version408    hex_sum = '8d1ad6384d358c547c50c60f1bfdb3362c6c4a7d'409    SUDO_SLEEP_DELAY = 15410    def _build_and_install(self, install_dir):411        """Install setuptools on the system."""412        logging.info('NOTE: setuptools install does not use install_dir.')413        return self._build_and_install_from_package(install_dir)414    def _build_and_install_current_dir(self, install_dir):415        egg_path = self._build_egg_using_setup_py()416        if not egg_path:417            return False418        print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'419        print 'About to run sudo to install setuptools', self.version420        print 'on your system for use by', sys.executable, '\n'421        print '!! ^C within', self.SUDO_SLEEP_DELAY, 'seconds to abort.\n'422        time.sleep(self.SUDO_SLEEP_DELAY)423        # Copy the egg to the local filesystem /var/tmp so that root can424        # access it properly (avoid NFS squashroot issues).425        temp_dir = self._get_temp_dir()426        try:427            shutil.copy(egg_path, temp_dir)428            egg_name = os.path.split(egg_path)[1]429            temp_egg = os.path.join(temp_dir, egg_name)430            p = subprocess.Popen(['sudo', '/bin/sh', temp_egg],431                                 stdout=subprocess.PIPE)432            regex = re.compile('Copying (.*?) to (.*?)\n')433            match = regex.search(p.communicate()[0])434            status = p.wait()435            if match:436                compiled = os.path.join(match.group(2), match.group(1))437                os.system("sudo chmod a+r '%s'" % compiled)438        finally:439            shutil.rmtree(temp_dir)440        if status:441            logging.error('install of setuptools from egg failed.')442            return False443        return True444class MySQLdbPackage(ExternalPackage):445    module_name = 'MySQLdb'446    version = '1.2.2'447    urls = ('http://downloads.sourceforge.net/project/mysql-python/'448            'mysql-python/%(version)s/MySQL-python-%(version)s.tar.gz'449            % dict(version=version),)450    local_filename = 'MySQL-python-%s.tar.gz' % version451    hex_sum = '945a04773f30091ad81743f9eb0329a3ee3de383'452    _build_and_install_current_dir = (453        ExternalPackage._build_and_install_current_dir_setup_py)454    def _build_and_install(self, install_dir):455        if not os.path.exists('/usr/bin/mysql_config'):456            logging.error('You need to install /usr/bin/mysql_config')457            logging.error('On Ubuntu or Debian based systems use this: '458                          'sudo apt-get install libmysqlclient15-dev')459            return False460        return self._build_and_install_from_package(install_dir)461class DjangoPackage(ExternalPackage):462    minimum_version = '1.3'463    version = '1.5.1'464    local_filename = 'Django-%s.tar.gz' % version465    urls = ('http://www.djangoproject.com/download/%s/tarball/' % version,)466    hex_sum = '0ab97b90c4c79636e56337f426f1e875faccbba1'467    _build_and_install = ExternalPackage._build_and_install_from_package468    _build_and_install_current_dir = (469        ExternalPackage._build_and_install_current_dir_noegg)470    def _get_installed_version_from_module(self, module):471        try:472            return module.get_version().split()[0]473        except AttributeError:474            return '0.9.6'475class NumpyPackage(ExternalPackage):476    version = '1.2.1'477    local_filename = 'numpy-%s.tar.gz' % version478    urls = ('http://downloads.sourceforge.net/project/numpy/NumPy/%(version)s/'479            'numpy-%(version)s.tar.gz' % dict(version=version),)480    hex_sum = '1aa706e733aea18eaffa70d93c0105718acb66c5'481    _build_and_install = ExternalPackage._build_and_install_from_package482    _build_and_install_current_dir = (483        ExternalPackage._build_and_install_current_dir_setupegg_py)484class PsUtilPackage(ExternalPackage):485    minimum_version = '0.4.0'486    version = '1.0.1'487    local_filename = 'psutil-%s.tar.gz' % version488    urls = ("https://psutil.googlecode.com/files/%s" % local_filename,)489    hex_sum = '3d3abb8b7a5479b7299a8d170ec25179410f24d1'490    _build_and_install = ExternalPackage._build_and_install_from_package491    _build_and_install_current_dir = (492        ExternalPackage._build_and_install_current_dir_setup_py)493# This requires numpy so it must be declared after numpy to guarantee that it494# is already installed.495class MatplotlibPackage(ExternalPackage):496    version = '0.98.5.3'497    short_version = '0.98.5'498    local_filename = 'matplotlib-%s.tar.gz' % version499    urls = ('http://downloads.sourceforge.net/project/matplotlib/matplotlib/'500            'matplotlib-%s/matplotlib-%s.tar.gz' % (short_version, version),)501    hex_sum = '2f6c894cf407192b3b60351bcc6468c0385d47b6'502    os_requirements = {'/usr/include/ft2build.h': 'libfreetype6-dev',503                       '/usr/include/png.h': 'libpng12-dev'}504    _build_and_install = ExternalPackage._build_and_install_from_package505    _build_and_install_current_dir = (506        ExternalPackage._build_and_install_current_dir_setupegg_py)507class AtForkPackage(ExternalPackage):508    version = '0.1.2'509    local_filename = 'atfork-%s.zip' % version510    urls = ('http://python-atfork.googlecode.com/files/' + local_filename,)511    hex_sum = '5baa64c73e966b57fa797040585c760c502dc70b'512    _build_and_install = ExternalPackage._build_and_install_from_package513    _build_and_install_current_dir = (514        ExternalPackage._build_and_install_current_dir_noegg)515class ParamikoPackage(ExternalPackage):516    version = '1.7.5'517    local_filename = 'paramiko-%s.tar.gz' % version518    urls = ('http://www.lag.net/paramiko/download/' + local_filename,519            'ftp://mirrors.kernel.org/gentoo/distfiles/' + local_filename,)520    hex_sum = '592be7a08290070b71da63a8e6f28a803399e5c5'521    _build_and_install = ExternalPackage._build_and_install_from_package522    def _check_for_pycrypto(self):523        # NOTE(gps): Linux distros have better python-crypto packages than we524        # can easily get today via a wget due to the library's age and staleness525        # yet many security and behavior bugs are fixed by patches that distros526        # already apply.  PyCrypto has a new active maintainer in 2009.  Once a527        # new release is made (http://pycrypto.org/) we should add an installer.528        try:529            import Crypto530        except ImportError:531            logging.error('Please run "sudo apt-get install python-crypto" '532                          'or your Linux distro\'s equivalent.')533            return False534        return True535    def _build_and_install_current_dir(self, install_dir):536        if not self._check_for_pycrypto():537            return False538        # paramiko 1.7.4 doesn't require building, it is just a module directory539        # that we can rsync into place directly.540        if not os.path.isdir('paramiko'):541            raise Error('no paramiko directory in %s.' % os.getcwd())542        status = system("rsync -r 'paramiko' '%s/'" % install_dir)543        if status:544            logging.error('%s rsync to install_dir failed.' % self.name)545            return False546        return True547class SimplejsonPackage(ExternalPackage):548    version = '2.0.9'549    local_filename = 'simplejson-%s.tar.gz' % version550    urls = ('http://pypi.python.org/packages/source/s/simplejson/' +551            local_filename,)552    hex_sum = 'b5b26059adbe677b06c299bed30557fcb0c7df8c'553    _build_and_install = ExternalPackage._build_and_install_from_package554    _build_and_install_current_dir = (555        ExternalPackage._build_and_install_current_dir_setup_py)556class AexpectPackage(ExternalPackage):557    version = '1.0.0'558    local_filename = 'aexpect-%s.tar.gz' % version559    urls = ('http://pypi.python.org/packages/source/a/aexpect/' +560            local_filename,)561    hex_sum = '5a1752dd26b4653f8c4ee413455e86879e47c269'562    def _get_installed_version_from_module(self, module):563        return self.version564    _build_and_install = ExternalPackage._build_and_install_from_package565    _build_and_install_current_dir = (566        ExternalPackage._build_and_install_current_dir_noegg)567class Httplib2Package(ExternalPackage):568    version = '0.6.0'569    local_filename = 'httplib2-%s.tar.gz' % version570    urls = ('http://httplib2.googlecode.com/files/' + local_filename,)571    hex_sum = '995344b2704826cc0d61a266e995b328d92445a5'572    def _get_installed_version_from_module(self, module):573        # httplib2 doesn't contain a proper version574        return self.version575    _build_and_install = ExternalPackage._build_and_install_from_package576    _build_and_install_current_dir = (577        ExternalPackage._build_and_install_current_dir_noegg)578class GwtPackage(ExternalPackage):579    """Fetch and extract a local copy of GWT used to build the frontend."""580    version = '2.7.0'581    local_filename = 'gwt-%s.zip' % version582    urls = ('http://goo.gl/t7FQSn',)583    hex_sum = '2688f2ed80a36ba0e6170a4ef656581d811f330f'584    name = 'gwt'585    about_filename = 'about.txt'586    module_name = None  # Not a Python module.587    def is_needed(self, install_dir):588        gwt_dir = os.path.join(install_dir, self.name)589        about_file = os.path.join(install_dir, self.name, self.about_filename)590        if not os.path.exists(gwt_dir) or not os.path.exists(about_file):591            logging.info('gwt not installed for autotest')592            return True593        f = open(about_file, 'r')594        version_line = f.readline()595        f.close()596        match = re.match(r'Google Web Toolkit (.*)', version_line)597        if not match:598            logging.info('did not find gwt version')599            return True600        logging.info('found gwt version %s', match.group(1))601        return match.group(1) != self.version602    def _build_and_install(self, install_dir):603        if not os.path.isdir(install_dir):604            os.makedirs(install_dir)605        os.chdir(install_dir)606        self._extract_compressed_package()607        extracted_dir = self.local_filename[:-len('.zip')]608        target_dir = os.path.join(install_dir, self.name)609        if os.path.exists(target_dir):610            shutil.rmtree(target_dir)611        os.rename(extracted_dir, target_dir)...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!!
