def main(argv=None): if hasattr(os, "getuid"): if os.getuid() == 0: pprint("RED", "Using bentomaker under root/sudo is *strongly* discouraged - do you want to continue ? y/N") ans = raw_input() if not ans.lower() in ["y", "yes"]: raise bento.errors.UsageException("bentomaker execution canceld (not using bentomaker with admin privileges)") if argv is None: argv = sys.argv[1:] options_context = create_global_options_context() popts = parse_global_options(options_context, argv) cmd_name = popts.cmd_name if popts.show_version: print(bento.__version__) return if popts.show_full_version: print(bento.__version__ + "git" + bento.__git_revision__) return # FIXME: top_node vs srcnode source_root = os.path.join(os.getcwd(), os.path.dirname(popts.bento_info)) build_root = os.path.join(os.getcwd(), popts.build_directory) # FIXME: create_root_with_source_tree should return source node and build # node so that we don't have to find them and take the risk of # inconsistency root = bento.core.node.create_root_with_source_tree(source_root, build_root) run_node = root.find_node(os.getcwd()) top_node = root.find_node(source_root) build_node = root.find_node(build_root) if run_node != top_node and run_node.is_src(): raise bento.errors.UsageException("You cannot execute bentomaker in a subdirectory of the source tree !") if run_node != build_node and run_node.is_bld(): raise bento.errors.UsageException("You cannot execute bentomaker in a subdirectory of the build tree !") global_context = GlobalContext(build_node.make_node(CMD_DATA_DUMP), CommandRegistry(), ContextRegistry(), OptionsRegistry(), CommandScheduler()) global_context.register_options_context_without_command("", options_context) if not popts.disable_autoconfigure: global_context.set_before("build", "configure") global_context.set_before("build_egg", "build") global_context.set_before("build_wininst", "build") global_context.set_before("install", "build") if cmd_name and cmd_name not in ["convert"]: return _wrapped_main(global_context, popts, run_node, top_node, build_node) else: # XXX: is cached package necessary here ? cached_package = None register_stuff(global_context) for cmd_name in global_context.command_names(): register_options(global_context, cmd_name) return _main(global_context, cached_package, popts, run_node, top_node, build_node)
def noexc_main(argv=None): def _print_debug(): if BENTOMAKER_DEBUG: tb = sys.exc_info()[2] traceback.print_tb(tb) def _print_error(msg): pprint('RED', msg) if not BENTOMAKER_DEBUG: pprint('RED', "(You can see the traceback by setting the " \ "BENTOMAKER_DEBUG=1 environment variable)") try: main(argv) except bento.errors.BentoError: _print_debug() e = extract_exception() _print_error(str(e)) sys.exit(2) except Exception: msg = """\ %s: Error: %s crashed (uncaught exception %s: %s). Please report this on bento issue tracker: http://github.com/cournape/bento/issues""" if not BENTOMAKER_DEBUG: msg += "\nYou can get a full traceback by setting BENTOMAKER_DEBUG=1" else: _print_debug() e = extract_exception() pprint('RED', msg % (SCRIPT_NAME, SCRIPT_NAME, e.__class__, str(e))) sys.exit(1)
def install_inplace(pkg): """Install scripts of pkg in the current directory.""" for basename, executable in pkg.executables.items(): version_str = ".".join([str(i) for i in sys.version_info[:2]]) scripts_node = root._ctx.srcnode for name in [basename, "%s-%s" % (basename, version_str)]: nodes = _create_executable(name, executable, scripts_node) installed = ",".join([n.path_from(scripts_node) for n in nodes]) pprint("GREEN", "installing %s in current directory" % installed)
def unix_installer(source, target, kind): if kind in ["executables"]: mode = "755" else: mode = "644" cmd = ["install", "-m", mode, source, target] strcmd = "INSTALL %s -> %s" % (source, target) pprint('GREEN', strcmd) if not os.path.exists(os.path.dirname(target)): os.makedirs(os.path.dirname(target)) subprocess.check_call(cmd)
def build_egg(ipkg, ctx, source_root, output_dir=None, output_file=None): meta = PackageMetadata.from_ipkg(ipkg) egg_info = EggInfo.from_ipkg(ipkg, ctx.build_node) # FIXME: fix egg name if output_dir is None: if output_file is None: egg = egg_filename(os.path.join("dist", meta.fullname)) else: egg = os.path.join("dist", output_file) else: if output_file is None: egg = egg_filename(os.path.join(output_dir, meta.fullname)) else: egg = os.path.join(output_dir, output_file) bento.utils.path.ensure_dir(egg) zid = compat.ZipFile(egg, "w", compat.ZIP_DEFLATED) try: ipkg.update_paths({ "prefix": source_root.abspath(), "eprefix": source_root.abspath(), "sitedir": source_root.abspath() }) for filename, cnt in egg_info.iter_meta(ctx.build_node): zid.writestr(os.path.join("EGG-INFO", filename), cnt) file_sections = ipkg.resolve_paths(source_root) for kind, source, target in iter_files(file_sections): if not kind in ["executables"]: zid.write(source.abspath(), target.path_from(source_root)) pprint("PINK", "Byte-compiling ...") for kind, source, target in iter_files(file_sections): if kind in ["pythonfiles"]: try: bytecode = bcompile(source.abspath()) except PyCompileError: e = extract_exception() warnings.warn("Error byte-compiling %r" % source.abspath()) else: zid.writestr("%sc" % target.path_from(source_root), bcompile(source.abspath())) finally: zid.close() return
def build_egg(ipkg, ctx, source_root, output_dir=None, output_file=None): meta = PackageMetadata.from_ipkg(ipkg) egg_info = EggInfo.from_ipkg(ipkg, ctx.build_node) # FIXME: fix egg name if output_dir is None: if output_file is None: egg = egg_filename(os.path.join("dist", meta.fullname)) else: egg = os.path.join("dist", output_file) else: if output_file is None: egg = egg_filename(os.path.join(output_dir, meta.fullname)) else: egg = os.path.join(output_dir, output_file) bento.utils.path.ensure_dir(egg) zid = compat.ZipFile(egg, "w", compat.ZIP_DEFLATED) try: ipkg.update_paths({"prefix": source_root.abspath(), "eprefix": source_root.abspath(), "sitedir": source_root.abspath()}) for filename, cnt in egg_info.iter_meta(ctx.build_node): zid.writestr(os.path.join("EGG-INFO", filename), cnt) file_sections = ipkg.resolve_paths(source_root) for kind, source, target in iter_files(file_sections): if not kind in ["executables"]: zid.write(source.abspath(), target.path_from(source_root)) pprint("PINK", "Byte-compiling ...") for kind, source, target in iter_files(file_sections): if kind in ["pythonfiles"]: try: bytecode = bcompile(source.abspath()) except PyCompileError: e = extract_exception() warnings.warn("Error byte-compiling %r" % source.abspath()) else: zid.writestr("%sc" % target.path_from(source_root), bcompile(source.abspath())) finally: zid.close() return
def convert(ctx, filename, setup_args, monkey_patch_mode, verbose, output, log, show_output=True): if monkey_patch_mode == "automatic": try: if verbose: pprint("PINK", "Catching monkey (this may take a while) ...") monkey_patch_mode = detect_monkeys(filename, show_output, log) if verbose: pprint("PINK", "Detected mode: %s" % monkey_patch_mode) except ValueError: e = extract_exception() raise UsageException("Error while detecting setup.py type " \ "(original error: %s)" % str(e)) monkey_patch(ctx.top_node, monkey_patch_mode, filename) dist, package_objects = analyse_setup_py(filename, setup_args) pkg, options = build_pkg(dist, package_objects, ctx.top_node) out = static_representation(pkg, options) if output == '-': for line in out.splitlines(): pprint("YELLOW", line) else: fid = open(output, "w") try: fid.write(out) finally: fid.close()
def whole_test(setup_py, verbose, log): if verbose: show_output = True else: show_output = False if not test_can_run(setup_py, show_output, log): pass if verbose: pprint("YELLOW", "----------------- Testing distutils ------------------") use_distutils = test_distutils(setup_py, show_output, log) if verbose: pprint("YELLOW", "----------------- Testing setuptools -----------------") use_setuptools = test_setuptools(setup_py, show_output, log) if verbose: pprint("YELLOW", "------------ Testing numpy.distutils -----------------") use_numpy = test_numpy(setup_py, show_output, log) if verbose: pprint("YELLOW", "--- Testing numpy.distutils patched by setuptools ----") use_setuptools_numpy = test_setuptools_numpy(setup_py, show_output, log) if verbose: print(("Is distutils ? %d" % use_distutils)) print(("Is setuptools ? %d" % use_setuptools)) print(("Is numpy distutils ? %d" % use_numpy)) print(("Is setuptools numpy ? %d" % use_setuptools_numpy)) if use_distutils and not (use_setuptools or use_numpy or use_setuptools_numpy): return "distutils" elif use_setuptools and not (use_numpy or use_setuptools_numpy): return "setuptools" elif use_numpy and not use_setuptools_numpy: return "numpy.distutils" elif use_setuptools_numpy: return "setuptools + numpy.distutils converter" else: return "Unsupported converter"
def whole_test(setup_py, verbose, log): if verbose: show_output = True else: show_output = False if not test_can_run(setup_py, show_output, log): pass if verbose: pprint("YELLOW", "----------------- Testing distutils ------------------") use_distutils = test_distutils(setup_py, show_output, log) if verbose: pprint("YELLOW", "----------------- Testing setuptools -----------------") use_setuptools = test_setuptools(setup_py, show_output, log) if verbose: pprint("YELLOW", "------------ Testing numpy.distutils -----------------") use_numpy = test_numpy(setup_py, show_output, log) if verbose: pprint("YELLOW", "--- Testing numpy.distutils patched by setuptools ----") use_setuptools_numpy = test_setuptools_numpy(setup_py, show_output, log) if verbose: print("Is distutils ? %d" % use_distutils) print("Is setuptools ? %d" % use_setuptools) print("Is numpy distutils ? %d" % use_numpy) print("Is setuptools numpy ? %d" % use_setuptools_numpy) if use_distutils and not (use_setuptools or use_numpy or use_setuptools_numpy): return "distutils" elif use_setuptools and not (use_numpy or use_setuptools_numpy): return "setuptools" elif use_numpy and not use_setuptools_numpy: return "numpy.distutils" elif use_setuptools_numpy: return "setuptools + numpy.distutils converter" else: return "Unsupported converter"
def test_simple_no_color(self): s = StringIO() pprint("RED", "foo", s) self.assertEqual(s.getvalue(), "foo\n")
def analyse_setup_py(filename, setup_args, verbose=False): # This is the dirty part: we run setup.py inside this process, and pass # data back through global variables. Not sure if there is a better way to # do this if verbose: pprint('PINK', "======================================================") pprint('PINK', " Analysing %s (running %s) .... " % (filename, filename)) # exec_globals contains the globals used to execute the setup.py exec_globals = {} exec_globals.update(globals()) # Some setup.py files call setup from their main, so execute them as if # they were the main script exec_globals["__name__"] = "__main__" exec_globals["__file__"] = op.abspath(filename) _saved_argv = sys.argv[:] _saved_sys_path = sys.path try: try: sys.argv = [filename] + setup_args + ["build_py"] # XXX: many packages import themselves to get version at build # time, and setuptools screw this up by inserting stuff first. Is # there a better way ? sys.path.insert(0, op.dirname(filename)) fid = open(filename, "r") try: exec(fid.read(), exec_globals) if type == "distutils" and "setuptools" in sys.modules and verbose: pprint("YELLOW", "Setuptools detected in distutils mode !!!") finally: fid.close() except ConvertionError: raise except Exception: e = extract_exception() pprint('RED', "Got exception: %s" % e) raise finally: sys.argv = _saved_argv sys.path = _saved_sys_path live_objects = PACKAGE_OBJECTS dist = DIST_GLOBAL if dist is None: raise ValueError("setup monkey-patching failed") else: if verbose: pprint('PINK', " %s analyse done " % filename) pprint('PINK', "======================================================") return dist, live_objects
def test_simple(self): s = StringIO() pprint("RED", "foo", s) self.assertEqual(s.getvalue(), "\x1b[01;31mfoo\x1b[0m\n")
def _print_error(msg): pprint('RED', msg) if not BENTOMAKER_DEBUG: pprint('RED', "(You can see the traceback by setting the " \ "BENTOMAKER_DEBUG=1 environment variable)")
def print_delim(string): if show_output: pprint("YELLOW", string)
def main(argv=None): if hasattr(os, "getuid"): if os.getuid() == 0: pprint( "RED", "Using bentomaker under root/sudo is *strongly* discouraged - do you want to continue ? y/N" ) ans = input() if not ans.lower() in ["y", "yes"]: raise bento.errors.UsageException( "bentomaker execution canceld (not using bentomaker with admin privileges)" ) if argv is None: argv = sys.argv[1:] options_context = create_global_options_context() popts = parse_global_options(options_context, argv) cmd_name = popts.cmd_name if popts.show_version: print(bento.__version__) return if popts.show_full_version: print(bento.__version__ + "git" + bento.__git_revision__) return source_root = os.path.join(os.getcwd(), os.path.dirname(popts.bento_info)) build_root = os.path.join(os.getcwd(), popts.build_directory) top_node, build_node, run_node = bento.core.node.create_base_nodes( source_root, build_root) if run_node != top_node and run_node.is_src(): raise bento.errors.UsageException( "You cannot execute bentomaker in a subdirectory of the source tree !" ) if run_node != build_node and run_node.is_bld(): raise bento.errors.UsageException( "You cannot execute bentomaker in a subdirectory of the build tree !" ) global_context = GlobalContext(build_node.make_node(CMD_DATA_DUMP), CommandRegistry(), ContextRegistry(), OptionsRegistry(), CommandScheduler()) global_context.register_options_context_without_command( "", options_context) if not popts.disable_autoconfigure: global_context.set_before("build", "configure") global_context.set_before("build_egg", "build") global_context.set_before("build_wheel", "build") global_context.set_before("build_wininst", "build") global_context.set_before("install", "build") if cmd_name and cmd_name not in ["convert"]: return _wrapped_main(global_context, popts, run_node, top_node, build_node) else: # XXX: is cached package necessary here ? cached_package = None register_stuff(global_context) for cmd_name in global_context.command_names(): register_options(global_context, cmd_name) return _main(global_context, cached_package, popts, run_node, top_node, build_node)