@@ -626,6 +626,22 @@ def is_library_by_path(path: str) -> bool:
626
626
return path .lower ().endswith ((".py" , ".java" , ".class" , "/" , os .sep ))
627
627
628
628
629
+ def update_python_path_and_env (
630
+ working_dir : str = "." , pythonpath : Optional [List [str ]] = None , environment : Optional [Dict [str , str ]] = None
631
+ ) -> None :
632
+ os .chdir (Path (working_dir ))
633
+
634
+ if pythonpath is not None :
635
+ for p in pythonpath :
636
+ absolute_path = str (Path (p ).absolute ())
637
+ if absolute_path not in sys .path :
638
+ sys .path .insert (0 , absolute_path )
639
+
640
+ if environment :
641
+ for k , v in environment .items ():
642
+ os .environ [k ] = v
643
+
644
+
629
645
__PRELOADED_MODULES : Optional [Set [ModuleType ]] = None
630
646
631
647
@@ -640,23 +656,12 @@ def _update_env(
640
656
top = file .parents [3 ]
641
657
for p in filter (lambda v : path_is_relative_to (v , top ), sys .path .copy ()):
642
658
sys .path .remove (p )
643
- wd = Path (working_dir )
644
659
645
660
importlib .invalidate_caches ()
646
661
647
662
gc .collect ()
648
663
649
- os .chdir (wd )
650
-
651
- if pythonpath is not None :
652
- for p in pythonpath :
653
- absolute_path = str (Path (p ).absolute ())
654
- if absolute_path not in sys .path :
655
- sys .path .insert (0 , absolute_path )
656
-
657
- if environment :
658
- for k , v in environment .items ():
659
- os .environ [k ] = v
664
+ update_python_path_and_env (working_dir , pythonpath , environment )
660
665
661
666
662
667
def unload_preloaded_modules () -> None :
@@ -885,7 +890,10 @@ def report_invalid_syntax(self, message: str, level: str = "ERROR") -> None:
885
890
886
891
887
892
def resolve_robot_variables (
888
- working_dir : str = "." , base_dir : str = "." , variables : Optional [Dict [str , Optional [Any ]]] = None
893
+ working_dir : str = "." ,
894
+ base_dir : str = "." ,
895
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
896
+ variables : Optional [Dict [str , Optional [Any ]]] = None ,
889
897
) -> Any :
890
898
from robot .variables import Variables
891
899
@@ -928,6 +936,10 @@ def resolve_robot_variables(
928
936
}.items ():
929
937
result [k ] = v
930
938
939
+ if command_line_variables :
940
+ for k , v in command_line_variables .items ():
941
+ result [f"${{{ k } }}" ] = v
942
+
931
943
if variables is not None :
932
944
933
945
vars = [_Variable (k , v ) for k , v in variables .items () if v is not None ]
@@ -942,10 +954,15 @@ def resolve_variable(
942
954
name : str ,
943
955
working_dir : str = "." ,
944
956
base_dir : str = "." ,
957
+ pythonpath : Optional [List [str ]] = None ,
958
+ environment : Optional [Dict [str , str ]] = None ,
959
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
945
960
variables : Optional [Dict [str , Optional [Any ]]] = None ,
946
961
ignore_errors : bool = True ,
947
962
) -> Any :
948
- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
963
+ update_python_path_and_env (working_dir , pythonpath , environment )
964
+
965
+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
949
966
950
967
return robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = ignore_errors )
951
968
@@ -978,6 +995,7 @@ def _find_library_internal(
978
995
base_dir : str = "." ,
979
996
pythonpath : Optional [List [str ]] = None ,
980
997
environment : Optional [Dict [str , str ]] = None ,
998
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
981
999
variables : Optional [Dict [str , Optional [Any ]]] = None ,
982
1000
) -> Tuple [str , Any ]:
983
1001
@@ -986,7 +1004,7 @@ def _find_library_internal(
986
1004
987
1005
_update_env (working_dir , pythonpath , environment )
988
1006
989
- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1007
+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
990
1008
991
1009
name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
992
1010
@@ -1007,10 +1025,13 @@ def find_library(
1007
1025
base_dir : str = "." ,
1008
1026
pythonpath : Optional [List [str ]] = None ,
1009
1027
environment : Optional [Dict [str , str ]] = None ,
1028
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
1010
1029
variables : Optional [Dict [str , Optional [Any ]]] = None ,
1011
1030
) -> str :
1012
1031
1013
- return _find_library_internal (name , working_dir , base_dir , pythonpath , environment , variables )[0 ]
1032
+ return _find_library_internal (
1033
+ name , working_dir , base_dir , pythonpath , environment , command_line_variables , variables
1034
+ )[0 ]
1014
1035
1015
1036
1016
1037
def get_library_doc (
@@ -1020,6 +1041,7 @@ def get_library_doc(
1020
1041
base_dir : str = "." ,
1021
1042
pythonpath : Optional [List [str ]] = None ,
1022
1043
environment : Optional [Dict [str , str ]] = None ,
1044
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
1023
1045
variables : Optional [Dict [str , Optional [Any ]]] = None ,
1024
1046
) -> LibraryDoc :
1025
1047
@@ -1070,6 +1092,7 @@ def get_test_library(
1070
1092
base_dir = base_dir ,
1071
1093
pythonpath = pythonpath ,
1072
1094
environment = environment ,
1095
+ command_line_variables = command_line_variables ,
1073
1096
variables = variables ,
1074
1097
)
1075
1098
@@ -1301,14 +1324,15 @@ def find_file(
1301
1324
base_dir : str = "." ,
1302
1325
pythonpath : Optional [List [str ]] = None ,
1303
1326
environment : Optional [Dict [str , str ]] = None ,
1327
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
1304
1328
variables : Optional [Dict [str , Optional [Any ]]] = None ,
1305
1329
file_type : str = "Resource" ,
1306
1330
) -> str :
1307
1331
from robot .utils .robotpath import find_file as robot_find_file
1308
1332
1309
1333
_update_env (working_dir , pythonpath , environment )
1310
1334
1311
- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1335
+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
1312
1336
1313
1337
name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
1314
1338
@@ -1394,6 +1418,7 @@ def complete_library_import(
1394
1418
base_dir : str = "." ,
1395
1419
pythonpath : Optional [List [str ]] = None ,
1396
1420
environment : Optional [Dict [str , str ]] = None ,
1421
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
1397
1422
variables : Optional [Dict [str , Optional [Any ]]] = None ,
1398
1423
) -> Optional [List [CompleteResult ]]:
1399
1424
@@ -1409,7 +1434,7 @@ def complete_library_import(
1409
1434
]
1410
1435
1411
1436
if name is not None :
1412
- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1437
+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
1413
1438
1414
1439
name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
1415
1440
@@ -1460,6 +1485,7 @@ def complete_resource_import(
1460
1485
base_dir : str = "." ,
1461
1486
pythonpath : Optional [List [str ]] = None ,
1462
1487
environment : Optional [Dict [str , str ]] = None ,
1488
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
1463
1489
variables : Optional [Dict [str , Optional [Any ]]] = None ,
1464
1490
) -> Optional [List [CompleteResult ]]:
1465
1491
@@ -1468,7 +1494,7 @@ def complete_resource_import(
1468
1494
result : List [CompleteResult ] = []
1469
1495
1470
1496
if name is not None :
1471
- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1497
+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
1472
1498
1473
1499
name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
1474
1500
@@ -1519,6 +1545,7 @@ def complete_variables_import(
1519
1545
base_dir : str = "." ,
1520
1546
pythonpath : Optional [List [str ]] = None ,
1521
1547
environment : Optional [Dict [str , str ]] = None ,
1548
+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
1522
1549
variables : Optional [Dict [str , Optional [Any ]]] = None ,
1523
1550
) -> Optional [List [CompleteResult ]]:
1524
1551
@@ -1527,7 +1554,7 @@ def complete_variables_import(
1527
1554
result : List [CompleteResult ] = []
1528
1555
1529
1556
if name is not None :
1530
- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1557
+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
1531
1558
1532
1559
name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
1533
1560
0 commit comments