Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright (C) 2015 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2016-09-21 15:03:54 +02:00
|
|
|
import bowerutil
|
Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
|
2015-11-18 10:19:52 -05:00
|
|
|
CACHE_DIR = os.path.expanduser(os.path.join(
|
|
|
|
'~', '.gerritcodereview', 'buck-cache', 'downloaded-artifacts'))
|
Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
def bower_cmd(bower, *args):
|
|
|
|
cmd = bower.split(' ')
|
|
|
|
cmd.extend(args)
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
|
|
def bower_info(bower, name, package, version):
|
|
|
|
cmd = bower_cmd(bower, '-l=error', '-j',
|
|
|
|
'info', '%s#%s' % (package, version))
|
2016-09-21 15:03:54 +02:00
|
|
|
try:
|
|
|
|
p = subprocess.Popen(cmd , stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
except:
|
|
|
|
sys.stderr.write("error executing: %s\n" % ' '.join(cmd))
|
|
|
|
raise
|
Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
out, err = p.communicate()
|
|
|
|
if p.returncode:
|
|
|
|
sys.stderr.write(err)
|
2016-09-21 15:03:54 +02:00
|
|
|
raise OSError('Command failed: %s' % ' '.join(cmd))
|
Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
info = json.loads(out)
|
|
|
|
except ValueError:
|
2016-09-21 15:03:54 +02:00
|
|
|
raise ValueError('invalid JSON from %s:\n%s' % (" ".join(cmd), out))
|
Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
info_name = info.get('name')
|
|
|
|
if info_name != name:
|
|
|
|
raise ValueError('expected package name %s, got: %s' % (name, info_name))
|
|
|
|
return info
|
|
|
|
|
|
|
|
|
|
|
|
def ignore_deps(info):
|
|
|
|
# Tell bower to ignore dependencies so we just download this component. This
|
|
|
|
# is just an optimization, since we only pick out the component we need, but
|
|
|
|
# it's important when downloading sizable dependency trees.
|
|
|
|
#
|
|
|
|
# As of 1.6.5 I don't think ignoredDependencies can be specified on the
|
|
|
|
# command line with --config, so we have to create .bowerrc.
|
|
|
|
deps = info.get('dependencies')
|
|
|
|
if deps:
|
|
|
|
with open(os.path.join('.bowerrc'), 'w') as f:
|
|
|
|
json.dump({'ignoredDependencies': deps.keys()}, f)
|
|
|
|
|
|
|
|
|
2015-11-18 10:19:52 -05:00
|
|
|
def cache_entry(name, package, version, sha1):
|
|
|
|
if not sha1:
|
|
|
|
sha1 = hashlib.sha1('%s#%s' % (package, version)).hexdigest()
|
|
|
|
return os.path.join(CACHE_DIR, '%s-%s.zip-%s' % (name, version, sha1))
|
Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
opts = optparse.OptionParser()
|
|
|
|
opts.add_option('-n', help='short name of component')
|
|
|
|
opts.add_option('-b', help='bower command')
|
|
|
|
opts.add_option('-p', help='full package name of component')
|
|
|
|
opts.add_option('-v', help='version number')
|
|
|
|
opts.add_option('-s', help='expected content sha1')
|
|
|
|
opts.add_option('-o', help='output file location')
|
2016-09-21 15:03:54 +02:00
|
|
|
opts, args_ = opts.parse_args(args)
|
|
|
|
|
|
|
|
assert opts.p
|
|
|
|
assert opts.v
|
|
|
|
assert opts.n
|
Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
|
2015-11-18 10:19:52 -05:00
|
|
|
cwd = os.getcwd()
|
|
|
|
outzip = os.path.join(cwd, opts.o)
|
|
|
|
cached = cache_entry(opts.n, opts.p, opts.v, opts.s)
|
|
|
|
|
|
|
|
if not os.path.exists(cached):
|
|
|
|
info = bower_info(opts.b, opts.n, opts.p, opts.v)
|
|
|
|
ignore_deps(info)
|
|
|
|
subprocess.check_call(
|
|
|
|
bower_cmd(opts.b, '--quiet', 'install', '%s#%s' % (opts.p, opts.v)))
|
|
|
|
bc = os.path.join(cwd, 'bower_components')
|
|
|
|
subprocess.check_call(
|
|
|
|
['zip', '-q', '--exclude', '.bower.json', '-r', cached, opts.n],
|
|
|
|
cwd=bc)
|
|
|
|
|
|
|
|
if opts.s:
|
|
|
|
path = os.path.join(bc, opts.n)
|
2016-09-21 15:03:54 +02:00
|
|
|
sha1 = bowerutil.hash_bower_component(hashlib.sha1(), path).hexdigest()
|
2015-11-18 10:19:52 -05:00
|
|
|
if opts.s != sha1:
|
|
|
|
print((
|
|
|
|
'%s#%s:\n'
|
|
|
|
'expected %s\n'
|
|
|
|
'received %s\n') % (opts.p, opts.v, opts.s, sha1), file=sys.stderr)
|
2015-12-04 17:07:10 -05:00
|
|
|
try:
|
|
|
|
os.remove(cached)
|
|
|
|
except OSError as err:
|
|
|
|
if path.exists(cached):
|
|
|
|
print('error removing %s: %s' % (cached, err), file=sys.stderr)
|
2015-11-18 10:19:52 -05:00
|
|
|
return 1
|
|
|
|
|
|
|
|
shutil.copyfile(cached, outzip)
|
Build bower_components with buck
Add support for downloading npm binaries including dependencies and
running them in buck genrules. In npm land, transitive dependencies
are generally included in the package distribution, and there are a
*lot* of them. Since we aren't redistributing these binaries and
they're only part of the build process, we don't have to worry too
much about licensing, only that they don't have anything totally
crazy.
We assume packages have a certain format and we can detect the binary
to run from the genrule output filename. Actually running the binary
is tricky as well, since we have to extract it first. But it might be
large, so we don't want to extract it on every invocation; and naive
extraction to a common location (in buck-out) is racy. So we need a
custom extractor scheme using atomic rename to make this work.
Download bower as an npm package and use it to download bower
packages. Bower packages can come from a variety of sources, usually
git repositories, so we can't simply use download_file. There is
additional logic in bower to read bower.json and strip out unneeded
files, so I didn't want to get into reimplementing that. The tricky
thing about bower is convincing it to avoid transitive dependencies so
we can let Buck handle parallelism and caching. To do this, we need to
read the package information from the upstream bower repository, and
explicitly ignore all listed dependencies when downloading.
We combine the flattened list of bower packages in a single
bower_components rule. It would be nice to have deps of each
bower_component so we didn't need to flatten these, but Buck genrules
don't have deps so this is a nonstarter. Considering we only expect to
have a single bower_components for the whole project, hopefully this
is not too onerous.
This change just gets us the bower_components directory. We still have
some work to do to use this from Gerrit. Plus even more work to
replace the gulpfile and actually package this stuff together into a
compiled JS app for the war distribution.
Change-Id: Id277d2d812ffcc3bce87ff00b5894bacdffc038e
2015-11-12 15:44:08 -05:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv[1:]))
|