|
| 1 | +import os, yaml, subprocess, shutil |
| 2 | +from checksumdir import dirhash |
| 3 | + |
| 4 | + |
| 5 | +def _check_metadata_extension(source_dir): |
| 6 | + """Check if the previewer metadata file has the correct extension""" |
| 7 | + |
| 8 | + # List all the files in the previewers directory |
| 9 | + files = os.listdir(".") |
| 10 | + |
| 11 | + if "previewer-meta.yaml" in files: |
| 12 | + return "previewer-meta.yaml" |
| 13 | + elif "previewer-meta.yml" in files: |
| 14 | + return "previewer-meta.yml" |
| 15 | + else: |
| 16 | + raise FileNotFoundError( |
| 17 | + f"No previewer metadata file found for source directory {source_dir}" |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +def build_react_app(source_dir, TARGET_DIR): |
| 22 | + """Builds the react app and copies the files to the target directory |
| 23 | +
|
| 24 | + Args: |
| 25 | + source_dir (str): Source directory of the react app. |
| 26 | + TARGET_DIR (str): Target directory to copy the files to. |
| 27 | + REPO_DIR (str): Directory of the repository. |
| 28 | + """ |
| 29 | + |
| 30 | + # Move to the source directory to read metadata and instructions |
| 31 | + os.chdir(source_dir) |
| 32 | + |
| 33 | + # Check if a previewer metadata file exists |
| 34 | + metadata_file = _check_metadata_extension(source_dir) |
| 35 | + |
| 36 | + # Read the previewers metadata |
| 37 | + metadata = yaml.safe_load(open(metadata_file)) |
| 38 | + |
| 39 | + # Create path map to deploy the necessary files |
| 40 | + extension_paths = { |
| 41 | + "js": os.path.join(TARGET_DIR, "js"), |
| 42 | + "css": os.path.join(TARGET_DIR, "css"), |
| 43 | + "html": TARGET_DIR, |
| 44 | + } |
| 45 | + |
| 46 | + # Get the hash of the source directory |
| 47 | + source_hash = dirhash(metadata["checkdir"], "sha256") |
| 48 | + |
| 49 | + if metadata.get("checksum") == source_hash: |
| 50 | + print(f"No changes detected for {metadata['name']}") |
| 51 | + return |
| 52 | + |
| 53 | + print(f"Changes detected for {metadata['name']} - Building previewer") |
| 54 | + |
| 55 | + for command in metadata["build"]: |
| 56 | + # Run the build commands |
| 57 | + subprocess.call(command, shell=True) |
| 58 | + |
| 59 | + for file in metadata["files"]: |
| 60 | + # Copy the files to the target directory |
| 61 | + fname = os.path.basename(file) |
| 62 | + extension = fname.split(".")[-1] |
| 63 | + shutil.copy(file, os.path.join(extension_paths[extension], fname)) |
| 64 | + |
| 65 | + # Update checksum in metadata file |
| 66 | + metadata["checksum"] = source_hash |
| 67 | + yaml.safe_dump(metadata, open(metadata_file, "w"), sort_keys=False) |
| 68 | + |
| 69 | + print("Successfully built previewer - Checksum updated") |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + # Build the react previewers |
| 74 | + BASE_DIR = "./previewers/react-source/" |
| 75 | + REPO_DIR = os.getcwd() |
| 76 | + TARGET_DIR = os.path.join(REPO_DIR, "previewers", "betatest") |
| 77 | + |
| 78 | + # Get all the react previewers |
| 79 | + react_previewers = [ |
| 80 | + os.path.join(BASE_DIR, path) |
| 81 | + for path in os.listdir(BASE_DIR) |
| 82 | + if not path.startswith(".") and path != "README.md" |
| 83 | + ] |
| 84 | + |
| 85 | + for source_dir in react_previewers: |
| 86 | + # Build the react app |
| 87 | + build_react_app(source_dir, TARGET_DIR) |
| 88 | + |
| 89 | + # Return to the repository directory |
| 90 | + os.chdir(REPO_DIR) |
0 commit comments