import os
import sys def find_file(root_dir, type):
dirs_pool = [root_dir]
dest_pool = [] def scan_dir(directory):
entries = os.walk(directory)
for root, dirs, files in entries:
dirs_pool.extend([os.path.join(root, dir_entry) for dir_entry in dirs])
for file_entry in files:
if type in str(file_entry)[-len(type):]:
dest_pool.append(''.join(os.path.join(root, file_entry))) while dirs_pool:
scan_dir(dirs_pool.pop())
return dest_pool def gen_ps(root_dir, type):
vim_cmd = 'vim -me -e -c ":hardcopy >%.ps" -c ":q" '
ps2pdf_cmd = 'ps2pdf {filename}.ps {filename}.pdf'
dests = find_file(root_dir, type)
if not dests:
return
print 'found these source code files:'
for dest in dests:
print dest
print 'begin generate ps files!'
for dest in dests:
command = ''.join([vim_cmd, dest])
print command
os.system(command)
for dest in dests:
command = ps2pdf_cmd.format(filename=dest)
print command
os.system(command) if __name__ == '__main__':
if len(sys.argv) == 3:
gen_ps(sys.argv[1], sys.argv[2])
else:
print 'usage: python code2pdf.py directory filetype\n' \
'such as: python code2pdf.py /home/bruce/python .py'