|
22 | 22 |
|
23 | 23 | import requests
|
24 | 24 | import json
|
25 |
| - |
| 25 | +import os |
26 | 26 | from string import Template
|
27 | 27 |
|
28 |
| -REPO = "chaoss/grimoirelab-perceval/" |
29 |
| -PATH_TO_FILE = "perceval/backend.py" |
| 28 | +years = "2015-2020" |
| 29 | +owner = "Bitergia" |
30 | 30 |
|
31 |
| -data = requests.get("https://api.github.com/repos/" + REPO + "commits?path=" + PATH_TO_FILE) |
32 |
| -data = json.loads(data.content) |
33 | 31 |
|
34 |
| -authors_data = {} |
| 32 | +def getlistoffiles(dirname): |
| 33 | + listoffile = os.listdir(dirname) |
| 34 | + allfiles = list() |
| 35 | + for entry in listoffile: |
| 36 | + fullpath = os.path.join(dirname, entry) |
| 37 | + if os.path.isdir(fullpath): |
| 38 | + allfiles = allfiles+getlistoffiles(fullpath) |
| 39 | + else: |
| 40 | + allfiles.append(fullpath) |
| 41 | + return(allfiles) |
35 | 42 |
|
36 |
| -for item in reversed(data): |
37 |
| - data = item['commit']['author'] |
38 |
| - if data['name'] not in authors_data.keys(): |
39 |
| - authors_data[data['name']] = data['email'] |
40 | 43 |
|
41 |
| -authors = [key + " <" + value + ">" for key, value in authors_data.items()] |
| 44 | +REPO = input("repo name: ") |
| 45 | +dirname = input("path to repo: ") |
42 | 46 |
|
43 |
| -# print(authors) |
| 47 | +listoffiles = getlistoffiles(dirname) |
44 | 48 |
|
45 |
| -template_file = open("gpl-v3.tmpl") |
| 49 | +FILES = [] |
46 | 50 |
|
47 |
| -src = Template(template_file.read()) |
| 51 | +for f in listoffiles: |
| 52 | + count = 0 |
| 53 | + try: |
| 54 | + for line in open(f): |
| 55 | + if "Copyright (C)" and "Authors" in line: |
| 56 | + count = count+1 |
| 57 | + if(count > 0): |
| 58 | + FILES.append(f) |
| 59 | + except UnicodeDecodeError: |
| 60 | + pass |
48 | 61 |
|
49 |
| -years = "2015-2020" |
50 |
| -owner = "Bitergia" |
51 | 62 |
|
52 |
| -sub_dict = { |
53 |
| - 'years': years, |
54 |
| - 'owner': owner, |
55 |
| - 'authors': '\n# '.join(authors) |
56 |
| -} |
| 63 | +for items2 in FILES: |
| 64 | + |
| 65 | + PATH_TO_FILE2 = items2.replace(dirname, '') |
| 66 | + PATH_TO_FILE = PATH_TO_FILE2.strip('/') |
| 67 | + CHAOSS_HTTP = "https://api.github.com/repos/chaoss/" |
| 68 | + |
| 69 | + data = requests.get(CHAOSS_HTTP + REPO + "/commits?path=" + PATH_TO_FILE) |
| 70 | + |
| 71 | + data = json.loads(data.content) |
| 72 | + |
| 73 | + authors_data = {} |
| 74 | + |
| 75 | + try: |
| 76 | + for item in reversed(data): |
| 77 | + data = item['commit']['author'] |
| 78 | + if data['name'] not in authors_data.keys(): |
| 79 | + authors_data[data['name']] = data['email'] |
| 80 | + except TypeError: |
| 81 | + pass |
| 82 | + |
| 83 | + authors = [key + " <" + value + ">" for key, value in authors_data.items()] |
| 84 | + authors.append('') |
| 85 | + |
| 86 | + # print(authors) |
| 87 | + |
| 88 | + template_file = open("gpl-v3.tmpl") |
| 89 | + |
| 90 | + src = Template(template_file.read()) |
57 | 91 |
|
58 |
| -result = src.substitute(sub_dict) |
| 92 | + sub_dict = { |
| 93 | + 'years': years, |
| 94 | + 'owner': owner, |
| 95 | + 'authors': '\n# '.join(authors) |
| 96 | + } |
59 | 97 |
|
60 |
| -# print(result) |
| 98 | + result = src.substitute(sub_dict) |
61 | 99 |
|
62 |
| -with open('./backend.py', 'r') as f: |
63 |
| - contents = f.readlines() |
64 |
| - i = 0 |
65 |
| - for item in contents: |
66 |
| - if item.startswith('#'): |
67 |
| - i += 1 |
| 100 | + with open(items2, 'r') as f: |
| 101 | + contents = f.readlines() |
| 102 | + i = 0 |
| 103 | + for item in contents: |
| 104 | + if item.startswith('#'): |
| 105 | + i += 1 |
68 | 106 |
|
69 |
| -with open('./backend.py', 'w') as f: |
70 |
| - f.writelines(contents[i:]) |
| 107 | + with open(items2, 'w') as f: |
| 108 | + f.writelines(contents[i:]) |
71 | 109 |
|
72 |
| -with open('./backend.py', 'r') as f: |
73 |
| - contents = f.readlines() |
74 |
| - contents.insert(0, result+"\n") |
| 110 | + with open(items2, 'r') as f: |
| 111 | + contents = f.readlines() |
| 112 | + contents.insert(0, result+"\n") |
75 | 113 |
|
76 |
| -with open('./backend.py', 'w') as f: |
77 |
| - contents = "".join(contents) |
78 |
| - f.write(contents) |
| 114 | + with open(items2, 'w') as f: |
| 115 | + contents = "".join(contents) |
| 116 | + f.write(contents) |
0 commit comments