aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xbin/fixmystreet.com/build_eastsussex_templates96
-rwxr-xr-xtemplates/web/eastsussex/build_eastsussex_templates.py96
2 files changed, 96 insertions, 96 deletions
diff --git a/bin/fixmystreet.com/build_eastsussex_templates b/bin/fixmystreet.com/build_eastsussex_templates
new file mode 100755
index 000000000..b22f5214f
--- /dev/null
+++ b/bin/fixmystreet.com/build_eastsussex_templates
@@ -0,0 +1,96 @@
+#!/usr/bin/env perl
+#
+# Build header.html and footer.html from online East Sussex template fragments.
+
+use strict;
+use warnings;
+use feature 'say';
+
+my %TEMPLATES = (
+ "header.html.template" => [
+ "HtmlTag",
+ "MetadataDesktop",
+ [
+ "HeaderDesktop",
+ [
+ ["<header>", '<header class="eastsussex">'],
+ ]
+ ],
+ ],
+ "footer.html.template" => [
+ "FooterDesktop",
+ ]
+);
+
+my $BASE_URL = "https://www.eastsussex.gov.uk/masterpages/remote/control.aspx?control=%s&host=mysociety.org";
+
+use File::Basename qw(dirname basename);
+use File::Spec;
+my $DIR;
+BEGIN {
+ $DIR = dirname(File::Spec->rel2abs($0)) . '/../..';
+ require "$DIR/setenv.pl";
+}
+
+use File::ChangeNotify;
+use Getopt::Long;
+use LWP::Simple qw($ua);
+use Path::Tiny;
+
+$ua->agent("FixMyStreet/1.0");
+# The server seems to have some issues with SSL negotiation
+$ua->ssl_opts(SSL_version => 'SSLv23:!SSLv2:!SSLv3:!TLSv11:!TLSv12');
+
+chdir "$DIR/templates/web/eastsussex";
+
+GetOptions('watch' => \my $watch);
+
+if ($watch) {
+ watch_local_files();
+} else {
+ update_templates();
+}
+
+# ---
+
+sub update_templates {
+ while (my ($template_path, $fragment_names) = each %TEMPLATES) {
+ my %fragments;
+ foreach my $name (@$fragment_names) {
+ my $patches;
+ ($name, $patches) = @$name if ref $name;
+
+ my $url = sprintf($BASE_URL, $name);
+ my $content = LWP::Simple::get($url);
+ # If we got nothing, bail out!
+ return unless $content;
+
+ $content =~ s/\r//g;
+ $content =~ s/$_->[0]/$_->[1]/ for @$patches;
+ $fragments{$name} = $content;
+ path("$name.html")->spew_utf8($fragments{$name});
+ }
+ my $template = path($template_path)->slurp_utf8;
+ $template =~ s/{$_}/$fragments{$_}/ for keys %fragments;
+ path(substr($template_path, 0, -9))->spew_utf8($template);
+ }
+ return 1;
+}
+
+sub watch_local_files {
+ say "Watching for changes to: " . join(', ', keys %TEMPLATES);
+ my $files = join('|', keys %TEMPLATES);
+ my $watcher = File::ChangeNotify->instantiate_watcher(
+ directories => '.',
+ filter => qr/$files/,
+ );
+
+ while ( my @events = $watcher->wait_for_events() ) {
+ for my $event (@events) {
+ my $filename = basename($event->path);
+ say "$filename has changed, updating templates...";
+ my $success = update_templates();
+ say $success ? "done." : 'Failed.';
+ }
+ }
+}
diff --git a/templates/web/eastsussex/build_eastsussex_templates.py b/templates/web/eastsussex/build_eastsussex_templates.py
deleted file mode 100755
index 389755042..000000000
--- a/templates/web/eastsussex/build_eastsussex_templates.py
+++ /dev/null
@@ -1,96 +0,0 @@
-#!/usr/bin/env python
-import urllib2
-import os
-import sys
-import argparse
-import time
-
-try:
- from fsevents import Stream, Observer
- WATCH_AVAILABLE = True
-except ImportError:
- WATCH_AVAILABLE = False
-
-TEMPLATES = {
- "header.html.template": (
- "HtmlTag",
- "MetadataDesktop",
- (
- "HeaderDesktop",
- (
- ("<header>", '<header class="eastsussex">'),
- )
- ),
- ),
- "footer.html.template": (
- "FooterDesktop",
- )
-}
-
-BASE_URL = "https://www.eastsussex.gov.uk/masterpages/remote/control.aspx?control={fragment}&host=mysociety.org"
-
-
-def patch_fragment(fragment, patches):
- if not patches:
- return fragment
- for search, replacement in patches:
- fragment = fragment.replace(search, replacement)
- return fragment
-
-
-def update_templates():
- for template_path, fragment_names in TEMPLATES.items():
- template = open(template_path).read()
- fragments = {}
- for name in fragment_names:
- if isinstance(name, tuple):
- name, patches = name
- else:
- patches = None
- url = BASE_URL.format(fragment=name)
- content = urllib2.urlopen(url).read().replace("\r", "")
- fragments[name] = patch_fragment(content, patches)
- open("{0}.html".format(name), "wb").write(fragments[name])
- with open(template_path[:-9], "wb") as outfile:
- outfile.write(template.format(**fragments))
-
-
-def event_callback(event):
- filename = os.path.basename(event.name)
- if filename in TEMPLATES.keys():
- print "{} has changed, updating templates...".format(filename)
- update_templates()
- print "done."
-
-def watch_local_files():
- print "Watching for changes to: {}".format(", ".join(TEMPLATES.keys()))
- observer = Observer()
- stream = Stream(event_callback, os.getcwd(), file_events=True)
- observer.schedule(stream)
- try:
- observer.start()
- while True:
- time.sleep(86400)
- except KeyboardInterrupt:
- observer.stop()
-
-
-def main():
- os.chdir(os.path.dirname(__file__))
-
- parser = argparse.ArgumentParser(description="Build header.html and footer.html from online East Sussex template fragments.")
- parser.add_argument("-w", "--watch", action="store_true")
-
- args = parser.parse_args()
-
- if args.watch:
- if not WATCH_AVAILABLE:
- print "Watch functionality not available. This currently needs OS X and the macfsevents Python package."
- sys.exit(1)
- watch_local_files()
- else:
- update_templates()
-
-
-if __name__ == '__main__':
- main()