#!/usr/bin/env python
import copy, getopt, re, os, sys, string, signal, stat, types, platform
from os import system, popen, path, makedirs, listdir
from os.path import basename, dirname, isdir, isfile
from time import sleep, localtime, strftime
from zipfile import ZipFile
def get_userhome():
return path.expanduser('~')
def mkdir_if_absent(dir_):
if not isdir(dir_):
makedirs(dir_)
def get_file_path():
pathLines=[]
path_file = path.join(os.path.curdir, 'path.txt')
if isfile(path_file):
file_object=open(path_file)
try:
pathLines=file_object.readlines()
finally:
file_object.close()
else:
curdir = os.getcwd()
print 'file %s not exists!'%(curdir+path_file)
return pathLines
def list_all_jars():
allfiles = []
pathList=get_file_path()
if len(pathList)>0:
print "length:%s"%len(pathList)
for path in pathList:
pathstring=string.rstrip(path)
if isdir(pathstring):
for root, dirs, files in os.walk(pathstring):
for name in files:
if name.endswith('.jar') or name.endswith('.zip'):
allfiles.append(os.path.join(root,name))
#print os.path.join(root,name)
else:
print "dictory %s is not exits!"%pathstring
else:
curdir = os.getcwd()
for root, dirs, files in os.walk(curdir):
for name in files:
if name.endswith('.jar') or name.endswith('.zip'):
print os.path.join(root,name)
allfiles.append(os.path.join(root,name))
return allfiles
class JarSummaryBase:
def __init__(self, summary_file):
self.summary_file = summary_file
self.sums = {}
self.read_summaries()
def read_summaries(self):
sum_file = self.summary_file
if not isfile(sum_file):
return
fp = open(sum_file, 'r')
lines = fp.readlines()
fp.close()
for line in lines:
if not line:
continue
if line.find(',') < 0:
continue
mtime, jar = string.split(line, ',', 1)
if not jar:
continue
jar = jar.strip()
self.sums[jar] = int(mtime)
def save_summaries(self):
fp = open(self.summary_file, 'w')
for jar, mtime in self.sums.iteritems():
fp.write('%s,%s' % (str(mtime), jar))
fp.write(os.linesep)
fp.flush()
fp.close()
def get_mtime(self, jar):
return os.stat(jar)[8]
def is_modified(self, jar):
if not self.sums.has_key(jar):
return True
mtime = self.sums[jar]
return self.get_mtime(jar) > mtime
class ClassLocator(JarSummaryBase):
def __init__(self):
user_home = get_userhome()
self.cache_dir = path.join(user_home, '.lookup', 'classcaches')
mkdir_if_absent(self.cache_dir)
JarSummaryBase.__init__(self, path.join(self.cache_dir, 'summary'))
self.jars = list_all_jars()
def get_index_file(self, jar):
#return path.join(self.cache_dir, jar.replace(path.os.sep, '_') + '.idx')
#return path.join(self.cache_dir, jar.replace("/","\\") + '.idx')
return path.join(self.cache_dir, jar + '.idx')
def get_jar_content(self, jar):
jarfile = ZipFile(jar)
file_list = jarfile.namelist()
jarfile.close()
return file_list
def build_indices(self):
for jar in self.jars:
index_file = self.get_index_file(jar)
if path.exists(index_file) and os.stat(index_file)[6] > 0 and not self.is_modified(jar):
continue
print strftime('[%Y-%m-%d %H:%M]', localtime()),
print 'building index of %s' % jar
classes = self.get_jar_content(jar)
fp = open(index_file, 'w')
for cls in classes:
fp.write(cls + os.linesep)
fp.close()
self.sums[jar] = self.get_mtime(jar)
self.save_summaries()
def find(self, keyword):
self.build_indices()
key = re.compile(keyword, re.I)
for jar in self.jars:
index_file = self.get_index_file(jar)
if not isfile(index_file):
continue
matches = []
fp = open(index_file, 'r')
while True:
line = fp.readline()
if not line:
break
line = line.strip()
if not line:
continue
if key.search(line) is not None:
matches.append(line)
fp.close()
if len(matches) > 0:
print jar + ':'
for line in matches:
print ' ' + line
print
os.remove(index_file);
def check_python_version():
version = string.split(string.split(sys.version)[0], '.')
if map(int, version[:2]) < [2, 4]:
print 'This script require Python "2.4" or above, current version is', sys.version
sys.exit(1)
def show_version():
print 'lookup v0.1'
def show_usage():
usage_message = '''
Usage: lookup class keyword
class - find classes
'''
print usage_message
sys.exit(0)
if __name__ == '__main__':
check_python_version()
opts, args = getopt.getopt(sys.argv[1:], "v:c",["class=",'help'])
argc = len(args)
if argc == 0:
show_usage()
command = args[0]
if command == 'class':
if argc < 2:
show_usage()
locator = ClassLocator()
locator.find(args[1])
elif command == 'version':
show_version()
else:
show_usage()
qq_16687781
- 粉丝: 0
- 资源: 1
最新资源
- 微电网(两台)主从控制孤岛-并网平滑切的分析 分析了: 1.孤岛下VF控制 2.并网下PQ控制 3.孤岛下主从控制 4.孤岛到并网的平滑切控制 5.除模型外还对分布式发电与主动配电网一些常见问题做了
- 第四组二手产品.zip
- MVIMG_20241222_194113.jpg
- 基于小程序的在线疫苗预约小程序源代码(java+小程序+mysql+LW).zip
- 基于小程序的岳阳市美术馆预约平台源代码(java+小程序+mysql+LW).zip
- 基于小程序的音乐播放器小程序源代码(java+小程序+mysql+LW).zip
- 多功能知识付费源码下载实现流量互导多渠道变现+搭建教程
- 3. Kafka入门-安装与基本命令
- 基于小程序的养老院管理系统源代码(java+小程序+mysql+LW).zip
- 基于小程序的新冠抗原自测平台小程序源代码(java+小程序+mysql+LW).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