46 parser = argparse.ArgumentParser(description=
'A script to generate the content of test files for use with the socat command.')
48 parser.add_argument(
'-f',
'--file', required=
False, type=str, default=
'',
49 help=
'The <filename> to generate.')
50 parser.add_argument(
'-sz',
'--size', required=
True, type=str, default=
'',
51 help=
'The size of the file to generate (e.g. 512, 32K, 8M, 1G).')
53 parser.add_argument(
'-inc',
'--increment', action=
"store_true",
54 help=
'Generate a ramp of incremented numbers')
55 parser.add_argument(
'-dec',
'--decrement', action=
"store_true",
56 help=
'Generate a ramp of decremented numbers')
57 parser.add_argument(
'-rnd',
'--random', action=
"store_true",
58 help=
'Generate random numbers')
59 parser.add_argument(
'-v',
'--verbose', action=
"store_true",
60 help=
'Enable verbosity')
61 args = parser.parse_args()
65 match = re.search(
"^[0-9]", args.size)
67 charList = re.findall(
"[0-9]", args.size)
71 suffixFileName += strInt
73 if len(args.size) > len(charList):
74 if args.size[len(charList)] ==
'K':
75 reqSize = reqSize*1024
77 elif args.size[len(charList)] ==
'M':
78 reqSize = reqSize*1024*1024
80 elif args.size[len(charList)] ==
'G':
81 reqSize = reqSize*1024*1024*1024
84 print(
"Unknown or un-supported prefix symbol '%c'.\n" % args.size[len(charList)])
87 print(
"ERROR: The file size must start with a digit!\n")
91 print(
"\nERROR: The size must be a multiple of 8 bytes.")
94 if args.increment == 0
and args.decrement == 0
and args.random == 0:
95 print(
"\nERROR: A generator type is required (rnd, inc or dec).\n")
103 fileName =
"inc_" + suffixFileName
105 fileName =
"dec_" + suffixFileName
107 fileName =
"rnd_" + suffixFileName
111 if os.path.exists(fileName):
112 print(
"\nWARNING: File already exist.")
113 answer =
input(
"\tOverwrite (y/n): ")
121 outFile = open(fileName,
'w')
126 for x
in range(0, size):
128 strTmp =
"{:08d}".format(x)
138 outFile.write(swapStr)
143 print(
"STREAM=%s" % strStream)
145 size =
int(reqSize / 8)
147 for x
in range(size-1, 0, -1):
149 strTmp =
"{:08d}".format(x)
159 outFile.write(swapStr)
164 print(
"STREAM=%s" % strStream)
166 size =
int(reqSize / 2)
168 for x
in range(0, size):
169 y = random.randint(0, 255)
170 outFile.write(
"{:02x}".format(y))
174 print(
"\nINFO: The following file was created:")
175 os.system(
"ls -lh " + fileName)