# HG changeset patch # User Jeff Hammel # Date 1322168312 28800 # Node ID cf920f85fb98db93ddb5ea0ea4951da79af055b4 # Parent ed9d64d1fd3245b6e1456c0f115dbbfe2c1c2259 more stubbing diff -r ed9d64d1fd32 -r cf920f85fb98 licenser/licenses.py --- a/licenser/licenses.py Wed Nov 23 14:29:17 2011 -0800 +++ b/licenser/licenses.py Thu Nov 24 12:58:32 2011 -0800 @@ -48,74 +48,83 @@ def hash_comments(): pass -type = {'py': hash_comments, - 'sh': hash_comments, - 'c': asterisk_comments} +suffixes = {'py': hash_comments, + 'sh': hash_comments, + 'c': asterisk_comments} class License(object): - """Abstract base class for a license""" + """Abstract base class for a license""" - variables = [] # required variables + variables = [] # required variables - def __init__(self): - if self.template and not os.path.isabs(self.template): - self.template = os.path.join(os.path.dirname(__file__), - 'licenses', - self.template) - assert os.path.exists(self.template) + def __init__(self): + if self.template: + if not os.path.isabs(self.template): + self.template = os.path.join(os.path.dirname(__file__), + 'licenses', + self.template) + assert os.path.exists(self.template) + + def license(self): + return file(self.template).read() - def license(self): - return file(self.template).read() + def print_license(self): + print self.license() - def print_license(self): - print self.license() + def token(self): + """a token indicative of the license, by default the first line""" + return self.license.splitlines()[0].strip() - def has_license(filename): - """does the file already have a license?""" - raise NotImplementedError + def has_license(self, filename): + """does the file already have a license?""" + token = self.token() + for line in file(filename).readlines(): + if token in line: + return True + return False - def __call__(self, directory, **kw): - variables = self.obtain_variables(**kw) - self.interpolate(directory, variables) + def __call__(self, directory, **kw): + variables = self.obtain_variables(**kw) + self.interpolate(directory, variables) - def obtain_variables(self, **kw): - for var in self.variables: - if var not in kw: - print 'Enter %s: ' % var, - kw[var] = raw_input() - self.pre(kw) - return kw + def obtain_variables(self, **kw): + for var in self.variables: + if var not in kw: + print 'Enter %s: ' % var, + kw[var] = raw_input() + self.pre(kw) + return kw def pre(self, variables): - """do anything that needs to be done before interpolation""" + """do anything that needs to be done with the variables 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() - - # open the file for writing - f = file(_file, 'w') + # get the file lines + f = file(_file) + lines = [ i.rstrip() for i in f.readlines() ] + f.close() + + # open the file for writing + f = file(_file, 'w') - # print shebang if it exists - if lines[0].startswith('#!'): - shebang = lines.pop(0) - print >> f, shebang - print >> f + # print shebang if it exists + if lines[0].startswith('#!'): + shebang = lines.pop(0) + print >> f, shebang + print >> f - # print the license - g = self.license() - for line in g.splitlines(): - line = line.rstrip() - _template = Template(line) - print >> f, '# %s' % _template.substitute(**variables) - g.close() + # print the license + g = self.license() + for line in g.splitlines(): + 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() + # print the rest of the file + for line in lines: + print >> f, line.rstrip() f.close() def interpolate(self, directory, variables): diff -r ed9d64d1fd32 -r cf920f85fb98 tests/doctest.txt --- a/tests/doctest.txt Wed Nov 23 14:29:17 2011 -0800 +++ b/tests/doctest.txt Thu Nov 24 12:58:32 2011 -0800 @@ -1,11 +1,12 @@ Test licenser ================ -The obligatory imports: +The obligatory imports:: >>> import licenser - -Run some tests. This test will fail, please fix it: + >>> import os - >>> assert True == False +Run some tests:: + >>> assert os.path.exists(directory) + diff -r ed9d64d1fd32 -r cf920f85fb98 tests/test.py --- a/tests/test.py Wed Nov 23 14:29:17 2011 -0800 +++ b/tests/test.py Thu Nov 24 12:58:32 2011 -0800 @@ -6,10 +6,11 @@ import doctest import os +import shutil import sys +import tempfile from optparse import OptionParser - def run_tests(raise_on_error=False, report_first=False): # add results here @@ -28,12 +29,21 @@ # run the tests for test in tests: + + # make a temporary directory for example files + tempdir = tempfile.mktemp() + shutil.copytree(os.path.join(directory, 'example'), tempdir) + doctest_args['extraglobs']['directory'] = tempdir + try: results[test] = doctest.testfile(test, **doctest_args) except doctest.DocTestFailure, failure: raise except doctest.UnexpectedException, failure: raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2] + finally: + # cleanup + shutil.rmtree(tempdir) return results