|
14 | 14 |
|
15 | 15 | import io
|
16 | 16 | import os
|
| 17 | +import pathlib |
17 | 18 | import platform
|
18 | 19 | import shlex
|
19 | 20 | import shutil
|
@@ -49,21 +50,36 @@ def _isdir_without_all_perms(dir_path):
|
49 | 50 | )
|
50 | 51 |
|
51 | 52 |
|
| 53 | +def get_default_samples_dir(): |
| 54 | + """Returns the default samples directory |
| 55 | +
|
| 56 | + The default samples directory is computed according to the following priorities: |
| 57 | + - all systems: ``KHIOPS_SAMPLES_DIR/khiops_data/samples`` if set |
| 58 | + - Windows: |
| 59 | + - ``%PUBLIC%\\khiops_data\\samples`` if ``%PUBLIC%`` is defined |
| 60 | + - ``%USERPROFILE%\\khiops_data\\samples`` otherwise |
| 61 | + - Linux/macOS: ``$HOME/khiops_data/samples`` |
| 62 | + """ |
| 63 | + if "KHIOPS_SAMPLES_DIR" in os.environ and os.environ["KHIOPS_SAMPLES_DIR"]: |
| 64 | + samples_dir = os.environ["KHIOPS_SAMPLES_DIR"] |
| 65 | + elif platform.system() == "Windows" and "PUBLIC" in os.environ: |
| 66 | + samples_dir = os.path.join(os.environ["PUBLIC"], "khiops_data", "samples") |
| 67 | + else: |
| 68 | + samples_dir = str(pathlib.Path.home() / "khiops_data" / "samples") |
| 69 | + return samples_dir |
| 70 | + |
| 71 | + |
52 | 72 | def _get_dir_status(a_dir):
|
53 | 73 | """Returns the status of a local or remote directory
|
54 | 74 |
|
55 | 75 | Against a local directory a real check is performed. A remote directory is detected
|
56 | 76 | but not checked.
|
57 | 77 | """
|
58 | 78 | if fs.is_local_resource(a_dir):
|
59 |
| - # Remove initial slash on windows systems |
60 |
| - # urllib's url2pathname does not work properly |
61 | 79 | a_dir_res = fs.create_resource(os.path.normpath(a_dir))
|
62 |
| - a_dir_path = a_dir_res.uri_info.path |
63 |
| - if platform.system() == "Windows": |
64 |
| - if a_dir_path.startswith("/"): |
65 |
| - a_dir_path = a_dir_path[1:] |
66 | 80 |
|
| 81 | + # a_dir_res is a LocalFilesystemResource already |
| 82 | + a_dir_path = a_dir_res.path |
67 | 83 | if not os.path.exists(a_dir_path):
|
68 | 84 | status = "non-existent"
|
69 | 85 | elif not os.path.isdir(a_dir_path):
|
@@ -98,31 +114,6 @@ def _check_samples_dir(samples_dir):
|
98 | 114 | )
|
99 | 115 |
|
100 | 116 |
|
101 |
| -def _extract_path_from_uri(uri): |
102 |
| - res = fs.create_resource(uri) |
103 |
| - if platform.system() == "Windows": |
104 |
| - # Case of file:///<LETTER>:/<REST_OF_PATH>: |
105 |
| - # Eliminate first slash ("/") from path if the first component |
106 |
| - if ( |
107 |
| - res.uri_info.scheme == "" |
108 |
| - and res.uri_info.path[0] == "/" |
109 |
| - and res.uri_info.path[1].isalpha() |
110 |
| - and res.uri_info.path[2] == ":" |
111 |
| - ): |
112 |
| - path = res.uri_info.path[1:] |
113 |
| - # Case of C:/<REST_OF_PATH>: |
114 |
| - # Just use the original path |
115 |
| - elif len(res.uri_info.scheme) == 1: |
116 |
| - path = uri |
117 |
| - # Otherwise return URI path as-is |
118 |
| - else: |
119 |
| - path = res.uri_info.path |
120 |
| - |
121 |
| - else: |
122 |
| - path = res.uri_info.path |
123 |
| - return path |
124 |
| - |
125 |
| - |
126 | 117 | def _khiops_env_file_exists(env_dir):
|
127 | 118 | """Check ``khiops_env`` exists relative to the specified environment dir"""
|
128 | 119 | khiops_env_path = os.path.join(env_dir, "khiops_env")
|
@@ -399,7 +390,7 @@ def root_temp_dir(self):
|
399 | 390 | def root_temp_dir(self, dir_path):
|
400 | 391 | # Check existence, directory status and permissions for local paths
|
401 | 392 | if fs.is_local_resource(dir_path):
|
402 |
| - real_dir_path = _extract_path_from_uri(dir_path) |
| 393 | + real_dir_path = fs.create_resource(dir_path).path |
403 | 394 | if os.path.exists(real_dir_path):
|
404 | 395 | if os.path.isfile(real_dir_path):
|
405 | 396 | raise KhiopsEnvironmentError(
|
@@ -439,7 +430,7 @@ def create_temp_file(self, prefix, suffix):
|
439 | 430 | # Local resource: Effectively create the file with the python file API
|
440 | 431 | if fs.is_local_resource(self.root_temp_dir):
|
441 | 432 | # Extract the path from the potential URI
|
442 |
| - root_temp_dir_path = _extract_path_from_uri(self.root_temp_dir) |
| 433 | + root_temp_dir_path = fs.create_resource(self.root_temp_dir).path |
443 | 434 |
|
444 | 435 | # Create the temporary file
|
445 | 436 | tmp_file_fd, tmp_file_path = tempfile.mkstemp(
|
@@ -470,7 +461,7 @@ def create_temp_dir(self, prefix):
|
470 | 461 | """
|
471 | 462 | # Local resource: Effectively create the directory with the python file API
|
472 | 463 | if fs.is_local_resource(self.root_temp_dir):
|
473 |
| - root_temp_dir_path = _extract_path_from_uri(self.root_temp_dir) |
| 464 | + root_temp_dir_path = fs.create_resource(self.root_temp_dir).path |
474 | 465 | temp_dir = tempfile.mkdtemp(prefix=prefix, dir=root_temp_dir_path)
|
475 | 466 | # Remote resource: Just return a highly probable unique path
|
476 | 467 | else:
|
@@ -919,7 +910,7 @@ class KhiopsLocalRunner(KhiopsRunner):
|
919 | 910 |
|
920 | 911 | - Windows:
|
921 | 912 |
|
922 |
| - - ``%PUBLIC%\khiops_data\samples%`` if it exists and is a directory |
| 913 | + - ``%PUBLIC%\khiops_data\samples%`` if ``%PUBLIC%`` is defined |
923 | 914 | - ``%USERPROFILE%\khiops_data\samples%`` otherwise
|
924 | 915 |
|
925 | 916 | - Linux and macOS:
|
@@ -1029,38 +1020,9 @@ def _initialize_khiops_environment(self):
|
1029 | 1020 |
|
1030 | 1021 | def _initialize_default_samples_dir(self):
|
1031 | 1022 | """See class docstring"""
|
1032 |
| - # Set the fallback value for the samples directory |
1033 |
| - home_samples_dir = Path.home() / "khiops_data" / "samples" |
1034 |
| - |
1035 |
| - # Take the value of an environment variable in priority |
1036 |
| - if "KHIOPS_SAMPLES_DIR" in os.environ: |
1037 |
| - self._samples_dir = os.environ["KHIOPS_SAMPLES_DIR"] |
1038 |
| - |
1039 |
| - # The samples location of Windows systems is: |
1040 |
| - # - %PUBLIC%\khiops_data\samples if %PUBLIC% exists |
1041 |
| - # - %USERPROFILE%\khiops_data\samples otherwise |
1042 |
| - elif platform.system() == "Windows": |
1043 |
| - if "PUBLIC" in os.environ: |
1044 |
| - public_samples_dir = os.path.join( |
1045 |
| - os.environ["PUBLIC"], "khiops_data", "samples" |
1046 |
| - ) |
1047 |
| - else: |
1048 |
| - public_samples_dir = None |
1049 |
| - |
1050 |
| - ok_statuses = ["ok", "remote"] |
1051 |
| - if ( |
1052 |
| - public_samples_dir is not None |
1053 |
| - and _get_dir_status(public_samples_dir) in ok_statuses |
1054 |
| - ): |
1055 |
| - self._samples_dir = public_samples_dir |
1056 |
| - else: |
1057 |
| - self._samples_dir = str(home_samples_dir) |
1058 |
| - |
1059 |
| - # The default samples location on Unix systems is: |
1060 |
| - # $HOME/khiops/samples on Linux and Mac OS |
1061 |
| - else: |
1062 |
| - self._samples_dir = str(home_samples_dir) |
1063 |
| - |
| 1023 | + samples_dir = get_default_samples_dir() |
| 1024 | + _check_samples_dir(samples_dir) |
| 1025 | + self._samples_dir = samples_dir |
1064 | 1026 | assert self._samples_dir is not None
|
1065 | 1027 |
|
1066 | 1028 | def _check_tools(self):
|
|
0 commit comments