~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/scripts/remove_charm.py

  • Committer: Benji York
  • Date: 2013-11-18 20:38:47 UTC
  • mto: This revision was merged to the branch mainline in revision 465.
  • Revision ID: benji@benjiyork.com-20131118203847-2mfs1w7b8aqy64mr
checkpoint

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
__metaclass__ = type
7
7
 
8
8
import sys
9
 
from charmworld.utils import (
10
 
    delete_charm,
11
 
    get_ini,
12
 
)
 
9
from pyelasticsearch.exceptions import ElasticHttpNotFoundError
 
10
from charmworld.utils import get_ini
13
11
from charmworld.models import (
 
12
    CharmSource,
 
13
    FeaturedSource,
14
14
    getconnection,
15
15
    getdb,
16
16
)
17
17
from charmworld.search import ElasticSearchClient
18
18
 
19
19
 
20
 
CHARM_ID_MESSAGE = (
21
 
    'Please provide a charm ID, including revision, e.g., '
22
 
    '~charmers/precise/apache2/14')
23
 
 
24
 
 
25
20
def main(argv, stderr=sys.stderr, db=None, index_client=None):
26
 
    """The main function for the charm removal script."""
 
21
    """The main function for the server start timestamp removal script."""
27
22
    if len(argv) != 2:
28
23
        print(
29
24
            'Please provide the ID of the charm you wish to remove.',
42
37
    charm_data = db.charms.find_one(charm_query)
43
38
    if charm_data is None:
44
39
        print('Unknown charm ID: %r' % charm_id, file=stderr)
45
 
        print(CHARM_ID_MESSAGE, file=stderr)
46
40
        return 1
47
41
 
48
42
    if index_client is None:
49
43
        index_client = ElasticSearchClient.from_settings(settings)
50
44
 
51
 
    delete_charm(charm_data, db, index_client)
 
45
    # The charm needs to be removed from the search engine.
 
46
    try:
 
47
        index_client.delete_charm(charm_data)
 
48
    except ElasticHttpNotFoundError:
 
49
        # It is fine if the charm was not indexed.
 
50
        pass
 
51
 
 
52
    # If the charm is featured, we want to unfeature it.
 
53
    source = FeaturedSource.from_db(db)
 
54
    source.clear_featured(charm_data, 'charm')
 
55
 
 
56
    # Finally, remove all the revisions of the charm document from the
 
57
    # database.
 
58
    charm_query = CharmSource.make_all_revisions_query(charm_data)
 
59
    db.charms.remove(charm_query, justOne=True)
52
60
    return 0
53
61
 
54
62