Beispiel #1
0
 def test_whitelisted_lines(self, mock_file, _):
     configs = [
         ("foo.c", "*.c"),
         (".c", "*.c"),
         ("foo/foo.c", "*.c"),
         ("foo/foo.c", "foo.c"),
         ("foo.c", "/*.c"),
         ("foo.c", "/foo.c"),
         ("foo.c", "foo.c"),
         ("foo.c", "foo.[ch]"),
         ("foo/bar/bla.c", "foo/**"),
         ("foo/bar/bla/blie.c", "foo/**/blie.c"),
         ("foo/bar/bla.c", "**/bla.c"),
         ("bla.c", "**/bla.c"),
         ("foo/bar", "foo/**/bar"),
         ("foo/bla/bar", "foo/**/bar"),
         ("foo/bar/", "bar/"),
         ("foo/bar/", "bar"),
         ("foo/bar/something", "foo/bar/*"),
     ]
     for (path, pattern) in configs:
         mock_file.return_value.__enter__.return_value = [pattern]
         patterns = IgnoreManager.get_config()
         self.assertEqual(
             (self.get_ignored(patterns), self.get_whitelisted(patterns)),
             ([pattern], []),
         )
         assert len(list(IgnoreManager.find_matching(path, patterns))) == 1
Beispiel #2
0
def upload(sync=True):  # pylint:disable=assign-to-new-keyword
    """Upload code of the current directory while respecting the .polyaxonignore file."""
    project = ProjectManager.get_config_or_raise()
    files = IgnoreManager.get_unignored_filepaths()
    try:
        with create_project_tarfile(files, project.name) as filepath:
            with get_files_by_paths("repo", [filepath]) as (files, files_size):
                try:
                    PolyaxonClient().project.upload_repo(project.user,
                                                         project.name,
                                                         files,
                                                         files_size,
                                                         sync=sync)
                except (
                        PolyaxonHTTPError,
                        PolyaxonShouldExitError,
                        PolyaxonClientException,
                ) as e:
                    handle_cli_error(
                        e,
                        message="Could not upload code for project `{}`.".
                        format(project.name),
                    )
                    Printer.print_error(
                        "Check the project exists, "
                        "and that you have access rights, "
                        "this could happen as well when uploading large files. "
                        "Please also make sure that you have enough space to upload the data."
                    )
                    sys.exit(1)
                Printer.print_success("Files uploaded.")
    except Exception as e:
        handle_cli_error(e, message="Could not upload the file.")
        sys.exit(1)
Beispiel #3
0
    def test_ignores_commented_lines(self, mock_file, _):
        file_data = ["", "# comment", "", "*.py"]
        mock_file.return_value.__enter__.return_value = file_data

        patterns = IgnoreManager.get_config()
        self.assertEqual(
            (self.get_ignored(patterns), self.get_whitelisted(patterns)),
            (["*.py"], []))
Beispiel #4
0
 def test_ignored_lines(self, mock_open, _):
     configs = [
         ("foo.c", "foo.[dh]"),
         ("foo/foo.c", "/foo.c"),
         ("foo/foo.c", "/*.c"),
         ("foo/bar/", "/bar/"),
         ("foo/bar/", "foo/bar/*"),
         ("foo/bar", "foo?bar"),
     ]
     for (path, pattern) in configs:
         mock_open.return_value.__iter__.return_value = [pattern]
         patterns = IgnoreManager.get_config()
         self.assertEqual(
             (self.get_ignored(patterns), self.get_whitelisted(patterns)),
             ([pattern], []),
         )
         assert list(IgnoreManager.find_matching(path, patterns)) == []
Beispiel #5
0
    def test_properly_interprets_whitelisted_globs(self, mock_file, _):
        file_data = ["", "# comment", "*.py", "!file1.py"]
        mock_file.return_value.__enter__.return_value = file_data

        patterns = IgnoreManager.get_config()
        self.assertEqual(
            (self.get_ignored(patterns), self.get_whitelisted(patterns)),
            (["*.py"], ["file1.py"]),
        )
Beispiel #6
0
    def test_trims_slash_prefix_from_abs_paths(self, mock_file, _):
        file_data = ["/test", "!/ignore"]
        mock_file.return_value.__enter__.return_value = file_data

        patterns = IgnoreManager.get_config()
        self.assertEqual(
            (self.get_ignored(patterns), self.get_whitelisted(patterns)),
            (["/test"], ["/ignore"]),
        )
