"""Remove a repository from the Arcyd instance."""
# =============================================================================
# CONTENTS
# -----------------------------------------------------------------------------
# abdcmd_rmrepo
#
# Public Functions:
#   getFromfilePrefixChars
#   setupParser
#   process
#
# -----------------------------------------------------------------------------
# (this contents block is generated, edits will be lost)
# =============================================================================

from __future__ import absolute_import

import os
import shutil

import phlsys_pid

import abdt_fs


def getFromfilePrefixChars():
    return None


def setupParser(parser):
    parser.add_argument(
        'name',
        type=str,
        metavar='STR',
        help="string identifier of the repository to remove.")


def process(args):

    fs = abdt_fs.make_default_accessor()

    with fs.lockfile_context():
        pid = fs.get_pid_or_none()
        if pid is not None and phlsys_pid.is_running(pid):
            raise Exception("cannot remove repo whilst arcyd is running.")

        repo_name = args.name

        os.remove(fs.layout.repo_try(repo_name))
        os.remove(fs.layout.repo_ok(repo_name))
        shutil.rmtree(fs.layout.repo(repo_name))
        fs.remove_repo_config(repo_name)


# -----------------------------------------------------------------------------
# Copyright (C) 2014 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------ END-OF-FILE ----------------------------------

"""Remove a repository from the Arcyd instance."""
# =============================================================================
# CONTENTS
# -----------------------------------------------------------------------------
# abdcmd_rmrepo
#
# Public Functions:
#   getFromfilePrefixChars
#   setupParser
#   process
#
# -----------------------------------------------------------------------------
# (this contents block is generated, edits will be lost)
# =============================================================================

from __future__ import absolute_import

import os
import shutil

import phlsys_pid

import abdt_fs


def getFromfilePrefixChars():
    return None


def setupParser(parser):
    parser.add_argument(
        'name',
        type=str,
        metavar='STR',
        help="string identifier of the repository to remove.")


def _remove_file_ignore_fail(path):
    try:
        os.remove(path)
    except OSError as e:
        print "Warning, problem removing file '{}':".format(path)
        print "  {}".format(e)
        print


def _remove_dir_ignore_fail(path):
    try:
        shutil.rmtree(path)
    except OSError as e:
        print "Warning, problem removing dir '{}':".format(path)
        print "  {}".format(e)
        print


def process(args):

    fs = abdt_fs.make_default_accessor()

    with fs.lockfile_context():
        pid = fs.get_pid_or_none()
        if pid is not None and phlsys_pid.is_running(pid):
            raise Exception("cannot remove repo whilst arcyd is running.")

        repo_name = args.name

        _remove_file_ignore_fail(fs.layout.repo_try(repo_name))
        _remove_file_ignore_fail(fs.layout.repo_ok(repo_name))
        _remove_dir_ignore_fail(fs.layout.repo(repo_name))
        fs.remove_repo_config(repo_name)


# -----------------------------------------------------------------------------
# Copyright (C) 2014 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------ END-OF-FILE ----------------------------------

Fix 'arcyd rm-repo' to tolerate missing files