def docker_run(image, args, mount=None, env_mount=None, flags=()): cmds_mount = [] cmds_env_mount = [] if mount: for (f1, f2) in mount: cmds_mount += [ docker_opt_mount(el.expand_file_name(f1), el.expand_file_name(f2)) ] if env_mount: for (d, e) in env_mount: el.make_directory(d) cmds_env_mount += [docker_opt_env_mount(e, el.expand_file_name(d))] cmds = [ "docker run", "-i" if "-T" in flags else docker_opt_tty(), docker_opt_cleanup(), "" if "-H" in flags else docker_opt_all_ports(), docker_opt_user(), docker_opt_display(), docker_opt_mount(dd, dd), lf("-w '{dd}'") ] cmds += cmds_env_mount cmds += cmds_mount cmds += [ docker_opt_mount("/etc/group", "/etc/group", "ro"), docker_opt_mount("/etc/passwd", "/etc/passwd", "ro"), docker_opt_mount(dd, dd), lf("{image} bash -e -c '{args}'") ] return " ".join(cmds)
def log_file_name(base_dir, book, recipe): sub_dir = "_".join(el.delete("", os.path.normpath(book).split(os.sep)[:-1])) full_dir = el.expand_file_name(sub_dir, base_dir) el.make_directory(full_dir) ts = el.replace_regexp_in_string(" ", "_", el.timestamp()) name = el.lf("{ts}-{recipe}.txt") return el.expand_file_name(name, full_dir)
def cp(fr, to): fr = el.expand_file_name(fr) to = el.expand_file_name(to) if el.file_exists_p(to) and file_equal(fr, to): print(lf("{to}: OK")) return False else: el.sc(sudo("cp '{fr}' '{to}'", to)) return True
def ng_install(recipe): local = el.expand_file_name("~/git/clojure/nailgun/") st.git_clone("https://github.com/facebook/nailgun", local, "84f3b05") st.install_package("maven") jar = el.expand_file_name("nailgun-server/target/nailgun-server-1.0.0.jar", local) st.make(jar, [lf("cd {local}"), "mvn clean install"], [local]) st.make("/usr/local/bin/ng", [lf("cd {local}"), "sudo make install"]) st.patch("~/.bashrc", addpath(jar))
def get_change_time(fname): fname = el.expand_file_name(fname) if el.file_directory_p(fname): git_dir = el.expand_file_name(".git", fname) if el.file_exists_p(git_dir): return float(git.mtime(fname).strftime("%s")) else: raise RuntimeError("Directory is not a git repo") else: return os.path.getctime(fname)
def git_clone(url, target_dir, commit=None): target_dir = el.expand_file_name(target_dir) if el.file_exists_p(target_dir): print(lf("{target_dir}: OK")) else: gdir = el.expand_file_name(target_dir) pdir = el.file_name_directory(gdir) if not el.file_exists_p(pdir): el.make_directory(pdir) (_, gdir) = el.parse_fname(gdir) sc("git clone --recursive {url} {gdir}") if commit: sc("cd {gdir} && git reset --hard {commit}")
def clone(remote, local): res = [] local = el.expand_file_name(local) if el.file_exists_p(local): if el.file_exists_p(el.expand_file_name(".git", local)): res += ["cd " + local, "git pull"] else: raise RuntimeError("Directory exists and is not a git repo", local) else: (bd, repo) = os.path.split(local) el.make_directory(bd) res += [lf("cd {bd}"), lf("git clone {remote} {repo}")] return res
def modules(full=False, match=False): cook_dir = el.file_name_directory(recipes.__file__) cook_modules = el.directory_files(cook_dir, full, match) user_dir = el.expand_file_name("~/.cook.d") if el.file_exists_p(user_dir): df = el.directory_files(user_dir, full, match) user_modules = [ f for f in df if os.path.isfile(el.expand_file_name(f, user_dir)) and os.path.splitext(f)[1] == ".py" ] else: user_modules = [] cook_modules += user_modules return list(filter(lambda s: not re.search("__", s), cook_modules))
def book_config(book): rc_file = el.expand_file_name("~/.cook.d/__config__.py") if el.file_exists_p(rc_file): mod = imp.load_source("book_config", rc_file) config = mod.config if book in config: return config[book] elif "*" in config: return config["*"] return {}
def ln(fr, to): fr_full = el.expand_file_name(fr) if not el.file_exists_p(fr_full): raise RuntimeError("File doesn't exist", fr_full) to_full = el.expand_file_name(to) if el.file_exists_p(to_full): if symlink_p(to_full): print(lf("{to_full}: OK")) else: if file_equal(fr_full, to_full): print(lf("{to_full} exists, contents equal")) else: print(lf("{to_full} exists, contents NOT equal")) else: if el.HOST: cmd = lf("ln -s {fr} {to}") else: fr_abbr = os.path.relpath(fr_full, os.path.dirname(to)) cmd = sudo(lf("ln -s {fr_abbr} {to_full}"), to_full) bash(cmd)
def cp_host(fr, to=None): # if el.file_exists_p(to) and file_equal(fr, to): # print(lf("{to}: OK")) # return False if to is None: to = el.file_name_nondirectory(fr).replace("_", "/") host = el.HOST with el.hostname(None): fr = el.expand_file_name(fr) el.sc("scp -r '{fr}' '{host}:{to}'") return True
def wget(url, download_dir="/tmp/"): if download_dir[:-1] == "/": fname = url.split("/")[-1] full_name = el.expand_file_name(fname, download_dir) else: full_name = download_dir if el.file_exists_p(full_name): print(lf("{full_name}: OK")) else: bash(lf("wget '{url}' -O {full_name}")) return full_name
def burn_to_usb(recipe, fname): disks = el.sc_l("df") media_disks = el.re_filter("/media/", disks) assert len(media_disks) == 1 usb_disk_data = media_disks[0].split() assert len(usb_disk_data) == 6 usb_disk = usb_disk_data[0] fname_full = el.expand_file_name(fname) return [ "sudo umount " + usb_disk, el. lf("sudo dd bs=4M if={fname_full} of={usb_disk} status=progress oflag=sync" ) ]
def install_go(version="1.17.1"): existing_exe = shutil.which("go") if existing_exe: existing_version = el.re_find("go([0-9]+\\.[0-9]+\\.[0-9]+)", scb(f"{existing_exe} version")) print("Found go: ", existing_version) if existing_version == version: print("Versions match. Exit") return bash("sudo rm -rf /usr/local/go /usr/local/bin/go") tar_file = f"go{version}.linux-amd64.tar.gz" tar_file_full = el.expand_file_name(tar_file, "/tmp/") url = f"https://golang.org/dl/go{version}.linux-amd64.tar.gz" fname = wget(url, "~/Software/") make("/usr/local/go", [ "sudo rm -rf '/usr/local/go'", f"cat {fname} | sudo tar -xz -C /usr/local"]) make("/usr/local/bin/go", [ "sudo ln -sf /usr/local/go/bin/go $@"])
def export_html(recipe, fname): fname = el.expand_file_name(fname) fhtml = re.sub("org$", "html", fname) el.sc(el.emacs_batch_eval(lf('(cs-org-to-html "{fname}")'))) if "INSIDE_EMACS" not in os.environ: return ["firefox " + shlex.quote(fhtml)]
def repo_p(d): return el.file_exists_p(el.expand_file_name(".git", d))
def __init__(self, d): self.d = el.expand_file_name(d) self._old_dir = None
def curl(link, directory="~/Software"): fname = el.expand_file_name(link.split("/")[-1], directory) make(fname, "curl " + link + " -o " + shlex.quote(fname)) return fname