Android Notification 检测

当应用代码量级达到一定程度之后,我们就需要对一些常用的模块进行管控,如联网请求,通知栏的发送等。
最近,在完成一个OEM项目的时候,厂家要求在用户允许之后才能弹出通知栏,这个任务完成之后,引起我的反思,于是,把代码中的Notification 发送的地方整理了一下并且写了一个可以配置化的脚本,用来检查通知栏的发送情况。现共享这个脚本,供大家偷懒用。哈哈,博主还是很懒的一个人。

import os;
import re;
import json;
arr = [];

def  check(intput_str):
   reg = "\\.notify\\([\\w]+";
   result = re.search(reg,intput_str);
   if result is None:
      return False;
   else:
      return True;

def  walk_files(intput_path):
   print "walk files"
   if not os.path.exists(intput_path):
        print "input path not exist";
   else :
        tree =os.walk(intput_path,topdown = True);
        for parent,dirs,files in tree:
            for file in files:
                path = os.path.join(parent,file);
                read_file_content(path);
def  read_file_content(input_file):
      if not input_file.endswith("java"):
         return;
      print "check file is %s",input_file;
      if os.path.isdir(input_file):
         pass;
      else:
        txt = None;
        try:
          txt = open(input_file,"r").read();
        except IOError as e:
               print e
        if check(txt):
          arr.append(input_file);
        else:
          pass;

def read_ignore_config():
  json = open("config.json").read();
  return json;
def remove_ignore():

  config =read_ignore_config();
  config_json = json.loads(config);
  print config_json
  print "the ingnore is %s",config_json['ingnore']
  for ingnore in config_json['ingnore']:
    for file in arr:
        format = os.path.normcase(file).replace("\\","/")
        if format.find(ingnore)>0:
            print file
            print ingnore
            arr.remove(file);
            print "remove from arr";
            break;


if __name__ == '__main__':
   print "start to check notification";
   config = read_ignore_config();
   input_json = json.loads(config);
   for file in input_json["input"]:
       walk_files(file);
   print "end to check notification";
   remove_ignore();
   print arr;
   if len(arr) > 0:
      raise Exception("WARNING FRO NOTICATION");

原谅我不是写Python的,写的脚本不是很漂亮,求饶啊,唯一一个需要注意点就是一定要在PyCharm 下格式化下代码,防止有问题哈!
还有一个配置文件,我写出来配置的格式哈,自己搞,还是很简单的。
这个玩意我把名字写死了,config.json,脚本和配置文件放在一个目录下就行

{
#配置输入的代码路径
"input":[
    ".././python"
],
#配置需要忽略的文件,总不能把自己也检测出来吧!
"ingnore": [
]
}

最后贴图一张,证明这个玩意是可行滴!
这里写图片描述

猜你喜欢

转载自blog.csdn.net/gezihau/article/details/53317848