Skip to content

Instantly share code, notes, and snippets.

@SweBarre
Created April 23, 2013 09:42
Show Gist options
  • Save SweBarre/5442217 to your computer and use it in GitHub Desktop.
Save SweBarre/5442217 to your computer and use it in GitHub Desktop.
Generate test data
#!/usr/bin/env python
import sys
import os
import optparse
VERBOSE = False
FILESIZE = False
def makeFolders(path, width, depth, files):
if depth:
for i in range(0,width):
newpath = '{path}folder{no:0=4}{sep}'.format(path=path, sep=os.sep, no=i)
#if not os.path.exists(path +'folder'+str(i)):
if not os.path.exists(newpath):
if VERBOSE:
print(newpath)
os.makedirs(newpath)
makeFiles(files, newpath)
makeFolders(newpath, width, depth-1, files)
def makeFiles(files,path):
for i in range(0,files):
fileName = '{path}file{no:0=4}.tmp'.format(path=path, no=i)
if VERBOSE:
print(fileName)
if FILESIZE:
f = open(fileName, 'w')
f.seek(FILESIZE-1)
f.write("\0")
f.close()
else:
open(fileName,'w').close()
def calculateSize(inSize):
suffix = inSize[len(inSize)-1]
if suffix == 'B':
multiplier = 1
elif suffix == 'k':
multiplier = 1024
elif suffix == 'M':
multiplier = 1024*1024
elif suffix == 'G':
multiplier = 1024*1024*1024
else:
print('Wrong suffix specified in size')
exit(3)
return int(inSize[:len(inSize)-1]) * multiplier
def main():
global VERBOSE
global FILESIZE
#makeFolders(int(sys.argv[1]),int(sys.argv[2]))
optParser = optparse.OptionParser()
optParser.add_option('-d', '--depth', dest='depth', type='int',
help='The folder depth', metavar='NUM')
optParser.add_option('-w', '--width', dest='width', type='int',
help='The folder width', metavar='NUM')
optParser.add_option('-f', '--files', dest='files', type='int',
help='The number of files generated in every folder', metavar='NUM')
optParser.add_option('-v', '--verbose', dest='verbose', action='store_true',
help='be more verbose')
optParser.add_option('-s', '--fileSize', dest='fileSize', type='string',
help="the size of the test files, sufix: (B)yte,(k)ilobyte,(M)egabyte,(G)igabyte", metavar='X[BkMG]')
optParser.add_option('-t', '--target', dest='targetFolder', type='string', default=os.getcwd(),
help='create the test data-set and start in FOLDER. Default is current working directory', metavar='FOLDER')
(options, args) = optParser.parse_args()
if not options.depth:
print('You have to specify the folder depth')
exit(2)
if not options.width:
print('You have to specify the folder width')
exit(2)
if not options.files:
print('You have to specify the number of files per folder')
exit(2)
if options.verbose:
VERBOSE = True
if options.fileSize:
FILESIZE = calculateSize(options.fileSize)
if not options.targetFolder[len(options.targetFolder)-1] == os.sep:
options.targetFolder = '{path}{sep}'.format(path=options.targetFolder, sep=os.sep)
makeFolders(options.targetFolder, options.width, options.depth, options.files)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment