def genbuild(args): whl = Wheel(args.whl) extra_deps = args.add_dependency or [] drop_deps = {d: None for d in args.drop_dependency or []} 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(":"))) attrs = [] attrs += [("name", '"entrypoint_%s"' % name)] attrs += [("srcs", '["%s"]' % entrypoint_file)] attrs += [("main", '"%s"' % entrypoint_file)] if args.python_version: attrs += [("python_version", '"%s"' % args.python_version)] attrs += [("deps", '[":pkg"]')] entrypoints_build += """ py_binary( {attrs} ) """.format(attrs=",\n ".join(['{} = {}'.format(k, v) for k, v in attrs])) attrs = [] if args.patches: attrs += [("patches", '["%s"]' % '", "'.join(args.patches))] if args.patch_tool: attrs += [("patch_tool", '"%s"' % args.patch_tool)] if args.patch_args: attrs += [("patch_args", '["%s"]' % '", "'.join(args.patch_args))] if args.patch_cmds: attrs += [("patch_cmds", '["%s"]' % '", "'.join(args.patch_cmds))] attrs += [("distribution", '"%s"' % whl.distribution().lower())] attrs += [("version", '"%s"' % whl.version())] attrs += [("wheel_size", "%s" % os.path.getsize(args.whl))] if args.python_version: attrs += [("python_version", '"%s"' % args.python_version)] with open(os.path.join(args.directory, 'BUILD'), 'w') as f: f.write(""" package(default_visibility = ["//visibility:public"]) load("@io_bazel_rules_python//python:python.bzl", "extract_wheel") load("@{repository}//:requirements.bzl", "requirement") filegroup( name = "wheel", data = ["{wheel}"], ) extract_wheel( name = "extracted", wheel = "{wheel}", {attrs} ) py_library( name = "pkg", imports = ["extracted"], deps = [ ":extracted",{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) ]), attrs=",\n ".join(['{} = {}'.format(k, v) for k, v in attrs]), 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))
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))