2222
2323def init_cfg ():
2424 """Initialize global variables with default values."""
25- global SUBMISSION_FILES , AUTOGRADER_TESTS , ADDITIONAL_FILES
25+ global SUBMISSION_FILES , AUTOGRADER_TESTS , ADDITIONAL_FILES , REQUIREMENTS_TXT
2626 global SUBMISSION_LIMIT , FUNCTION_TIMEOUT , SCHOOL_TIME_ZONE , INSTALL_PYTHON_V
2727
2828 SUBMISSION_FILES = []
2929 AUTOGRADER_TESTS = []
3030 ADDITIONAL_FILES = []
31+ REQUIREMENTS_TXT = []
3132 SUBMISSION_LIMIT = os .getenv ("SUBMISSION_LIMIT" , - 1 )
3233 FUNCTION_TIMEOUT = os .getenv ("FUNCTION_TIMEOUT" , 5 )
3334 SCHOOL_TIME_ZONE = os .getenv ("SCHOOL_TIME_ZONE" , "US/Eastern" )
@@ -107,8 +108,8 @@ def make_cfg():
107108 for filename in test_files :
108109 module_name = filename [:- 3 ].replace (os .path .sep , "." )
109110 module = importlib .import_module (module_name )
110- for name in ["SUBMISSION_FILES" , "AUTOGRADER_TESTS" , "ADDITIONAL_FILES" ,
111- "SUBMISSION_LIMIT" , "FUNCTION_TIMEOUT" , "SCHOOL_TIME_ZONE" ]:
111+ for name in ["SUBMISSION_FILES" , "AUTOGRADER_TESTS" , "ADDITIONAL_FILES" , "REQUIREMENTS_TXT" ,
112+ "SUBMISSION_LIMIT" , "FUNCTION_TIMEOUT" , "SCHOOL_TIME_ZONE" , "INSTALL_PYTHON_V" ]:
112113 if hasattr (module , name ):
113114 globals ()[name ] = getattr (module , name )
114115
@@ -139,6 +140,7 @@ def make_cfg():
139140 " " .join (SUBMISSION_FILES ).replace ("\\ " , "/" ),
140141 " " .join (AUTOGRADER_TESTS ).replace ("\\ " , "/" ),
141142 " " .join (ADDITIONAL_FILES ).replace ("\\ " , "/" ),
143+ " " .join (REQUIREMENTS_TXT ).replace ("\\ " , "/" ),
142144 SUBMISSION_LIMIT ,
143145 FUNCTION_TIMEOUT ,
144146 SCHOOL_TIME_ZONE ,
@@ -165,6 +167,7 @@ def build_cmd(setup=False):
165167 print (" SUBMISSION_FILES =" , SUBMISSION_FILES )
166168 print (" AUTOGRADER_TESTS =" , AUTOGRADER_TESTS )
167169 print (" ADDITIONAL_FILES =" , ADDITIONAL_FILES )
170+ print (" REQUIREMENTS_TXT =" , REQUIREMENTS_TXT )
168171 print (" SUBMISSION_LIMIT =" , SUBMISSION_LIMIT )
169172 print (" FUNCTION_TIMEOUT =" , FUNCTION_TIMEOUT )
170173 print (" SCHOOL_TIME_ZONE =" , SCHOOL_TIME_ZONE )
@@ -181,6 +184,16 @@ def build_cmd(setup=False):
181184 points += getattr (function , "weight" , 0 )
182185 print (f"\033 [1;34mAutograder Points: { points } \033 [0m" )
183186
187+ # Generate requirements.txt file if needed
188+ if REQUIREMENTS_TXT :
189+ if not os .path .exists ("requirements.txt" ):
190+ print ("Creating requirements.txt" )
191+ with open ("requirements.txt" , "w" ) as file :
192+ for line in REQUIREMENTS_TXT :
193+ file .write (line + "\n " )
194+ else :
195+ print ("\033 [1;31mIgnoring REQUIREMENTS_TXT (requirements.txt exists)\033 [0m" )
196+
184197 # Copy template files if not already exist
185198 if setup :
186199 for filename in CONFIG_FILES :
@@ -251,6 +264,13 @@ def clean_cmd():
251264 for dirname in CACHE_DIRS :
252265 shutil .rmtree (dirname , True )
253266
267+ # Delete requirements.txt if auto generated
268+ if "REQUIREMENTS_TXT" in globals () and REQUIREMENTS_TXT :
269+ with open ("requirements.txt" ) as file :
270+ lines = [line .rstrip () for line in file ]
271+ if lines == REQUIREMENTS_TXT :
272+ delete_file ("requirements.txt" , "auto generated" )
273+
254274 # Delete template files (if not modified)
255275 for filename in CONFIG_FILES + SCRIPT_FILES :
256276 if os .path .exists (filename ):
0 commit comments