Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ba6ed1

Browse files
author
Richard Boulton
committed
Add topic_changes.move_topics command
This coordinates a potentially large set of changes. Also add move_in_panopticon and delete_topic scripts, to do the updates required in panopticon.
1 parent c4c2ece commit 5ba6ed1

File tree

1 file changed

+98
-4
lines changed

1 file changed

+98
-4
lines changed

topic_change.py

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from fabric import api
22

3+
import csv
34
import glob
45
import os
56
import re
@@ -23,6 +24,10 @@ def prepare(output_directory, source_topic, destination_topic):
2324
The CSV files are then downloaded to the local directory specified.
2425
2526
"""
27+
do_prepare(output_directory, source_topic, destination_topic)
28+
29+
30+
def do_prepare(output_directory, source_topic, destination_topic):
2631
if not os.path.isdir(output_directory):
2732
os.makedirs(output_directory)
2833
prepare_publisher_csv(output_directory, source_topic, destination_topic)
@@ -41,6 +46,10 @@ def process(file_pattern):
4146
to process.
4247
4348
"""
49+
do_process(file_pattern)
50+
51+
52+
def do_process(file_pattern):
4453
paths_by_app = {}
4554
for path in glob.glob(file_pattern):
4655
app = os.path.basename(path).split("_")[0]
@@ -53,6 +62,80 @@ def process(file_pattern):
5362
', '.join(paths_by_app.keys()))
5463

5564

65+
@api.task
66+
def move_in_panopticon(source_topic, destination_topic):
67+
"""Move content tagged in panopticon
68+
69+
"""
70+
do_move_in_panopticon(source_topic, destination_topic)
71+
72+
def do_move_in_panopticon(source_topic, destination_topic):
73+
util.use_random_host('class-backend')
74+
print("\nRetagging content on %s" % (api.env.host_string,))
75+
util.rake('panopticon', 'move_content_to_new_topic',
76+
source=source_topic, dest=destination_topic)
77+
78+
79+
@api.task
80+
def delete_topic(old_topic):
81+
"""Delete a topic, causing it to return a 410 gone status.
82+
83+
"""
84+
do_delete_topic(old_topic)
85+
86+
def do_delete_topic(old_topic):
87+
util.use_random_host('class-backend')
88+
print("\nDeleting old topic %s" % (old_topic,))
89+
util.rake('panopticon', 'specialist_sector_cleanup', SLUG=old_topic)
90+
91+
92+
@api.task
93+
def move_topics(csv_filename, output_directory, action="all"):
94+
"""Apply a set of topic changes from a CSV.
95+
96+
This takes a CSV which maps from old topic slug to new topic slug, and
97+
coordinates running all the changes.
98+
99+
"""
100+
topic_slug_changes = read_topic_slug_changes(csv_filename)
101+
action = action.lower()
102+
103+
if action in ["all", "prepare"]:
104+
if os.path.exists(output_directory):
105+
print "Output directory already exists"
106+
print "Aborting, to avoid risk of pulling in unexpected changes."
107+
exit(1)
108+
os.makedirs(output_directory)
109+
for old_slug, new_slug in topic_slug_changes.items():
110+
do_prepare(output_directory, old_slug, new_slug)
111+
112+
if action in ["all", "process"]:
113+
do_process(output_directory + "/*.csv")
114+
115+
if action in ["all", "panopticon"]:
116+
for old_slug, new_slug in topic_slug_changes.items():
117+
move_in_panopticon(old_slug, new_slug)
118+
119+
if action in ["all", "delete"]:
120+
for old_slug in topic_slug_changes.keys():
121+
delete_topic(old_slug)
122+
123+
124+
def read_topic_slug_changes(csv_filename):
125+
changes = {}
126+
with open(csv_filename) as fobj:
127+
reader = csv.reader(fobj)
128+
headings = [
129+
heading.lower().strip().replace(' ', '_')
130+
for heading in reader.next()
131+
]
132+
old_index = headings.index('old_topic_slug')
133+
new_index = headings.index('new_topic_slug')
134+
for row in reader:
135+
changes[row[old_index]] = row[new_index]
136+
return changes
137+
138+
56139
def slugify(topic):
57140
return re.sub('[^a-z0-9]', '_', topic.lower())
58141

@@ -68,7 +151,7 @@ def build_filename(app, source_topic, destination_topic):
68151

69152
def prepare_publisher_csv(output_directory, source_topic, destination_topic):
70153
util.use_random_host('class-backend')
71-
print("Fetching publisher change CSV from %s" % (api.env.host_string,))
154+
print("\nFetching publisher change CSV from %s" % (api.env.host_string,))
72155

73156
filename = build_filename('publisher', source_topic, destination_topic)
74157
remote_path = '/var/apps/publisher/tmp/%s' % (filename,)
@@ -82,7 +165,7 @@ def prepare_publisher_csv(output_directory, source_topic, destination_topic):
82165

83166
def prepare_whitehall_csv(output_directory, source_topic, destination_topic):
84167
util.use_random_host('class-whitehall_backend')
85-
print("Fetching whitehall change CSV from %s" % (api.env.host_string,))
168+
print("\nFetching whitehall change CSV from %s" % (api.env.host_string,))
86169

87170
filename = build_filename('whitehall', source_topic, destination_topic)
88171
remote_path = '/var/apps/whitehall/tmp/%s' % (filename,)
@@ -103,17 +186,25 @@ def process_publisher_csvs(paths):
103186

104187
def process_publisher_csv(path):
105188
filename = os.path.basename(path)
106-
print("Applying publisher change CSV %s on %s" % (
189+
print("\nApplying publisher change CSV %s on %s" % (
107190
filename,
108191
api.env.host_string,
109192
))
193+
if file_length(path) == 1:
194+
print("No changes in file - skipping")
195+
return
110196

111197
remote_path = '/var/apps/publisher/tmp/%s' % (filename, )
112198
api.put(path, remote_path, use_sudo=True)
113199
util.rake('publisher', 'topic_changes:process', remote_path)
114200
api.sudo("rm '%s'" % (remote_path,))
115201

116202

203+
def file_length(path):
204+
with open(path) as fobj:
205+
return len(fobj.readlines())
206+
207+
117208
def process_whitehall_csvs(paths):
118209
util.use_random_host('class-whitehall_backend')
119210
for path in paths:
@@ -122,10 +213,13 @@ def process_whitehall_csvs(paths):
122213

123214
def process_whitehall_csv(path):
124215
filename = os.path.basename(path)
125-
print("Applying whitehall change CSV %s on %s" % (
216+
print("\nApplying whitehall change CSV %s on %s" % (
126217
filename,
127218
api.env.host_string,
128219
))
220+
if file_length(path) == 1:
221+
print("No changes in file - skipping")
222+
return
129223

130224
remote_path = '/var/apps/whitehall/tmp/%s' % (filename, )
131225
api.put(path, remote_path, use_sudo=True)

0 commit comments

Comments
 (0)