Skip to content

Instantly share code, notes, and snippets.

@kokukuma
Created April 8, 2012 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kokukuma/2335403 to your computer and use it in GitHub Desktop.
Save kokukuma/2335403 to your computer and use it in GitHub Desktop.
import os
import csv
import re
#from subprocess import *
from bzrlib.branch import Branch
from bzrlib.workingtree import WorkingTree
from bzrlib.commands import Command,register_command
#from itertools import chain
CTAGS_PATH = "./tags"
IGNORE_LIST = [
"ApacheLog",
]
SERVER_LIST = [
["karino","karino"],
["karino2","karino2"],
["karino3","karino3"],
]
class CTAGS(object):
CTAGS_PATH = ""
CTREADER = None
CTDICT = {}
LATEST_ASCII_HEAD = ""
def __init__(self, path):
self.CTAGS_PATH = path
#self.CTFILE = self.read_ctags_file()
self.CTREADER = csv.reader(self.get_file_object(self.CTAGS_PATH),delimiter='\t')
return
def get_file_object(self,path):
filename = os.path.expanduser(path)
file_object = open(filename, 'rb')
return file_object
def get_dependence(self, target_file):
# get function
# filename = os.path.expanduser(target_file)
# file_object = open(filename, 'rb')
file_object = self.get_file_object(target_file)
# get function
func_list = self.get_class(file_object )
# get involved function and file path list
res = []
dep_files = []
print
print target_file
for func_name in func_list:
#print func_name
# check ignore list
if func_name in IGNORE_LIST:
continue
# print
print '|'
print '+--', func_name
# search path
dep_files = self.get_ctags_file_path(func_name)
if dep_files != None:
res += dep_files
# print
print '| ',dep_files
return res
def get_ctags_file_path(self, func_name):
# chk target file
for row in self.CTREADER:
# read one
tmp = []
if row[0] in self.CTDICT.keys():
tmp = self.CTDICT[row[0]]
tmp.append(row[1])
self.CTDICT[row[0]] = tmp
self.LATEST_ASCII_HEAD = int(hex(ord(row[0][0])),16)
# search
if func_name in self.CTDICT.keys():
return self.CTDICT[func_name]
# check ascii head
func_name_ascii_head = int(hex(ord(func_name[0])),16)
if self.LATEST_ASCII_HEAD > func_name_ascii_head:
return
return
def get_class(self, f):
func_list = []
for line in f:
# get by ::
func = self.re_search(line, r'([A-Z]\w+)::')
if func != None and not(func in func_list):
func_list.append(func)
# get by new
func = self.re_search(line, r'new ([A-Z]\w+)')
if func != None and not(func in func_list):
func_list.append(func)
return func_list
def re_search(self, line, pattern):
rec = re.compile(pattern)
try:
mo = rec.search(line)
except IndexError:
return None
return mo.group(1) if mo else None
class cmd_rel(Command):
takes_args = ["revision?"]
def run(self, revision=None):
# difine server name
self.target_server_num = self.user_input('input number > ')
# get changes
changes = {'add':[],'mod':[]}
if revision==None:
changes = self.get_changes_from_status()
else:
# make changes
changes = self.get_changes_from_log(revision)
changes_list = changes['add'] + changes['mod']
# get dependence files
target_files = []
ct = CTAGS(CTAGS_PATH)
print
print 'dependence_file'
for f in changes_list:
target_files += ct.get_dependence(f)
print
target_files = target_files + changes['add'] + changes['mod']
target_files = sorted(set(target_files), key=target_files.index)
# print makuo
print '-------------------------------'
print
if target_files!=[]:
self.print_makuo(target_files)
print
if target_files==[]:
print "There is no change files"
print
print '-------------------------------'
print
def user_input(self, prompt):
for i in range(len(SERVER_LIST)):
print i, " : ", SERVER_LIST[i][0], " ", SERVER_LIST[i][1]
num = raw_input(prompt)
if num=="" or num==None:
return 0
elif int(num) in range(len(SERVER_LIST)):
return int(num)
else:
return 0
""" print makuo function """
def print_makuo(self, files):
print "makuo -n \\"
for i in range(len(files)):
if i == len(files)-1:
print SERVER_LIST[self.target_server_num][1] + "/" + files[i]
else:
print SERVER_LIST[self.target_server_num][1] + "/" + files[i] + " \\"
return
""" get changes functions """
def get_changes_from_status(self):
# get workingtree
wt = WorkingTree.open_containing(os.getcwd())[0]
# get changes
changes = wt.changes_from(wt.basis_tree(),want_unversioned=True)
# result
result = {"add":[],"mod":[]}
for add in changes.added:
result['add'].append(add[0].encode('utf-8'))
for mod in changes.modified:
result['mod'].append(mod[0].encode('utf-8'))
return result
def get_changes_from_log(self, revision):
changes = {'add':[],'mod':[]}
for revno in revision.split(','):
changes_tmp = self.get_changes(revno)
for f in changes_tmp['add']:
changes['add'].append(f)
for f in changes_tmp['mod']:
if f in changes['add']+changes['mod']:
pass
else:
changes['mod'].append(f)
return changes
def get_changes(self, revno):
# get workingtree
branch = Branch.open_containing(os.getcwd())[0]
# get changes
target_id = branch.get_rev_id( int(revno) )
changes = branch.repository.get_revision_delta(target_id)
# result
result = {"add":[],"mod":[]}
for add in changes.added:
result['add'].append(add[0].encode('utf-8'))
for mod in changes.modified:
result['mod'].append(mod[0].encode('utf-8'))
return result
register_command(cmd_rel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment