[Tools/dev] Check if *.py is running on shell
authorJihoon Lee <jhoon.it.lee@samsung.com>
Thu, 2 Apr 2020 01:56:54 +0000 (10:56 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Mon, 6 Apr 2020 05:00:13 +0000 (14:00 +0900)
Add `if __name__ == '__main__'` to check if *.py is running on
shell to prevent unwanted crash if users happen to import those files
 to customize tools.

Signed-off-by: Jihoon Lee <jhoon.it.lee@samsung.com>
tools/development/count_test_cases.py
tools/development/nnstreamerCodeGenCustomFilter.py

index 88ef224..9eec259 100644 (file)
@@ -51,12 +51,12 @@ def readSSAT(filename):
         print("No SSAT results.")
     return (0, 0, 0, 0, 0)
 
-def main():
+if __name__ == '__main__':
     if len(sys.argv) != 3:
         print("Usage:")
         print(" $ "+sys.argv[0]+" <gtest xml path> <ssat summary path>")
         print("")
-        return 1
+        sys.exit(1)
 
     tg = 0
     pg = 0
@@ -84,6 +84,5 @@ def main():
     print("  Passed: " + str(p) + " / Failed: " + str(f) + " / Ignored: " + str(i) + " | Positive: " + str(t - n) + " / Negative: " + str(n))
     print("Grand Total: " + str(pg + t) + " cases (negatives : " + str(ng + n) + ")")
     print("  Passed: " + str(pg+p) + " / Failed: " + str(fg + f) + " / Ignored: " + str(ig + i))
-    return 0
+    sys.exit(0)
 
-main()
index 5c56509..76656bc 100644 (file)
@@ -411,56 +411,57 @@ shared_library('{fname}',
 """
 
 
-## This is for debugging. To be removed.
-today = date.today()
-
-## 1. Ask for name.
-name = getinput('Please enter the name of the nnstreamer custom filter: ')
-sname = ''.join(re.findall(r"([a-zA-Z0-9_]+)", name))
-def_fname = ''.join(re.findall(r"([a-zA-Z0-9_]+)", name))
-## @todo @warning We may require prefix for all custom filter in later versions.
-## 2. Ask/Check for fname (file & official custom filter name)
-print('Please enter the custom filter name registered to tensor_filter.')
-fname = getinput('Or press enter without name if ['+def_fname+'] is ok: ')
-fname = ''.join(re.findall(r"([a-zA-Z0-9_]+)", fname))
-if len(fname) < 1:
-  fname = def_fname
-
-result = common_head
-
-## 3. Ask for options (dimension configuration modes)
-while 1:
-  option = getinput('Are dimensions of input/output tensors fixed? (yes/no):')
-  option = option.lower()
-  if option == 'y' or option == 'yes':
-    result += dim_fixed
-    break
-  if option == 'n' or option == 'no':
-    result += dim_variable
-    break
-  print("Please enter yes/y or no/n")
-
-## 4. Ask for options (memory allocation modes)
-while 1:
-  option = getinput('Are you going to allocate output buffer in your code? (yes/no):')
-  option = option.lower()
-  if option == 'y' or option == 'yes':
-    result += invoke_allocate
-    break
-  if option == 'n' or option == 'no':
-    result += invoke_no_allocate
-    break
-  print("Please enter yes/y or no/n")
-
-## 5. Generate .C file
-result += common_tail
-ccode = result.format(fname=fname, name=name, sname=sname, today=today)
-cfile = open(fname+".c", "w")
-cfile.write(ccode)
-cfile.close()
-
-## 6. Generate .meson file
-mesoncode = meson_script.format(fname=fname, name=name, sname=sname, today=today)
-mesonfile = open("meson.build", "w")
-mesonfile.write(mesoncode)
-mesonfile.close()
+if __name__ == '__main__':
+  ## This is for debugging. To be removed.
+  today = date.today()
+
+  ## 1. Ask for name.
+  name = getinput('Please enter the name of the nnstreamer custom filter: ')
+  sname = ''.join(re.findall(r"([a-zA-Z0-9_]+)", name))
+  def_fname = ''.join(re.findall(r"([a-zA-Z0-9_]+)", name))
+  ## @todo @warning We may require prefix for all custom filter in later versions.
+  ## 2. Ask/Check for fname (file & official custom filter name)
+  print('Please enter the custom filter name registered to tensor_filter.')
+  fname = getinput('Or press enter without name if ['+def_fname+'] is ok: ')
+  fname = ''.join(re.findall(r"([a-zA-Z0-9_]+)", fname))
+  if len(fname) < 1:
+    fname = def_fname
+
+  result = common_head
+
+  ## 3. Ask for options (dimension configuration modes)
+  while 1:
+    option = getinput('Are dimensions of input/output tensors fixed? (yes/no):')
+    option = option.lower()
+    if option == 'y' or option == 'yes':
+      result += dim_fixed
+      break
+    if option == 'n' or option == 'no':
+      result += dim_variable
+      break
+    print("Please enter yes/y or no/n")
+
+  ## 4. Ask for options (memory allocation modes)
+  while 1:
+    option = getinput('Are you going to allocate output buffer in your code? (yes/no):')
+    option = option.lower()
+    if option == 'y' or option == 'yes':
+      result += invoke_allocate
+      break
+    if option == 'n' or option == 'no':
+      result += invoke_no_allocate
+      break
+    print("Please enter yes/y or no/n")
+
+  ## 5. Generate .C file
+  result += common_tail
+  ccode = result.format(fname=fname, name=name, sname=sname, today=today)
+  cfile = open(fname+".c", "w")
+  cfile.write(ccode)
+  cfile.close()
+
+  ## 6. Generate .meson file
+  mesoncode = meson_script.format(fname=fname, name=name, sname=sname, today=today)
+  mesonfile = open("meson.build", "w")
+  mesonfile.write(mesoncode)
+  mesonfile.close()