Package mbuild :: Module header_tag
[frames] | no frames]

Source Code for Module mbuild.header_tag

  1  #!/usr/bin/env python  
  2  # -*- python -*- 
  3  #BEGIN_LEGAL 
  4  # 
  5  #Copyright (c) 2016 Intel Corporation 
  6  # 
  7  #  Licensed under the Apache License, Version 2.0 (the "License"); 
  8  #  you may not use this file except in compliance with the License. 
  9  #  You may obtain a copy of the License at 
 10  # 
 11  #      http://www.apache.org/licenses/LICENSE-2.0 
 12  # 
 13  #  Unless required by applicable law or agreed to in writing, software 
 14  #  distributed under the License is distributed on an "AS IS" BASIS, 
 15  #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 16  #  See the License for the specific language governing permissions and 
 17  #  limitations under the License. 
 18  #   
 19  #END_LEGAL 
 20   
 21  import sys 
 22  import os 
 23  import re 
 24  from stat import * 
 25   
26 -def _get_mode(fn):
27 "get the mode of the file named fn, suitable for os.chmod() or open() calls" 28 mode = os.stat(fn)[ST_MODE] 29 cmode = S_IMODE(mode) 30 return cmode
31
32 -def _replace_original_with_new_file(file,newfile):
33 "Replace file with newfile" 34 # os.system(" mv -f %s %s" % ( newfile, file)) 35 os.unlink(file) 36 os.rename(newfile,file)
37
38 -def _remove_existing_header(contents,prefix="#"):
39 "remove existing legal header, if any" 40 retval = [] 41 skipping = False 42 start_pattern = re.compile(r"^(/[*]BEGIN_LEGAL)|(" + prefix + "BEGIN_LEGAL)") 43 stop_pattern = re.compile(r"^[ ]*(END_LEGAL[ ]?[*]/)|(" + prefix + "[ ]*END_LEGAL)") 44 for line in contents: 45 if start_pattern.match(line): 46 skipping = True 47 if skipping == False: 48 retval.append(line) 49 if stop_pattern.match(line): 50 skipping = False 51 return retval
52
53 -def _prepend_script_comment(header,prefix="#"):
54 "Apply script comment marker to each line" 55 retval = [] 56 for line in header: 57 retval.append( prefix + line ) 58 return retval
59
60 -def apply_header_to_source_file(header, file):
61 "apply header to file using C++ comment style" 62 f = open(file,"r") 63 mode = _get_mode(file) 64 contents = f.readlines() 65 f.close() 66 trimmed_contents = _remove_existing_header(contents) 67 newfile = file + ".new" 68 o = open(newfile,"w") 69 o.write("/*BEGIN_LEGAL \n") 70 o.writelines(header) 71 o.write("END_LEGAL */\n") 72 o.writelines(trimmed_contents) 73 o.close() 74 os.chmod(newfile,mode) 75 _replace_original_with_new_file(file,newfile)
76 77 # FIXME: this will flag files that have multiline C-style comments 78 # with -*- in them even though the splitter will not look for the 79 # comment properly 80
81 -def _shell_script(lines):
82 """return true if the lines are the start of shell script or 83 something that needs a mode comment at the top""" 84 85 first = "" 86 second = "" 87 if len(lines) > 0: 88 first = lines[0]; 89 if len(lines) > 1: 90 second = lines[1]; 91 92 if re.match("#!",first): 93 #print "\t\t First script test true" 94 return True 95 if re.search("-\*-",first) or re.search("-\*-",second): 96 #print "\t\t Second script test true" 97 return True 98 return False
99
100 -def _split_script(lines):
101 "Return a tuple of (header, body) for shell scripts, based on an input line list" 102 header = [] 103 body = [] 104 105 f = lines.pop(0) 106 while re.match("#",f) or re.search("-\*-",f): 107 header.append(f) 108 f = lines.pop(0) 109 110 # tack on the first non matching line from the above loop 111 body.append(f); 112 body.extend(lines); 113 return (header,body)
114
115 -def _write_script_header(o,lines,prefix="#"):
116 "Write the file header for a script" 117 o.write(prefix+"BEGIN_LEGAL\n") 118 o.writelines(lines) 119 o.write(prefix+"END_LEGAL\n")
120
121 -def apply_header_to_data_file(header, file, prefix="#"):
122 "apply header to file using script comment style" 123 f = open(file,"r") 124 mode = _get_mode(file) 125 #print "file: " + file + " mode: " + "%o" % mode 126 contents = f.readlines() 127 f.close() 128 trimmed_contents = _remove_existing_header(contents, prefix) 129 newfile = file + ".new" 130 o = open(newfile,"w") 131 augmented_header = _prepend_script_comment(header,prefix) 132 if _shell_script(trimmed_contents): 133 (script_header, script_body) = _split_script(trimmed_contents) 134 o.writelines(script_header) 135 _write_script_header(o, augmented_header, prefix) 136 o.writelines(script_body) 137 else: 138 _write_script_header(o,augmented_header,prefix) 139 o.writelines(trimmed_contents) 140 o.close() 141 os.chmod(newfile,mode) 142 _replace_original_with_new_file(file,newfile)
143 144 #################################################################### 145 ### MAIN 146 #################################################################### 147 if __name__ == '__main__': 148 if len(sys.argv) < 4: 149 print "Usage " + sys.argv[0] + " [-s|-t] legal-header file-name [file-name...]\n" 150 sys.exit(1) 151 152 type = sys.argv[1] 153 header_file = sys.argv[2] 154 if not os.path.exists(header_file): 155 print "Could not find header file: [%s]\n" % (header_file) 156 sys.exit(1) 157 158 files_to_tag = sys.argv[3:] 159 f = open(header_file,"r") 160 header = f.readlines() 161 f.close() 162 163 sources = files_to_tag 164 165 if type == "-s": 166 for file in sources: 167 if re.search(".svn",file) == None and re.search(".new$",file) == None: 168 apply_header_to_source_file(header, file.strip()) 169 elif type == "-t": 170 for file in sources: 171 if re.search(".svn",file) == None and re.search(".new$",file) == None: 172 apply_header_to_data_file(header, file.strip()) 173 else: 174 print "2nd argument must be -s or -t\n" 175 sys.exit(1) 176