File: //usr/local/aegis/PythonLoader/third_party/aegis_checker/common/common_func.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
###############################################################
#
# common function
# Version : 1.0.0.1
#
###############################################################
import glob
import logging
import shutil
import sys, os, urllib, re, zipfile
import time
import hashlib
import traceback
def calc_file_sha1(file_path):
with open(file_path, 'rb') as f:
sha1obj = hashlib.sha1()
sha1obj.update(f.read())
return sha1obj.hexdigest()
def print_dict(dict_data, sort=False):
key_list = dict_data.keys()
if sort:
key_list.sort()
for key in key_list:
print key, ":", dict_data[key]
def print_list(list_data, sort=False):
if sort:
list_data.sort()
for item in list_data:
print item
def print_class(obj):
d = obj.__dict__
regular = re.compile("__.*__")
for var in d:
match_obj = regular.search(var)
if match_obj is None:
print "object.%s = %s" % (var, d[var])
# delete symbols in string except space
# if delete a symbol, it will replace with a space for it
# if %del_num% is True, it will delete numbers in string too
def delete_symbol(string, del_num=True):
ret = ""
last_is_symbol = False
if del_num:
for char in string:
if char.isalpha() or (del_num and char.isdigit()):
ret += char
last_is_symbol = False
else:
if not last_is_symbol:
last_is_symbol = True
ret += ' '
return ret
def is_hex_string(string):
for char in string:
num = ord(char)
if ord('9') >= num >= ord('0'):
continue
if ord('f') >= num >= ord('a'):
continue
if ord('F') >= num >= ord('A'):
continue
return False
return True
def find_no_case(string, substr, begin=0):
string = string.lower()
substr = substr.lower()
return string.find(substr, begin)
def get_domain(url):
proto, rest = urllib.splittype(url)
res, rest = urllib.splithost(rest)
return res
def collect_info_in_file(file_path, regular, index=0, hit_once=True, max_line=0xFFFFFFFF):
"""
match the regular in file, and return the %index% group(begin from 1 which is in "()" at regular),
if %hit_once% is True, only match one at most
if %index% is -1, return match groups, user must get the value by self
"""
info_list = []
if not os.path.exists(file_path):
print(file_path, "is not exits")
return info_list
with open(file_path) as fo:
i = 0
reg = re.compile(regular, re.I)
for line in fo:
match_obj = reg.match(line)
if match_obj:
if index < 0:
info_list.append(match_obj.groups())
else:
info_list.append(match_obj.group(index))
if hit_once:
break
i += 1
if i > max_line:
break
return info_list
def decompress_zip(zip_path, decompress_file_keyword, output=None):
if not os.path.exists(zip_path):
print zip_path, "is not exists"
return
if not output:
output = os.path.dirname(zip_path)
with zipfile.ZipFile(zip_path) as zipf:
name_list = zipf.namelist()
for file_name in name_list:
if -1 == file_name.find(decompress_file_keyword):
continue
file_data = zipf.read(file_name, "virus")
save_path = os.path.join(output, os.path.basename(file_name))
with open(save_path, "wb") as f:
f.write(file_data)
return save_path
# just check the first char "MZ"
def is_pe(file_path):
with open(file_path) as f:
data = f.read(2)
if str(data) == "MZ":
return True
return False
def get_items_from_file(file_path):
with open(file_path) as f:
return [item.strip() for item in f]
class PrintCostTime:
"""usage : print_cost_time = PrintCostTime()"""
def __init__(self):
self.__start = time.time()
def __del__(self):
print "Cost time :", round(time.time() - self.__start, 3), "s"
def get_yes_no(prompt="Please input yes or no : "):
input_str = raw_input(prompt)
return input_str and ('y' == input_str[0] or 'Y' == input_str[0])
def find_by_glob(pattern):
"""
:param pattern: glob search pattern, if pattern contain '[' or ']', it will be replace as "[[]" or "[]]", so don't use wildcard '[' or ']'
:return: if find, return the first path return by glob.glob(), others, return None
"""
format_pattern = ""
for char in pattern:
if char in "[]":
format_pattern += "[" + char + "]"
else:
format_pattern += char
find = glob.glob(format_pattern)
if find:
return find[0]
def clear_dir(dir_path):
if not os.path.exists(dir_path):
os.mkdir(dir_path)
sub_dirs = os.listdir(dir_path)
for sub_name in sub_dirs:
sub_path = os.path.join(dir_path, sub_name)
try:
if os.path.isdir(sub_path):
shutil.rmtree(sub_path, True)
elif os.path.isfile(sub_path):
os.remove(sub_path)
except:
logging.exception("catch exception when clear dir %s" % dir_path)
##################### start #####################
if __name__ == '__main__':
print_time = PrintCostTime()
if get_yes_no():
print "input yes"
else:
print "input no"