def test_update_file(self):
        # setup
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_update_file'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = recipe_name
        file_name = 'description.json'
        message = 'test update CLI-test_update_file'
        api_file_key = file_name
        update_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        runner = CliRunner()  # for the CLI level
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        temp_dir = tempfile.mkdtemp(prefix='unit-tests',
                                    dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(parent_kitchen, temp_dir)
        parent_kitchen_dir = os.path.join(temp_dir, parent_kitchen)
        os.chdir(parent_kitchen_dir)
        original_file = self._get_recipe_file_contents(runner, parent_kitchen,
                                                       recipe_name,
                                                       recipe_file_key,
                                                       file_name)
        result = runner.invoke(
            dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        test_kitchen_dir = os.path.join(temp_dir, test_kitchen)
        os.chdir(test_kitchen_dir)
        new_kitchen_file = self._get_recipe_file_contents(
            runner, test_kitchen, recipe_name, recipe_file_key, file_name,
            temp_dir)
        self.assertEqual(original_file, new_kitchen_file)
        new_kitchen_file_dict = self._get_the_dict(new_kitchen_file)
        new_kitchen_file_abspath = os.path.join(
            test_kitchen_dir, os.path.join(recipe_file_key, file_name))
        new_kitchen_file_dict[test_kitchen] = update_str
        new_kitchen_file2 = self._get_the_json_str(new_kitchen_file_dict)
        with open(new_kitchen_file_abspath, 'w') as rfile:
            rfile.seek(0)
            rfile.truncate()
            rfile.write(new_kitchen_file2)
        # test
        orig_dir = os.getcwd()
        working_dir = os.path.join(test_kitchen_dir, recipe_name)
        os.chdir(working_dir)
        result = runner.invoke(dk, [
            'file-update', '--recipe', recipe_name, '--message', message,
            api_file_key
        ])
        os.chdir(orig_dir)
        self.assertTrue('ERROR' not in result.output)
        new_kitchen_file3 = self._get_recipe_file_contents(
            runner, test_kitchen, recipe_name, recipe_file_key, file_name)
        self.assertEqual(new_kitchen_file2, new_kitchen_file3)

        # cleanup
        runner.invoke(dk, ['kitchen-delete', '--kitchen', test_kitchen])
        shutil.rmtree(temp_dir, ignore_errors=True)
 def _get_recipe_file_contents(self, runner, kitchen, recipe_name, recipe_file_key, file_name, temp_dir=None):
     delete_temp_dir = False
     if temp_dir is None:
         td = tempfile.mkdtemp(prefix='unit-tests-grfc', dir=TestCommandLine._TEMPFILE_LOCATION)
         delete_temp_dir = True
         DKKitchenDisk.write_kitchen(kitchen, td)
         kitchen_directory = os.path.join(td, kitchen)
     else:
         td = temp_dir
         kitchen_directory = os.getcwd()
     cwd = os.getcwd()
     os.chdir(kitchen_directory)
     result = runner.invoke(dk, ['recipe-get', recipe_name])
     os.chdir(cwd)
     rv = result.output
     self.assertTrue(recipe_name in rv)
     abspath = os.path.join(td, os.path.join(kitchen, recipe_file_key, file_name))
     if os.path.isfile(abspath):
         with open(abspath, 'r') as rfile:
             rfile.seek(0)
             the_file = rfile.read()
         rc = the_file
     else:
         rc = None
     if delete_temp_dir is True:
         shutil.rmtree(td, ignore_errors=True)
     return rc
    def write_recipe_state_file_update(recipe_dir, file_recipe_path):
        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(recipe_dir)
        if kitchen_meta_dir is None:
            print "Unable to find kitchen meta directory in '%s'" % recipe_dir
            return False
        recipes_meta_dir = DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir)
        if recipes_meta_dir is None:
            print "Unable to find recipes meta directory in '%s'" % recipe_dir
            return False

        _, recipe_name = os.path.split(recipe_dir)
        recipe_meta_dir = os.path.join(recipes_meta_dir, recipe_name)
        recipe_sha_file = os.path.join(recipe_meta_dir, FILE_SHA)

        contents = DKFileUtils.read_file(recipe_sha_file)
        new_contents = ''
        for line in contents.split('\n'):
            if line.startswith(
                    os.path.join(recipe_name, file_recipe_path) + ':'):
                full_file_path = os.path.join(recipe_dir, file_recipe_path)
                the_sha = DKRecipeDisk.get_sha(full_file_path)
                the_path = os.path.join(recipe_name, file_recipe_path)
                new_contents += '%s:%s\n' % (the_path, the_sha)
            else:
                new_contents += '%s\n' % line
        new_contents = new_contents.strip('\n')
        DKFileUtils.write_file(recipe_sha_file, new_contents)
 def _get_recipe_file_contents(self,
                               runner,
                               kitchen,
                               recipe_name,
                               recipe_file_key,
                               file_name,
                               temp_dir=None):
     delete_temp_dir = False
     if temp_dir is None:
         td = tempfile.mkdtemp(prefix='unit-tests-grfc',
                               dir=TestCommandLine._TEMPFILE_LOCATION)
         delete_temp_dir = True
         DKKitchenDisk.write_kitchen(kitchen, td)
         kitchen_directory = os.path.join(td, kitchen)
     else:
         td = temp_dir
         kitchen_directory = os.getcwd()
     cwd = os.getcwd()
     os.chdir(kitchen_directory)
     result = runner.invoke(dk, ['recipe-get', recipe_name])
     os.chdir(cwd)
     rv = result.output
     self.assertTrue(recipe_name in rv)
     abspath = os.path.join(
         td, os.path.join(kitchen, recipe_file_key, file_name))
     if os.path.isfile(abspath):
         with open(abspath, 'r') as rfile:
             rfile.seek(0)
             the_file = rfile.read()
         rc = the_file
     else:
         rc = None
     if delete_temp_dir is True:
         shutil.rmtree(td, ignore_errors=True)
     return rc
    def test_delete_file_top(self):
        # setup
        temp_dir = None
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_delete_file_top'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = recipe_name
        file_name = 'description.json'
        message = ' test Delete CLI-test_delete_file_top'
        runner = CliRunner()
        cwd = os.getcwd()
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        try:
            temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=TestCommandLine._TEMPFILE_LOCATION)
        except Exception as e:
            self.assertTrue(False, 'Problem creating temp folder %s' % e)
        os.chdir(temp_dir)
        result = runner.invoke(dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kitchen_dir = os.path.join(temp_dir, test_kitchen)
        os.chdir(kitchen_dir)
        result = runner.invoke(dk, ['file-delete',
                                    '--recipe', recipe_name,
                                    '--message', message,
                                    file_name
                                    ])
        self.assertTrue('ERROR' not in result.output)
        self.assertTrue(self._get_recipe_file_contents(runner, test_kitchen, recipe_name,
                                                       recipe_file_key, file_name, temp_dir) is None, "Found the file")
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        os.chdir(cwd)
        shutil.rmtree(temp_dir, ignore_errors=True)
    def get_changed_files(start_dir, recipe_name):

        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(start_dir)
        if kitchen_meta_dir is None:
            print "Unable to find kitchen meta directory in '%s'" % start_dir
            return False, None, None, None
        recipes_meta_dir = DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir)
        if recipes_meta_dir is None:
            print "Unable to find recipes meta directory in '%s'" % start_dir
            return False, None, None, None

        recipe_meta_dir = os.path.join(recipes_meta_dir, recipe_name)

        current_shas = DKRecipeDisk.fetch_shas(start_dir)

        saved_shas = DKRecipeDisk.load_saved_shas(recipe_meta_dir)

        if not saved_shas:
            return False, None, None, None

        current_paths = set(current_shas.keys())
        saved_paths = set(saved_shas.keys())

        common_paths = saved_paths & current_paths

        new_paths = current_paths - saved_paths
        removed_paths = saved_paths - current_paths

        changed_paths = [
            path for path in common_paths
            if current_shas[path] != saved_shas[path]
        ]

        return True, new_paths, changed_paths, removed_paths
    def test_create_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'
        runner = CliRunner()

        # create test kitchen
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        result = runner.invoke(
            dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir = tempfile.mkdtemp(prefix='unit-test_create_file',
                                    dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kd = os.path.join(temp_dir, test_kitchen)
        orig_dir = os.getcwd()
        os.chdir(kd)
        self._get_recipe(runner, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            f = open(api_file_key, 'w')
            f.write(file_contents)
            f.close()
        except ValueError as e:
            print(('could not write file %s.' % e))
            self.assertTrue(False)

        # add file from disk THE TEST
        result = runner.invoke(dk, [
            'file-add', '--recipe', recipe_name, '--message', message,
            api_file_key
        ])
        self.assertTrue('ERROR' not in result.output)

        # make sure file is in kitchen (get file)
        file_contents2 = self._get_recipe_file_contents(
            runner, test_kitchen, recipe_name, recipe_file_key, file_name)
        self.assertEqual(file_contents, file_contents2, 'Create check')

        # cleanup
        os.chdir(orig_dir)
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_update_file(self):
        # setup
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_update_file'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = recipe_name
        file_name = 'description.json'
        message = 'test update CLI-test_update_file'
        api_file_key = file_name
        update_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        runner = CliRunner()  # for the CLI level
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(parent_kitchen, temp_dir)
        parent_kitchen_dir = os.path.join(temp_dir, parent_kitchen)
        os.chdir(parent_kitchen_dir)
        original_file = self._get_recipe_file_contents(runner, parent_kitchen, recipe_name, recipe_file_key, file_name)
        result = runner.invoke(dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        test_kitchen_dir = os.path.join(temp_dir, test_kitchen)
        os.chdir(test_kitchen_dir)
        new_kitchen_file = self._get_recipe_file_contents(runner, test_kitchen, recipe_name,
                                                          recipe_file_key, file_name, temp_dir)
        self.assertEqual(original_file, new_kitchen_file)
        new_kitchen_file_dict = self._get_the_dict(new_kitchen_file)
        new_kitchen_file_abspath = os.path.join(test_kitchen_dir, os.path.join(recipe_file_key, file_name))
        new_kitchen_file_dict[test_kitchen] = update_str
        new_kitchen_file2 = self._get_the_json_str(new_kitchen_file_dict)
        with open(new_kitchen_file_abspath, 'w') as rfile:
            rfile.seek(0)
            rfile.truncate()
            rfile.write(new_kitchen_file2)
        # test
        orig_dir = os.getcwd()
        working_dir = os.path.join(test_kitchen_dir, recipe_name)
        os.chdir(working_dir)
        result = runner.invoke(dk, ['file-update',
                                    '--recipe', recipe_name,
                                    '--message', message,
                                    api_file_key])
        os.chdir(orig_dir)
        self.assertTrue('ERROR' not in result.output)
        new_kitchen_file3 = self._get_recipe_file_contents(runner, test_kitchen, recipe_name,
                                                           recipe_file_key, file_name)
        self.assertEqual(new_kitchen_file2, new_kitchen_file3)

        # cleanup
        runner.invoke(dk, ['kitchen-delete', '--kitchen', test_kitchen])
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_kitchen_which(self):

        kn = 'bobo'
        temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=TestCommandLine._TEMPFILE_LOCATION)
        os.chdir(temp_dir)
        DKKitchenDisk.write_kitchen(kn, temp_dir)
        os.chdir(os.path.join(temp_dir, kn))

        runner = CliRunner()
        result = runner.invoke(dk, ['kitchen-which'])
        self.assertTrue(0 == result.exit_code)
        self.assertIn('bobo', result.output)
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_kitchen_which(self):

        kn = 'bobo'
        temp_dir = tempfile.mkdtemp(prefix='unit-tests',
                                    dir=TestCommandLine._TEMPFILE_LOCATION)
        os.chdir(temp_dir)
        DKKitchenDisk.write_kitchen(kn, temp_dir)
        os.chdir(os.path.join(temp_dir, kn))

        runner = CliRunner()
        result = runner.invoke(dk, ['kitchen-which'])
        self.assertTrue(0 == result.exit_code)
        self.assertIn('bobo', result.output)
        shutil.rmtree(temp_dir, ignore_errors=True)
Example #11
0
    def _get_my_recipe_meta(kitchen_meta_dir, recipe_name):
        recipe_meta_file_path = os.path.join(DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir), recipe_name,
                                             RECIPE_META)
        if not os.path.isfile(recipe_meta_file_path):
            return None

        with open(recipe_meta_file_path, 'r') as recipe_meta_file:
            contents = recipe_meta_file.read()
        return contents
    def _get_my_recipe_meta(kitchen_meta_dir, recipe_name):
        recipe_meta_file_path = os.path.join(DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir), recipe_name,
                                             RECIPE_META)
        if not os.path.isfile(recipe_meta_file_path):
            return None

        with open(recipe_meta_file_path, 'r') as recipe_meta_file:
            contents = recipe_meta_file.read()
        return contents
    def test_delete_file_deeper_multi(self):
        # setup
        temp_dir = None
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_delete_file_deeper_multi'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = 'resources/very_cool.sql'
        file_name = 'very_cool.sql'
        file2 = 'description.json'
        message = ' test Delete CLI-test_delete_file_deeper_multi'
        runner = CliRunner()
        cwd = os.getcwd()
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        try:
            temp_dir = tempfile.mkdtemp(prefix='unit-tests',
                                        dir=TestCommandLine._TEMPFILE_LOCATION)
        except Exception as e:
            self.assertTrue(False, 'Problem creating temp folder %s' % e)
        os.chdir(temp_dir)
        result = runner.invoke(
            dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kitchen_dir = os.path.join(temp_dir, test_kitchen)
        os.chdir(kitchen_dir)

        result = runner.invoke(dk, [
            'file-delete', '--recipe', recipe_name, '--message', message,
            recipe_file_key, file2
        ])
        self.assertTrue('ERROR' not in result.output)
        self.assertTrue(
            self._get_recipe_file_contents(
                runner, test_kitchen, recipe_name,
                os.path.join(recipe_name, recipe_file_key), file_name,
                temp_dir) is None)
        runner.invoke(dk, ['kitchen-delete', '--kitchen', test_kitchen])
        os.chdir(cwd)
        shutil.rmtree(temp_dir, ignore_errors=True)
    def find_recipe_meta_dir(check_dir=None):
        recipe_root_dir = DKRecipeDisk.find_recipe_root_dir(check_dir)
        if recipe_root_dir is None:
            return None

        recipe_name = DKRecipeDisk.find_recipe_name(recipe_root_dir)
        if recipe_name is None:
            return None

        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(recipe_root_dir)
        if kitchen_meta_dir is None:
            print "Unable to find kitchen meta directory in '%s'" % check_dir
            return False

        recipes_meta_dir = DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir)
        if recipes_meta_dir is None:
            print "Unable to find recipes meta directory in '%s'" % check_dir
            return False

        recipe_meta_dir = os.path.join(recipes_meta_dir, recipe_name)
        return recipe_meta_dir
    def write_recipe_state(recipe_dir):

        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(recipe_dir)
        if kitchen_meta_dir is None:
            print "Unable to find kitchen meta directory in '%s'" % recipe_dir
            return False
        recipes_meta_dir = DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir)
        if recipes_meta_dir is None:
            print "Unable to find recipes meta directory in '%s'" % recipe_dir
            return False

        _, recipe_name = os.path.split(recipe_dir)

        recipe_meta_dir = os.path.join(recipes_meta_dir, recipe_name)

        shas = DKRecipeDisk.fetch_shas(recipe_dir)

        recipe_sha_file = os.path.join(recipe_meta_dir, FILE_SHA)

        with open(recipe_sha_file, 'w') as f:
            f.write('\n'.join(['%s:%s' % item for item in shas.items()]))
    def find_recipe_meta_dir(check_dir=None):
        recipe_root_dir = DKRecipeDisk.find_recipe_root_dir(check_dir)
        if recipe_root_dir is None:
            return None

        recipe_name = DKRecipeDisk.find_recipe_name(recipe_root_dir)
        if recipe_name is None:
            return None

        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(recipe_root_dir)
        if kitchen_meta_dir is None:
            print "Unable to find kitchen meta directory in '%s'" % check_dir
            return False

        recipes_meta_dir = DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir)
        if recipes_meta_dir is None:
            print "Unable to find recipes meta directory in '%s'" % check_dir
            return False

        recipe_meta_dir = os.path.join(recipes_meta_dir, recipe_name)
        return recipe_meta_dir
    def write_recipe_state_file_add(recipe_dir, file_recipe_path):
        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(recipe_dir)
        if kitchen_meta_dir is None:
            print "Unable to find kitchen meta directory in '%s'" % recipe_dir
            return False
        recipes_meta_dir = DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir)
        if recipes_meta_dir is None:
            print "Unable to find recipes meta directory in '%s'" % recipe_dir
            return False

        _, recipe_name = os.path.split(recipe_dir)
        recipe_meta_dir = os.path.join(recipes_meta_dir, recipe_name)
        recipe_sha_file = os.path.join(recipe_meta_dir, FILE_SHA)

        contents = DKFileUtils.read_file(recipe_sha_file)
        the_path = os.path.join(recipe_name, file_recipe_path)
        full_file_path = os.path.join(recipe_dir, file_recipe_path)
        the_sha = DKRecipeDisk.get_sha(full_file_path)
        new_line = '\n%s:%s' % (the_path, the_sha)
        contents += new_line
        new_contents = contents.strip('\n')
        DKFileUtils.write_file(recipe_sha_file, new_contents)
