1# Copyright (c) 2012-2016 Seafile Ltd. 2 3import os 4from fabric.api import task 5 6@task 7def update(path): 8 """Add copyright stuff to the begining of files. 9 """ 10 for filename in path_to_pyfile_list(path): 11 do_update(filename) 12 13@task 14def check(path): 15 """Check copyright stuff for files. 16 """ 17 for filename in path_to_pyfile_list(path): 18 do_check(filename) 19 20def do_update(filename): 21 if 'migrations' in filename: 22 print('skip migration file: %s' % filename) 23 return 24 25 with open(filename) as f: 26 # try read first line of file 27 try: 28 head = [next(f) for x in range(1)] 29 except StopIteration: 30 print('%s is empty, skip' % filename) 31 return 32 33 copy_str = '# Copyright (c) 2012-2016 Seafile Ltd.' 34 35 need_update = True 36 for line in head: 37 line = line.lower() 38 if 'seafile ltd.' in line: 39 need_update = False 40 41 if not need_update: 42 print('%s is ok.' % filename) 43 return 44 45 line_prepender(filename, copy_str) 46 print('%s Done.' % filename) 47 48def path_to_pyfile_list(path): 49 is_dir = False 50 if os.path.isdir(path): 51 is_dir = True 52 53 if not is_dir: 54 py_files = [path] 55 else: 56 py_files = [] 57 for root, directories, filenames in os.walk(path): 58 for directory in directories: 59 f = os.path.join(root, directory) 60 if f.endswith('.py'): 61 py_files.append(f) 62 for filename in filenames: 63 if filename.endswith('.py'): 64 py_files.append(os.path.join(root, filename)) 65 return py_files 66 67 68def line_prepender(filename, line): 69 with open(filename, 'r+') as f: 70 content = f.read() 71 f.seek(0, 0) 72 f.write(line.rstrip('\r\n') + '\n' + content) 73 74def do_check(filename): 75 if 'migrations' in filename: 76 return 77 78 with open(filename) as f: 79 # try read first line of file 80 try: 81 head = [next(f) for x in range(1)] 82 except StopIteration: 83 return 84 85 need_update = True 86 for line in head: 87 line = line.lower() 88 if 'seafile ltd.' in line: 89 need_update = False 90 91 if need_update: 92 print('No copyright info in %s.' % filename) 93