Beispiel #7
0
    def test_escaping_of_globs_that_start_with_reserved_chars(self, mock_open, _):
        file_data = ["", "# comment", "\#file1", "\!file2"]
        mock_open.return_value.__iter__.return_value = file_data

        patterns = IgnoreManager.get_config()
        self.assertEqual(
            (self.get_ignored(patterns), self.get_whitelisted(patterns)),
            (["#file1", "!file2"], []),
        )
Beispiel #8
0
def upload():
    """N.B. This is not available in all distributions.

    Upload code of the current directory while respecting the .polyaxonignore file.
    """
    import sys

    from polyaxon.cli.errors import handle_cli_error
    from polyaxon.client import PolyaxonClient
    from polyaxon.exceptions import (
        PolyaxonClientException,
        PolyaxonHTTPError,
        PolyaxonShouldExitError,
    )
    from polyaxon.managers.ignore import IgnoreManager
    from polyaxon.managers.project import ProjectManager
    from polyaxon.utils.formatting import Printer
    from polyaxon.utils.path_utils import create_project_tarfile, get_files_by_paths

    project = ProjectManager.get_config_or_raise()
    files = IgnoreManager.get_unignored_filepaths()
    try:
        with create_project_tarfile(files, project.name) as filepath:
            with get_files_by_paths("repo", [filepath]) as (files, files_size):
                try:
                    PolyaxonClient().project.upload_repo(project.user,
                                                         project.name,
                                                         files,
                                                         files_size,
                                                         sync=sync)
                except (
                        PolyaxonHTTPError,
                        PolyaxonShouldExitError,
                        PolyaxonClientException,
                ) as e:
                    handle_cli_error(
                        e,
                        message="Could not upload code for project `{}`.".
                        format(project.name),
                    )
                    Printer.print_error(
                        "Check the project exists, "
                        "and that you have access rights, "
                        "this could happen as well when uploading large files. "
                        "Please also make sure that you have enough space to upload the data."
                    )
                    sys.exit(1)
                Printer.print_success("Files uploaded.")
    except Exception as e:
        handle_cli_error(e, message="Could not upload the file.")
        sys.exit(1)
Beispiel #9
0
def init(project, polyaxonfile, purge):
    """Initialize a new polyaxonfile specification."""
    owner, project_name = get_project_or_local(project)
    try:
        polyaxon_client = PolyaxonClient()
        project_config = polyaxon_client.projects_v1.get_project(
            owner, project_name)
    except (ApiException, HTTPError) as e:
        Printer.print_error(
            "Make sure you have a project with this name `{}`".format(project))
        handle_cli_error(
            e,
            message="You can a create new project with this command: "
            "polyaxon project create "
            "--name={} [--description=...] [--tags=...]".format(project_name),
        )
        sys.exit(1)

    if purge:
        ProjectManager.purge()
        IgnoreManager.purge()
    init_project = False
    if ProjectManager.is_initialized():
        local_project = ProjectManager.get_config()
        click.echo(
            "Warning! This project is already initialized with the following project:"
        )
        with indentation.indent(4):
            indentation.puts("User: {}".format(local_project.user))
            indentation.puts("Project: {}".format(local_project.name))
        if click.confirm("Would you like to override this current config?",
                         default=False):
            init_project = True
    else:
        init_project = True

    if init_project:
        ProjectManager.purge()
        config = polyaxon_client.api_client.sanitize_for_serialization(
            project_config)
        ProjectManager.set_config(config, init=True)
        Printer.print_success("Project was initialized")
    else:
        Printer.print_header("Project config was not changed.")

    init_ignore = False
    if IgnoreManager.is_initialized():
        click.echo("Warning! Found a .polyaxonignore file.")
        if click.confirm("Would you like to override it?", default=False):
            init_ignore = True
    else:
        init_ignore = True

    if init_ignore:
        IgnoreManager.init_config()
        Printer.print_success("New .polyaxonignore file was created.")
    else:
        Printer.print_header(".polyaxonignore file was not changed.")

    if polyaxonfile:
        create_polyaxonfile()
        create_debug_polyaxonfile()
Beispiel #10
0
 def test_returns_two_empty_lists_if_file_is_not_present(self, _):
     patterns = IgnoreManager.get_config()
     self.assertEqual(patterns, [])
Beispiel #11
0
 def test_default_props(self):
     assert IgnoreManager.is_global() is False
     assert IgnoreManager.IS_POLYAXON_DIR is False
     assert IgnoreManager.CONFIG_FILE_NAME == ".polyaxonignore"
     assert IgnoreManager.CONFIG is None