Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6828726

Browse files
committed
some more refactoring
1 parent 91a0b5d commit 6828726

3 files changed

Lines changed: 100 additions & 77 deletions

File tree

lib/core/common.py

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"""
2424

2525
import codecs
26-
import cProfile
2726
import inspect
2827
import os
2928
import random
@@ -1191,75 +1190,6 @@ def isHexEncodedString(subject):
11911190
"""
11921191
return re.match(r"\A[0-9a-fA-F]+\Z", subject) is not None
11931192

1194-
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
1195-
try:
1196-
from extra.gprof2dot import gprof2dot
1197-
from extra.xdot import xdot
1198-
import gobject
1199-
import gtk
1200-
import pydot
1201-
except ImportError, e:
1202-
errMsg = "profiling requires third-party libraries (%s)" % getUnicode(e)
1203-
logger.error(errMsg)
1204-
return
1205-
1206-
if profileOutputFile is None:
1207-
profileOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.raw")
1208-
1209-
if dotOutputFile is None:
1210-
dotOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.dot")
1211-
1212-
if imageOutputFile is None:
1213-
imageOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.png")
1214-
1215-
if os.path.exists(profileOutputFile):
1216-
os.remove(profileOutputFile)
1217-
1218-
if os.path.exists(dotOutputFile):
1219-
os.remove(dotOutputFile)
1220-
1221-
if os.path.exists(imageOutputFile):
1222-
os.remove(imageOutputFile)
1223-
1224-
infoMsg = "profiling the execution into file %s" % profileOutputFile
1225-
logger.info(infoMsg)
1226-
1227-
# Start sqlmap main function and generate a raw profile file
1228-
cProfile.run("start()", profileOutputFile)
1229-
1230-
infoMsg = "converting profile data into a dot file '%s'" % dotOutputFile
1231-
logger.info(infoMsg)
1232-
1233-
# Create dot file by using extra/gprof2dot/gprof2dot.py
1234-
# http://code.google.com/p/jrfonseca/wiki/Gprof2Dot
1235-
dotFilePointer = codecs.open(dotOutputFile, 'wt', conf.dataEncoding)
1236-
parser = gprof2dot.PstatsParser(profileOutputFile)
1237-
profile = parser.parse()
1238-
profile.prune(0.5/100.0, 0.1/100.0)
1239-
dot = gprof2dot.DotWriter(dotFilePointer)
1240-
dot.graph(profile, gprof2dot.TEMPERATURE_COLORMAP)
1241-
dotFilePointer.close()
1242-
1243-
infoMsg = "converting dot file into a graph image '%s'" % imageOutputFile
1244-
logger.info(infoMsg)
1245-
1246-
# Create graph image (png) by using pydot (python-pydot)
1247-
# http://code.google.com/p/pydot/
1248-
pydotGraph = pydot.graph_from_dot_file(dotOutputFile)
1249-
pydotGraph.write_png(imageOutputFile)
1250-
1251-
infoMsg = "displaying interactive graph with xdot library"
1252-
logger.info(infoMsg)
1253-
1254-
# Display interactive Graphviz dot file by using extra/xdot/xdot.py
1255-
# http://code.google.com/p/jrfonseca/wiki/XDot
1256-
win = xdot.DotWindow()
1257-
win.connect('destroy', gtk.main_quit)
1258-
win.set_filter("dot")
1259-
win.open_file(dotOutputFile)
1260-
gobject.timeout_add(1000, win.update, dotOutputFile)
1261-
gtk.main()
1262-
12631193
def getConsoleWidth(default=80):
12641194
width = None
12651195

@@ -1453,12 +1383,6 @@ def getUnicode(value, encoding=None):
14531383
else:
14541384
return unicode(value) #encoding ignored for non-basestring instances
14551385

1456-
def getBruteUnicode(string):
1457-
retVal = unicode()
1458-
for char in string:
1459-
retVal += unichr(ord(char))
1460-
return retVal
1461-
14621386
# http://boredzo.org/blog/archives/2007-01-06/longest-common-prefix-in-python-2
14631387
def longestCommonPrefix(*sequences):
14641388
if len(sequences) == 1:

lib/core/profiling.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
$Id$
5+
6+
This file is part of the sqlmap project, http://sqlmap.sourceforge.net.
7+
8+
Copyright (c) 2010 Miroslav Stampar <[email protected]>
9+
10+
sqlmap is free software; you can redistribute it and/or modify it under
11+
the terms of the GNU General Public License as published by the Free
12+
Software Foundation version 2 of the License.
13+
14+
sqlmap is distributed in the hope that it will be useful, but WITHOUT ANY
15+
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16+
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17+
details.
18+
19+
You should have received a copy of the GNU General Public License along
20+
with sqlmap; if not, write to the Free Software Foundation, Inc., 51
21+
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
"""
23+
24+
import codecs
25+
import os
26+
import cProfile
27+
28+
from lib.core.data import conf
29+
from lib.core.data import logger
30+
from lib.core.data import paths
31+
32+
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
33+
try:
34+
from extra.gprof2dot import gprof2dot
35+
from extra.xdot import xdot
36+
import gobject
37+
import gtk
38+
import pydot
39+
except ImportError, e:
40+
errMsg = "profiling requires third-party libraries (%s)" % getUnicode(e)
41+
logger.error(errMsg)
42+
return
43+
44+
if profileOutputFile is None:
45+
profileOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.raw")
46+
47+
if dotOutputFile is None:
48+
dotOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.dot")
49+
50+
if imageOutputFile is None:
51+
imageOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.png")
52+
53+
if os.path.exists(profileOutputFile):
54+
os.remove(profileOutputFile)
55+
56+
if os.path.exists(dotOutputFile):
57+
os.remove(dotOutputFile)
58+
59+
if os.path.exists(imageOutputFile):
60+
os.remove(imageOutputFile)
61+
62+
infoMsg = "profiling the execution into file %s" % profileOutputFile
63+
logger.info(infoMsg)
64+
65+
# Start sqlmap main function and generate a raw profile file
66+
cProfile.run("start()", profileOutputFile)
67+
68+
infoMsg = "converting profile data into a dot file '%s'" % dotOutputFile
69+
logger.info(infoMsg)
70+
71+
# Create dot file by using extra/gprof2dot/gprof2dot.py
72+
# http://code.google.com/p/jrfonseca/wiki/Gprof2Dot
73+
dotFilePointer = codecs.open(dotOutputFile, 'wt', conf.dataEncoding)
74+
parser = gprof2dot.PstatsParser(profileOutputFile)
75+
profile = parser.parse()
76+
profile.prune(0.5/100.0, 0.1/100.0)
77+
dot = gprof2dot.DotWriter(dotFilePointer)
78+
dot.graph(profile, gprof2dot.TEMPERATURE_COLORMAP)
79+
dotFilePointer.close()
80+
81+
infoMsg = "converting dot file into a graph image '%s'" % imageOutputFile
82+
logger.info(infoMsg)
83+
84+
# Create graph image (png) by using pydot (python-pydot)
85+
# http://code.google.com/p/pydot/
86+
pydotGraph = pydot.graph_from_dot_file(dotOutputFile)
87+
pydotGraph.write_png(imageOutputFile)
88+
89+
infoMsg = "displaying interactive graph with xdot library"
90+
logger.info(infoMsg)
91+
92+
# Display interactive Graphviz dot file by using extra/xdot/xdot.py
93+
# http://code.google.com/p/jrfonseca/wiki/XDot
94+
win = xdot.DotWindow()
95+
win.connect('destroy', gtk.main_quit)
96+
win.set_filter("dot")
97+
win.open_file(dotOutputFile)
98+
gobject.timeout_add(1000, win.update, dotOutputFile)
99+
gtk.main()

sqlmap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
from lib.controller.controller import start
4646
from lib.core.common import banner
4747
from lib.core.common import getUnicode
48-
from lib.core.common import profile
4948
from lib.core.common import setPaths
5049
from lib.core.common import weAreFrozen
5150
from lib.core.data import conf
@@ -54,6 +53,7 @@
5453
from lib.core.exception import exceptionsTuple
5554
from lib.core.exception import unhandledException
5655
from lib.core.option import init
56+
from lib.core.profiling import profile
5757
from lib.core.xmldump import closeDumper
5858
from lib.parse.cmdline import cmdLineParser
5959

0 commit comments

Comments
 (0)