|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import urllib |
| 4 | +import subprocess |
| 5 | + |
| 6 | +def download(file_url,local_filename): |
| 7 | + web_file = urllib.urlopen(file_url) |
| 8 | + local_file = open(local_filename, 'w') |
| 9 | + local_file.write(web_file.read()) |
| 10 | + web_file.close() |
| 11 | + local_file.close() |
| 12 | + |
| 13 | +def get_windows_pip_path(): |
| 14 | + python_dir = sys.executable |
| 15 | + split = python_dir.split("\\") |
| 16 | + pip_path = "" |
| 17 | + for i in range(0,len(split)-1): |
| 18 | + pip_path = "%s/%s" %(pip_path,split[i]) |
| 19 | + pip_path = "%s/Scripts/pip" %pip_path[1:] |
| 20 | + |
| 21 | + return pip_path |
| 22 | + |
| 23 | +def pip_install_module(module_name): |
| 24 | + pip_path = "pip" |
| 25 | + DEVNULL = open(os.devnull,'wb') |
| 26 | + new_installation = True |
| 27 | + |
| 28 | + try: |
| 29 | + subprocess.call(["pip"], stdout=DEVNULL) # verify if pip is already installed |
| 30 | + except OSError as e: |
| 31 | + if(sys.platform[:3] == "win"): |
| 32 | + pip_path = get_windows_pip_path() |
| 33 | + try: |
| 34 | + subprocess.call([pip_path],stdout=DEVNULL) |
| 35 | + new_installation = False |
| 36 | + print "[+] Found Windows pip executable at '%s'" %pip_path |
| 37 | + except: |
| 38 | + pass |
| 39 | + |
| 40 | + if(new_installation): |
| 41 | + print "[!] pip is not currently installed." |
| 42 | + |
| 43 | + if(os.path.isfile("get-pip.py") is False): |
| 44 | + print "[*] Downloading get-pip.py.." |
| 45 | + download("https://bootstrap.pypa.io/get-pip.py","get-pip.py") |
| 46 | + else: |
| 47 | + print "[+] get-pip-py found in the current directory." |
| 48 | + |
| 49 | + os.system("python get-pip.py") |
| 50 | + |
| 51 | + try: |
| 52 | + subprocess.call(["pip"],stdout=DEVNULL) |
| 53 | + except: |
| 54 | + if(sys.platform[:3] == "win"): |
| 55 | + python_dir = sys.executable # "C:\\Python27\\python.exe" |
| 56 | + split = python_dir.split("\\") |
| 57 | + pip_path = "" |
| 58 | + for i in range(0,len(split)-1): # let's avoid python.exe |
| 59 | + pip_path = "%s/%s" %(pip_path,split[i]) |
| 60 | + |
| 61 | + pip_path = "%s/Scripts/pip" %pip_path[1:] |
| 62 | + |
| 63 | + if(new_installation): |
| 64 | + try: |
| 65 | + os.remove("get-pip.py") |
| 66 | + except: |
| 67 | + pass |
| 68 | + |
| 69 | + os.system("%s install --upgrade pip" %pip_path) |
| 70 | + print "\n[*] Installing module '%s'" %module_name |
| 71 | + os.system("%s install %s" %(pip_path,module_name)) |
| 72 | + |
0 commit comments