# HG changeset patch # User Jeff Hammel # Date 1292275591 28800 # Node ID 0a01ed263c06a58afc1f9fa93a555ef3ff7ed7ec # Parent f7d485cedc80a4a0b3074442a0079c1cbf30cc1e add ability to interpolate a single file; should move to makeitso diff -r f7d485cedc80 -r 0a01ed263c06 licenser/licenses.py --- a/licenser/licenses.py Mon May 10 12:28:59 2010 -0700 +++ b/licenser/licenses.py Mon Dec 13 13:26:31 2010 -0800 @@ -70,33 +70,37 @@ def pre(self, variables): """do anything that needs to be done before interpolation""" + def interpolate_file(self, _file, variables): + + # get the file lines + f = file(_file) + lines = [ i.rstrip() for i in f.readlines() ] + f.close() + f = file(_file, 'w') + + # print shebang if it exists + if lines[0].startswith('#!'): + shebang = lines.pop(0) + print >> f, shebang + print >> f + + # print the license + g = file(self.template) + for line in g.readlines(): + line = line.rstrip() + _template = Template(line) + print >> f, '# %s' % _template.substitute(**variables) + g.close() + + # print the rest of the file + for line in lines: + print >> f, line.rstrip() + f.close() + + def interpolate(self, directory, variables): for _file in self.files(directory): - - # get the file lines - f = file(_file) - lines = [ i.rstrip() for i in f.readlines() ] - f.close() - f = file(_file, 'w') - - # print shebang if it exists - if lines[0].startswith('#!'): - shebang = lines.pop(0) - print >> f, shebang - print >> f - - # print the license - g = file(self.template) - for line in g.readlines(): - line = line.rstrip() - _template = Template(line) - print >> f, '# %s' % _template.substitute(**variables) - g.close() - - # print the rest of the file - for line in lines: - print >> f, line.rstrip() - f.close() + self.interpolate_file(_file, variables) def isempty(self, path): """ diff -r f7d485cedc80 -r 0a01ed263c06 licenser/main.py --- a/licenser/main.py Mon May 10 12:28:59 2010 -0700 +++ b/licenser/main.py Mon Dec 13 13:26:31 2010 -0800 @@ -101,8 +101,13 @@ variables = license.obtain_variables() - for directory in args: - license.interpolate(directory, variables) + for arg in args: + if os.path.isdir(arg): + license.interpolate(arg, variables) + elif os.path.isfile(arg): + license.interpolate_file(arg, variables) + else: + raise IOError("File not found: '%s'" % arg) if __name__ == '__main__': main()