1010from re import escape , compile
1111from requests import get as wget
1212from threading import Thread
13- import settings # Import project settings
13+ from sys import argv
1414
1515__author__ = "help@castellanidavide.it"
1616__version__ = "5.4 2020-12-12"
1717
1818class create_structure :
19- def __init__ (self ):
19+ def __init__ (self , TOKEN , SOUCES_OF_TEMPLATES , ORGANIZATION_NAME , IGNORE_FOLDERS ):
2020 """Main function
2121 """
22-
23- Thread (target = self .login ()).start () # Login
22+ self .TOKEN = TOKEN
23+ self .SOUCES_OF_TEMPLATES = SOUCES_OF_TEMPLATES
24+ self .ORGANIZATION_NAME = ORGANIZATION_NAME
25+ self .IGNORE_FOLDERS = IGNORE_FOLDERS
26+
27+ self .login () # Login
2428
2529 # Make questions
2630 self .asks ()
@@ -40,7 +44,7 @@ def __init__ (self):
4044 def login (self ):
4145 """Made the login in GitHub
4246 """
43- self .g = Github (TOKEN )
47+ self .g = Github (self . TOKEN )
4448
4549 def asks (self ):
4650 questions = [["name" , "Name of the project (es. create_structure): " ],
@@ -56,7 +60,7 @@ def asks(self):
5660 for question_tag , current_quest in questions :
5761 if question_tag == "team" :
5862 self .results ["team" ] = "" # default value
59- if ORGANIZATION_NAME != "" : # If there is an organization
63+ if self . ORGANIZATION_NAME != "" : # If there is an organization
6064 if input (current_quest ) == "Y" :
6165 self .choose_team ()
6266 else :
@@ -67,30 +71,37 @@ def asks(self):
6771 def choose_team (self ):
6872 """Choose a team
6973 """
70- try : # No teams
71- teams = self .g .get_organization (ORGANIZATION_NAME ).get_teams ()
72- enumerate (teams )[0 ]
74+ try :
75+ # Search teams
76+ teams = self .g .get_organization (self .ORGANIZATION_NAME ).get_teams ()
77+
78+ nteams = 0
79+ # Give the option to the user
7380 for i , team in enumerate (teams ):
81+ nteams += 1
7482 print (f"{ i } )\t { team .name } " )
7583
84+ assert (nteams != 0 )
85+
86+ # Save the team choosen
7687 try :
7788 self .results ["team" ] = teams [int (input ("Insert your team number: " ))].id
7889 except :
7990 print ("This team didn't exist, try again" )
8091 self .choose_team ()
81- except :
92+ except : # No teams
8293 print ("Sorry, you didn't have any team. Create a new team to use this option" )
8394
8495 def create_repo (self ):
8596 """Create the repo
8697 """
87- if ORGANIZATION_NAME == "" :
98+ if self . ORGANIZATION_NAME == "" :
8899 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 )
89100 else :
90101 if self .results ["team" ] == "" :
91- 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 )
102+ self .repo = self .g .get_organization (self . 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 )
92103 else :
93- 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" ])
104+ self .repo = self .g .get_organization (self . 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" ])
94105
95106 print (f"Repo built" )
96107
@@ -100,20 +111,21 @@ def choose_template(self):
100111 # If there wasn't any other template for your type of extention and no one default into SOURCES list, give my default code
101112 self .template_name = "CastellaniDavide/default-template"
102113
103- # Check if there is my template
104- for source in SOUCES_OF_TEMPLATES :
114+ # Check if there is wanted template
115+ for source in self . SOUCES_OF_TEMPLATES :
105116 if source != "TODO" and self .template_name == "CastellaniDavide/default-template" :
106117 try :
107- self .template_name = self .g .get_repo (f"{ source } /{ results ['extention' ]} -template" ).full_name
118+ self .template_name = self .g .get_repo (f"{ source } /{ self . results ['extention' ]} -template" ).full_name
108119 break
109120 except :
110121 pass
111122
112123 # Check if there was a default template
113124 if self .template_name == "CastellaniDavide/default-template" :
114- for source in SOUCES_OF_TEMPLATES :
125+ for source in self . SOUCES_OF_TEMPLATES :
115126 if source != "TODO" and self .template_name == "CastellaniDavide/default-template" :
116127 try :
128+ print (f"Try: { source } /default-template" )
117129 self .template_name = self .g .get_repo (f"{ source } /default-template" ).full_name
118130 break
119131 except :
@@ -127,7 +139,7 @@ def scan_and_elaborate(self, loc=""):
127139 """
128140 contents = self .template .get_contents (f"{ loc } " )
129141 for content_file in contents :
130- if not content_file .path in [".castellanidavide" , "" ] + IGNORE_FOLDERS :
142+ if not content_file .path in [".castellanidavide" , "" ] + self . IGNORE_FOLDERS :
131143 if content_file .type == "file" :
132144 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 ()
133145 else :
@@ -169,10 +181,63 @@ def create_file (self, path, file):
169181 self .create_file (path , file )
170182
171183if __name__ == "__main__" :
172- assert TOKEN != "TODO" , "You must to put your tocken into TOKEN variable"
173- assert ORGANIZATION_NAME != "TODO" , "You must to set ORGANIZATION_NAME variable"
174-
184+ """ Read the argv, and sometimes writes the documentation
185+ """
186+
187+ # Default
188+ TOKEN = None
189+ SOUCES_OF_TEMPLATES = ['CastellaniDavide' ]
190+ ORGANIZATION_NAME = ""
191+ IGNORE_FOLDERS = []
192+
193+ # Check if there were all argv
175194 try :
176- create_structure ()
195+ # Go to documentation if requested
196+ assert (not ("-h" in argv or "--help" in argv ))
197+
198+ # Read arguments
199+ for arg in argv :
200+ # find tocken
201+ if "--token=" in arg or "-t=" in arg :
202+ TOKEN = arg .replace ("--token=" , "" ).replace ("-t=" , "" )
203+ # find souces
204+ if "--sources=" in arg or "-s=" in arg :
205+ SOUCES_OF_TEMPLATES = [i for i in arg .replace ("--sources=" , "" ).replace ("-s=" , "" ).replace ("'" , "" ).replace ('"' , "" )[1 :- 1 ].split ("," )]
206+ # find tocken
207+ if "--organization=" in arg or "-o=" in arg :
208+ ORGANIZATION_NAME = arg .replace ("--organization=" , "" ).replace ("-o=" , "" )
209+ # find tocken
210+ if "--ignore=" in arg or "-i=" in arg :
211+ IGNORE_FOLDERS = [i for i in arg .replace ("--ignore=" , "" ).replace ("-i=" , "" ).replace ("'" , "" ).replace ('"' , "" )[1 :- 1 ].split ("," )]
212+
213+ # Check all data
214+ assert (TOKEN != None )
215+
216+ # Start with code
217+ try :
218+ create_structure (TOKEN , SOUCES_OF_TEMPLATES , ORGANIZATION_NAME , IGNORE_FOLDERS )
219+ except :
220+ print ("There is an error, try to check if the repo name is already used." )
221+
177222 except :
178- print ("There is an error, try to check if the repo name is already used." )
223+
224+ documentation = ["usage create_structure" ,
225+ "\t [--token= | -t=]" ,
226+ "\t [--sources= | -s=]" ,
227+ "\t [--organization= | -o=]" ,
228+ "\t [--ignore= | -i=]" ,
229+ "" ,
230+ "These are the create_structure arguments:" ,
231+ "\t --token= or -t= The GitHub tocken with repo and organization permission" ,
232+ "\t --sources= or -s= (optional) The array with your favourite sources, for eg. ['CastellaniDavide']" ,
233+ "\t --organization= or -o= (optional) The organization name, leave empty if you want to create repos in your personal account" ,
234+ "\t --ignore= or -i= (optional) The folders to be ignored" ,
235+ "" ,
236+ "Extra situation(s):" ,
237+ "\t --help or -h To see the documentation" ,
238+ "" ,
239+ "Made with ❤ by Castellani Davide (@DavideC03)" ,
240+ "" ]
241+
242+ for line in documentation :
243+ print (line )
0 commit comments