def extract(args): whl = Wheel(args.whl) whl.expand(args.directory)
def extract(args): # Extract the files into the current directory whl = Wheel(args.whl) whl.expand(args.directory) extra_deps = args.add_dependency or [] drop_deps = {d: None for d in args.drop_dependency or []} imports = ['.'] external_deps = [ d for d in itertools.chain(whl.dependencies(), extra_deps) if d not in drop_deps ] contents = [] add_build_content = args.add_build_content or [] for name in add_build_content: with open(name) as f: contents.append(f.read() + '\n') contents = '\n'.join(contents) parser = whl.entrypoints() entrypoints_build = '' if parser: if parser.has_section('console_scripts'): for name, location in parser.items('console_scripts'): # Assumes it doesn't depend on extras. TODO(conrado): fix entrypoint_file = 'entrypoint_%s.py' % name with open(os.path.join(args.directory, entrypoint_file), 'w') as f: f.write("""from %s import %s as main; main()""" % tuple(location.split(":"))) entrypoints_build += """ py_binary( name = "{entrypoint_name}", srcs = ["{entrypoint_file}"], main = "{entrypoint_file}", deps = [":pkg"], )""".format(entrypoint_name='entrypoint_%s' % name, entrypoint_file=entrypoint_file) with open(os.path.join(args.directory, 'BUILD'), 'w') as f: f.write(""" package(default_visibility = ["//visibility:public"]) load("@{repository}//:requirements.bzl", "requirement") filegroup( name = "wheel", data = ["{wheel}"], ) py_library( name = "pkg", srcs = glob(["**/*.py"]), data = glob(["**/*"], exclude=["**/*.py", "**/* *", "*.whl", "BUILD", "WORKSPACE"]), # This makes this directory a top-level in the python import # search path for anything that depends on this. imports = [{imports}], deps = [ {dependencies} ], ) {extras} {entrypoints_build} {contents}""".format(wheel=whl.basename(), repository=args.repository, dependencies=''.join([ ('\n "%s",' % d) if d[0] == "@" else ('\n requirement("%s"),' % d) for d in sorted(external_deps) ]), imports=','.join(['"%s"' % i for i in sorted(imports)]), extras='\n\n'.join([ """py_library( name = "{extra}", deps = [ ":pkg",{deps} ], )""".format(extra=extra, deps=','.join([ 'requirement("%s")' % dep for dep in sorted(whl.dependencies(extra)) ])) for extra in args.extras or [] ]), entrypoints_build=entrypoints_build, contents=contents))