1111import os
1212import logging
1313from fnmatch import fnmatch
14+
1415try :
1516 from subprocess import TimeoutExpired , check_call , CalledProcessError
1617except ImportError :
1718 from subprocess32 import TimeoutExpired , check_call , CalledProcessError
18- from common_tasks import (
19- run_check_call ,
20- process_glob_string ,
21- )
19+ from common_tasks import run_check_call , compare_python_version
2220
2321logging .getLogger ().setLevel (logging .INFO )
2422
2523root_dir = os .path .abspath (os .path .join (os .path .abspath (__file__ ), ".." , ".." , ".." ))
2624
25+ MINIMUM_TESTED_PYTHON_VERSION = ">=3.7.0"
26+
2727"""
2828Some samples may "run forever" or need to be timed out after a period of time. Add them here in the following format:
2929TIMEOUT_SAMPLES = {
5454 "recv_with_custom_starting_position_async.py" : (10 ),
5555 "sample_code_eventhub_async.py" : (10 ),
5656 "send_and_receive_amqp_annotated_message.py" : (10 ),
57- "send_and_receive_amqp_annotated_message_async.py" : (10 )
57+ "send_and_receive_amqp_annotated_message_async.py" : (10 ),
5858 },
5959 "azure-eventhub-checkpointstoreblob" : {
6060 "receive_events_using_checkpoint_store.py" : (10 ),
61- "receive_events_using_checkpoint_store_storage_api_version.py" : (10 )
61+ "receive_events_using_checkpoint_store_storage_api_version.py" : (10 ),
6262 },
6363 "azure-eventhub-checkpointstoreblob-aio" : {
6464 "receive_events_using_checkpoint_store_async.py" : (10 ),
65- "receive_events_using_checkpoint_store_storage_api_version_async.py" : (10 )
65+ "receive_events_using_checkpoint_store_storage_api_version_async.py" : (10 ),
6666 },
6767 "azure-servicebus" : {
6868 "failure_and_recovery.py" : (10 ),
7171 "session_pool_receive.py" : (20 ),
7272 "receive_iterator_queue_async.py" : (10 ),
7373 "sample_code_servicebus_async.py" : (30 ),
74- "session_pool_receive_async.py" : (20 )
75- }
74+ "session_pool_receive_async.py" : (20 ),
75+ },
7676}
7777
7878
8181 "azure-eventgrid" : [
8282 "__init__.py" ,
8383 "consume_cloud_events_from_eventhub.py" ,
84- "consume_eventgrid_events_from_service_bus_queue.py" ],
84+ "consume_eventgrid_events_from_service_bus_queue.py" ,
85+ ],
8586 "azure-eventhub" : [
8687 "connection_to_custom_endpoint_address.py" ,
8788 "proxy.py" ,
8889 "connection_to_custom_endpoint_address_async.py" ,
8990 "iot_hub_connection_string_receive_async.py" ,
90- "proxy_async.py"
91- ],
92- "azure-eventhub-checkpointstoretable" :[
93- "receive_events_using_checkpoint_store.py"
91+ "proxy_async.py" ,
9492 ],
93+ "azure-eventhub-checkpointstoretable" : ["receive_events_using_checkpoint_store.py" ],
9594 "azure-servicebus" : [
9695 "mgmt_queue.py" ,
9796 "mgmt_rule.py" ,
104103 "mgmt_subscription_async.py" ,
105104 "mgmt_topic_async.py" ,
106105 "proxy_async.py" ,
107- "receive_deferred_message_queue_async.py"
106+ "receive_deferred_message_queue_async.py" ,
108107 ],
109108 "azure-communication-chat" : [
110109 "chat_client_sample_async.py" ,
111110 "chat_client_sample.py" ,
112111 "chat_thread_client_sample_async.py" ,
113- "chat_thread_client_sample.py"
112+ "chat_thread_client_sample.py" ,
114113 ],
115114 "azure-communication-phonenumbers" : [
116115 "purchase_phone_number_sample_async.py" ,
117116 "purchase_phone_number_sample.py" ,
118117 "release_phone_number_sample_async.py" ,
119- "release_phone_number_sample.py"
118+ "release_phone_number_sample.py" ,
120119 ],
121120 "azure-ai-translation-document" : [
122121 "sample_list_document_statuses_with_filters_async.py" ,
130129 "sample_manage_custom_models.py" ,
131130 "sample_manage_custom_models_async.py" ,
132131 ],
133- "azure-ai-language-questionanswering" : [
134- "sample_chat.py"
135- ]
132+ "azure-ai-language-questionanswering" : ["sample_chat.py" ],
136133}
137134
138-
139135def run_check_call_with_timeout (
140136 command_array ,
141137 working_directory ,
142138 timeout ,
143139 pass_if_timeout ,
144140 acceptable_return_codes = [],
145- always_exit = False
141+ always_exit = False ,
146142):
147143 """This is copied from common_tasks.py with some additions.
148144 Don't want to break anyone that's using the original code.
@@ -163,13 +159,9 @@ def run_check_call_with_timeout(
163159 return err
164160 except TimeoutExpired as err :
165161 if pass_if_timeout :
166- logging .info (
167- "Sample timed out successfully"
168- )
162+ logging .info ("Sample timed out successfully" )
169163 else :
170- logging .info (
171- "Fail: Sample timed out"
172- )
164+ logging .info ("Fail: Sample timed out" )
173165 return err
174166
175167
@@ -180,9 +172,7 @@ def execute_sample(sample, samples_errors, timed):
180172 if sys .version_info < (3 , 5 ) and sample .endswith ("_async.py" ):
181173 return
182174
183- logging .info (
184- "Testing {}" .format (sample )
185- )
175+ logging .info ("Testing {}" .format (sample ))
186176 command_array = [sys .executable , sample ]
187177
188178 if not timed :
@@ -195,13 +185,24 @@ def execute_sample(sample, samples_errors, timed):
195185 sample_name = os .path .basename (sample )
196186 if errors :
197187 samples_errors .append (sample_name )
198- logging .info (
199- "ERROR: {}" .format (sample_name )
200- )
188+ logging .info ("ERROR: {}" .format (sample_name ))
201189 else :
202- logging .info (
203- "SUCCESS: {}." .format (sample_name )
204- )
190+ logging .info ("SUCCESS: {}." .format (sample_name ))
191+
192+
193+ def resolve_sample_ignore (sample_file , package_name ):
194+ ignored_files = [
195+ (f , ">=2.7" ) if not isinstance (f , tuple ) else f
196+ for f in IGNORED_SAMPLES .get (package_name , [])
197+ ]
198+ ignored_files_dict = {key : value for (key , value ) in ignored_files }
199+
200+ if sample_file in ignored_files_dict and compare_python_version (
201+ ignored_files_dict [sample_file ]
202+ ):
203+ return False
204+ else :
205+ return True
205206
206207
207208def run_samples (targeted_package ):
@@ -219,7 +220,7 @@ def run_samples(targeted_package):
219220 try :
220221 with open (samples_dir_path + "/sample_dev_requirements.txt" ) as sample_dev_reqs :
221222 for dep in sample_dev_reqs .readlines ():
222- check_call ([sys .executable , '-m' , ' pip' , ' install' , dep ])
223+ check_call ([sys .executable , "-m" , " pip" , " install" , dep ])
223224 except IOError :
224225 pass
225226
@@ -232,14 +233,18 @@ def run_samples(targeted_package):
232233 timeout , pass_if_timeout = timeout
233234 else :
234235 pass_if_timeout = True
235- timed_sample_paths .append ((os .path .abspath (os .path .join (path , name )), timeout , pass_if_timeout ))
236- elif fnmatch (name , "*.py" ) and name not in IGNORED_SAMPLES .get (package_name , []):
236+ timed_sample_paths .append (
237+ (
238+ os .path .abspath (os .path .join (path , name )),
239+ timeout ,
240+ pass_if_timeout ,
241+ )
242+ )
243+ elif fnmatch (name , "*.py" ) and resolve_sample_ignore (name , package_name ):
237244 sample_paths .append (os .path .abspath (os .path .join (path , name )))
238245
239246 if not sample_paths and not timed_sample_paths :
240- logging .info (
241- "No samples found in {}" .format (targeted_package )
242- )
247+ logging .info ("No samples found in {}" .format (targeted_package ))
243248 exit (0 )
244249
245250 for sample in sample_paths :
@@ -252,9 +257,7 @@ def run_samples(targeted_package):
252257 logging .error ("Sample(s) that ran with errors: {}" .format (samples_errors ))
253258 exit (1 )
254259
255- logging .info (
256- "All samples ran successfully in {}" .format (targeted_package )
257- )
260+ logging .info ("All samples ran successfully in {}" .format (targeted_package ))
258261
259262
260263if __name__ == "__main__" :
@@ -275,6 +278,8 @@ def run_samples(targeted_package):
275278 service_dir = os .path .join ("sdk" , args .target_package )
276279 target_dir = os .path .join (root_dir , service_dir )
277280
278- logging .info ("User opted to run samples" )
279-
280- run_samples (target_dir )
281+ if compare_python_version (MINIMUM_TESTED_PYTHON_VERSION ):
282+ logging .info (
283+ "User opted to run samples, and python version is greater than minimum supported."
284+ )
285+ run_samples (target_dir )
0 commit comments