def clean_orphans(d): """Cleans out orphaned files int he given directory""" res = True (texs, omdocs, pdfs, sms) = find_files(d, "tex", "omdoc", "pdf", "sms") # # Orphaned omdocs # for file in omdocs: if not (file[:-len(".omdoc")]+".tex" in texs): if not rm_untracked(file, "orphaned omdoc"): res = False # # Orphaned pdf # for file in pdfs: if not (file[:-len(".pdf")]+".tex" in texs): if not rm_untracked(file, "orphaned pdf"): res = False # # Orphaned sms # for file in sms: if not (file[:-len(".sms")]+".tex" in texs): if not rm_untracked(file, "orphaned sms"): res = False return res
def find(rep, args): """Finds pattern in repositories""" match = args.matcher replace = args.replace[0] if args.apply else None # Find files in the repository files = find_files(match_repo(rep, abs=True), "tex")[0] return find_cached(files, match, replace)
def clean_logs(d): """Cleans out logs in the given directory. """ (ltxlog, pdflog) = find_files(d, "ltxlog", "pdflog") for f in ltxlog+pdflog: if not rm_untracked(f, "log file"): res = False res = True return res
def copy_template_dir(source, destination, vars): """Copies over a template directory. """ # Make absolute paths source = os.path.abspath(source) destination = os.path.abspath(destination) try: # Remove already existing files. distutils.dir_util.copy_tree(source, destination) # Find all the template files for f in find_files(destination, "tpl")[0]: # Substitute everything in the template newcontent = Template(read_file(f)).safe_substitute(vars) write_file(f[:-len(".tpl")], newcontent) # Delete the tpl file os.remove(f) except Exception as e: err("Error initalising template. ") err(e) return False return True
def movemod(source, dest, modules, no_depcrawl, simulate = False): """Moves modules from source to dest. """ # change directory to MathHub root, makes paths easier if simulate: std("cd "+lmh_locate("content")) else: os.chdir(lmh_locate("content")) finds = [] replaces = [] # Match the repos source = match_repo(source, root=lmh_locate("content")) dest = match_repo(dest, root=lmh_locate("content")) if source == None: err("Source repository does not exist, make sure it is installed. ") return False if dest == None: err("Destination repository does not exist, make sure it is installed. ") return False if source == dest: err("Cannot move modules when source and destination are the same. ") return False # Store original source and destination osource = source odest = dest # Make a list of all the moved files. moved_files = [] local_finds = [] local_replaces = [] def run_lmh_find_moved(find, replace): if simulate: # We will run it over dest only. std("lmh", "find", json.dumps(find), "--replace", json.dumps(replace), "--apply", odest) else: # Append it to to a list. local_finds.append(find) local_replaces.append(replace) for module in modules: dest = odest # Figure out the full path to the source srcpath = source + "/source/" + module # Assemble source paths further srcargs = (source + "/" + module).split("/") srcapath = "\\/".join(srcargs[:-1]) srcbpath = srcargs[-1] # Assemble all the commands oldcall = "\[" + srcapath + "\]\{"+srcbpath+"\}" oldcall_long = "\[(.*)repos=" + srcapath + "(.*)\]\{"+srcbpath+"\}" oldcall_local = "\{"+srcbpath+ "\}" newcall = "[" + dest + "]{"+srcbpath+"}" newcall_long = "[$g1" + dest + "$g2]{"+srcbpath+"}" dest += "/source/" # Move the files if simulate: std("mv "+srcpath + ".*.tex"+ " "+ dest + " 2>/dev/null || true") std("mv "+srcpath + ".tex"+ " "+ dest + " 2>/dev/null || true") else: try: shutil.move(srcpath + ".tex", dest) moved_files.append(os.path.join(dest, os.path.basename(srcpath + ".tex"))) except: pass for pat in glob.glob(srcpath + ".*.tex"): # try to move the file if it exists try: shutil.move(pat, dest) moved_files.append(os.path.join(dest, os.path.basename(pat))) except: pass def run_lmh_find(find, replace): finds.append(find) replaces.append(replace) # Run all the commands m = "("+"|".join(["gimport", "guse", "gadopt"])+")" run_lmh_find(r'\\'+m+oldcall, '\\$g0'+newcall) run_lmh_find(r'\\'+m+oldcall_local, '\\$g0'+newcall) m = "("+ "|".join(["importmhmodule", "usemhmodule", "adoptmhmodule", "usemhvocab"]) + ")" run_lmh_find(r'\\'+m+oldcall_long, '\\$g0'+newcall_long) run_lmh_find(r'\\'+m+oldcall_local, '\\$g0'+newcall_long) # For the moved files, repalce gimport, guse, gadpot run_lmh_find_moved(r"\\("+"|".join(["gimport", "guse", "gadopt"])+")\["+dest[-len("/source/")]+"\]\{(.*)\}", "\\$g1{$g2}") # Update the moved files. run_lmh_find_moved(r"\\("+"|".join(["gimport", "guse", "gadopt"])+")\{(((?!(?<=\{)("+"|".join(modules)+")\}).)*?)\}", "\\$g1{$g2}") # Make the repo paths absolute osource = match_repo(osource, abs=True) odest = match_repo(odest, abs=True) files = reduce([find_files(r, "tex")[0] for r in match_repos(lmh_locate("content"), abs=True)]) if simulate: for (f, r) in zip(finds, replaces): std("lmh find", json.dumps(f), "--replace", json.dumps(r), "--apply") if not no_depcrawl: calc_deps(False, dirname=osource) calc_deps(False, dirname=odest) return True else: std("updating paths in the following files: ") res1 = find_cached(files, finds, replace=replaces) res2 = find_cached(moved_files, local_finds, replace=local_replaces) if not no_depcrawl: res3 = calc_deps(True, osource) res4 = calc_deps(True, odest) else: res3 = True res4 = True return res1 and res2 and res3 and res4
def rename(where, renamings, simulate = False): """Moves modules from source to dest. """ where = os.path.abspath(where) if not os.path.isdir(where): err("Cannot rename:", where, "is not a directory. ") return False if len(renamings) % 2 != 0: err("You must provide renamings in pairs. ") return False if len(renamings) == 0: std("Nothing to rename ...") return True regexes = [] replaces = [] # Compile regexes i = 0 while i< len(renamings): # What we need to find find = renamings[i] find_parts = find.split("-") find_args = r"\{"+(r"\}\{".join(find_parts))+r"\}" find_i = "i"*len(find_parts) # What we need to replace replace = renamings[i+1] replace_parts = replace.split("-") replace_args = "{"+("}{".join(replace_parts))+"}" replace_i = "i"*len(replace_parts) # defi regexes.append(re.compile(r"\\def"+find_i+r"\["+find+r"\]"+find_args)) replaces.append("\\def"+replace_i+"["+replace+"]"+replace_args) # defi (Michael) regexes.append(re.compile(r"(\\def(?:i{1,3}))\["+find+r"\](\{(?:[^\}]*)\})")) replaces.append("\\1["+replace+"]\\2") # trefi regexes.append(re.compile(r"\\tref"+find_i+r"\[([^\]]*)\]"+find_args)) replaces.append("\\\\tref"+replace_i+"[\\1]"+replace_args) # atrefi regexes.append(re.compile(r"\\atref"+find_i+r"\[([^\]]*)\]\{([^\}]*)\}"+find_args)) replaces.append("\\\\atref"+replace_i+"[\\1]{\\2}"+replace_args) # mtrefi regexes.append(re.compile(r"\\mtref"+find_i+r"\[([^\]\?]*)\?"+find+r"\]"+find_args)) replaces.append("\\mtref"+replace_i+"[\\1?"+replace+"]"+replace_args) # go to the next pattern. i = i+2 actions = zip(regexes, replaces) # Find all the files for file in find_files(where, "tex")[0]: # Read a file content = read_file(file) # Run all of the actions for (f, r) in actions: content = f.sub(r, content) write_file(file, content) return True
def rename(where, renamings, simulate=False): """Moves modules from source to dest. """ where = os.path.abspath(where) if not os.path.isdir(where): err("Cannot rename:", where, "is not a directory. ") return False if len(renamings) % 2 != 0: err("You must provide renamings in pairs. ") return False if len(renamings) == 0: std("Nothing to rename ...") return True regexes = [] replaces = [] # Compile regexes i = 0 while i < len(renamings): # What we need to find find = renamings[i] find_parts = find.split("-") find_args = r"\{" + (r"\}\{".join(find_parts)) + r"\}" find_i = "i" * len(find_parts) # What we need to replace replace = renamings[i + 1] replace_parts = replace.split("-") replace_args = "{" + ("}{".join(replace_parts)) + "}" replace_i = "i" * len(replace_parts) # defi regexes.append( re.compile(r"\\def" + find_i + r"\[" + find + r"\]" + find_args)) replaces.append("\\def" + replace_i + "[" + replace + "]" + replace_args) # defi (Michael) regexes.append( re.compile(r"(\\def(?:i{1,3}))\[" + find + r"\](\{(?:[^\}]*)\})")) replaces.append("\\1[" + replace + "]\\2") # trefi regexes.append( re.compile(r"\\tref" + find_i + r"\[([^\]]*)\]" + find_args)) replaces.append("\\\\tref" + replace_i + "[\\1]" + replace_args) # atrefi regexes.append( re.compile(r"\\atref" + find_i + r"\[([^\]]*)\]\{([^\}]*)\}" + find_args)) replaces.append("\\\\atref" + replace_i + "[\\1]{\\2}" + replace_args) # mtrefi regexes.append( re.compile(r"\\mtref" + find_i + r"\[([^\]\?]*)\?" + find + r"\]" + find_args)) replaces.append("\\mtref" + replace_i + "[\\1?" + replace + "]" + replace_args) # go to the next pattern. i = i + 2 actions = zip(regexes, replaces) # Find all the files for file in find_files(where, "tex")[0]: # Read a file content = read_file(file) # Run all of the actions for (f, r) in actions: content = f.sub(r, content) write_file(file, content) return True
def movemod(source, dest, modules, no_depcrawl, simulate=False): """Moves modules from source to dest. """ # change directory to MathHub root, makes paths easier if simulate: std("cd " + lmh_locate("content")) else: os.chdir(lmh_locate("content")) finds = [] replaces = [] # Match the repos source = match_repo(source, root=lmh_locate("content")) dest = match_repo(dest, root=lmh_locate("content")) if source == None: err("Source repository does not exist, make sure it is installed. ") return False if dest == None: err("Destination repository does not exist, make sure it is installed. " ) return False if source == dest: err("Cannot move modules when source and destination are the same. ") return False # Store original source and destination osource = source odest = dest # Make a list of all the moved files. moved_files = [] local_finds = [] local_replaces = [] def run_lmh_find_moved(find, replace): if simulate: # We will run it over dest only. std("lmh", "find", json.dumps(find), "--replace", json.dumps(replace), "--apply", odest) else: # Append it to to a list. local_finds.append(find) local_replaces.append(replace) for module in modules: dest = odest # Figure out the full path to the source srcpath = source + "/source/" + module # Assemble source paths further srcargs = (source + "/" + module).split("/") srcapath = "\\/".join(srcargs[:-1]) srcbpath = srcargs[-1] # Assemble all the commands oldcall = "\[" + srcapath + "\]\{" + srcbpath + "\}" oldcall_long = "\[(.*)repos=" + srcapath + "(.*)\]\{" + srcbpath + "\}" oldcall_local = "\{" + srcbpath + "\}" newcall = "[" + dest + "]{" + srcbpath + "}" newcall_long = "[$g1" + dest + "$g2]{" + srcbpath + "}" dest += "/source/" # Move the files if simulate: std("mv " + srcpath + ".*.tex" + " " + dest + " 2>/dev/null || true") std("mv " + srcpath + ".tex" + " " + dest + " 2>/dev/null || true") else: try: shutil.move(srcpath + ".tex", dest) moved_files.append( os.path.join(dest, os.path.basename(srcpath + ".tex"))) except: pass for pat in glob.glob(srcpath + ".*.tex"): # try to move the file if it exists try: shutil.move(pat, dest) moved_files.append( os.path.join(dest, os.path.basename(pat))) except: pass def run_lmh_find(find, replace): finds.append(find) replaces.append(replace) # Run all the commands m = "(" + "|".join(["gimport", "guse", "gadopt"]) + ")" run_lmh_find(r'\\' + m + oldcall, '\\$g0' + newcall) run_lmh_find(r'\\' + m + oldcall_local, '\\$g0' + newcall) m = "(" + "|".join([ "importmhmodule", "usemhmodule", "adoptmhmodule", "usemhvocab" ]) + ")" run_lmh_find(r'\\' + m + oldcall_long, '\\$g0' + newcall_long) run_lmh_find(r'\\' + m + oldcall_local, '\\$g0' + newcall_long) # For the moved files, repalce gimport, guse, gadpot run_lmh_find_moved( r"\\(" + "|".join(["gimport", "guse", "gadopt"]) + ")\[" + dest[-len("/source/")] + "\]\{(.*)\}", "\\$g1{$g2}") # Update the moved files. run_lmh_find_moved( r"\\(" + "|".join(["gimport", "guse", "gadopt"]) + ")\{(((?!(?<=\{)(" + "|".join(modules) + ")\}).)*?)\}", "\\$g1{$g2}") # Make the repo paths absolute osource = match_repo(osource, abs=True) odest = match_repo(odest, abs=True) files = reduce([ find_files(r, "tex")[0] for r in match_repos(lmh_locate("content"), abs=True) ]) if simulate: for (f, r) in zip(finds, replaces): std("lmh find", json.dumps(f), "--replace", json.dumps(r), "--apply") if not no_depcrawl: calc_deps(False, dirname=osource) calc_deps(False, dirname=odest) return True else: std("updating paths in the following files: ") res1 = find_cached(files, finds, replace=replaces) res2 = find_cached(moved_files, local_finds, replace=local_replaces) if not no_depcrawl: res3 = calc_deps(True, osource) res4 = calc_deps(True, odest) else: res3 = True res4 = True return res1 and res2 and res3 and res4