Compile a Python applet to calculate the keyword analysis of the test script

Source: Internet
Author: User

Compile a Python applet to calculate the keyword analysis of the test script

Generally, when the automated test project reaches a certain program, a lot of test code will naturally be written. If some basic functions and business functions need to be modified, it is necessary to find the places where the modified function has been referenced. Some ides support full-text search and reference search, while some are simple and may not, because the statistical functions and other requirements will be used in the future, a script is written. In addition to searching for referenced files in the full text under the same directory, you can also count the number of searched files. Multiple keywords can be searched at a time and can be classified by primary keywords.
# Encoding: UTF-8
Import OS
Import sys
Import re
 
Reload (sys)
Sys. setdefaultencoding ("UTF-8 ")
 
Short_exclude = [". svn", "sendbox"] # names of files and directories that are not checked
Long_exclude = [] # full path of the file and directory that does not contain the check
Extend_name = [". rb"] # specify the file suffix for check
Temp_key_words = [
{
"Key": "# Author :",
"Display": "author ",
"Times":-1,
"Match": "include ",
"Primary_key": True,
},
{
"Key": "# [summary]",
"Display": "number of completed use cases ",
"Times":-1,
"Match": "include ",
},
{
"Key": "File. expand_path ",
"Display": "stateful rows ",
"Times":-1,
"Ignore_case": True,
},
{
"Key": "def \ s + test _",
"Display": "Number of valid use cases ",
"Times":-1,
"Match": "regex ",
"Ignore_case": True,
},
{
"Key": "# def \ s + test _",
"Display": "Number of comment cases ",
"Times":-1,
"Match": "regex ",
"Ignore_case": True,
},
]
 
For kv in temp_key_words:
If not "key" in kv:
There is no [key] value in the list below raise! \ N % s "% kv
If not "key" in kv:
Raise "The following list does not contain the" display "value! \ N % s "% kv
Kv ['times '] = kv. get ('times',-1) # Check times are unlimited by default.
If kv. get ("ignore_case", True) = False: # case insensitive by default
Flag = 0
Else:
Flag = re. I
Kv ['pattern'] = re. compile (kv ['key'], flag)
If kv. get ("primary_key", False ):
Kv ['times '] = 1
Import copy
Key_words = []
 
Def deepcopy (objs ):
T_list = []
For obj in objs:
T_list.append (copy. copy (obj ))
Return t_list
 
Def loop_case (root_dir ):
T_sum = []
Print root_dir
Sub_gen = OS. listdir (root_dir)
For sub in sub_gen:
If sub in short_exclude: # not checking the file and directory range
Continue
Abs_path = OS. path. join (root_dir, sub)
If long_exclude:
Is_exclude = False
For exclude in long_exclude:
If exclude = abs_path [-len (exclude):]:
Is_exclude = True
Break
If is_exclude:
Continue
Print abs_path
If OS. path. isdir (abs_path ):
Print "dir"
T_sum.extend (loop_case (abs_path ))
Elif OS. path. isfile (abs_path ):
If not "." + abs_path.rsplit (".", 1) [1] in extend_name: # not in the suffix Check range
Continue
Print "file"
Global key_words
Key_words = deepcopy (temp_key_words)
T_sum.append (count_case (abs_path ))
Return t_sum
 
Def count_case (abs_path ):
T_dict = {}
With open (abs_path) as f:
For l in f:
L = l. strip ()
Match_rule (l)
Index = 0
Count_result = [0] * len (key_words)
For kv in key_words:
If 'Primary _ key' in kv:
T_dict ['Primary _ key'] = kv. get ('display ')
T_dict ['Primary _ key_value '] = kv. get ('Primary _ key_value', "None ")
Count_result [index] =-1-kv ['times ']
Index + = 1
T_dict ['match _ result'] = count_result
T_dict ['file _ path'] = abs_path
Return t_dict
 
Def match_rule (line ):
Primary_key = None
For kv in key_words:
Match = False
If kv ['times '] = 0: # Check times are full and no longer checked

Continue
If kv. get ('match', "") = "regex": # specifies the matching mode as regular
If kv ['pattern']. match (line): # Regular Expression matching successful
Match = True
Else: # default matching method: Include
If kv ['key'] in line: # contains the specified string
Match = True
If match:
If kv. get ('Primary _ key', False ):
Kv ['Primary _ key_value '] = line. split (kv ['key']) [1]. strip ()
# Kv ['Primary _ key'] = False
Kv ['times ']-= 1 # matching successful. Likewise, the number of remaining matches-1
Return primary_key
 
Def format_info (sum_list ):
Tip_list = []
P_k_dict = {}
For d in sum_list:
P_k = d ['Primary _ key_value ']
If p_k not in p_k_dict:
P_k_dict [p_k] = [0] * len (key_words)
Temp_list = []
M = d ['match _ result']
Temp_list.append ("file Name: % s \ n % s: % s \ n" % (d ['file _ path'], d ['Primary _ key'], d ['Primary _ key_value '])
For I in range (len (m )):
If 'Primary _ key' in key_words [I]:
Continue
Else:
T_s = str (m [I])
Temp_list.append ("% s: % s \ n" % (key_words [I] ["display"], t_s ))
P_k_dict [p_k] [I] + = m [I]
Tip_list.append ("". join (temp_list ))
P_k_dict [p_k] [0] + = 1
Tip_list.append ("======================== primary key statistics segmentation line ======== ===================================== ")
Total_dict = {}
For kv in key_words:
If 'Primary _ key' not in kv:
Total_dict [kv ['display'] = 0
Total_dict ['total files'] = 0
For k, v in p_k_dict.items ():
Temp_list = []
Temp_list.append ("primary key: % s \ n total number of files: % s \ n" % (k, v [0])
For I in range (1, len (v )):
Temp_list.append ("% s: % s \ n" % (key_words [I] ["display"], str (v [I])
Total_dict [key_words [I] ["display"] + = v [I]
Tip_list.append ("". join (temp_list ))
Total_dict ['total files'] + = v [0]
Tip_list.append ("===================================== all statistics segmentation line ======== ===================================== ")
Temp_list = []
For k, v in total_dict.items ():
Temp_list.append ("All % s: % s \ n" % (k, v ))
Tip_list.append ("". join (temp_list ))
Tip_msg = "\ n". join (tip_list)
Print tip_msg
Open (r "sum_case.log", "w"). write (tip_msg)
 
If _ name __= = "_ main __":
If len (sys. argv)> 1:
Root_list = sys. argv [1:]
Else:
Root_list = [OS. curdir]
Sum_list = []
For root_dir in root_list:
If OS. path. exists (root_dir) and OS. path. isdir (root_dir ):
Sum_list.extend (loop_case (root_dir ))
Format_info (sum_list)
Else:
Print "the specified root directory is invalid \ n % s" % root_dir
You can check the keyword, file type, and files and directories by setting the beginning of the configuration.
 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.