def create_repo(pkg_name): """Create new gitlab repo""" ws = Workspace() ws.recreate_index(write=True) pkg_list = ws.get_catkin_package_names() pkg_dicts = ws.get_wstool_packages() if not pkg_name: pkg_name = os.path.basename(ws.org_dir) if pkg_name not in pkg_list: click.secho( "{0} does not seem to be a catkin package.".format(pkg_name), fg="red") sys.exit(1) click.echo("Creating repo for {0}".format(pkg_name)) for ps in ws.wstool_config.get_config_elements(): if ps.get_local_name() == pkg_name: click.secho("Repository has a url already: {0}".format( ps.get_path_spec().get_uri())) sys.exit(1) ws.cd_src() os.chdir(pkg_name) git = Git() ssh_url = git.create_repo(pkg_name) subprocess.call("git init", shell=True) subprocess.call("git remote add origin " + ssh_url + " >/dev/null 2>&1", shell=True) ws.recreate_index() click.echo( "You should run 'mrt maintenance update_url_in_package_xml' now")
def main(action, args): """ A wrapper for wstool. """ ws = Workspace() ws.cd_src() if action == "init": click.secho("Removing wstool database src/.rosinstall", fg="yellow") os.remove(".rosinstall") click.echo("Initializing wstool...") ws.recreate_index() return # Need to init wstool? if not os.path.exists(".rosinstall"): ws.write() if action == "update": # Test git credentials to avoid several prompts if ws.contains_https(): test_git_credentials() ws.recreate_index() # Rebuild .rosinstall in case a package was deletetd manually # Speedup the pull process by parallelization if not [a for a in args if a.startswith("-j")]: args += ("-j10",) if action == "fetch": if ws.contains_https(): test_git_credentials() action = "foreach" args = ("git fetch", ) if action == "push": # Search for unpushed commits unpushed_repos = ws.unpushed_repos() if len(unpushed_repos) > 0: if click.confirm("Push them now?"): for x in unpushed_repos: ws.cd_src() os.chdir(x) subprocess.call("git push", shell=True) else: click.echo("No unpushed commits.") sys.exit(0) # Pass the rest to wstool if len(args) == 0: process = subprocess.Popen(["wstool", action, "-t", ws.src]) else: process = subprocess.Popen(["wstool", action, "-t", ws.src] + list(args)) process.wait() # Wait for process to finish and set returncode if action == "status": ws.unpushed_repos() sys.exit(process.returncode)
def update_rosinstall(): """Reinitialise the workspace index""" ws = Workspace() ws.cd_src() click.secho("Removing wstool database src/.rosinstall", fg="yellow") os.remove(".rosinstall") click.echo("Initializing wstool...") ws.recreate_index(write=True)
def update_cmakelists(package, this): """Update CMAKELISTS""" ws = Workspace() catkin_packages = ws.get_catkin_package_names() # Read in newest CMakeLists.txt current_version = None # download newest version: click.echo("Downloading newest template from gitlab") git = Git() mrt_build_repo = git.find_repo("mrt_build") new_cmakelists = git.server.getrawfile( mrt_build_repo['id'], "master", 'mrt_tools/templates/CMakeLists.txt') for line in new_cmakelists.splitlines(): if line.startswith("#pkg_version="): current_version = line[:-1] break if not current_version: click.secho("current pkg_version could not be found.", fg='red') sys.exit(1) if this: package = os.path.basename(ws.org_dir) if package not in catkin_packages: click.secho( "{0} does not seem to be a catkin package.".format(package), fg="red") sys.exit(1) if not package: for pkg_name in catkin_packages: ws.cd_src() check_and_update_cmakelists(pkg_name, current_version) else: ws.cd_src() check_and_update_cmakelists(package, current_version)
def update_url_in_package_xml(): """Updates missing or wrong URL into package.xml""" def insert_url(file_name, url): with open(file_name, 'r') as f: contents = f.readlines() click.clear() for index, item in enumerate(contents): click.echo("{0}: {1}".format(index, item[:-1])) linenumber = click.prompt( "\n\nPlease specify the line to insert the url in", type=click.INT) contents.insert(linenumber, ' <url type="repository">{0}</url>\n'.format(url)) contents = "".join(contents) with open(file_name, 'w') as f: f.write(contents) click.clear() if click.confirm("OK, did that. Commit these changes?"): org_dir = os.getcwd() os.chdir(os.path.dirname(file_name)) subprocess.call("git add {0}".format(file_name), shell=True) subprocess.call( "git commit -m 'Added repository url to package.xml'", shell=True) os.chdir(org_dir) ws = Workspace() ws.catkin_pkg_names = ws.get_catkin_package_names() ws.config = wstool_config.Config([], ws.src) ws.cd_src() for pkg_name in ws.catkin_pkg_names: filename = os.path.join(ws.src, pkg_name, "package.xml") # Try reading it from git repo try: # TODO Maybe try to always get the https/ssh url? Right now, it is only checked against how YOU have it # configured. with open(pkg_name + "/.git/config", 'r') as f: git_url = next(line[7:-1] for line in f if line.startswith("\turl")) except (IOError, StopIteration): git_url = None # Try to read it from package xml try: if len(ws.catkin_pkgs[pkg_name].urls) > 1: raise IndexError xml_url = ws.catkin_pkgs[pkg_name].urls[0].url except IndexError: xml_url = None # Testing all cases: if xml_url is not None and git_url is not None: if xml_url != git_url: click.secho( "WARNING in {0}: URL declared in src/{1}/package.xml, differs from the git repo url for {" "0}!".format(pkg_name.upper(), pkg_name), fg="red") click.echo("PackageXML: {0}".format(xml_url)) click.echo("Git repo : {0}".format(git_url)) if click.confirm( "Replace the url in package.xml with the correct one?" ): subprocess.call( "sed -i -e '/ <url/d' {0}".format(filename), shell=True) insert_url(filename, git_url) if xml_url is not None and git_url is None: click.secho( "WARNING in {0}: URL declared in package.xml, but {1} does not seem to be a remote " "repository!".format(pkg_name.upper(), pkg_name), fg="yellow") if click.confirm("Remove the url in package.xml?"): click.secho("Fixing...", fg="green") subprocess.call("sed -i -e '/ <url/d' {0}".format(filename), shell=True) if xml_url is None and git_url is not None: click.secho( "WARNING in {0}: No URL (or multiple) defined in package.xml!". format(pkg_name.upper()), fg="yellow") if click.confirm( "Insert (Replace) the url in package.xml with the correct one?" ): subprocess.call("sed -i -e '/ <url/d' {0}".format(filename), shell=True) insert_url(filename, git_url) if xml_url is None and git_url is None: click.secho( "INFO in {0}: Does not seem to be a git repository. You should use Version Control for your " "code!".format(pkg_name.upper()), fg="cyan") if git_url is not None: ws.add(pkg_name, git_url, update=False) ws.write()
def rename_pkg(new_name): """ """ ws = Workspace() package = os.path.basename(ws.org_dir) catkin_packages = ws.get_catkin_package_names() if package not in catkin_packages: click.secho( "{0} does not seem to be a catkin package.".format(package), fg="red") sys.exit(1) if new_name in catkin_packages: click.secho( "{0} does already exist in your workspace.".format(new_name), fg="red") sys.exit(1) # Test files for dirName, subdirList, fileList in os.walk(ws.src + "/" + package): if "/.git" in dirName: continue for fname in fileList: if fname.endswith(".h") or fname.endswith(".hh") or fname.endswith(".hpp") \ or fname.endswith(".cc") or fname.endswith(".cpp"): # Adjust includes subprocess.call("sed -i -e 's:#include\s[\"<]" + package + "/:#include \"" + new_name + "/:g' " + dirName + "/" + fname, shell=True) else: # Rename all other occurrences subprocess.call("sed -i -e 's/" + package + "/" + new_name + "/g' " + dirName + "/" + fname, shell=True) # Move include folder if os.path.exists(ws.src + "/" + package + "/include/" + package): shutil.move(ws.src + "/" + package + "/include/" + package, ws.src + "/" + package + "/include/" + new_name) # Test for git repo if not os.path.exists(ws.src + "/" + package + "/.git"): click.echo("Renamed package " + package + " to " + new_name) return os.chdir(ws.src + "/" + package) click.echo("The following files in this package have been changed:") subprocess.call("git status -s", shell=True) click.echo("") click.echo("Next steps:") click.echo("\t-Review changes") click.echo("\t-Commit changes") click.echo("") while ws.test_for_changes(package, quiet=True): click.prompt("Continue, when changes are commited and pushed...") click.echo("") click.confirm("Do you want to move the gitlab project now?", abort=True) click.echo("Moving gitlab project...") git = Git() project = git.find_repo(package) namespace = project["namespace"]["name"] project_id = project["id"] if not git.server.editproject(project_id, name=new_name, path=new_name): click.secho("There was a problem, moving the project. Aborting!", fg="red") sys.exit(1) click.echo("Updating git remote...") os.chdir(ws.src + "/" + package) project = git.find_repo(new_name, namespace) new_url = project[git.get_url_string()] subprocess.call("git remote set-url origin " + new_url + " >/dev/null 2>&1", shell=True) click.echo("Updating local ws...") ws.cd_src() shutil.move(package, new_name) os.chdir(new_name) os.remove(ws.src + "/.rosinstall") ws.recreate_index(write=True) click.echo("") click.echo("Next steps:") click.echo("\t-Adjust includes in other packages")