Example #18
0
    def add_conflict_to_conflicts_meta(conflict_info, folder_in_recipe, recipe_name, kitchen_dir):
        recipe_meta_dir = DKKitchenDisk.get_recipe_meta_dir(recipe_name, kitchen_dir)
        conflicts_meta = DKRecipeDisk.get_conflicts_meta(recipe_meta_dir)
        conflict_key = '%s|%s|%s|%s|%s' % (conflict_info['from_kitchen'], conflict_info['to_kitchen'], recipe_name,
                                           folder_in_recipe, conflict_info['filename'])

        conflict_for_save = conflict_info.copy()
        conflict_for_save['folder_in_recipe'] = folder_in_recipe
        conflict_for_save['status'] = 'unresolved'
        if folder_in_recipe not in conflicts_meta:
            conflicts_meta[folder_in_recipe] = {}
        conflicts_meta[folder_in_recipe][conflict_key] = conflict_for_save
        return DKRecipeDisk.save_conflicts_meta(recipe_meta_dir, conflicts_meta)
    def _find_recipe(walk_dir=None, return_recipe_root_path=False):

        if walk_dir is None:
            walk_dir = os.getcwd()

        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(walk_dir)
        if kitchen_meta_dir is None:
            # We aren't in a kitchen folder.
            return None

        kitchen_root_dir = DKKitchenDisk.find_kitchen_root_dir(walk_dir)
        if kitchen_root_dir is None:
            # We aren't in a kitchen folder.
            return None

        if walk_dir == kitchen_root_dir or walk_dir == kitchen_meta_dir:
            # We are in the kitchen root folder. Can't do anything here.
            return None

        current_dir_abs = os.path.abspath(walk_dir)
        kitchen_root_dir_abs = os.path.abspath(kitchen_root_dir)

        common = os.path.commonprefix([current_dir_abs, kitchen_root_dir_abs])
        current_dir_relative = current_dir_abs.replace(common + os.sep, '')
        parts = current_dir_relative.split(os.sep)
        if len(parts) == 0:
            # Looks like we are in the kitchen folder.
            return None

        recipe_name = parts[0]
        recipe_meta_contents = DKRecipeDisk._get_my_recipe_meta(
            kitchen_meta_dir, recipe_name)
        if recipe_name == recipe_meta_contents:
            if not return_recipe_root_path:
                return recipe_name
            else:
                return os.path.join(kitchen_root_dir, recipe_name)
        else:
            return None
    def add_conflict_to_conflicts_meta(conflict_info, folder_in_recipe, recipe_name, kitchen_dir):
        recipe_meta_dir = DKKitchenDisk.get_recipe_meta_dir(recipe_name, kitchen_dir)
        conflicts_meta = DKRecipeDisk.get_conflicts_meta(recipe_meta_dir)
        conflict_key = '%s|%s|%s|%s|%s' % (conflict_info['from_kitchen'], conflict_info['to_kitchen'], recipe_name,
                                           folder_in_recipe, conflict_info['filename'])

        conflict_for_save = conflict_info.copy()
        conflict_for_save['folder_in_recipe'] = folder_in_recipe
        conflict_for_save['status'] = 'unresolved'
        if folder_in_recipe not in conflicts_meta:
            conflicts_meta[folder_in_recipe] = {}
        conflicts_meta[folder_in_recipe][conflict_key] = conflict_for_save
        return DKRecipeDisk.save_conflicts_meta(recipe_meta_dir, conflicts_meta)
    def _find_recipe(walk_dir=None, return_recipe_root_path=False):

        if walk_dir is None:
            walk_dir = os.getcwd()

        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(walk_dir)
        if kitchen_meta_dir is None:
            # We aren't in a kitchen folder.
            return None

        kitchen_root_dir = DKKitchenDisk.find_kitchen_root_dir(walk_dir)
        if kitchen_root_dir is None:
            # We aren't in a kitchen folder.
            return None

        if walk_dir == kitchen_root_dir or walk_dir == kitchen_meta_dir:
            # We are in the kitchen root folder. Can't do anything here.
            return None

        current_dir_abs = os.path.abspath(walk_dir)
        kitchen_root_dir_abs = os.path.abspath(kitchen_root_dir)

        common = os.path.commonprefix([current_dir_abs, kitchen_root_dir_abs])
        current_dir_relative = current_dir_abs.replace(common + os.sep, '')
        parts = current_dir_relative.split(os.sep)
        if len(parts) == 0:
            # Looks like we are in the kitchen folder.
            return None

        recipe_name = parts[0]
        recipe_meta_contents = DKRecipeDisk._get_my_recipe_meta(kitchen_meta_dir, recipe_name)
        if recipe_name == recipe_meta_contents:
            if not return_recipe_root_path:
                return recipe_name
            else:
                return os.path.join(kitchen_root_dir, recipe_name)
        else:
            return None
    def test_create_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'
        runner = CliRunner()

        # create test kitchen
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        result = runner.invoke(
            dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir = tempfile.mkdtemp(prefix='unit-test_create_file',
                                    dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kd = os.path.join(temp_dir, test_kitchen)
        orig_dir = os.getcwd()
        os.chdir(kd)
        self._get_recipe(runner, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            f = open(api_file_key, 'w')
            f.write(file_contents)
            f.close()
        except ValueError, e:
            print('could not write file %s.' % e)
            self.assertTrue(False)
    def write_recipe_meta(self, start_dir):
        if not DKKitchenDisk.is_kitchen_root_dir(start_dir):
            print "'%s' is not a Kitchen directory" % start_dir
            return False

        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(start_dir)
        if kitchen_meta_dir is None:
            print "Unable to find kitchen meta directory in '%s'" % start_dir
            return False
        recipes_meta_dir = DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir)
        if recipes_meta_dir is None:
            print "Unable to find recipes meta directory in '%s'" % start_dir
            return False

        recipe_meta_dir = os.path.join(recipes_meta_dir, self._recipe_name)
        if not os.path.isdir(recipe_meta_dir):
            try:
                os.makedirs(recipe_meta_dir)
            except OSError as e:
                print "%s - %s - %s" % (e.filename, e.errno, e.message)
                return False
        recipes_meta_file = os.path.join(recipe_meta_dir, RECIPE_META)
        try:
            with open(recipes_meta_file, 'w') as f:
                f.write(self._recipe_name)
        except OSError as e:
            print "%s - %s - %s" % (e.filename, e.errno, e.message)
            return False

        orig_head_file = os.path.join(recipe_meta_dir, ORIG_HEAD)
        try:
            with open(orig_head_file, 'w') as f:
                f.write(self._recipe_sha)
        except OSError as e:
            print "%s - %s - %s" % (e.filename, e.errno, e.message)
            return False
        return True
    def write_recipe_meta(self, start_dir):
        if not DKKitchenDisk.is_kitchen_root_dir(start_dir):
            print "'%s' is not a Kitchen directory" % start_dir
            return False

        kitchen_meta_dir = DKKitchenDisk.find_kitchen_meta_dir(start_dir)
        if kitchen_meta_dir is None:
            print "Unable to find kitchen meta directory in '%s'" % start_dir
            return False
        recipes_meta_dir = DKKitchenDisk.get_recipes_meta_dir(kitchen_meta_dir)
        if recipes_meta_dir is None:
            print "Unable to find recipes meta directory in '%s'" % start_dir
            return False

        recipe_meta_dir = os.path.join(recipes_meta_dir, self._recipe_name)
        if not os.path.isdir(recipe_meta_dir):
            try:
                os.makedirs(recipe_meta_dir)
            except OSError as e:
                print "%s - %s - %s" % (e.filename, e.errno, e.message)
                return False
        recipes_meta_file = os.path.join(recipe_meta_dir, RECIPE_META)
        try:
            with open(recipes_meta_file, 'w') as f:
                f.write(self._recipe_name)
        except OSError as e:
            print "%s - %s - %s" % (e.filename, e.errno, e.message)
            return False

        orig_head_file = os.path.join(recipe_meta_dir, ORIG_HEAD)
        try:
            with open(orig_head_file, 'w') as f:
                f.write(self._recipe_sha)
        except OSError as e:
            print "%s - %s - %s" % (e.filename, e.errno, e.message)
            return False
        return True
    def test_create_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'
        runner = CliRunner()

        # create test kitchen
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        result = runner.invoke(dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir = tempfile.mkdtemp(prefix='unit-test_create_file', dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kd = os.path.join(temp_dir, test_kitchen)
        orig_dir = os.getcwd()
        os.chdir(kd)
        self._get_recipe(runner, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            f = open(api_file_key, 'w')
            f.write(file_contents)
            f.close()
        except ValueError, e:
            print('could not write file %s.' % e)
            self.assertTrue(False)
 def write_recipe_state_from_kitchen(self, start_dir):
     if not DKKitchenDisk.is_kitchen_root_dir(start_dir):
         print "'%s' is not a Kitchen directory" % start_dir
         return False
     DKRecipeDisk.write_recipe_state(
         os.path.join(start_dir, self._recipe_name))