|
4 | 4 | If there was any type of problem you can contact me on my help email: help@castellanidavide.it |
5 | 5 | """ |
6 | 6 | # Some imports |
7 | | -import os |
8 | | -from datetime import * |
9 | | -from github import Github |
10 | | -import pygit2 |
| 7 | +from datetime import datetime as dt |
11 | 8 | from getpass import getpass |
12 | | -import requests |
| 9 | +from github import Github |
| 10 | +from re import escape, compile |
| 11 | +from requests import get as wget |
| 12 | +from threading import Thread |
13 | 13 |
|
14 | 14 | __author__ = "help@castellanidavide.it" |
15 | | -__version__ = "5.0 2020-11-21" |
| 15 | +__version__ = "5.3 2020-11-29" |
16 | 16 |
|
17 | 17 | TOKEN = "TODO" |
18 | 18 | SOUCES_OF_TEMPLATES = ["TODO", "CastellaniDavide"] |
19 | 19 | ORGANIZATION_NAME = "TODO" # Leave empty if you want to add into your personal GitHub account |
| 20 | +IGNORE_FOLDERS = ["TODO", ".vs"] |
20 | 21 |
|
21 | 22 | class create_structure: |
22 | 23 | def __init__ (self): |
23 | 24 | """Main function |
24 | 25 | """ |
25 | | - g = create_structure.login() # Login |
| 26 | + |
| 27 | + Thread(target = self.login()).start() # Login |
26 | 28 |
|
27 | 29 | # Make questions |
| 30 | + self.asks() |
| 31 | + |
| 32 | + # Make repo |
| 33 | + Thread(target = self.create_repo()).start() |
| 34 | + |
| 35 | + # Get template |
| 36 | + self.choose_template() |
| 37 | + |
| 38 | + # Get changes |
| 39 | + self.change_map() |
| 40 | + |
| 41 | + # Make all |
| 42 | + Thread(target = self.scan_and_elaborate()).start() |
| 43 | + |
| 44 | + def login(self): |
| 45 | + """Made the login in GitHub |
| 46 | + """ |
| 47 | + self.g = Github(TOKEN) |
| 48 | + |
| 49 | + def asks(self): |
28 | 50 | questions = [["name", "Name of the project (es. create_structure): "], |
29 | 51 | ["extention", "Extenction of the main programm (es. py): "], |
30 | 52 | ["descr", "Description of the project: "], |
31 | | - ["prefix", "Insert the prefix of the repository (or don't insert something): "], |
| 53 | + ["prefix", "Insert the prefix of the repository (or don't insert anything): "], |
32 | 54 | ["team", "Do you want insert this repo into a team? [Y/n]: "], |
33 | | - ["private", "Is that private?(Y/N): "], |
| 55 | + ["private", "Is that private? [Y/n]: "], |
34 | 56 | ] |
35 | | - results = {} |
36 | | - |
| 57 | + self.results = {} |
| 58 | + |
37 | 59 | # Get infos |
38 | 60 | for question_tag, current_quest in questions: |
39 | 61 | if question_tag == "team": |
| 62 | + self.results["team"] = "" # default value |
40 | 63 | if ORGANIZATION_NAME != "": # If there is an organization |
41 | 64 | if input(current_quest) == "Y": |
42 | | - create_structure.choose_team(g, results) |
43 | | - else: |
44 | | - results["team"] = "" |
| 65 | + self.choose_team() |
45 | 66 | else: |
46 | | - results[question_tag] = input(current_quest) |
47 | | - |
48 | | - print() |
| 67 | + self.results[question_tag] = input(current_quest) |
49 | 68 |
|
50 | | - # Make repo |
51 | | - repo = create_structure.create_repo(results, g) |
52 | | - print(f"Repo built") |
53 | | - |
54 | | - typerepo = create_structure.choose_template(results, g) |
55 | | - print(f"Template founded ({typerepo})") |
56 | | - |
57 | | - create_structure.scan_and_elaborate(repo, g.get_repo(typerepo), "", typerepo, results) |
58 | | - |
59 | | - def login(): |
60 | | - """Made the login in GitHub |
61 | | - """ |
62 | | - return Github(TOKEN) |
| 69 | + print() |
63 | 70 |
|
64 | | - def choose_team(g, results): |
| 71 | + def choose_team(self): |
65 | 72 | """Choose a team |
66 | 73 | """ |
67 | | - teams = g.get_organization('CastellaniDavide').get_teams() |
68 | | - for i, team in enumerate(teams): |
69 | | - print(f"{i})\t{team.name}") |
70 | | - |
71 | | - try: |
72 | | - results["team"] = teams[int(input("Insert your team number: "))].id |
| 74 | + try: # No teams |
| 75 | + teams = self.g.get_organization(ORGANIZATION_NAME).get_teams() |
| 76 | + enumerate(teams)[0] |
| 77 | + for i, team in enumerate(teams): |
| 78 | + print(f"{i})\t{team.name}") |
| 79 | + |
| 80 | + try: |
| 81 | + self.results["team"] = teams[int(input("Insert your team number: "))].id |
| 82 | + except: |
| 83 | + print("This team didn't exist, try again") |
| 84 | + self.choose_team() |
73 | 85 | except: |
74 | | - print("This team didn't exist, try again") |
75 | | - create_structure.choose_team(g, results) |
| 86 | + print("Sorry, you didn't have any team. Create a new team to use this option") |
76 | 87 |
|
77 | | - def create_repo(results, g): |
78 | | - """Create the repo in CastellaniDavide repository |
| 88 | + def create_repo(self): |
| 89 | + """Create the repo |
79 | 90 | """ |
80 | 91 | if ORGANIZATION_NAME == "": |
81 | | - return g.get_user().create_repo(results['name'] if(results['prefix'] == "") else f"{results['prefix']}-{results['name']}", description=results['descr'], private=results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False) |
| 92 | + self.repo = self.g.get_user().create_repo(self.results['name'] if(self.results['prefix'] == "") else f"{self.results['prefix']}-{self.results['name']}", description=self.results['descr'], private=self.results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False) |
82 | 93 | else: |
83 | | - if results["team"] == "": |
84 | | - return g.get_organization(ORGANIZATION_NAME).create_repo(results['name'] if(results['prefix'] == "") else f"{results['prefix']}-{results['name']}", description=results['descr'], private=results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False) |
| 94 | + if self.results["team"] == "": |
| 95 | + self.repo = self.g.get_organization(ORGANIZATION_NAME).create_repo(self.results['name'] if(self.results['prefix'] == "") else f"{self.results['prefix']}-{self.results['name']}", description=self.results['descr'], private=self.results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False) |
85 | 96 | else: |
86 | | - return g.get_organization(ORGANIZATION_NAME).create_repo(results['name'] if(results['prefix'] == "") else f"{results['prefix']}-{results['name']}", description=results['descr'], private=results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False, team_id=results["team"]) |
| 97 | + self.repo = self.g.get_organization(ORGANIZATION_NAME).create_repo(self.results['name'] if(self.results['prefix'] == "") else f"{self.results['prefix']}-{self.results['name']}", description=self.results['descr'], private=self.results['private'] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False, team_id=self.results["team"]) |
| 98 | + |
| 99 | + print(f"Repo built") |
87 | 100 |
|
88 | | - def choose_template(results, g): |
| 101 | + def choose_template(self): |
89 | 102 | """This helps to find the correct template |
90 | 103 | """ |
| 104 | + # If there wasn't any other template for your type of extention and no one default into SOURCES list, give my default code |
| 105 | + self.template_name = "CastellaniDavide/default-template" |
| 106 | + |
91 | 107 | # Check if there is my template |
92 | 108 | for source in SOUCES_OF_TEMPLATES: |
93 | | - if source != "TODO": |
| 109 | + if source != "TODO" and self.template_name == "CastellaniDavide/default-template": |
94 | 110 | try: |
95 | | - return g.get_repo(f"{source}/{results['extention']}-template").full_name |
| 111 | + self.template_name = self.g.get_repo(f"{source}/{results['extention']}-template").full_name |
| 112 | + break |
96 | 113 | except: |
97 | 114 | pass |
98 | 115 |
|
99 | 116 | # Check if there was a default template |
100 | | - for source in SOUCES_OF_TEMPLATES: |
101 | | - if source != "TODO": |
102 | | - try: |
103 | | - return g.get_repo(f"{source}/default-template").full_name |
104 | | - except: |
105 | | - pass |
106 | | - |
107 | | - # If there wasn't any template for your type of extention and no one default into SOURCES list, give my default code |
108 | | - return "CastellaniDavide\default" |
109 | | - |
110 | | - def scan_and_elaborate(repo, template, loc, typerepo, results): |
| 117 | + if self.template_name == "CastellaniDavide/default-template": |
| 118 | + for source in SOUCES_OF_TEMPLATES: |
| 119 | + if source != "TODO" and self.template_name == "CastellaniDavide/default-template": |
| 120 | + try: |
| 121 | + self.template_name = self.g.get_repo(f"{source}/default-template").full_name |
| 122 | + break |
| 123 | + except: |
| 124 | + pass |
| 125 | + |
| 126 | + self.template = self.g.get_repo(self.template_name) |
| 127 | + print(f"Template founded ({self.template_name})") |
| 128 | + |
| 129 | + def scan_and_elaborate(self, loc=""): |
111 | 130 | """Scan all files in the repository and push it in the new directory (cahanging the necessary) |
112 | 131 | """ |
113 | | - contents = template.get_contents(f"{loc}") |
| 132 | + contents = self.template.get_contents(f"{loc}") |
114 | 133 | for content_file in contents: |
115 | | - if not content_file.path in [".castellanidavide", "", ".vs"]: |
116 | | - print(content_file.path) |
117 | | - if "." in content_file.path and not content_file.path[0] == "." and not "default" in content_file.path: |
118 | | - try: |
119 | | - create_structure.scan_and_elaborate(repo, template, content_file.path, typerepo, results) |
120 | | - except: |
121 | | - create_structure.elaborate_file(repo, typerepo, content_file.path, results) |
| 134 | + if not content_file.path in [".castellanidavide", ""] + IGNORE_FOLDERS: |
| 135 | + if content_file.type == "file": |
| 136 | + Thread(target = self.create_file, args = (self.change(content_file.path), f"{self.change(wget(f'https://raw.githubusercontent.com/{self.template_name}/master/{content_file.path}').text)}")).start() |
122 | 137 | else: |
123 | | - try: |
124 | | - create_structure.scan_and_elaborate(repo, template, content_file.path, typerepo, results) |
125 | | - except: |
126 | | - create_structure.elaborate_file(repo, typerepo, content_file.path, results) |
| 138 | + Thread(target = self.scan_and_elaborate, args = (content_file.path, )).start() |
| 139 | + |
| 140 | + def change_map(self): |
| 141 | + """Returns a map of changes |
| 142 | + """ |
| 143 | + time = dt.now() |
| 144 | + |
| 145 | + # repo changes |
| 146 | + change_map = eval(wget(f"https://raw.githubusercontent.com/{self.template_name}/master/.castellanidavide/change.json").text) |
127 | 147 |
|
128 | | - def elaborate_file(repo, typerepo, filepath, results): |
129 | | - """Elaborate the file |
130 | | - """ |
131 | | - page = requests.get(f"https://raw.githubusercontent.com/{typerepo}/master/{filepath}").text |
132 | | - |
133 | | - try: |
134 | | - page = create_structure.change(page, typerepo, results) |
135 | | - repo.create_file(filepath.replace("template", results['name']), f"Created {filepath}", f"{page}") |
136 | | - print(f"Created {filepath.replace('template', results['name'])}") |
137 | | - except: |
138 | | - print(f"Error {filepath.replace('template', results['name'])}\t{type(page)}\n{page}") |
| 148 | + # answer changes |
| 149 | + for key, value in self.results.items(): |
| 150 | + change_map[f"sol{key}sol"] = value |
139 | 151 |
|
140 | | - def change(page, typerepo, results): |
141 | | - """Returns the changed page |
142 | | - """ |
143 | | - data = eval(requests.get(f"https://raw.githubusercontent.com/{typerepo}/master/.castellanidavide/change.json").text) |
144 | | - advanced_values = { f"sol{i}sol" : j for i, j in results.items()} |
145 | | - time = datetime.now() |
146 | | - time = f"{str(time.year)}-{str(time.month)}-{str(time.day)}" |
| 152 | + # special changes |
| 153 | + change_map["time__now"] = f"{str(time.year)}-{str(time.month)}-{str(time.day)}" |
| 154 | + change_map["time_now"] = f"{str(time.year)}{str(time.month)}{str(time.day)}" |
147 | 155 |
|
148 | | - for i in data.keys(): |
149 | | - page = page.replace(i.replace("'''", '"'), data[i].replace("'''", '"')) |
| 156 | + # re dict, because I can use it faster (eg. for changes) |
| 157 | + self.change_map = dict((escape(k), v) for k, v in change_map.items()) |
150 | 158 |
|
151 | | - for i in advanced_values.keys(): |
152 | | - page = page.replace(i, advanced_values[i]) |
| 159 | + def change(self, text): |
| 160 | + """Returns the changed page |
| 161 | + Change two times for special keys |
| 162 | + """ |
| 163 | + return compile("|".join(self.change_map.keys())).sub(lambda m: self.change_map[escape(m.group(0))], compile("|".join(self.change_map.keys())).sub(lambda m: self.change_map[escape(m.group(0))], text)) |
153 | 164 |
|
154 | | - page = page.replace("time__now", time) |
155 | | - page = page.replace("time_now", time.replace("_", "").replace("-", "")) |
156 | | - |
157 | | - return page |
| 165 | + def create_file (self, path, file): |
| 166 | + """Create the file into the repo |
| 167 | + """ |
| 168 | + try: |
| 169 | + self.repo.create_file(path, f"Created {path}", file) |
| 170 | + print(f"Created {path}") |
| 171 | + except: |
| 172 | + # If it's an error, possible with multitreading, try again |
| 173 | + self.create_file (path, file) |
158 | 174 |
|
159 | 175 | if __name__ == "__main__": |
160 | 176 | assert TOKEN != "TODO", "You must to put your tocken into TOKEN variable" |
|
0 commit comments