|
7 | 7 |
|
8 | 8 | logger = logging.getLogger(__name__) |
9 | 9 |
|
10 | | -ALLOWED_BINDER_DIR = [".binder", "binder", "."] |
| 10 | +# As documented in https://repo2docker.readthedocs.io/en/latest/specification/ |
| 11 | +ALLOWED_REPO2DOCKER_DIR = [".binder", "binder", "."] |
11 | 12 |
|
12 | | -ALLOWED_BINDER_CONFIGURATION_MAPPING = { |
| 13 | +ALLOWED_REPO2DOCKER_CONFIGURATION_MAPPING = { |
13 | 14 | "environment.yml": CondaBuildPack, |
14 | 15 | "requirements.txt": RequirementsBuildPack, |
15 | 16 | "install.R": InstallBuildPack, |
16 | 17 | } |
17 | 18 |
|
| 19 | +# As documented in https://notebook.link/docs/user-guide/enable-your-github-repository#define-the-environment |
| 20 | +ALLOWED_NOTEBOOK_LINK_DIR = [".nblink", "."] |
18 | 21 |
|
19 | | -def find_configuration_file(path_to_repository): |
20 | | - for configuration_file in ALLOWED_BINDER_CONFIGURATION_MAPPING: |
21 | | - for binder_dir in ALLOWED_BINDER_DIR: |
22 | | - file_path = os.path.join(path_to_repository, binder_dir, configuration_file) |
23 | | - logger.debug("Searching for %s ...", file_path) |
24 | | - if os.path.isfile(file_path): |
25 | | - logger.debug("Found %s", file_path) |
26 | | - return file_path |
| 22 | +ALLOWED_NOTEBOOK_LINK_CONFIGURATION_MAPPING = { |
| 23 | + "environment.yml": CondaBuildPack, |
| 24 | +} |
27 | 25 |
|
28 | | - raise RuntimeError("Configuration file not found.") |
| 26 | +CONFIGURATION_PROVIDERS = { |
| 27 | + "repo2docker": (ALLOWED_REPO2DOCKER_CONFIGURATION_MAPPING, ALLOWED_REPO2DOCKER_DIR), |
| 28 | + "notebook.link": ( |
| 29 | + ALLOWED_NOTEBOOK_LINK_CONFIGURATION_MAPPING, |
| 30 | + ALLOWED_NOTEBOOK_LINK_DIR, |
| 31 | + ), |
| 32 | +} |
29 | 33 |
|
30 | 34 |
|
31 | 35 | def get_buildpack(path_to_repository, ide, output_dir, forgiving=False): |
32 | | - configuration_file_path = find_configuration_file(path_to_repository) |
| 36 | + configuration_file_path = None |
| 37 | + BuildPack = None |
| 38 | + |
| 39 | + for provider, ( |
| 40 | + configuration_mapping, |
| 41 | + allowed_dir, |
| 42 | + ) in CONFIGURATION_PROVIDERS.items(): |
| 43 | + logger.debug("Testing for %s as provider ...", provider) |
| 44 | + for configuration_file in configuration_mapping: |
| 45 | + for binder_dir in allowed_dir: |
| 46 | + file_path = os.path.join( |
| 47 | + path_to_repository, binder_dir, configuration_file |
| 48 | + ) |
| 49 | + logger.debug("Searching for %s ...", file_path) |
| 50 | + if os.path.isfile(file_path): |
| 51 | + logger.debug("Found %s", file_path) |
| 52 | + configuration_file_path = file_path |
| 53 | + BuildPack = configuration_mapping[configuration_file] |
| 54 | + break |
| 55 | + |
| 56 | + if configuration_file_path is not None: |
| 57 | + break |
33 | 58 |
|
34 | | - configuration_file = os.path.basename(configuration_file_path) |
| 59 | + if configuration_file_path is not None: |
| 60 | + break |
35 | 61 |
|
36 | | - for ( |
37 | | - buildpack_configuration_file, |
38 | | - BuildPack, |
39 | | - ) in ALLOWED_BINDER_CONFIGURATION_MAPPING.items(): |
40 | | - if configuration_file == buildpack_configuration_file: |
41 | | - return BuildPack( |
42 | | - path_to_repository, configuration_file_path, ide, output_dir, forgiving |
43 | | - ) |
| 62 | + if configuration_file_path is None: |
| 63 | + raise RuntimeError("Configuration file not found.") |
44 | 64 |
|
45 | | - raise RuntimeError("Build pack not found.") |
| 65 | + return BuildPack( |
| 66 | + path_to_repository, configuration_file_path, ide, output_dir, forgiving |
| 67 | + ) |
0 commit comments