Giter Club home page Giter Club logo

Comments (4)

jwodder avatar jwodder commented on July 17, 2024

@yarikoptic How will the Python version be installed? If not via pip, what third-party libraries can I use?

from containers.

yarikoptic avatar yarikoptic commented on July 17, 2024

Ideally none which datalad-container does not use, or even better - just whatever comes with python. Which particular library you have in mind for this?

FWIW, here is the code chatgpt generated for 1-to-1 dummy translation of that script into python - if of any help
#!/usr/bin/env python3
import os
import sys
import subprocess
import tempfile
import argparse

# Helper function to perform in-place modifications with sed-like behavior
def sed_i(filename, *args):
    with tempfile.NamedTemporaryFile(mode="w", delete=False) as tf:
        subprocess.run(["sed", *args, filename], stdout=tf)
        os.replace(tf.name, filename)

# Error function to print error messages
def error(*args):
    print("ERROR:", *args, file=sys.stderr)

# Parse command line arguments
def parse_args():
    parser = argparse.ArgumentParser(description="Freeze or copy container versions")
    parser.add_argument("containers", nargs="+", help="containers to freeze or copy")
    parser.add_argument("--save-dataset", help="save the dataset to a different location")
    return parser.parse_args()

# Get the path to the top directory
topd = os.path.join(os.path.dirname(sys.argv[0]), "..")
if os.path.realpath(topd) == os.path.realpath("."):
    topd = "."

args = parse_args()
save_ds = args.save_dataset
target_ds = save_ds or topd
topd_rel = ""
frozen = []

print("topd_rel:", topd_rel)

for arg in args.containers:
    if arg.startswith("--"):
        print(f"Unknown option '{arg}'", file=sys.stderr)
        sys.exit(1)

    frozen.append(arg)  # Store arguments for commit message
    img, ver = arg.split("=") if "=" in arg else (arg, "")
    print(f"I: {img} -> {ver}")
    if "=" in arg:
        imgprefix = os.path.join(topd, f"images/{img.split('-')[0]}/{img}--{ver}")
        if os.path.isdir(imgprefix):
            imgpath = imgprefix
        else:
            imgpaths = [p for p in os.listdir(f"{topd}/images/{img.split('-')[0]}") if p.startswith(f"{img}--")]
            if len(imgpaths) == 0:
                error(f"There is no {imgprefix}.* files. Available images for the app are:")
                print("\n".join([f"  {p}" for p in os.listdir(f"{topd}/images/{img.split('-')[0]}")]))
                sys.exit(1)
            elif len(imgpaths) == 1:
                imgpath = os.path.realpath(os.path.join(topd, imgpaths[0]))
            else:
                error("There are multiple images available. Include extension into your version specification. Available images are:")
                print("\n".join([f"  {p}" for p in imgpaths]))
                sys.exit(1)
    else:
        imgpath = topd_rel + subprocess.check_output(["git", "-C", topd, "config", "-f", ".datalad/config",
                                                     f"datalad.containers.{img}.image"]).decode().strip()

    subprocess.run(["git", "-C", target_ds, "config", "--replace-all",
                    f"datalad.containers.{img}.image", imgpath])

    if topd_rel:
        git_config_output = subprocess.check_output(["git", "-C", topd, "config", "-f", ".datalad/config",
                                                     "--get-regexp", f"containers.{img}."]).decode()
        for line in git_config_output.splitlines():
            var, value = line.split(" ", 1)
            if var.endswith(".image"):
                continue
            if "{img_dspath}/" in value:
                value = value.replace("{img_dspath}/", f"{topd_rel}")
            else:
                value = f"{topd_rel}{value}"
            subprocess.run(["git", "-C", target_ds, "config", "--replace-all", var, value])
    else:
        sed_i(os.path.join(topd, ".datalad/config"), f"s,{imgpath}([ \t].*)*$,{imgpath}  # frozen,")

if save_ds is not None:
    subprocess.run(["datalad", "save", "-d", save_ds, "-m", f"Freeze container versions {' '.join(frozen)}",
                    os.path.join(save_ds.rstrip("/"), ".datalad/config")])

from containers.

jwodder avatar jwodder commented on July 17, 2024

@yarikoptic

Which particular library you have in mind for this?

I initially thought this issue was about that Perl script you were working on recently, in which case I would likely have needed at least requests and beautifulsoup4.

from containers.

yarikoptic avatar yarikoptic commented on July 17, 2024

no - perl script will remain perl script for now.

from containers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.