<gh_stars>100-1000
#!/usr/bin/env python
#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# 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.
#
#
#
# Uses Closure Compiler Service API to compile JavaScript file.
# ./compile_js.py <path_to_input_js_file> ... <path_to_output_js_file>

import httplib
import urllib
import re
import sys

CLOSURE_SERVICE_DOMAIN = 'closure-compiler.appspot.com'

BASE_URL = 'https://raw.githubusercontent.com/google/simian/master/src/simian/mac/admin/js/'

JS_FILES = ['main.js', 'forms.js', 'menu.js', 'net.js', 'tags.js', 'groups.js']
CODE_URLS = [BASE_URL + f for f in JS_FILES]

output_js_file = sys.argv[1]

# Param docs: https://developers.google.com/closure/compiler/docs/api-ref
params = [
    ('compilation_level', 'ADVANCED_OPTIMIZATIONS'),
    ('output_format', 'text'),
    ('output_info', 'compiled_code'),
    ('use_closure_library', True),
  ]
for url in CODE_URLS:
  params.append(('code_url', url))
params = urllib.urlencode(params)

# Always use the following value for the Content-type header.
headers = { "Content-type": "application/x-www-form-urlencoded" }
conn = httplib.HTTPSConnection(CLOSURE_SERVICE_DOMAIN)
conn.request('POST', '/compile', params, headers)
response = conn.getresponse()
response_text = response.read()
conn.close()

if response.status != 200 or response_text.startswith('Error'):
  print >>sys.stderr, 'JS compilation failed: %s' % response_text
  sys.exit(1)

f = open(output_js_file, 'w')
f.write(response_text)
f.close()
