Back to index

moin  1.9.0~rc2
mk_POTFILES.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import os
00004 from sys import argv
00005 
00006 # Define the starting directory.
00007 
00008 startdir = ""
00009 
00010 if len(argv) == 2:
00011     startdir = os.path.join(argv[1])
00012 else:
00013     startdir = os.path.join("..") # MoinMoin
00014 
00015 # Define a blacklist.
00016 
00017 blacklist = ["_tests",
00018              os.path.join("script", "old"),
00019              "support",
00020              os.path.join("filter", "EXIF.py"),
00021              os.path.join("web", "static", "htdocs"),
00022             ]
00023 
00024 # Define an output file for the filenames.
00025 
00026 outname_in = "POTFILES.in"
00027 outname_final = "POTFILES"
00028 
00029 # Functions.
00030 
00031 def get_files((files, prefix, blacklist), d, names):
00032 
00033     """
00034     Store pathnames in 'files', removing 'prefix', excluding those mentioned
00035     in the 'blacklist', building such pathnames from the directory 'd' and
00036     the given 'names'.
00037     """
00038 
00039     for name in names:
00040         if name.endswith(".py"):
00041             path = os.path.join(d, name)
00042 
00043             # Strip the prefix.
00044             if path.startswith(prefix):
00045                 path = path[len(prefix):]
00046 
00047             # Test for exact blacklist matches.
00048             if path in blacklist:
00049                 continue
00050 
00051             # Test for directory blacklist matches.
00052             found = 0
00053             for blackitem in blacklist:
00054                 if path.startswith(blackitem):
00055                     found = 1
00056                     break
00057 
00058             if not found:
00059                 files.append(path)
00060 
00061 def find_files(startdir, blacklist):
00062     "Find files under 'startdir' excluding those in the 'blacklist'."
00063 
00064     # Calculate the prefix from the start directory.
00065     prefix = os.path.join(startdir, "")
00066 
00067     # Start with an empty list of files.
00068 
00069     files = []
00070     os.path.walk(startdir, get_files, (files, prefix, blacklist))
00071     return files
00072 
00073 if __name__ == "__main__":
00074 
00075     # Find those files using the module defaults.
00076     files = find_files(startdir, blacklist)
00077 
00078     # Write the names out.
00079     outfile = open(outname_in, "w")
00080     try:
00081         for file in files:
00082             outfile.write(file + "\n")
00083     finally:
00084         outfile.close()
00085 
00086     # Write the processed list out, ready for other purposes.
00087     outfile = open(outname_final, "w")
00088     outfile.write("POTFILES = \\\n")
00089     try:
00090         for file in files[:-1]:
00091             outfile.write("\t" + os.path.join(startdir, file) + " \\\n")
00092         if files[-1]:
00093             file = files[-1]
00094             outfile.write("\t" + os.path.join(startdir, file) + "\n")
00095     finally:
00096         outfile.close()
00097 
00098 # vim: tabstop=4 expandtab shiftwidth=4