| 1 |  |  | from coalib.bears.requirements.PackageRequirement import PackageRequirement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import subprocess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | class PipRequirement(PackageRequirement): | 
            
                                                        
            
                                    
            
            
                | 6 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 7 |  |  |     This class is a subclass of ``PackageRequirement``, and helps specifying | 
            
                                                        
            
                                    
            
            
                | 8 |  |  |     requirements from ``pip``, without using the manager name. | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |     def __init__(self, package, version=""): | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |         Constructs a new ``PipRequirement``, using the ``PackageRequirement`` | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |         constructor. | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |         >>> pr = PipRequirement('setuptools', '19.2') | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |         >>> pr.manager | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |         'pip' | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |         >>> pr.package | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |         'setuptools' | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |         >>> pr.version | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |         '19.2' | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |         :param package: A string with the name of the package to be installed. | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |         :param version: A version string. Leave empty to specify latest version. | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |         PackageRequirement.__init__(self, 'pip', package, version) | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |     def install_command(self): | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         Creates the installation command for the instance of the class. | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |         >>> PipRequirement('setuptools', '19.2').install_command() | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         'pip install setuptools==19.2' | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |         :param return: A string with the installation command. | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |         return "pip install {}=={}".format(self.package, self.version) | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     def is_installed(self): | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         Checks if the dependency is installed. | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         >>> PipRequirement('pip').is_installed() | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         True | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         >>> PipRequirement('some_package').is_installed() | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         False | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         :param return: True if dependency is installed, false otherwise. | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         """ | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         return not subprocess.call(['pip', 'show', self.package], | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |                                    stdout=subprocess.DEVNULL) | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  |