Mercurial > mozilla > hg > licenser
changeset 6:0a01ed263c06
add ability to interpolate a single file; should move to makeitso
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 13 Dec 2010 13:26:31 -0800 |
parents | f7d485cedc80 |
children | dce6a9351c1e |
files | licenser/licenses.py licenser/main.py |
diffstat | 2 files changed, 36 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- 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): """
--- 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()