Skip to content

Commit 1ab01c0

Browse files
committed
v3.1
1 parent ff8f38f commit 1ab01c0

File tree

3 files changed

+93
-78
lines changed

3 files changed

+93
-78
lines changed

bin/create_structure.py

Lines changed: 76 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
from github import Github
77
import pygit2
88
from getpass import getpass
9+
import requests
910

1011
__author__ = "davidecastellani@castellanidavide.it", "chiara@sabaini.com"
11-
__version__ = "03.01 2020-08-15"
12+
__version__ = "3.1 2020-08-18"
1213

13-
class create_structure_03_01:
14+
class create_structure:
1415
def __init__ (self):
1516
"""Main function
1617
"""
17-
questions = [ "Inserisci nome progetto (es. create_structur): ", #0
18+
questions = [ "Inserisci nome progetto (es. create_structure): ", #0
1819
"Inserisci estensione file principale (es. py): ", #1
1920
"Inserisci una descrizione del progetto: ", #2
2021
"Il progetto è con Boscaini (<nome della classe>-<numero consegna>), Bellini (b) o qualcunaltro(lasciare vuoto)? ", #3
@@ -33,24 +34,33 @@ def __init__ (self):
3334

3435
print()
3536

37+
g = create_structure.login(results)
38+
print(f"Credentials OK")
39+
3640
folder_name = f"./{results[0].lower()}_01_01" if (results[3] == "b") else (f"./{results[0]}" if(results[3] == "") else f"./{results[3]}-{results[0]}")
37-
create_structure_03_01.create_folder(folder_name)
38-
print(f"Folder built")
41+
if results[5] == "N":
42+
create_structure.create_folder(folder_name)
43+
print(f"Folder built")
3944

40-
repo = create_structure_03_01.create_repo(results)
45+
repo = create_structure.create_repo(results, g)
4146
print(f"Repo built")
4247

43-
create_structure_03_01.create_source(repo, results)
44-
print(f"Source file built")
45-
46-
create_structure_03_01.create_README(repo, results)
47-
print(f"README.md built")
48+
typerepo = create_structure.choose_template(results, g)
49+
print(f"Template founded ({typerepo})")
4850

49-
create_structure_03_01.create_log(repo)
50-
print(f"trace.log built")
51+
template = g.get_repo(f"CastellaniDavide/{typerepo}-template")
52+
create_structure.scan_and_elaborate(repo, template, "", typerepo, results)
5153

52-
create_structure_03_01.download_repo(repo, folder_name)
53-
print(f"repo downloaded")
54+
if results[5] == "N":
55+
create_structure.download_repo(repo, folder_name)
56+
print(f"repo downloaded")
57+
else:
58+
print(f"Your repo is redy online")
59+
60+
def login(results):
61+
"""Made the login in GitHub
62+
"""
63+
return Github(results[6], results[7])
5464

5565
def create_folder(directory):
5666
"""Creates a folder
@@ -61,74 +71,68 @@ def create_folder(directory):
6171
except OSError:
6272
print ('Error: Creating directory. ' + directory)
6373

64-
def create_repo(results):
74+
def create_repo(results, g):
6575
"""Create the repo in CastellaniDavide repository
6676
"""
6777
if results[4] == "Y":
68-
repo = Github(results[6], results[7]).get_organization("CastellaniDavide").create_repo(results[0], description=results[2], private=results[5] == "Y", has_issues=False, has_wiki=False, has_downloads=True, has_projects=False, team_id=4008430)
78+
repo = g.get_organization("CastellaniDavide").create_repo(results[0] if(results[3] == "") else f"{results[3]}-{results[0]}", description=results[2], private=results[5] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False, team_id=4008430)
6979
else:
70-
repo = Github(results[6], results[7]).get_organization("CastellaniDavide").create_repo(results[0], description=results[2], private=results[5] == "Y", has_issues=False, has_wiki=False, has_downloads=True, has_projects=False)
71-
80+
repo = g.get_organization("CastellaniDavide").create_repo(results[0] if(results[3] == "") else f"{results[3]}-{results[0]}", description=results[2], private=results[5] == "Y", has_issues=True, has_wiki=False, has_downloads=True, has_projects=False)
81+
7282
return repo
7383

74-
def create_source(repo, results):
75-
"""Writes the standards line of code based on the extension
84+
def choose_template(results, g):
85+
"""This helps to find the correct template
7686
"""
77-
time = datetime.now()
78-
79-
if(results[1] == "py"):
80-
text = f'"""{results[0]}\n"""\n\n__author__ = "davidecastellani@castellanidavide.it", "chiara@sabaini.com"\n__version__ = "01.01 {str(time.day)}-{str(time.month)}-{str(time.day)}"\n\n' # first part
81-
text += f'class {results[0]}:\n\tdef __init__ (self):\n\nif __name__ == "__main__":\n\t{results[0]}()'
82-
elif(results[1] == "js"):
83-
text = f'/**\n * @file {results[0]}.{results[1]}\n *\n * @version 01.01 {str(time.year)}{str(time.month)}{str(time.day)}\n *\n * @brief desc\n *\n * @ingroup {results[0]}\n * (Note: this needs exactly one @defgroup somewhere)\n *\n * @author Castellani Davide & Sabaini Chiara\n *\n *Contact: davidecastellani@castellanidavide.it, chiara@sabaini.com\n *\n */'
84-
elif(results[1] == "c"):
85-
init = f'/**\n * @file {results[0]}.{results[1]}\n *\n * @version 01.01 {str(time.year)}{str(time.month)}{str(time.day)}\n *\n * @brief desc\n *\n * @ingroup {results[0]}\n * (Note: this needs exactly one @defgroup somewhere)\n *\n * @author Castellani Davide & Sabaini Chiara\n *\n *Contact: davidecastellani@castellanidavide.it, chiara@sabaini.com\n *\n */'
86-
text = init + ""
87-
elif(results[1] == "cpp"):
88-
init = f'/**\n * @file {results[0]}.{results[1]}\n *\n * @version 01.01 {str(time.year)}{str(time.month)}{str(time.day)}\n *\n * @brief desc\n *\n * @ingroup {results[0]}\n * (Note: this needs exactly one @defgroup somewhere)\n *\n * @author Castellani Davide & Sabaini Chiara\n *\n *Contact: davidecastellani@castellanidavide.it, chiara@sabaini.com\n *\n */'
89-
text = init + f'\n\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {"{"}\n return 0;\n{"}"}'
90-
elif(results[1] == "cs"):
91-
init = f'/**\n * @file {results[0]}.{results[1]}\n *\n * @version 01.01 {str(time.year)}{str(time.month)}{str(time.day)}\n *\n * @brief desc\n *\n * @ingroup {results[0]}\n * (Note: this needs exactly one @defgroup somewhere)\n *\n * @author Castellani Davide & Sabaini Chiara\n *\n *Contact: davidecastellani@castellanidavide.it, chiara@sabaini.com\n *\n */'
92-
elif(results[1] == "html"):
93-
text = f'<!DOCTYPE html>\n<!--\n@author: Castellani Davide & Sabaini Chiara 3CI a.s. 2019/2020\n@version: 01.01 {str(time.year)}-{str(time.month)}-{str(time.day)}\n-->\n<html>\n\n<head>\n\n\t<title>{results[0]}</title>\n\n\t<meta charset="utf-8">\n\n\t<meta name="viewport" content="width=device-width, initial-scale=1">\n\n\t<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">\n\n\t<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>\n\n\t <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>\n\n\t<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>\n\n\t<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>\n\n</head>\n\n\n\n<body>\n\n\t<div class="container">\n\n\t</div>\n\n</body>\n\n</html>'
94-
elif(results[1] == "bat"):
95-
text = f"@ECHO OFF\nCLS\n\nREM\n{results[0]}\nREM Made by Castellani Davide & Sabaini Chiara 3CI a.s. 2019/2020\n"
96-
elif(results[1] == "ino"):
97-
text =f"// Author: Castellani Davide & Sabaini Chiara\n// Version: 01.01 {str(time.year)}-{str(time.month)}-{str(time.day)}\nvoid setup(){'{'}\n Serial.begin(9600);\n{'}'}\nvoid loop(){'{'}\n\n{'}'}"
98-
else:
99-
text = f"# {results[0]}"
100-
101-
if(results[4] == "N"):
102-
text = text.replace(" & Sabaini Chiara", "").replace(', "chiara@sabaini.com"', "")
103-
104-
repo.create_file(f"bin/{results[0]}.{results[1]}", f"Created {results[0]}.{results[1]}", text)
87+
try:
88+
g.get_repo(f"CastellaniDavide/{results[1]}-template")
89+
return f"{results[1]}"
90+
except:
91+
return "default"
10592

106-
def create_README(repo, results):
107-
"""Creates the README.md
93+
def scan_and_elaborate(repo, template, loc, typerepo, results):
94+
"""Scan all files in the repository and push it in the new directory (cahanging the necessary)
95+
"""
96+
contents = template.get_contents(f"{loc}")
97+
for content_file in contents:
98+
if not content_file.path in [".castellanidavide", "", ".vs"]:
99+
if "." in content_file.path and not content_file.path[0] == ".":
100+
create_structure.elaborate_file(repo, typerepo, content_file.path, results)
101+
else:
102+
try:
103+
create_structure.scan_and_elaborate(repo, template, content_file.path, typerepo, results)
104+
except:
105+
create_structure.elaborate_file(repo, typerepo, content_file.path, results)
106+
107+
def elaborate_file(repo, typerepo, filepath, results):
108+
"""Elaborate the file
109+
"""
110+
try:
111+
page = requests.get(f"https://raw.githubusercontent.com/CastellaniDavide/{typerepo}-template/master/{filepath}").text
112+
page = create_structure.change(page, typerepo, results)
113+
repo.create_file(filepath.replace("template", results[0]), f"Created {filepath}", f"{page}")
114+
print(f"Created {filepath.replace('template', results[0])}")
115+
except:
116+
print(f"Error {filepath.replace('template', results[0])}\t{type(page)}\n{page}")
117+
118+
def change(page, typerepo, results):
119+
"""Returns the changed page
108120
"""
121+
data = eval(requests.get(f"https://raw.githubusercontent.com/CastellaniDavide/{typerepo}-template/master/.castellanidavide/change.json").text)
122+
advanced_values = { f"sol{i}sol" : j for i, j in enumerate(results)}
109123
time = datetime.now()
124+
time = f"{str(time.year)}-{str(time.month)}-{str(time.day)}"
110125

111-
init = f"# {results[0]}\n"
112-
tags = f"\n## Tags\n #tag1, #tag2\n"
113-
desc = f"\n## Description\n{results[2]}\n"
114-
req = f"\n## Required\n - \n"
115-
dir_structure = f"\n### Directories structure\n - bin\n\t - {results[0]}.{results[1]}\n - doc\n\t - README.md\n - log\n\t - trace.log\n"
116-
exe_examples = f"\n### Execution examples\n - {results[0]}.{results[1]}\n\t - \n"
117-
changelog = f"\n## Changelog\n- [01.01_{str(time.year)}-{str(time.month)}-{str(time.day)}](#01.01_{str(time.year)}-{str(time.month)}-{str(time.day)})\n"
118-
version = f"\n### 01.01_{str(time.year)}-{str(time.month)}-{str(time.day)}\n - first version\n"
119-
finish = f"\nMade by Castellani Davide & Sabaini Chiara\nIf you have any problem please contact us:\n- davidecastellani@castellanidavide.it\n- chiara@sabaini.com"
120-
sep = f"\n---"
121-
text = init + tags + desc + req + sep + dir_structure + sep + exe_examples + sep + changelog + sep + version + sep + finish
122-
123-
if(results[4] == "N"):
124-
text = text.replace(" & Sabaini Chiara", "").replace("\n- chiara@sabaini.com", "")
125-
126-
repo.create_file("docs/README.md", f"Created README.md", text)
126+
for i in data.keys():
127+
page = page.replace(i.replace("'''", '"'), data[i].replace("'''", '"'))
127128

128-
def create_log(repo):
129-
"""Creates the trace.log
130-
"""
131-
repo.create_file("log/trace.log", f"Created trace.log", "")
129+
for i in advanced_values.keys():
130+
page = page.replace(i, advanced_values[i])
131+
132+
page = page.replace("time__now", time)
133+
page = page.replace("time_now", time.replace("_", ""))
134+
135+
return page
132136

133137
def download_repo(repo, folder_name):
134138
"""Downloads the repo into folder_name
@@ -137,6 +141,6 @@ def download_repo(repo, folder_name):
137141

138142
if __name__ == "__main__":
139143
try:
140-
create_structure_03_01()
144+
create_structure()
141145
except:
142146
print("There is an error, try again.")

docs/README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ This is the magic "robottino" by Castellani Davide & Sabaini Chiara
66

77
## Required
88
- python3
9-
- PyGithub (pip3 install PyGithub)
10-
- pygit2 (pip3 install pygit2)
9+
- some pip pakeges, to install it go in requirements folder and execute: pip3 install -r requirements.txt
1110
- Internet connection
1211
- GitHub account
1312

@@ -17,25 +16,33 @@ This is the magic "robottino" by Castellani Davide & Sabaini Chiara
1716
- bug_report.md
1817
- feature-request.md
1918
- bin
20-
- create_structure.py
19+
- create_structure.py
2120
- doc
2221
- LICENSE
2322
- README.md
2423
- log
25-
- trace.log
24+
- trace.log
25+
- requirements
26+
- requirements.txt
2627

2728
### Execution examples
2829
- python3 create_structure.py
2930

3031
# Changelog
32+
- [3.0_2020-8-18](#3.0_2020-8-18)
3133
- [3.0_2020-8-15](#3.0_2020-8-15)
3234
- [2.0_2020-3-24](#2.0_2020-3-24)
3335
- [1.0_2020-3-24](#1.0_2020-3-24)
34-
36+
37+
### 3.1_2020-8-18
38+
#### Changes
39+
- optimized the code
40+
- added requirements.txt
3541

3642
### 3.0_2020-8-15
37-
- #### Changes
43+
#### Changes
3844
- add GitHub support with a lot of options (eg. repo private or public)
45+
3946
### 2.0_2020-3-24
4047
#### Changes
4148
- adjusted some bugs

requirements/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DateTime>=4.3
2+
PyGithub>=1.53
3+
pygit2>=1.2.1
4+
requests>=2.24.0

0 commit comments

Comments
 (0)