|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Copyright (c) 2014-2016 pocsuite developers (https://seebug.org) |
| 6 | +See the file 'docs/COPYING' for copying permission |
| 7 | +""" |
| 8 | + |
| 9 | +import locale |
| 10 | +import os |
| 11 | +import re |
| 12 | +import subprocess |
| 13 | +import time |
| 14 | + |
| 15 | +from pocsuite.lib.core.common import dataToStdout |
| 16 | +from pocsuite.lib.core.common import poll_process |
| 17 | +from pocsuite.lib.core.data import conf |
| 18 | +from pocsuite.lib.core.data import logger |
| 19 | +from pocsuite.lib.core.data import paths |
| 20 | +from pocsuite.lib.core.revision import getRevisionNumber |
| 21 | +from pocsuite.lib.core.settings import GIT_REPOSITORY |
| 22 | +from pocsuite.lib.core.settings import IS_WIN |
| 23 | +from pocsuite.lib.core.settings import SITE |
| 24 | + |
| 25 | + |
| 26 | +def update(): |
| 27 | + if not conf.update: |
| 28 | + return |
| 29 | + |
| 30 | + success = False |
| 31 | + |
| 32 | + if not os.path.exists(os.path.join( |
| 33 | + os.path.dirname(paths.POCSUITE_ROOT_PATH), ".git")): |
| 34 | + err_msg = ("not a git repository. Please checkout the 'pocsuite' " |
| 35 | + "repository from GitHub (e.g. 'git clone --depth 1" |
| 36 | + "https://github.com/knownsec/Pocsuite.git pocsuite')") |
| 37 | + logger.error(err_msg) |
| 38 | + else: |
| 39 | + info_msg = ("updating pocsuite to the latest development version from " |
| 40 | + "the GitHub repository") |
| 41 | + logger.info(info_msg) |
| 42 | + |
| 43 | + debug_msg = "pocsuite will try to update itself using 'git' command" |
| 44 | + logger.debug(debug_msg) |
| 45 | + |
| 46 | + dataToStdout("\r[%s] [INFO] update in progress " % time.strftime("%X")) |
| 47 | + |
| 48 | + try: |
| 49 | + # Reference: |
| 50 | + # http://blog.stastnarodina.com/honza-en/spot/python-unicodeencodeerror/ |
| 51 | + process = subprocess.Popen( |
| 52 | + "git checkout . && git pull %s HEAD" % GIT_REPOSITORY, |
| 53 | + shell=True, |
| 54 | + stdout=subprocess.PIPE, |
| 55 | + stderr=subprocess.PIPE, |
| 56 | + cwd=paths.POCSUITE_ROOT_PATH.encode( |
| 57 | + locale.getpreferredencoding()) |
| 58 | + ) |
| 59 | + poll_process(process, True) |
| 60 | + stdout, stderr = process.communicate() |
| 61 | + success = not process.returncode |
| 62 | + except (IOError, OSError) as ex: |
| 63 | + success = False |
| 64 | + stderr = "{}".format(ex) |
| 65 | + |
| 66 | + if success: |
| 67 | + info_msg = "{0} the latest revision '{1}'".format( |
| 68 | + "already at" if "Already" in stdout else "updated to", |
| 69 | + getRevisionNumber()) |
| 70 | + logger.info(info_msg) |
| 71 | + else: |
| 72 | + if "Not a git repository" in stderr: |
| 73 | + err_msg = ("not a valid git repository. Please checkout the " |
| 74 | + "'pocsuite' repository from GitHub " |
| 75 | + "(e.g. 'git clone --depth 1 " |
| 76 | + "https://github.com/knownsec/Pocsuite.git " |
| 77 | + "pocsuite')") |
| 78 | + logger.error(err_msg) |
| 79 | + else: |
| 80 | + err_msg = "update could not be completed ('{}')".format( |
| 81 | + re.sub(r"\W+", " ", stderr).strip()) |
| 82 | + logger.error(err_msg) |
| 83 | + |
| 84 | + if not success: |
| 85 | + if IS_WIN: |
| 86 | + info_msg = ("for Windows platform it's recommended to use a Github" |
| 87 | + "for Windows client for updating purposes " |
| 88 | + "(http://windows.github.com/) or just download the " |
| 89 | + "latest snapshot from {}".format(SITE)) |
| 90 | + else: |
| 91 | + info_msg = ("for Linux platform it's required to install a " |
| 92 | + "standard 'git' package (e.g.: " |
| 93 | + "'sudo apt-get install git')") |
| 94 | + logger.info(info_msg) |
0 commit comments