def CustomizeFiles(sim_name): Print.new_step("Customize files") try: # README.md ModifyFileContent(sim_name + "/README.md", lambda content: "# " + sim_name + "\n") # CMakelists.txt ModifyFileContent( sim_name + "/CMakeLists.txt", lambda content: content.replace("my-simulation", sim_name)) # source files include_guard = sim_name.upper().replace("-", "_") + "_H_" ModifyFileContent( sim_name + "/src/my-simulation.h", lambda c: c.replace("MY_SIMULATION_H_", include_guard)) ModifyFileContent(sim_name + "/src/my-simulation.cc", lambda c: c.replace("my-simulation", sim_name)) # rename os.rename(sim_name + "/src/my-simulation.h", sim_name + "/src/" + sim_name + ".h") os.rename(sim_name + "/src/my-simulation.cc", sim_name + "/src/" + sim_name + ".cc") except: Print.error("Error: File customizations failed") CleanupOnError(sim_name)
def DemoCommand(demo_name, destination=None): if not demo_name: print('Usage: biodynamo demo <demo name> [target directory]') print('Known demos:\n {}'.format('\n '.join(KNOWN_DEMOS))) return if demo_name not in KNOWN_DEMOS: Print.error('Demo name "{}" is not known.'.format(demo_name)) print('Known demos:\n {}'.format('\n '.join(KNOWN_DEMOS))) sys.exit(1) if destination is None: destination = '.' if os.path.exists(destination): destination = os.path.join(destination, demo_name) if os.path.exists(destination): Print.error('Destination directory "{}" exists.'.format(destination)) sys.exit(2) src_dir = os.path.join(DEMO_DIR, demo_name) print('Copying files from "{}" to "{}"...'.format(src_dir, destination)) shutil.copytree(src_dir, destination) CopySupportFiles(destination) InitializeNewGitRepo(destination) Print.success('The demo "{}" has been created in "{}".'.format( demo_name, destination))
def CopyTemplate(sim_name): Print.new_step("Copy simulation template") try: src_path = "{0}/simulation-template".format(os.environ['BDMSYS']) sp.check_output(["cp", "-R", src_path, "./{0}".format(sim_name)]) except sp.CalledProcessError as err: Print.error("Error while copying the template project.") # Do not use CleanupOnError here # One failure could be an already existing directory # we must not remove it sys.exit(1)
def DemoCommand(demo_name, destination=None): if not demo_name: print("Usage: biodynamo demo <demo name> [target directory]") print("Known demos:\n {}".format("\n ".join(KNOWN_DEMOS))) return if demo_name not in KNOWN_DEMOS: Print.error("Demo name \"{}\" is not known.".format(demo_name)) print("Known demos:\n {}".format("\n ".join(KNOWN_DEMOS))) sys.exit(1) if destination is None: destination = "." if os.path.exists(destination): destination = os.path.join(destination, demo_name) if os.path.exists(destination): Print.error("Destination directory \"{}\" exists.".format(destination)) Print.error( "Please remove it or create the demo in a different place.") Print.error("Abort \"biodynamo demo {}\".".format(demo_name)) sys.exit(2) src_dir = os.path.join(DEMO_DIR, demo_name) print("Copying files from \"{}\" to \"{}\"...".format( src_dir, destination)) shutil.copytree(src_dir, destination) CopySupportFiles(destination) InitializeNewGitRepo(destination) Print.success("The demo \"{}\" has been created in \"{}\".".format( demo_name, destination))
def DownloadTemplateRepository(sim_name): Print.new_step("Download template repository") try: sp.check_output([ "git", "clone", "https://github.com/BioDynaMo/simulation-templates.git", sim_name ]) sp.check_output(["rm", "-rf", sim_name + "/.git"]) except sp.CalledProcessError as err: Print.error( "Error while downloading the template project from BioDynaMo") # Do not use CleanupOnError here # One failure could be an already existing directory # we must not remove it sys.exit(1)
def BuildCommand(clean=False, debug=False, build=True): build_dir = "build" debug_dir = "debug" Print.new_step("Build") if clean or debug: Print.new_step("Clean build directory") sp.check_output(["rm", "-rf", build_dir]) sp.check_output(["mkdir", build_dir]) else: if not os.path.exists(build_dir): sp.check_output(["mkdir", build_dir]) if debug: if not os.path.exists(debug_dir): sp.check_output(["mkdir", debug_dir]) with open(debug_dir + '/cmake_output.log', "w") as file: try: sp.check_call( ["cmake", "-B./" + build_dir, "-H."], stdout=file, stderr=file) except sp.CalledProcessError as err: Print.error( "Failed to run CMake. Generating debug/cmake_output.log..." ) return with open(debug_dir + '/make_output.log', "w") as file: try: sp.check_call( ["make", "-C", build_dir], stdout=file, stderr=file) except sp.CalledProcessError as err: Print.error( "Compilation failed. Generating debug/make_output.log..." ) return elif build: # if CMakeCache.txt does not exist, run cmake if not Path(build_dir + "/CMakeCache.txt").is_file(): try: sp.check_output(["cmake", "-B./" + build_dir, "-H."]) except sp.CalledProcessError as err: Print.error("Failed to run CMake. Check the debug output above.") sys.exit(1) try: sp.check_output(["make", "-j4", "-C", build_dir]) except: Print.error("Compilation failed. Check the debug output above.") sys.exit(1)
def RunCommand(args, debug=False): sim_name = GetBinaryName() args_str = ' '.join(args) cmd = "./build/" + sim_name try: BuildCommand() Print.new_step("Run " + sim_name + ' ' + args_str) if debug: sp.check_output([cmd, "&>", "debug/runtime_output.log"]) else: print( sp.check_output([cmd, args_str], stderr=sp.STDOUT).decode('utf-8')) Print.success("Finished successfully") except sp.CalledProcessError as err: print(err.output.decode("utf-8")) Print.error("Error during execution of {0}".format(cmd))
def ValidateSimName(sim_name): pattern = re.compile("^[a-zA-Z]+[a-zA-Z0-9\-_]+$") if not pattern.match(sim_name): Print.error("Error: simulation name '{0}' is not valid.".format(sim_name)) Print.error(" Allowed characters are a-z A-Z 0-9 - and _") Print.error(" Must start with a-z or A-Z") sys.exit(1)
def RunCommand(args, debug=False): sim_name = GetBinaryName() args_str = " ".join(args) cmd = "./build/" + sim_name if platform.system() == "Darwin": launcher = os.environ["BDMSYS"] + "/bin/launcher.sh" else: launcher = "" try: BuildCommand() Print.new_step("Run " + sim_name + " " + args_str) if debug: sp.check_output( [launcher + " " + cmd, "&>", "debug/runtime_output.log"]) else: print( sp.check_output([launcher + " " + cmd, args_str], stderr=sp.STDOUT, shell=True).decode("utf-8")) Print.success("Finished successfully") except sp.CalledProcessError as err: print(err.output.decode("utf-8")) Print.error("Error during execution of {0}".format(cmd))
def CreateNewGithubRepository(sim_name): Print.new_step("Create Github repository") gh_user = input("Please enter your Github username: "******"Please enter your Github password: "******"name": sim_name, "description": "Simulation powered by BioDynaMo" } headers = {'Content-Type': 'application/json'} bytes = json.dumps(data).encode('utf-8') url = "https://api.github.com/user/repos" request = urllib.request.Request(url, data=bytes, headers=headers) credentials = ('%s:%s' % (gh_user, gh_pass)) encoded_credentials = base64.b64encode(credentials.encode('ascii')) request.add_header('Authorization', 'Basic %s' % encoded_credentials.decode("ascii")) result = urllib.request.urlopen(request) except urllib.error.HTTPError as err: Print.error("Github repository creation failed.") Print.error(err) CleanupOnError(sim_name) # Connect github repository with local try: repo_url = "https://github.com/" + gh_user + "/" + sim_name + ".git" sp.check_output(["git", "remote", "add", "origin", repo_url], cwd=sim_name) except sp.CalledProcessError as err: Print.error( "Error: Setting remote github url ({0}) failed.".format(repo_url)) CleanupOnError(sim_name)
def CleanupOnError(sim_name): try: sp.check_output(["rm", "-rf", sim_name]) except: Print.error("Error: Failed to remove folder {0}".format(sim_name)) sys.exit(1)
def ValidateSimName(sim_name): pattern = re.compile("^[a-zA-Z]+[a-zA-Z0-9\-_]+$") if not pattern.match(sim_name): Print.error( "Error: simulation name \"{}\" is not valid.".format(sim_name)) Print.error(" Allowed characters are a-z A-Z 0-9 - and _") Print.error(" Must start with a-z or A-Z") sys.exit(1) if os.path.isdir(sim_name): Print.error("The directory \"{}\" already exists.".format(sim_name)) Print.error("Please remove it or choose a different name.") Print.error("Abort \"biodynamo new {}\".".format(sim_name)) sys.exit(1)