Skip to content

Instantly share code, notes, and snippets.

@wkettler
Last active July 21, 2023 08:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wkettler/9397410 to your computer and use it in GitHub Desktop.
Save wkettler/9397410 to your computer and use it in GitHub Desktop.
A Python wrapper for the GNU dd command.
from execute import execute, Retcode
def dd(ifile, ofile, bs, count=None, seek=None):
"""
Wrapper for the GNU dd command.
Inputs:
ifile (str): Input file
ofile (str): Output file
bs (str): Block size
count (str): Number of blocks to copy
seek (str): Seek x blocks
Outputs:
time (float): Duration
tput (float): Throughput in MiB/s
iops (float): IOPS
"""
# Execute dd command
cmd = "dd if=%s of=%s bs=%s" % (ifile, ofile, bs)
if count is not None:
cmd = " ".join([cmd, "count=%s" % count])
if seek is not None:
cmd = " ".join([cmd, "seek=%s" % seek])
try:
retcode, output = execute(cmd)
except:
raise
else:
if retcode:
raise Retcode(output)
# Split lines into fields
records_in, records_out, summary = output.split("\n")
# Parse for record counts
# If we run out of disk space requested vs actual will vary
s = re.search(r"^([0-9]*)\+.*", records_in)
records_in = int(s.group(1))
s = re.search(r"^([0-9]*)\+.*", records_out)
records_out = int(s.group(1))
# There is no reason the record counts should not match but if they
# do raise an exception because it will effect calculations.
if records_in != records_out:
raise Exception('records mismatch')
# Parse for the byte total and time
s = re.search(r"^([0-9]*).*([0-9]+\.[0-9]+) s", summary)
size = int(s.group(1))
time = float(s.group(2))
# Calculate throughput in Mib, i.e. base 2
# Note that dd returns base 10 calcaulation
tput = size / time / 1024**2
# Calculate IOPS
iops = records_in / time
return time, tput, iops
@nguoiyeudau2008
Copy link

Ahihi

@orengt
Copy link

orengt commented Sep 9, 2017

May I ask a question.
I'm quite ignorant in py and programming however this script might be very useful for me.
But I have some issues I don't know how to resolve with it.
I'm on Win 10 working in python with PyCharm.
I downloaded the script and put it in the site-packages folder.
Then I tried to import it and got the following error:
Traceback (most recent call last): File "C:\Users\oreng\AppData\Roaming\Python\Python36\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-8-208035e6d611>", line 1, in <module> execute NameError: name 'execute' is not defined
Can anyone tell what am I doing wrong?
Thanks,
Oren

@jaydeepch15
Copy link

Exactly the same error ,
install execute via pip.
When you type import execute it works
However from execute import execute, Retcode not working

 In [1]: from execute import execute, Retcode
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-bd13b4cc107c> in <module>()
----> 1 from execute import execute, Retcode

ImportError: cannot import name 'execute'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment