Example #1
0
def init_java11_dir_gradle_files():
    print(f"Copying Gradle Files to {config.java11_dir}")
    source = config.java11_resources
    destination = config.java11_dir
    exists(source)
    exists(destination)
    shutil.copytree(source, destination, dirs_exist_ok=True)
Example #2
0
def __parse_test_case(subdir, infile):
    """
    subdir:  The subdirectory in which the infile was found
             (either "testdata" or "refdata")
    infile:  The input filename (path relative to the directory above)
    """
    # The possible posthf cases:
    posthf_cases = ["mp2", "fci"]

    # A test case is split up over these files:
    #   case.in.yaml  -> molsturm hartree_fock input and test metadata
    #   case.hf.yaml  -> hartree fock reference result
    #   case.hf.hdf5  -> hartree fock reference result
    #   case.mp2.yaml -> mp2 reference result
    #   case.fci.yaml -> fci reference result
    # Since some of these files might get rather large,
    # they might not exist inside the actual repository,
    # but in fact reside on a webserver from which they are downloaded
    # into the binary directory by CMake.
    # In either case the part first part of the file name gives
    # the name of the test case:
    name = infile[:-len(".in.yaml")]

    basepath = os.path.join(subdir, name)
    ret = {
        "testing": {
            "name": name
        },
    }

    # Find and load "hf" subtree:
    for ext in ["yaml", "hdf5"]:
        filename = basepath + ".hf." + ext
        if directories.exists(filename):
            ret["hf"] = molsturm.load_state(directories.find(filename))
            break
    if "hf" not in ret:
        raise ValueError("No hf results found for " + infile)

    # Load "input_parameters" and "testing" subtree from in file
    infile_full = directories.find(basepath + ".in.yaml")
    with open(infile_full, "r") as f:
        params = yaml.safe_load(f)
    ret["input_parameters"] = params["input_parameters"]

    # Forward testing metadata and ScfParameters
    if "testing" in params:
        ret["testing"].update(params["testing"])

    # Read posthf subcases:
    molsturm.yaml_utils.install_constructors()
    for sub in posthf_cases:
        yamlfile = basepath + "." + sub + ".yaml"
        if directories.exists(yamlfile):
            with open(directories.find(yamlfile)) as f:
                ret[sub] = yaml.safe_load(f)

    return ret
Example #3
0
def extract_all_examples():
    exists(config.markdown_dir)
    exists(config.github_code_dir)
    initialize_example_dir()  # Doesn't erase
    create_dir(config.java11_dir)

    for chapter in config.markdown_dir.glob("*.md"):
        if "_Java_11" in chapter.name:
            extract_examples_from_chapter(chapter, config.java11_dir)
        else:
            extract_examples_from_chapter(chapter, config.example_dir)
Example #4
0
def combine():
    """Combine markdown files for global editing"""
    combined = ""
    for md in exists(markdown_dir).glob("*.md"):
        combined += md.read_text(errors="backslashreplace") + "\n\n"
    combined_markdown.write_text(combined)
    print(f"{combined_markdown}")
    subprocess.Popen(["code", str(combined_markdown)])
Example #5
0
def process_markdown_through_pandoc():
    """Use Pandoc to reformat markdown"""
    click.echo("Incomplete, performs undesirable conversions")
    return
    twocr = two_char_range()
    for md in exists(markdown_dir).glob("*.md"):
        # pandoc_cmd = f"pandoc -f markdown -t markdown-fenced_code_attributes -o {md}  {md} --markdown-headings=atx --wrap=preserve --id-prefix={next(twocr)}"
        pandoc_cmd = f"pandoc -f markdown -t markdown-fenced_code_attributes -o {md}  {md} --markdown-headings=atx --wrap=preserve"
        print(f"{pandoc_cmd}")
        os.system(pandoc_cmd)
        md.write_text(md.read_text().replace("{\#", "{#"))
Example #6
0
def init_example_dir_gradle_files():
    print(f"Copying Gradle Files to {config.example_dir}")
    source = config.github_code_dir
    exists(source)

    def sources_generator():
        paths = [(source, "*gradle*"), (source, "*.xml"), (source, "*.yml"),
                 (source, "*.md"), (source / "buildSrc", "*")]
        for base, pattern in paths:
            yield [path for path in base.rglob(pattern) if path.is_file()]

    for gradle_path in [
            path for source in sources_generator() for path in source
    ]:  # Flatten
        destination = config.example_dir / gradle_path.relative_to(source)
        destination.parent.mkdir(parents=True, exist_ok=True)
        debug(f"{gradle_path.relative_to(config.rootPath.parent)}" +
              f"\n\t-> {destination.relative_to(config.example_dir)}")
        print(f"{gradle_path.name}...", end="")
        shutil.copy(str(gradle_path), str(destination))
    print("\n---")
Example #7
0
def extract_code_examples():
    """Extract code examples into directory tree"""
    erase(example_dir)
    example_dir.mkdir()
    for md in exists(markdown_dir).glob("*.md"):
        print(md.name)
        text = md.read_text()
        for example in java_example.findall(text):
            example = example.strip()
            slugline = example.splitlines()[0]
            if not (slugline.startswith("//") and slugline.endswith(".scala")):
                continue
            file_path = example_dir / Path(slugline.split(maxsplit=1)[1])
            file_path.parent.mkdir(parents=True, exist_ok=True)
            file_path.write_text(example + "\n")
Example #8
0
def create_tasks():
    tasks = """
def javaClassPath = sourceSets.main.runtimeClasspath

"""
    task_dict = {}
    print("Creating tasks.gradle ...")
    print(exists(config.example_dir))
    for java_file in config.example_dir.rglob("*.java"):
        text = java_file.read_text()
        lines = text.splitlines()
        if not re.search(r"public\s+static\s+void\s+main", text):
            continue
        package_name = None
        for line in lines:
            if line.startswith("package "):
                package_name = line.split()[1][:-1]
        k, v = make_task(java_file.stem, package_name)
        task_dict[k] = v
    for k in sorted(task_dict):
        tasks += task_dict[k]
    tasks += """
task run (dependsOn: [
"""
    for k in sorted(task_dict):
        tasks += f"    '{k}',\n"
    tasks = tasks[:-1]  # Strip last comma
    tasks += """
    ]) {
    doLast {
        println '*** run complete ***'
    }
}
"""
    (config.example_dir / "gradle" / "tasks.gradle").write_text(tasks)
    print(f"{len(task_dict)} tasks")