"""Module Documentation here
"""
#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2017"
__credits__ = ("Wayne Boucher, Ed Brooksbank, Rasmus H Fogh, Luca Mureddu, Timothy J Ragan & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license",
"or ccpnmodel.ccpncore.memops.Credits.CcpnLicense for licence text")
__reference__ = ("For publications, please use reference from http://www.ccpn.ac.uk/v3-software/downloads/license",
"or ccpnmodel.ccpncore.memops.Credits.CcpNmrReference")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: CCPN $"
__dateModified__ = "$dateModified: 2017-07-07 16:33:20 +0100 (Fri, July 07, 2017) $"
__version__ = "$Revision: 3.0.0 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: CCPN $"
__date__ = "$Date: 2017-04-07 10:28:48 +0000 (Fri, April 07, 2017) $"
#=========================================================================================
# Start of code
#=========================================================================================
"""
======================COPYRIGHT/LICENSE START==========================
headers.py: license header generation code for CCPN framework
Copyright (C) 2005 Rasmus Fogh (CCPN Project)
=======================================================================
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
A copy of this license can be found in ../../../../license/GPL.license
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
======================COPYRIGHT/LICENSE END============================
for further information, please contact :
- CCPN website (http://www.ccpn.ac.uk/)
- email: ccpn@bioc.cam.ac.uk
=======================================================================
If you are using this software for academic purposes, we suggest
quoting the following references:
===========================REFERENCE START=============================
R. Fogh, J. Ionides, E. Ulrich, W. Boucher, W. Vranken, J.P. Linge, M.
Habeck, W. Rieping, T.N. Bhat, J. Westbrook, K. Henrick, G. Gilliland,
H. Berman, J. Thornton, M. Nilges, J. Markley and E. Laue (2002). The
CCPN project: An interim report on a data model for the NMR community
(Progress report). Nature Struct. Biol. 9, 416-418.
Rasmus H. Fogh, Wayne Boucher, Wim F. Vranken, Anne
Pajon, Tim J. Stevens, T.N. Bhat, John Westbrook, John M.C. Ionides and
Ernest D. Laue (2005). A framework for scientific data modeling and automated
software development. Bioinformatics 21, 1678-1684.
===========================REFERENCE END===============================
"""
import types
import os
import sys
import re
from ccpnmodel.ccpncore.memops import Path
from ccpnmodel.ccpncore.memops.license.data import licenses, references, formats, stdContacts
from ccpnmodel.ccpncore.memops.metamodel.MetaModel import MemopsError
# Python 2.1 hack
try:
StringType = types.StringType
except AttributeError:
StringType = str
infoFileName = '_licenseInfo'
# marker lines
licenseStartLine = "======================COPYRIGHT/LICENSE START=========================="
licenseEndLine = "======================COPYRIGHT/LICENSE END============================"
referenceStartLine = "===========================REFERENCE START============================="
referenceEndLine = "===========================REFERENCE END==============================="
separatorLine = "======================================================================="
headerStartLine = licenseStartLine + '\n'
headerEndLine = referenceEndLine + '\n'
# global filtering parameters. These are always skipped
globalExcludeDirs = ['local','CVS']
#
globalExcludeFiles = ['_licenseInfo.py', '__init__.py']
#
exprs = [
'.+\.pyc$','.+\.pyo$','.+\.jar$','.+\.class$',
'.+\.o$','.+\.so$', '.+\.exe$', '^\..*'
]
# for ii in range(len(globalExcludeFileMatches)):
# globalExcludeFileMatches[ii] = re.compile(globalExcludeFileMatches[ii])
globalExcludeFileMatches = [re.compile(x) for x in exprs]
# header and copyright holder
copyrights = """
%(fileName)s: %(programFunction)s
Copyright (C) 2007 %(author)s (%(organization)s)
"""
# contact information
useContacts = """
To obtain more information about this %(programType)s, contact:
"""
# references
useReferences = """
If you are using this software for academic purposes, we suggest
quoting the following references:
"""
[docs]def setLicenses(
directory=None, licenseDir=None, warnSkippedFiles=(not 0), mode='act'
):
""" wrapper - set license header for 'directory' and all subdirectories
licenseDir is the relative path from 'directory'
to the directory with license texts
"""
# necessary as the first directory may be a relative directory name,
# playing havoc with import commands
firstLookupDir = sys.path[0]
sys.path[0] = ''
try:
global testInfo
testInfo = {}
if directory is None:
directory = Path.getTopDirectory()
else:
directory = os.path.abspath(directory)
doSetLicenses(
directory, licenseDir, None, 0, warnSkippedFiles, mode
)
if mode == 'test':
print(showTestInfo())
finally:
sys.path[0] = firstLookupDir
[docs]def doSetLicenses(curDir, licenseDir, infoModule, level, warnSkippedFiles, mode):
""" set license header for a directory and all subdirectories
licenseDir is the relative path from the level 0 directory
to the directory with license texts
"""
print('Checking...', curDir)
# set-up - handle directory changes
olddir = os.getcwd()
os.chdir(curDir)
# get dir contents
contents = os.listdir(curDir)
# set-up - get license info
if '%s.py' % infoFileName in contents:
if sys.modules.has_key(infoFileName):
del sys.modules[infoFileName]
infoModule = __import__(infoFileName)
setattr(
infoModule,'moduleLocation',os.path.join(curDir,'%s.py' % infoFileName)
)
ll = level*['..']#
if licenseDir:
ll.append(licenseDir)
licenseLocation = '/'.join(ll)
# set-up - prepare exclude patterns
if infoModule is None:
print('No active _licenseInfo')
infoRange = ()
excludeDirs = globalExcludeDirs
includeDirs = ()
excludeFiles = 'all'
else:
infoRange = range(len(infoModule.licenseInfo))
if infoModule.excludeFiles == 'all':
excludeFiles = 'all'
else:
excludeFiles = globalExcludeFiles + infoModule.excludeFiles
ll = [re.compile(x) for x in infoModule.excludeFileMatches]
excludeFileMatches = globalExcludeFileMatches + ll
if infoModule.includeDirs:
includeDirs = infoModule.includeDirs
excludeDirs = []
if infoModule.excludeDirs:
raise (
"Error in %s/%s.py: \nYou cannot have both includeDirs and excludeDirs"
% (curDir,infoFileName)
)
else:
includeDirs = []
if infoModule.excludeDirs == 'all':
excludeDirs = 'all'
else:
excludeDirs = globalExcludeDirs + infoModule.excludeDirs
# separate directory contents, filtering on exclude patterns
dirs = []
files = []
for ss in contents:
if os.path.isdir(ss):
if excludeDirs != 'all':
if ss not in excludeDirs:
dirs.append(ss)
elif os.path.isfile(ss):
# NB this test skips symbolic links, which is what we want
if excludeFiles != 'all':
if ss not in excludeFiles:
for expr in excludeFileMatches:
if expr.match(ss):
break
else:
files.append(ss)
# reset dirs in case where includeDirs not empty
if includeDirs:
dirs = [x for x in includeDirs if x in dirs]
files.sort()
dirs.sort()
# Process files
for ii in infoRange:
dd1 = infoModule.licenseInfo[ii]
oldFiles = files
files = []
dd1['licenseLocation'] = licenseLocation
infi = dd1.get('includeFiles')
if infi == 'all':
for ss in oldFiles:
addHeader(curDir, ss, ii, infoModule, mode)
del dd1['licenseLocation']
break
else:
includeFileMatches = [
re.compile(x) for x in dd1['includeFileMatches']
]
for ss in oldFiles:
if ss in infi:
addHeader(curDir, ss, ii, infoModule, mode)
else:
for expr in includeFileMatches:
if expr.match(ss):
addHeader(curDir, ss, ii, infoModule, mode)
break
else:
files.append(ss)
del dd1['licenseLocation']
# Process not-found files
if files and warnSkippedFiles:
for ss in files:
print(" No match found for %s" % os.path.join(curDir,ss))
# Process directories:
for ss in dirs:
ff = os.path.join(curDir,ss)
doSetLicenses(ff, licenseDir, infoModule, level+1, warnSkippedFiles, mode)
# clean-up
os.chdir(olddir)
[docs]def showTestInfo():
""" return formatted string with test info summary
Using global testInfo dictionary for input
"""
strings = []
strapp = strings.append
# reorder and sort testInfo
ll0 = []
for infoMod,dd in testInfo.items():
ll0.append((infoMod.moduleLocation,infoMod,dd))
ll0.sort()
# prepare string
for location,infoMod,dd in ll0:
strapp(72*'#')
strapp('#')
strapp('# %s' % location)
strapp('#\n')
ll1 = dd.items()
ll1.sort()
for paramIndex, ll2 in ll1:
strapp('# licenseInfo set %s :\n{' % paramIndex)
ll3 = infoMod.licenseInfo[paramIndex].items()
ll3.sort()
for tag,val in ll3:
strapp( "'%s' : '''%s'''," % (tag,val))
strapp('}')
strapp('# File, format, directory :')
for tt in ll2:
strapp(" %s, %s, %s" % tt)
strapp('\n\n')
#
return '\n'.join(strings)
if __name__ == '__main__':
if sys.argv[1:]:
ddir = sys.argv[1]
else:
ddir = None
setLicenses(
directory=ddir,
mode='test'
)