def test_mv_dir2nonexdir(): assert os.system("sk run") == 0 assert os.system("sk mv code/subdf code/subdf2") == 0 # check the files assert os.path.isfile("code/subdf2/pagesd.Rmd") assert os.path.isfile("report/out_md/code/subdf2/pagesd.md") # check the yaml assert "code/subdf2/pagesd.Rmd" in yaml_in()["analysis"].keys() assert "code/subdf2/non_rmd_dep.txt" in yaml_in( )["analysis"]["code/subdf2/pagesd.Rmd"]
def test_mv_mul2dir(): assert os.system("sk run") == 0 assert os.system("sk mv code/*.Rmd code/subdir") == 0 # check the files assert os.path.isfile("code/subdir/page1.Rmd") assert os.path.isfile("code/subdir/page2.Rmd") assert os.path.isfile("report/out_md/code/subdir/page1.md") assert os.path.isfile("report/out_md/code/subdir/page2.md") # check the yaml assert "code/subdir/page1.Rmd" in yaml_in()["analysis"].keys() assert "code/subdir/page2.Rmd" in yaml_in()["analysis"].keys()
def sk_mv(args): """Rename an Rmd in scikick.yml and associated files""" config = yaml_in(need_pages=True) # multiple args src = [os.path.normpath(p) for p in args.src] # only a single arg dest = [os.path.normpath(p) for p in args.dest] #check if src and dest are valid sk_move_check(src, dest) # save the (src, dest) pairs (to be used with moves in scikick.yml) (new_src, new_dest) = sk_move_prepare_src_dest(src, dest) new_src = list(map(os.path.normpath, new_src)) new_dest = list(map(os.path.normpath, new_dest)) # leave only unique pairs of (src, dest) files mv_dict = ordereddict() for k in new_src: i = new_src.index(k) mv_dict[new_src[i]] = new_dest[i] # perform the move operation (using the initial args) for s in src: git_retcode = 0 if args.git: print(f"sk: git mv {s} {dest[0]}") git_res = subprocess.run(f"git mv {s} {dest[0]}", shell=True, \ stderr=subprocess.PIPE) git_retcode = git_res.returncode if git_retcode != 0: warn("sk: Warning: Git returned an error:") warn(git_res.stderr.decode().strip()) warn("sk: Warning: Falling back to mv") if (git_retcode != 0) or (not args.git): print(f"sk: mv {s} {dest[0]}") shutil.move(s, dest[0]) sk_move_extras(mv_dict)
def test_mv_dir2exdir(): assert os.system("sk run") == 0 assert os.system("sk mv code/subdf code/subdir") == 0 # check the files assert os.path.isfile("code/subdir/subdf/pagesd.Rmd") assert os.path.isfile("report/out_md/code/subdir/subdf/pagesd.md") # check the yaml assert "code/subdir/subdf/pagesd.Rmd" in yaml_in()["analysis"].keys()
def test_mv_file2file(): assert os.system("sk run") == 0 assert os.system("sk mv code/page1.Rmd code/page3.Rmd") == 0 # check the files assert os.path.isfile("code/page3.Rmd") assert os.path.isfile("report/out_md/code/page3.md") # check the yaml assert "code/page3.Rmd" in yaml_in()["analysis"].keys()
def test_mv_file2subdirfile_runagain(): assert os.system("sk run") == 0 assert os.system("sk mv code/page1.Rmd code/subdf/page1.Rmd") == 0 # check the files assert os.path.isfile("code/subdf/page1.Rmd") assert os.path.isfile("report/out_md/code/subdf/page1.md") # check the yaml assert "code/subdf/page1.Rmd" in yaml_in()["analysis"].keys() assert os.system("sk run > /dev/null") == 0
def write_snakefile_arg(arg, val): """Set the specified arg's value in scikick.yml args -- name of the argument val -- value of the argument """ yml = yaml_in() if "snakefile_args" not in yml.keys(): yml["snakefile_args"] = dict() yml["snakefile_args"][arg] = val warn(f"sk: Argument {arg} set to {yml['snakefile_args'][arg]}") yaml_dump(yml)
def main(): """Parse the arguments and run the according function""" args = parser.parse_args() try: func = args.func except AttributeError: parser.print_help() return if args.which in ["run", "config", "status"]: # check for unsupported fields ymli = yaml_in() yaml_check(ymli) func(args)
def sk_move_check(src, dest): """Perform checks on src and dest and quit if bad arguments are given src -- list of files to move des -- list containing the file/dir to move to """ config = yaml_in() for s in src: if not os.path.exists(s): reterr(f"sk: Error: file or directory {s} doesn't exist") if len(src) > 1: if not os.path.isdir(dest[0]): reterr("sk: Error: moving multiple files to a single one") elif len(src) == 1 and (not os.path.isdir(dest[0])): old_ext = os.path.splitext(src[0])[1] new_ext = os.path.splitext(dest[0])[1] if old_ext.lower() != new_ext.lower(): warn("sk: Warning: changing file extension") if (src[0] in config["analysis"].keys()) and \ (new_ext.lower() not in map(str.lower, supported_extensions)): reterr( f"sk: Error: only extensions {', '.join(supported_extensions)} are supported ({new_ext} given)" )
def sk_move_extras(mv_dict): """Performs move operations specific to scikick Moves md files, figures in output/ directories and renames files in scikick.yml mv_dict -- dict with keys as src and values as dest files """ # Moving mds, knitmetas and output figures in out_md/; ## No need to change _site.ymls, since ## they are recreated after each change in scikick.yml yaml_dict = yaml_in() analysis = yaml_dict["analysis"] reportdir = yaml_dict["reportdir"] for src, dest in mv_dict.items(): # if not in analysis.keys -> regular file # in this case, rename and continue if src not in analysis.keys(): if 1 == rename(src, dest): warn("sk: %s renamed to %s in ./scikick.yml" % (src, dest)) else: warn("sk: Warning: %s not found in ./scikick.yml" % src) continue md_rootdir = os.path.join(reportdir, "out_md") md_destdir = os.path.join(md_rootdir, os.path.dirname(dest)) md_srcdir = os.path.join(md_rootdir, os.path.dirname(src)) if not os.path.isdir(md_destdir): os.makedirs(md_destdir) # Move .md md_src = os.path.join(md_rootdir, os.path.splitext(src)[0] + ".md") md_dest = os.path.join( md_destdir, os.path.splitext(os.path.basename(dest))[0] + ".md") if os.path.isfile(md_src): shutil.move(md_src, md_dest) # Move knitmeta k_src = sub(pattern="\.md$", repl=".knitmeta.RDS", string=md_src) k_dest = sub(pattern="\.md$", repl=".knitmeta.RDS", string=md_dest) if os.path.isfile(k_src): shutil.move(k_src, k_dest) # Move markdown outputs (figures) tabname_src = sub(string=os.path.basename(md_src), pattern="\.md$", repl="") tabname_dest = sub(string=os.path.basename(md_dest), pattern="\.md$", repl="") # "figure" must match execute_code.R fig.path md_srcfigdir = os.path.join(md_srcdir, "figure", tabname_src) md_destfigdir = os.path.join(md_destdir, "figure", tabname_dest) if os.path.isdir(md_srcfigdir): dest_dirname = os.path.dirname(md_srcfigdir) if not os.path.isdir(dest_dirname): os.makedirs(dest_dirname, exist_oke=True) print(f"sk: mv {md_srcfigdir} {md_destfigdir}") shutil.move(md_srcfigdir, md_destfigdir) # rename the figure/ directory name in the dest md ## get the initial timestamp initial_timestamp = os.path.getmtime(md_dest) md_file = open(md_dest, 'r+') md_lines = [ sub(string=line, pattern=f'src="figure/{tabname_src}', repl=f'src="figure/{tabname_dest}') for line in md_file ] md_file.seek(0) for l in md_lines: md_file.write(l) md_file.close() # set the initial timestamp back to avoid reexecution os.utime(md_dest, (initial_timestamp, initial_timestamp)) # rename all entries in scikick.yml from from to f_dest if 1 == rename(src, dest): warn("sk: %s renamed to %s in ./scikick.yml" % (src, dest)) else: warn("sk: Warning: %s not found in ./scikick.yml" % src)
def read(self, need_pages=False): """Read scikick.yml, eventually to replace yaml_in()""" self.config = yaml_in(self.filename, need_pages)