1414import zipfile
1515
1616from lib .core .common import dataToStdout
17+ from lib .core .common import extractRegexResult
1718from lib .core .common import getLatestRevision
1819from lib .core .common import getSafeExString
1920from lib .core .common import openFile
2728from lib .core .settings import GIT_REPOSITORY
2829from lib .core .settings import IS_WIN
2930from lib .core .settings import VERSION
31+ from lib .core .settings import TYPE
3032from lib .core .settings import ZIPBALL_PAGE
3133from thirdparty .six .moves import urllib as _urllib
3234
@@ -36,7 +38,34 @@ def update():
3638
3739 success = False
3840
39- if not os .path .exists (os .path .join (paths .SQLMAP_ROOT_PATH , ".git" )):
41+ if TYPE == "pip" :
42+ infoMsg = "updating sqlmap to the latest stable version from the "
43+ infoMsg += "PyPI repository"
44+ logger .info (infoMsg )
45+
46+ debugMsg = "sqlmap will try to update itself using 'pip' command"
47+ logger .debug (debugMsg )
48+
49+ dataToStdout ("\r [%s] [INFO] update in progress" % time .strftime ("%X" ))
50+
51+ output = ""
52+ try :
53+ process = subprocess .Popen ("pip install -U sqlmap" , shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , cwd = paths .SQLMAP_ROOT_PATH )
54+ pollProcess (process , True )
55+ output , _ = process .communicate ()
56+ success = not process .returncode
57+ except Exception as ex :
58+ success = False
59+ output = getSafeExString (ex )
60+ finally :
61+ output = getText (output )
62+
63+ if success :
64+ logger .info ("%s the latest revision '%s'" % ("already at" if "already up-to-date" in output else "updated to" , extractRegexResult (r"\binstalled sqlmap-(?P<result>\d+\.\d+\.\d+)" , output ) or extractRegexResult (r"\((?P<result>\d+\.\d+\.\d+)\)" , output )))
65+ else :
66+ logger .error ("update could not be completed ('%s')" % re .sub (r"[^a-z0-9:/\\]+" , " " , output ).strip ())
67+
68+ elif not os .path .exists (os .path .join (paths .SQLMAP_ROOT_PATH , ".git" )):
4069 warnMsg = "not a git repository. It is recommended to clone the 'sqlmapproject/sqlmap' repository "
4170 warnMsg += "from GitHub (e.g. 'git clone --depth 1 %s sqlmap')" % GIT_REPOSITORY
4271 logger .warn (warnMsg )
@@ -95,6 +124,7 @@ def update():
95124 os .chmod (os .path .join (directory , "sqlmap.py" ), attrs )
96125 except OSError :
97126 logger .warning ("could not set the file attributes of '%s'" % os .path .join (directory , "sqlmap.py" ))
127+
98128 else :
99129 infoMsg = "updating sqlmap to the latest development revision from the "
100130 infoMsg += "GitHub repository"
0 commit comments