cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
genTestFile.py
Go to the documentation of this file.
1 # *
2 # * Copyright 2016 -- 2021 IBM Corporation
3 # *
4 # * Licensed under the Apache License, Version 2.0 (the "License");
5 # * you may not use this file except in compliance with the License.
6 # * You may obtain a copy of the License at
7 # *
8 # * http://www.apache.org/licenses/LICENSE-2.0
9 # *
10 # * Unless required by applicable law or agreed to in writing, software
11 # * distributed under the License is distributed on an "AS IS" BASIS,
12 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # * See the License for the specific language governing permissions and
14 # * limitations under the License.
15 # *
16 
17 # *****************************************************************************
18 # * @file : genTestFile.py
19 # * @brief : A script to generate test files for use with 'socat' command.
20 # *
21 # * System: : cloudFPGA
22 # * Component : cFp_Monolithic/ROLE
23 # * Language : Python 3
24 # *
25 # *****************************************************************************
26 
27 
28 # ### REQUIRED PYTHON PACKAGES ################################################
29 import argparse
30 import datetime
31 import os.path
32 import random
33 import re
34 import time
35 
36 # ### REQUIRED TESTCASE MODULES ###############################################
37 
38 
43 
44 # STEP-1: Parse the command line strings into Python objects
45 # -----------------------------------------------------------------------------
46 parser = argparse.ArgumentParser(description='A script to generate the content of test files for use with the socat command.')
47 # Positional arguments
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).')
52 # Optional arguments
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()
62 
63 # Parse the size argument
64 suffixFileName = ""
65 match = re.search("^[0-9]", args.size)
66 if match:
67  charList = re.findall("[0-9]", args.size)
68  strInt = ""
69  for e in charList:
70  strInt += e
71  suffixFileName += strInt
72  reqSize = int(strInt)
73  if len(args.size) > len(charList):
74  if args.size[len(charList)] == 'K':
75  reqSize = reqSize*1024
76  suffixFileName += 'K'
77  elif args.size[len(charList)] == 'M':
78  reqSize = reqSize*1024*1024
79  suffixFileName += 'M'
80  elif args.size[len(charList)] == 'G':
81  reqSize = reqSize*1024*1024*1024
82  suffixFileName += 'G'
83  else:
84  print("Unknown or un-supported prefix symbol '%c'.\n" % args.size[len(charList)])
85  exit(1)
86 else:
87  print("ERROR: The file size must start with a digit!\n")
88  exit(1)
89 
90 if reqSize % 8:
91  print("\nERROR: The size must be a multiple of 8 bytes.")
92  exit(1)
93 
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")
96  exit(1)
97 
98 # STEP-2: Generate filename and check if file already exists
99 # -----------------------------------------------------------------------------
100 fileName = ""
101 if args.file == "":
102  if args.increment:
103  fileName = "inc_" + suffixFileName
104  if args.decrement:
105  fileName = "dec_" + suffixFileName
106  if args.random:
107  fileName = "rnd_" + suffixFileName
108 else:
109  fileName = args.file
110 
111 if os.path.exists(fileName):
112  print("\nWARNING: File already exist.")
113  answer = input("\tOverwrite (y/n): ")
114  if answer != 'y':
115  exit(0)
116  else:
117  os.remove(fileName)
118 
119 # STEP-3: Open file and generate content
120 # -----------------------------------------------------------------------------
121 outFile = open(fileName, 'w')
122 
123 if args.increment:
124  strStream = ""
125  size = int(reqSize / 8)
126  for x in range(0, size):
127  swapStr = ""
128  strTmp = "{:08d}".format(x)
129  # Swap the generated 8 bytes
130  swapStr += strTmp[7]
131  swapStr += strTmp[6]
132  swapStr += strTmp[5]
133  swapStr += strTmp[4]
134  swapStr += strTmp[3]
135  swapStr += strTmp[2]
136  swapStr += strTmp[1]
137  swapStr += strTmp[0]
138  outFile.write(swapStr)
139  if size < 1024:
140  strStream += strTmp
141  strStream += '\n'
142  if size < 1024:
143  print("STREAM=%s" % strStream)
144 elif args.decrement:
145  size = int(reqSize / 8)
146  strStream = ""
147  for x in range(size-1, 0, -1):
148  swapStr = ""
149  strTmp = "{:08d}".format(x)
150  # Swap the generated 8 bytes
151  swapStr += strTmp[7]
152  swapStr += strTmp[6]
153  swapStr += strTmp[5]
154  swapStr += strTmp[4]
155  swapStr += strTmp[3]
156  swapStr += strTmp[2]
157  swapStr += strTmp[1]
158  swapStr += strTmp[0]
159  outFile.write(swapStr)
160  if size < 1024:
161  strStream += strTmp
162  strStream += '\n'
163  if size < 1024:
164  print("STREAM=%s" % strStream)
165 else:
166  size = int(reqSize / 2)
167  strStream = ""
168  for x in range(0, size):
169  y = random.randint(0, 255)
170  outFile.write("{:02x}".format(y))
171 
172 outFile.close()
173 
174 print("\nINFO: The following file was created:")
175 os.system("ls -lh " + fileName)
string input
Definition: test.py:9