Guest User

Untitled

a guest
Jul 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. for i in *.cc # or whatever other pattern...
  2. do
  3. if ! grep -q Copyright $i
  4. then
  5. cat copyright.txt $i >$i.new && mv $i.new $i
  6. fi
  7. done
  8.  
  9. #!/bin/bash
  10. for x in $*; do
  11. head -$LICENSELEN $x | diff license.txt - || ( ( cat license.txt; echo; cat $x) > /tmp/file; mv /tmp/file $x )
  12. done
  13.  
  14. export LICENSELEN=`wc -l license.txt | sed 's/[^0-9]//g`
  15. find dir -type f -name ( *.cpp -o -type *.h ) -print0 | xargs -0 ./addlicense.sh
  16.  
  17. # updates the copyright information for all .cs files
  18. # usage: call recursive_traversal, with the following parameters
  19. # parent directory, old copyright text content, new copyright text content
  20.  
  21. import os
  22.  
  23. excludedir = ["..\Lib"]
  24.  
  25. def update_source(filename, oldcopyright, copyright):
  26. utfstr = chr(0xef)+chr(0xbb)+chr(0xbf)
  27. fdata = file(filename,"r+").read()
  28. isUTF = False
  29. if (fdata.startswith(utfstr)):
  30. isUTF = True
  31. fdata = fdata[3:]
  32. if (oldcopyright != None):
  33. if (fdata.startswith(oldcopyright)):
  34. fdata = fdata[len(oldcopyright):]
  35. if not (fdata.startswith(copyright)):
  36. print "updating "+filename
  37. fdata = copyright + fdata
  38. if (isUTF):
  39. file(filename,"w").write(utfstr+fdata)
  40. else:
  41. file(filename,"w").write(fdata)
  42.  
  43. def recursive_traversal(dir, oldcopyright, copyright):
  44. global excludedir
  45. fns = os.listdir(dir)
  46. print "listing "+dir
  47. for fn in fns:
  48. fullfn = os.path.join(dir,fn)
  49. if (fullfn in excludedir):
  50. continue
  51. if (os.path.isdir(fullfn)):
  52. recursive_traversal(fullfn, oldcopyright, copyright)
  53. else:
  54. if (fullfn.endswith(".cs")):
  55. update_source(fullfn, oldcopyright, copyright)
  56.  
  57.  
  58. oldcright = file("oldcr.txt","r+").read()
  59. cright = file("copyrightText.txt","r+").read()
  60. recursive_traversal("..", oldcright, cright)
  61. exit()
  62.  
  63. #!/usr/bin/python
  64. """
  65. This script attempts to add a header to each file in the given directory
  66. The header will be put the line after a Shebang (#!) if present.
  67. If a line starting with a regular expression 'skip' is present as first line or after the shebang it will ignore that file.
  68. If filename is given only files matchign the filename regex will be considered for adding the license to,
  69. by default this is '*'
  70.  
  71. usage: python addheader.py headerfile directory [filenameregex [dirregex [skip regex]]]
  72.  
  73. easy example: add header to all files in this directory:
  74. python addheader.py licenseheader.txt .
  75.  
  76. harder example adding someone as copyrightholder to all python files in a source directory,exept directories named 'includes' where he isn't added yet:
  77. python addheader.py licenseheader.txt src/ ".*.py" "^((?!includes).)*$" "#Copyright .* Jens Timmerman*"
  78. where licenseheader.txt contains '#Copyright 2012 Jens Timmerman'
  79. """
  80. import os
  81. import re
  82. import sys
  83.  
  84. def writeheader(filename,header,skip=None):
  85. """
  86. write a header to filename,
  87. skip files where first line after optional shebang matches the skip regex
  88. filename should be the name of the file to write to
  89. header should be a list of strings
  90. skip should be a regex
  91. """
  92. f = open(filename,"r")
  93. inpt =f.readlines()
  94. f.close()
  95. output = []
  96.  
  97. #comment out the next 3 lines if you don't wish to preserve sbebangs
  98. if len(inpt) > 0 and inpt[0].startswith("#!"):
  99. output.append(inpt[0])
  100. inpt = inpt[1:]
  101.  
  102. if skip and skip.match(inpt[0]): #skip matches, so skip this file
  103. return
  104.  
  105. output.extend(header) #add the header
  106. for line in inpt:
  107. output.append(line)
  108. try:
  109. f = open(filename,'w')
  110. f.writelines(output)
  111. f.close()
  112. print "added header to %s" %filename
  113. except IOError,err:
  114. print "something went wrong trying to add header to %s: %s" % (filename,err)
  115.  
  116.  
  117. def addheader(directory,header,skipreg,filenamereg,dirregex):
  118. """
  119. recursively adds a header to all files in a dir
  120. arguments: see module docstring
  121. """
  122. listing = os.listdir(directory)
  123. print "listing: %s " %listing
  124. #for each file/dir in this dir
  125. for i in listing:
  126. #get the full name, this way subsubdirs with the same name don't get ignored
  127. fullfn = os.path.join(directory,i)
  128. if os.path.isdir(fullfn): #if dir, recursively go in
  129. if (dirregex.match(fullfn)):
  130. print "going into %s" % fullfn
  131. addheader(fullfn, header,skipreg,filenamereg,dirregex)
  132. else:
  133. if (filenamereg.match(fullfn)): #if file matches file regex, write the header
  134. writeheader(fullfn, header,skipreg)
  135.  
  136.  
  137. def main(arguments=sys.argv):
  138. """
  139. main function: parses arguments and calls addheader
  140. """
  141. ##argument parsing
  142. if len(arguments) > 6 or len(arguments) < 3:
  143. sys.stderr.write("Usage: %s headerfile directory [filenameregex [dirregex [skip regex]]]n"
  144. "Hint: '.*' is a catch all regexnHint:'^((?!regexp).)*$' negates a regexn"%sys.argv[0])
  145. sys.exit(1)
  146.  
  147. skipreg = None
  148. fileregex = ".*"
  149. dirregex = ".*"
  150. if len(arguments) > 5:
  151. skipreg = re.compile(arguments[5])
  152. if len(arguments) > 3:
  153. fileregex = arguments[3]
  154. if len(arguments) > 4:
  155. dirregex = arguments[4]
  156. #compile regex
  157. fileregex = re.compile(fileregex)
  158. dirregex = re.compile(dirregex)
  159. #read in the headerfile just once
  160. headerfile = open(arguments[1])
  161. header = headerfile.readlines()
  162. headerfile.close()
  163. addheader(arguments[2],header,skipreg,fileregex,dirregex)
  164.  
  165. #call the main method
  166. main()
Add Comment
Please, Sign In to add comment