def run(self, destination, packages, mode="prod"): # setup compiler from utils.compiler import Compiler compiler = Compiler(self.manifest) # process builds for package in packages: manifest, build = self._find_manifest_and_build(package) p_destination = os.path.join(destination, package) if not build: raise Exception("No Build Found ({})".format(package)) # run the compiler compiler.run(p_destination, mode=mode, packages=build["packages"]) # process the html css_imports = [] js_imports = [] test_imports = [] head_indent = " " meta_indent = " " for p in build["packages"]: parts = p.split(".") parts.append("main") is_sub_package = len(parts) > 2 if is_sub_package: parts.insert(1, "packages") file = "/".join(parts) css_imports.append( meta_indent + "<link rel=\"stylesheet\" type=\"text/css\" href=\"{}-dev.css\" />".format(file) ) js_imports.append( meta_indent + "<script type=\"text/javascript\" language=\"javascript\" src=\"{}-dev.js\"></script>".format(file) ) # add tests file parts[-1] = "tests.js" file = "/".join(parts) tests_file = os.path.join(p_destination, file) if os.path.exists(tests_file): test_imports.append( meta_indent + "<script type=\"text/javascript\" language=\"javascript\" src=\"{}\"></script>".format( file ) ) html_files = {} html_paths = build["html"] if isinstance(build["html"], (list, tuple)) else [build["html"]] for html_path in html_paths: html_dev_path = os.path.join(p_destination, html_path.split("/")[-1].replace(".", "-dev.")) html_files[html_dev_path] = file_get_contents(os.path.join(manifest["source_path"], html_path)).replace( "</head>", "\n{}\n\n{}\n{}</head>".format( "\n".join(css_imports), "\n".join(js_imports), head_indent ) ) for html_path in html_files: # process dev html dev_html = html_files[html_path].replace( "<head>", "<head>" + "\n" + meta_indent + "<meta content=\"utf-8\" http-equiv=\"encoding\" />" + "\n" + meta_indent + "<meta http-equiv=\"cache-control\" content=\"no-cache\">" + "\n" + meta_indent + "<meta http-equiv=\"expires\" content=\"0\">" + "\n" + meta_indent + "<meta http-equiv=\"pragma\" content=\"no-cache\">" + "\n" ) dev_html = dev_html.replace( "</head>", "\n{}\n{}</head>".format( "\n".join(test_imports), head_indent ) ) needle = re.search("id=[\"|']OJ[\"|']", dev_html) if needle: index = needle.end(0) dev_html = dev_html[:index] + " mode=\"development\" " + dev_html[index:] else: dev_html = dev_html.replace("<body", "<body mode=\"development\" ") file_put_contents(html_path, dev_html) build["html_files"] = html_files self._build_zip(destination, package, build, "dev") # process prod html if mode == "prod": prod_html_files = {} for html_path in html_files: prod_html = html_files[html_path].replace("-dev.", ".") prod_html_path = html_path.replace("-dev.", ".") prod_html_files[prod_html_path] = prod_html file_put_contents(prod_html_path, prod_html) build["html_files"] = prod_html_files self._build_zip(destination, package, build, mode)
def run(src_path): # setup script arguments parser = argparse.ArgumentParser(description="Compile Objective-JS Package(s).") parser.add_argument( "action", choices=["add", "build", "compile", "dist", "remove", "setup", "install"], default="compile", help="The action to take when running the script." ) parser.add_argument( "packages", type=str, nargs="*", default=ALL, help="Action \"add\": The classes you want to add. At least one class must be specified.\n" + "Action \"build\": The packages you want to build. Will build all packages if nothing is specified.\n" + "Action \"compile\": The packages you want to compile. Will compile all packages if nothing is specified.\n" + "Action \"dist\": The packages you want to distribute.\n" + "Action \"remove\": The classes you want to remove. At least one class must be specified.\n" + "Action \"setup\": The first argument is the package name and the second is the destination.\n" + "***Note: Package names 'android', 'ios', 'linux', 'osx', & 'windows' are reserved." ) parser.add_argument( "-v", action="store_true", default=False, help="Show verbose output." ) parser.add_argument( "--mode", choices=[DEV, PROD], default="prod", help="The mode to compile in. Note that prod will automatically update dev as well." ) parser.add_argument( "--types", nargs="*", choices=[ALL, CSS, JS, TEMPLATE, THEME], default=ALL, help="The file types to compile. Will compile all file types if nothing is specified." ) parser.add_argument( "--destination", type=str, default=os.path.join(src_path, "builds"), help="The path to the build directory." ) parser.add_argument( "--verbose", type=int, default=0, help="Show verbose output." ) # process the script args args = parser.parse_args() kwargs = { "types": args.types } if ALL in args.types: kwargs["types"] = TYPES # setup verbose setting utils.VERBOSE = args.verbose if args.v and utils.VERBOSE == 0: utils.VERBOSE = 1 destination, mode, packages = _process_args(args) # figure out what action to take if args.action == "add": # setup compiler instance from utils.manage import add kwargs["classes"] = packages add(src_path, **kwargs) elif args.action == "build": # check that at least one package was specified if args.packages == ALL: raise Exception("When building at least one package must be specified. All/empty is unsupported.") # setup compiler instance from utils.builder import Builder builder = Builder("manifest.json") # build the compile kwargs kwargs["mode"] = mode kwargs.pop("types") # run the compiler builder.run(destination, packages, **kwargs) elif args.action == "compile": # setup compiler instance from utils.compiler import Compiler compiler = Compiler("manifest.json") # build the compile kwargs kwargs["mode"] = mode kwargs["packages"] = packages # run the compiler compiler.run(destination, **kwargs) elif args.action == "dist": from zipfile import ZipFile for package in packages: zip_name = "{}{}.zip".format( package, "-dev" if mode == DEV else "" ) with ZipFile(os.path.join("builds", "dist", zip_name)) as zip: zip.extractall(destination) elif args.action == "install": sh.npm.install("-g", "uglify-es") sh.npm.install("-g", "clean-css") sh.npm.install("-g", "clean-css-cli") elif args.action == "remove": # setup compiler instance from utils.manage import remove kwargs["classes"] = packages remove(src_path, **kwargs) elif args.action == "setup": from utils.manage import setup setup(src_path, *packages)
if args.dmodel: density = DensityModel(path=args.vmodel) else: density = DensityModel(shape=tuple(args.grid)) # get the velocity model dimension dimension = len(vel.shape()) if dimension == 2: nz, nx = tuple(args.grid) dz, dx = tuple(args.spacing) else: nz, nx, ny = tuple(args.grid) dz, dx, dy = tuple(args.spacing) compiler = Compiler(program_version=args.tool, c_code=args.ccode) grid = Grid(shape=vel.shape()) grid.add_source() # apply CFL conditions dt = cfl.calc_dt(dimension=dimension, space_order=2, spacing=tuple(args.spacing), vel_model=vel.model) timesteps = cfl.calc_num_timesteps(args.time, dt) params = { 'compiler': compiler, 'grid': grid, 'vel_model': vel,