Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 import re
00041 import os
00042 import sys
00043
00044 SUFFIX_JAVASCRIPT = ".js"
00045
00046 RE_REQUIRE = "@requires?:?\s+(\S*)\s*\n"
00047
00048 class MissingImport(Exception):
00049 """Exception raised when a listed import is not found in the lib."""
00050
00051 class SourceFile:
00052 """
00053 Represents a Javascript source code file.
00054 """
00055
00056 def __init__(self, filepath, source, cfgExclude):
00057 """
00058 """
00059 self.filepath = filepath
00060 self.source = source
00061
00062 self.excludedFiles = []
00063 self.requiredFiles = []
00064 auxReq = re.findall(RE_REQUIRE, self.source)
00065 for filename in auxReq:
00066 if undesired(filename, cfgExclude):
00067 self.excludedFiles.append(filename)
00068 else:
00069 self.requiredFiles.append(filename)
00070
00071 self.requiredBy = []
00072
00073
00074 def _getRequirements(self):
00075 """
00076 Extracts the dependencies specified in the source code and returns
00077 a list of them.
00078 """
00079 return self.requiredFiles
00080
00081 requires = property(fget=_getRequirements, doc="")
00082
00083
00084
00085 def usage(filename):
00086 """
00087 Displays a usage message.
00088 """
00089 print "%s [-c <config file>] <output.js> <directory> [...]" % filename
00090
00091
00092 class Config:
00093 """
00094 Represents a parsed configuration file.
00095
00096 A configuration file should be of the following form:
00097
00098 [first]
00099 3rd/prototype.js
00100 core/application.js
00101 core/params.js
00102 # A comment
00103
00104 [last]
00105 core/api.js # Another comment
00106
00107 [exclude]
00108 3rd/logger.js
00109 exclude/this/dir
00110
00111 All headings are required.
00112
00113 The files listed in the `first` section will be forced to load
00114 *before* all other files (in the order listed). The files in `last`
00115 section will be forced to load *after* all the other files (in the
00116 order listed).
00117
00118 The files list in the `exclude` section will not be imported.
00119
00120 Any text appearing after a # symbol indicates a comment.
00121
00122 """
00123
00124 def __init__(self, filename):
00125 """
00126 Parses the content of the named file and stores the values.
00127 """
00128 lines = [re.sub("#.*?$", "", line).strip()
00129 for line in open(filename)
00130 if line.strip() and not line.strip().startswith("#")]
00131
00132 self.forceFirst = lines[lines.index("[first]") + 1:lines.index("[last]")]
00133
00134 self.forceLast = lines[lines.index("[last]") + 1:lines.index("[include]")]
00135 self.include = lines[lines.index("[include]") + 1:lines.index("[exclude]")]
00136 self.exclude = lines[lines.index("[exclude]") + 1:]
00137
00138 def undesired(filepath, excludes):
00139
00140 exclude = filepath in excludes
00141 if not exclude:
00142
00143 for excludepath in excludes:
00144 if not excludepath.endswith("/"):
00145 excludepath += "/"
00146 if filepath.startswith(excludepath):
00147 exclude = True
00148 break
00149 return exclude
00150
00151
00152 def getNames (sourceDirectory, configFile = None):
00153 return run(sourceDirectory, None, configFile, True)
00154
00155
00156 def run (sourceDirectory, outputFilename = None, configFile = None,
00157 returnAsListOfNames = False):
00158 cfg = None
00159 if configFile:
00160 cfg = Config(configFile)
00161
00162 allFiles = []
00163
00164
00165 for root, dirs, files in os.walk(sourceDirectory):
00166 for filename in files:
00167 if filename.endswith(SUFFIX_JAVASCRIPT) and not filename.startswith("."):
00168 filepath = os.path.join(root, filename)[len(sourceDirectory)+1:]
00169 filepath = filepath.replace("\\", "/")
00170 if cfg and cfg.include:
00171 if filepath in cfg.include or filepath in cfg.forceFirst:
00172 allFiles.append(filepath)
00173 elif (not cfg) or (not undesired(filepath, cfg.exclude)):
00174 allFiles.append(filepath)
00175
00176
00177 HEADER = "/* " + "=" * 70 + "\n %s\n" + " " + "=" * 70 + " */\n\n"
00178
00179 files = {}
00180
00181
00182
00183 for filepath in allFiles:
00184 print "Importing: %s" % filepath
00185 fullpath = os.path.join(sourceDirectory, filepath).strip()
00186 content = open(fullpath, "U").read() # TODO: Ensure end of line @ EOF?
00187 files[filepath] = SourceFile(filepath, content, cfg.exclude)
00188
00189 print
00190
00191 from toposort import toposort
00192
00193 complete = False
00194 resolution_pass = 1
00195
00196 while not complete:
00197 complete = True
00198
00199
00200 print "Resolution pass %s... " % resolution_pass
00201 resolution_pass += 1
00202
00203 for filepath, info in files.items():
00204 for path in info.requires:
00205 if not files.has_key(path):
00206 complete = False
00207 fullpath = os.path.join(sourceDirectory, path).strip()
00208 if os.path.exists(fullpath):
00209 print "Importing: %s" % path
00210 content = open(fullpath, "U").read() # TODO: Ensure end of line @ EOF?
00211 files[path] = SourceFile(path, content, cfg.exclude)
00212 else:
00213 raise MissingImport("File '%s' not found (required by '%s')." % (path, filepath))
00214
00215
00216 dependencies = {}
00217 for filepath, info in files.items():
00218 dependencies[filepath] = info.requires
00219
00220 print "Sorting..."
00221 order = toposort(dependencies)
00222
00223
00224 if cfg:
00225 print "Re-ordering files..."
00226 order = cfg.forceFirst + [item
00227 for item in order
00228 if ((item not in cfg.forceFirst) and
00229 (item not in cfg.forceLast))] + cfg.forceLast
00230
00231 print
00232
00233 result = []
00234
00235
00236 if returnAsListOfNames:
00237 for fp in order:
00238 fName = os.path.normpath(os.path.join(sourceDirectory, fp)).replace("\\","/")
00239 print "Append: ", fName
00240 f = files[fp]
00241 for fExclude in f.excludedFiles:
00242 print " Required file \"%s\" is excluded." % fExclude
00243 result.append(fName)
00244 print "\nTotal files: %d " % len(result)
00245 return result
00246
00247
00248 for fp in order:
00249 f = files[fp]
00250 print "Exporting: ", f.filepath
00251 for fExclude in f.excludedFiles:
00252 print " Required file \"%s\" is excluded." % fExclude
00253 result.append(HEADER % f.filepath)
00254 source = f.source
00255 result.append(source)
00256 if not source.endswith("\n"):
00257 result.append("\n")
00258
00259 print "\nTotal files merged: %d " % len(files)
00260
00261 if outputFilename:
00262 print "\nGenerating: %s" % (outputFilename)
00263 open(outputFilename, "w").write("".join(result))
00264 return "".join(result)
00265
00266 if __name__ == "__main__":
00267 import getopt
00268
00269 options, args = getopt.getopt(sys.argv[1:], "-c:")
00270
00271 try:
00272 outputFilename = args[0]
00273 except IndexError:
00274 usage(sys.argv[0])
00275 raise SystemExit
00276 else:
00277 sourceDirectory = args[1]
00278 if not sourceDirectory:
00279 usage(sys.argv[0])
00280 raise SystemExit
00281
00282 configFile = None
00283 if options and options[0][0] == "-c":
00284 configFile = options[0][1]
00285 print "Parsing configuration file: %s" % filename
00286
00287 run( sourceDirectory, outputFilename, configFile )