Get http_status working under Bazel

Change-Id: Ife3c04bc254c1bc06b8208d026df2323862533dc
diff --git a/aos/externals/seasocks/BUILD b/aos/externals/seasocks/BUILD
new file mode 100644
index 0000000..d4411f1
--- /dev/null
+++ b/aos/externals/seasocks/BUILD
@@ -0,0 +1,7 @@
+py_binary(
+  name = 'gen_embedded',
+  visibility = ['//visibility:public'],
+  srcs = [
+    'gen_embedded.py',
+  ],
+)
diff --git a/aos/externals/seasocks/gen_embedded.bzl b/aos/externals/seasocks/gen_embedded.bzl
new file mode 100644
index 0000000..4bba776
--- /dev/null
+++ b/aos/externals/seasocks/gen_embedded.bzl
@@ -0,0 +1,54 @@
+def _gen_embedded_impl(ctx):
+  ctx.action(
+    inputs = ctx.files.srcs,
+    outputs = [ ctx.outputs.header ],
+    executable = ctx.executable._gen_embedded,
+    arguments = [ ctx.outputs.header.path ] + [f.path for f in ctx.files.srcs],
+    progress_message = 'Generating %s' % ctx.outputs.header.short_path,
+    mnemonic = 'GenEmbedded',
+  )
+
+  return struct(
+    files = set([ ctx.outputs.header ]),
+  )
+
+_do_gen_embedded = rule(
+  implementation = _gen_embedded_impl,
+  attrs = {
+    'srcs': attr.label_list(
+      mandatory = True,
+      non_empty = True,
+      allow_files = True,
+    ),
+    '_gen_embedded': attr.label(
+      executable = True,
+      default = Label('//aos/externals/seasocks:gen_embedded'),
+    ),
+  },
+  outputs = {
+    'header': 'embedded.h',
+  },
+  output_to_genfiles = True,
+)
+
+'''Generates the header for Seasocks to load the embedded files.
+
+This always outputs a file named "embedded.h" in the current package, so there
+can be a maximum of one of these rules in each package.
+
+Attrs:
+  srcs: Files to allow loading.
+'''
+def gen_embedded(name, srcs, visibility = None):
+  _do_gen_embedded(
+    name = name + '__do_gen',
+    visibility = ['//visibility:private'],
+    srcs = srcs,
+  )
+  native.cc_library(
+    name = name,
+    visibiltity = visibility,
+    hdrs = [
+      ':%s__do_gen' % name,
+    ],
+  )
diff --git a/aos/externals/seasocks/gen_embedded.py b/aos/externals/seasocks/gen_embedded.py
index d0fd603..416e37b 100755
--- a/aos/externals/seasocks/gen_embedded.py
+++ b/aos/externals/seasocks/gen_embedded.py
@@ -10,23 +10,21 @@
 
 import os, os.path, sys
 
-output = sys.argv[1]
-assert output[0] == '"'
-assert output[-1] == '"'
-output = output[1:-1]
+output = sys.argv[1].replace('"', '')
 
 if not os.path.exists(os.path.dirname(output)):
   os.makedirs(os.path.dirname(output))
-o = open(output, 'w')
 
-web = []
-for root, dirs, files in os.walk("./www_defaults", topdown=False):
-  for name in files:
-    web.append(os.path.join(root, name))
-  for name in dirs:
-    web.append(os.path.join(root, name))
+if len(sys.argv) >= 3:
+  web = sys.argv[2:]
+else:
+  web = []
+  for root, dirs, files in os.walk("./www_defaults", topdown=False):
+    for name in files + dirs:
+      web.append(os.path.join(root, name))
 
-o.write("""
+with open(output, 'w') as o:
+  o.write("""
 #include "internal/Embedded.h"
 
 #include <string>
@@ -37,13 +35,14 @@
 std::unordered_map<std::string, EmbeddedContent> embedded = {
 """)
 
-for f in web:
-  bytes = open(f, 'rb').read()
-  o.write('{"/%s", {' % os.path.basename(f))
-  o.write('"' + "".join(['\\x%02x' % ord(x) for x in bytes]) + '"')
-  o.write(',%d }},' % len(bytes))
+  for f in web:
+    with open(f, 'rb') as file:
+      bytes = file.read()
+    o.write('{"/%s", {' % os.path.basename(f))
+    o.write('"' + "".join(['\\x%02x' % ord(x) for x in bytes]) + '"')
+    o.write(',%d }},' % len(bytes))
 
-o.write("""
+  o.write("""
 };
 
 } // namespace
@@ -56,5 +55,3 @@
   return &found->second;
 }
 """)
-
-o.close()
diff --git a/y2015/http_status/BUILD b/y2015/http_status/BUILD
index 1bb5c8f..9de423c 100644
--- a/y2015/http_status/BUILD
+++ b/y2015/http_status/BUILD
@@ -1,12 +1,19 @@
-package(default_visibility = ['//visibility:public'])
+load('/aos/externals/seasocks/gen_embedded', 'gen_embedded')
+
+gen_embedded(
+  name = 'gen_embedded',
+  srcs = glob(['www_defaults/**/*']),
+)
 
 cc_binary(
   name = 'http_status',
+  visibility = ['//visibility:public'],
   srcs = [
     'http_status.cc',
     'http_status.h',
   ],
   deps = [
+    ':gen_embedded',
     '//aos/linux_code:init',
     '//aos/common/logging',
     '//third_party/seasocks',