Grigoriy Petukhov avatar Grigoriy Petukhov committed 86c0502

Initial commit

Comments (0)

Files changed (3)

+syntax:glob
+
+# Common
+*.pyc
+*.pyo
+*.swp
+*.swo
+*.orig
+==========
+Copywriter
+==========
+
+That script recursively walks over all files in current directory
+and put in each python file the copyright information which by 
+default is got from ~/.copyright
+
+If there is "copyright" or "author:" words in the file content when it is not
+altered and the file name is displayed after all files are processed
+
+Copyright is inserted as first as possible but after following items:
+
+ * #! line
+ # coding: vim directive
+ * first docstring (module docstring)
+#!/usr/bin/env python
+"""
+That script recursively walks over all files in current directory
+and put in each python file the copyright information which by 
+default is got from ~/.copyright
+
+If there is "copyright" or "author:" words in the file content when it is not
+altered and the file name is displayed after all files are processed
+
+Copyright is inserted as first as possible but after following items:
+
+ * #! line
+ # coding: vim directive
+ * first docstring (module docstring)
+
+"""
+# Copyright: 2011, Grigoriy Petukhov
+# Author: Grigoriy Petukhov (http://lorien.name)
+# License: BSD
+import re
+import os
+
+IGNORE_DIRS = ['autom4te.cache', 'blib', '_build', '.bzr', '.cdv', 'cover_db',
+               'CVS', '_darcs', '~.dep', '~.dot', '.git', '.hg', '~.nib',
+               '.pc', '~.plst', 'RCS', 'SCCS', '_sgbak', '.svn']
+
+# Filenames
+IGNORE_PATTERNS = [re.compile(x) for x in ('^\.', '^__init__\.py$')]
+MATCH_PATTERN = re.compile('^.+\.py$')
+
+# Fragments
+COPYRIGHT = re.compile(r'^(.*(?:copyright|author:).*$)', re.I | re.M)
+SHA_BANG = re.compile('^(#!.+?\n)(.+)', re.S)
+ENCODING = re.compile(r'^(#[^\n]*coding:[^\n]+\n)(.+)', re.S)
+MODULE_DOCSTRING = re.compile(r'^((?:[ ]*\n)*(?:\'\'\'|""").+?(?:\'\'\'|""")[ ]*\n)(.+)', re.S)
+
+
+def main():
+    skiped = []
+    curdir = os.getcwd()
+    copyright_data = open(os.path.expanduser('~/.copyright')).read()
+
+    for root, dirs, files in os.walk(curdir):
+        # Do not process some dirs
+        for name in dirs[:]:
+            if name in IGNORE_DIRS:
+                dirs.remove(name)
+            if name in dirs:
+                for rex in IGNORE_PATTERNS:
+                    if rex.search(name):
+                        dirs.remove(name)
+                        break
+
+        for name in files:
+            result = False
+            ignore = False
+
+            for rex in IGNORE_PATTERNS:
+                if rex.search(name):
+                    ignore = True
+                    break
+
+            if not MATCH_PATTERN.match(name):
+                ignore = True
+
+            path = os.path.join(root, name)
+            if not ignore:
+                result = process(path, skiped, copyright_data)
+                print path, 'YES' if result else 'NO' 
+
+    if skiped:
+        print
+        print 'Skiped files:'
+        for path, match in skiped:
+            print path, '|', match
+
+
+
+def process(path, skiped, copyright_data):
+    head = ''
+    content = open(path).read()
+
+    match = COPYRIGHT.search(content)
+    if match:
+        skiped.append((path, match.group(1)))
+        return False
+
+    match = SHA_BANG.match(content)
+    if match:
+        head, content = match.groups()
+
+    match = ENCODING.match(content)
+    if match:
+        head_extra, content = match.groups()
+        head += head_extra
+
+    match = MODULE_DOCSTRING.match(content)
+    if match:
+        head_extra, content = match.groups()
+        head += head_extra
+
+    open(path, 'w').write(head + copyright_data + content)
+    return True
+
+
+if __name__ == '__main__':
+    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.