def get_paths(self, branch): rpath = match_repo(self.repo, abs=True) if not self.get_branch_path(branch): return (rpath, False) dpath = os.path.join(rpath, self.get_branch_path(branch)) return (rpath, dpath)
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 __init__(self, rep): """ Loads information about generated repositories. """ # Get the repository. self.repo = match_repo(rep) # Check if we exist at all. if not self.repo: err("Unable to find repository '" + rep + "'") # Initialise the cache. self.__branch_cache = None
def __init__(self, rep): """ Loads information about generated repositories. """ # Get the repository. self.repo = match_repo(rep) # Check if we exist at all. if not self.repo: err("Unable to find repository '"+rep+"'") # Initialise the cache. self.__branch_cache = None
def init_branch(self, branch): """ Creates a new branch for status information. """ # Get name and path. bsplit = branch.split(":") if len(bsplit) == 1: (name, pth) = (bsplit[0], bsplit[0]) else: (name, pth) = (bsplit[0], bsplit[1]) std("Creating branch '" + name + "' at '" + pth + "'. ") # Check if the branch already exists. if name in self.get_all_branches(tuple=False): err("Branch '" + name + "' already exists, can not create it again. Use --install to install. " ) return False # Get the paths rpath = match_repo(self.repo, abs=True) dpath = os.path.join(rpath, pth) meta_inf_path = os.path.join(rpath, "META-INF", "MANIFEST.MF") # Find paths for the .gitignore gitignore_path = os.path.join(rpath, ".gitignore") gitignore_entry = os.path.join("/", pth) + "\n" # and either add to it or create it. if os.path.isfile(gitignore_path): write_file(gitignore_path, read_file(gitignore_path) + gitignore_entry) else: write_file(gitignore_path, gitignore_entry) # Update the meta-inf written = False lines = get_metainf_lines(self.repo) # try to append it to a line that already exists. for (i, l) in enumerate(lines): if l.startswith(gbranchstring): lines[i] = lines[i].rstrip("\n") + " " + branch written = True break # or make a new one. if written == False: lines.extend([gbranchstring + " " + branch]) # and write that file. write_file(meta_inf_path, lines) # Create the orphaned branch. if not make_orphan_branch(rpath, name): return False # push it if not do(rpath, "push", "-u", "origin", name): err("Pushing branch to origin failed. ") return False # Clear the deploy branch cache for this repository. self.clear_branch_cache() # install it. if not self.install_branch(name): return False # change the commit message if not do(dpath, "commit", "--amend", "--allow-empty", "-m", "Create deploy branch. "): return False # and push it. if not do(dpath, "push", "--force", "origin", name): return False std("Generated files branch '" + name + "' created, installed and pushed. ") std("Please commit and push META-INF/MANIFEST.MF and .gitignore to publish installation. " ) return True
def init_branch(self, branch): """ Creates a new branch for status information. """ # Get name and path. bsplit = branch.split(":") if len(bsplit) == 1: (name, pth) = (bsplit[0], bsplit[0]) else: (name, pth) = (bsplit[0], bsplit[1]) std("Creating branch '"+name+"' at '"+pth+"'. ") # Check if the branch already exists. if name in self.get_all_branches(tuple=False): err("Branch '"+name+"' already exists, can not create it again. Use --install to install. ") return False # Get the paths rpath = match_repo(self.repo, abs=True) dpath = os.path.join(rpath, pth) meta_inf_path = os.path.join(rpath, "META-INF", "MANIFEST.MF") # Find paths for the .gitignore gitignore_path = os.path.join(rpath, ".gitignore") gitignore_entry = os.path.join("/", pth)+"\n" # and either add to it or create it. if os.path.isfile(gitignore_path): write_file(gitignore_path, read_file(gitignore_path)+gitignore_entry) else: write_file(gitignore_path, gitignore_entry) # Update the meta-inf written = False lines = get_metainf_lines(self.repo) # try to append it to a line that already exists. for (i, l) in enumerate(lines): if l.startswith(gbranchstring): lines[i] = lines[i].rstrip("\n") + " " + branch written = True break # or make a new one. if written == False: lines.extend([gbranchstring+" "+ branch]) # and write that file. write_file(meta_inf_path, lines) # Create the orphaned branch. if not make_orphan_branch(rpath, name): return False # push it if not do(rpath, "push", "-u", "origin", name): err("Pushing branch to origin failed. ") return False # Clear the deploy branch cache for this repository. self.clear_branch_cache() # install it. if not self.install_branch(name): return False # change the commit message if not do(dpath, "commit", "--amend", "--allow-empty", "-m", "Create deploy branch. "): return False # and push it. if not do(dpath, "push", "--force", "origin", name): return False std("Generated files branch '"+name+"' created, installed and pushed. ") std("Please commit and push META-INF/MANIFEST.MF and .gitignore to publish installation. ") 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 create(reponame, type="none", remote = True): """Creates a new repository in the given directory""" # Resolve the repository to create repo = match_repo(reponame, existence=False) if repo == None: err("Can not resolve repository to create. ") return False # Remote creation currently disabled if remote: err("Remote cration currently disabled. ") remote = False # and the full path absrepo = match_repo(reponame, abs=True, existence=False) repo_group = repo.split("/")[0] repo_name = repo.split("/")[1] # Check if it is already installed. # TODO: Use the other is_installed if is_installed(repo): err("Repository", repo, "already installed. ") err("Do you maybe want to push this to the remote?") return False # Make the directory if it does not yet exist. try: mkdir_p(absrepo) except: err("Can not create repository directory") return False if not get_config("init::allow_nonempty"): if not (not os.listdir(absrepo)): err("Target Directory is non-empty. ") err("If you want to enable lmh init on non-empty directories, please run") err(" lmh config init::allow_nonempty true") return False # Template Variables. tpl_vars = { "repo": repo, "repo_group": repo_group, "repo_name": repo_name, "install_dir": lmh_locate() } # Copy the base template if not copy_template_dir(os.path.join(emptyrepo_dir, "none"), absrepo, tpl_vars): err("Unable to create repository base. ") return False # Copy the specific type. if type != "none": type_dir = os.path.join(emptyrepo_dir, type) if os.path.isdir(type_dir): if not copy_template_dir(type_dir, absrepo, tpl_vars): err("Unable to use repository template. ") return False else: err("Unknown repository type: ", type) return False if git_root(absrepo) != absrepo: # Now lets make an init if not git_do(absrepo, "init"): err("Error creating git repository. ") err("The directory has been created successfully, however git init failed. ") err("Please run it manually. ") return False # Create the initial commit. if not (git_do(absrepo, "add", "-A") and git_commit(absrepo, "-m", "Repository created by lmh")): err("Error creating inital commit. ") err("The directory has been created successfully, however git commit failed. ") err("Please run it manually. ") return False # Can we find a remote for this? source = find_source(repo, quiet=True) # Don't do anything remote => we are done. if not remote: if source: if not git_do(absrepo, "remote", "add", "origin", source): err("Can not add origin. ") err("git is suddenly weird. ") return False else: std("Skipping remote creation because --no-remote is given. ") std("Repository created successfully. ") return True # Source does not exist => we will have to create it. if not source: source = create_remote(repo_group, repo_name) if not source: err("local repository created but remote creation failed. ") return False # Add the origin. if not git_do(absrepo, "remote", "add", "origin", source): err("Can not add origin. ") err("git is suddenly weird. ") return False if not git_push(absrepo, "-u", "origin", "master"): err("Repository created but could not push created repository. ") err("Check your network connection and try again using git push. ") return False std("Created new repository successfully. ") 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