File: copyrighter.py

package info (click to toggle)
spades 3.13.1+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 22,172 kB
  • sloc: cpp: 136,213; ansic: 48,218; python: 16,809; perl: 4,252; sh: 2,115; java: 890; makefile: 507; pascal: 348; xml: 303
file content (138 lines) | stat: -rwxr-xr-x 5,697 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python3

############################################################################
# Copyright (c) 2015 Saint Petersburg State University
# Copyright (c) 2011-2014 Saint Petersburg Academic University
# All Rights Reserved
# See file LICENSE for details.
############################################################################

##### for updating copyrights (e.g. from 2014 to 2015):  #####
### grep -rl "# Copyright (c) 2011-2014 Saint-Petersburg Academic University" . | xargs sed -i 's/# Copyright (c) 2011-2014 Saint-Petersburg Academic University/# Copyright (c) 2011-2015 Saint-Petersburg Academic University/g'
#####

##### for removing copyright (script-style, only SPbAU) #####
###  perl -0777 -i -pe 's/############################################################################\n# Copyright \(c\) 2011-2014 Saint-Petersburg Academic University\n# All Rights Reserved\n# See file LICENSE for details.\n############################################################################\n\n//igs' *.{py,sh}
#####

##### for removing copyright (code-style, two universities) #####
###  grep -rl "State University" . | grep -v "kmer_index.hpp" | xargs perl -0777 -i -pe 's/\Q\/\/***************************************************************************\E\n\Q\/\/* Copyright (c) 2015 Saint Petersburg State University\E\n\Q\/\/* Copyright (c) 2011-2014 Saint Petersburg Academic University\E\n\Q\/\/* All Rights Reserved\E\n\Q\/\/* See file LICENSE for details.\E\n\Q\/\/***************************************************************************\E\n\n//igs'
#####

##### for updating one-university copyright to two-university one #####
### grep -rl "Copyright (c) 2011-2014" . | xargs perl -0777 -i -pe 's/\Q\/\/***************************************************************************\E\n\Q\/\/* Copyright (c) 2011-2014 Saint-Petersburg Academic University\E\n\Q\/\/* All Rights Reserved\E\n\Q\/\/* See file LICENSE for details.\E\n\Q\/\/****************************************************************************\E\n/\/\/***************************************************************************\n\/\/* Copyright \(c\) 2015 Saint Petersburg State University\n\/\/* Copyright \(c\) 2011-2014 Saint Petersburg Academic University\n\/\/* All Rights Reserved\n\/\/* See file LICENSE for details.\n\/\/***************************************************************************\n/igs'
#####

##### for removing SPbAU copyrights from new files (specified in <LIST OF FILES>)
### for i in `cat <LIST OF FILES>`; do sed -i '' '/Saint Petersburg Academic University/d' $i; done
#####


import os
import shutil
import sys

script_comment = [
    '############################################################################',
    '# Copyright (c) 2015 Saint Petersburg State University',
    #'# Copyright (c) 2011-2014 Saint Petersburg Academic University',  # new copyrights should be with SPbSU only
    '# All Rights Reserved',
    '# See file LICENSE for details.',
    '############################################################################', 
    '']

code_comment = [
    '//***************************************************************************',
    '//* Copyright (c) 2015 Saint Petersburg State University',
    #'//* Copyright (c) 2011-2014 Saint Petersburg Academic University',  # new copyrights should be with SPbSU only
    '//* All Rights Reserved',
    '//* See file LICENSE for details.',
    '//***************************************************************************',
    '']

only_show = False


def insert_in_script(filename):
    print(filename)
    if only_show:
       return

    lines = open(filename).readlines()
    if (script_comment[1] + '\n') in lines:
        return

    modified = open(filename, 'w')
    if len(lines) and lines[0].startswith('#!'):
        modified.write(lines[0])
        modified.write('\n')
        lines = lines[1:]
    
    for com_line in script_comment:
        modified.write(com_line + '\n')
    
    for line in lines:
        modified.write(line)

    modified.close() 


def insert_in_code(filename):       
    print(filename)
    if only_show:
       return

    lines = open(filename).readlines()
    if (code_comment[1] + '\n') in lines:
        return

    modified = open(filename, 'w')
        
    for com_line in code_comment:
        modified.write(com_line + '\n')
    
    for line in lines:
        modified.write(line)

    modified.close()


def visit(arg, dirname, names):
    for name in names:
        path = os.path.join(dirname, name)
        if not os.path.isfile(path):
            continue
        if os.system('grep -Gq "Copyright (c) \([0-9]\{4\}-\)\{0,1\}[0-9]\{4\} Saint Petersburg" ' + path) == 0: # already copyrighted
            continue
        ext = os.path.splitext(name)[1]
        if arg and ext != arg:
            continue
        if (ext in ['.py', '.sh']) or name.lower().startswith('cmake'):
            insert_in_script(path)
        elif ext in ['.hpp', '.cpp', '.h', '.c']:
            insert_in_code(path)


if len(sys.argv) < 2 or len(sys.argv) > 4:
    print ("Usage: " + sys.argv[0] + " <dirname/filename> [.ext -- only file with this extension will be modified; 'all' for all files] ['only-show' -- only show filepaths that will be copyrighted]")
    sys.exit(1)

start_dir = sys.argv[1]

if not os.path.exists(start_dir):
    print("Error! " + start_dir + " does not exist!")
    sys.exit(1)   

arg = None
if len(sys.argv) >= 3:
    arg = sys.argv[2]
    if arg == "all":
        arg = None

if len(sys.argv) >= 4:
    only_show = True

if os.path.isfile(start_dir):
    visit(arg, '', [start_dir])
else:
    os.path.walk(start_dir, visit, arg)