def __install_camltracer(): git_clone(CAMLTRACER_REPO, CAMLTRACER_LOCAL_DIR, CAMLTRACER_RELEASE_TAG) run_command('patch {} {}'.format( os.path.join(CAMLTRACER_LOCAL_DIR, 'setup.py'), CAMLTRACER_SETUP_PATCH_FILE)) run_command('pip install ' + CAMLTRACER_LOCAL_DIR)
def create_merge_request(wip: bool, web: bool) -> None: try: run_command("glab mr view") except CalledProcessError: pass else: if web: run_command("glab mr view --web") return print("MR already exists.") exit(1) branch = run_command("git branch --show-current")[0] split_branch = branch.split("-") if split_branch[0] != getuser(): print("Current branch doesn't start with username") exit(1) if len(split_branch) < 3: print("Expected branch to contain at least 2 dashes") exit(1) ticket = split_branch[1] title_parts = split_branch[2:] if not any([ticket == "noticket", ticket.isnumeric()]): print( 'Expected branch to have ticket number or "noticket" as second part' ) title = title_parts[0].title() + " " title += " ".join(title_part.lower() for title_part in title_parts[1:]) prefix = "" if wip: prefix = "Draft: " maybe_hash_char = "" if ticket.isnumeric(): maybe_hash_char = "#" command = f"glab mr create -t '{prefix}[{maybe_hash_char}{ticket}] {title}' --fill --yes --remove-source-branch" try: run_command(command) except CalledProcessError as e: if "could not find any commits between" in str(e.stderr): print( "Creating MR failed: no commits between source and target branch." ) exit(1) if web: run_command("glab mr view --web") return
def run_trish(sessionA, sessionB): if shutil.which('trish') is None: run_command(TRISH_INSTALL_CMD) score = 0 for fileA in sessionA._get_submitted_source_files(): fileB = os.path.join( sessionB.submission().local_dir(), os.path.relpath(fileA, sessionA.submission().local_dir())) if os.path.isfile(fileB): output = run_command(f'trish "{fileA}" "{fileB}"') output.check_returncode() score += float(output.stdout) return score
def git_clone(repo, dest, tag=None): if os.path.exists(dest): raise IOError('Cannot clone, path already exists.') # If required, creating parent directory containing_dir = parent_dir(dest) existing_dir = containing_dir created_dir = None while not os.path.isdir(existing_dir): created_dir = existing_dir existing_dir = parent_dir(existing_dir) if created_dir is not None: os.makedirs(created_dir) # Running git try: cmd = 'git clone ' + repo + ' ' + dest if tag is not None: cmd += ' --branch=' + tag if run_command(cmd).returncode is not 0: raise GitException("Cannot clone " + repo) except: if created_dir is not None: shutil.rmtree(created_dir) raise
def git_checkout_date(date, hour): rev_list = run_command('git rev-list -n 1 --before="' + str(date) + ' ' + str(hour) + '" master') if rev_list.returncode is not 0: raise GitException("Cannot find last commit before " + str(date) + " " + hour) git_checkout_tag(rev_list.stdout)
def __build(self, project_dir, pb_item): csproj_files = folder_find(project_dir, includes=['.*\\.csproj']) if len(csproj_files) != 1: self.problems().add(pb_item, 'wrong .csproj count') else: res = run_command('msbuild \'{}\''.format(csproj_files[0])) if res.returncode != 0: self.problems().add(pb_item, 'build failed')
def global_git_status() -> None: git_repos = run_command( "find ~ -maxdepth 4 -name .git -type d -prune -exec dirname {} \\; | grep -v '/\\.' | sort" ) dirty_repos = 0 for git_repo in git_repos: branch_name = run_command( f"git -C {git_repo} rev-parse --abbrev-ref HEAD")[0] uncommited_files = run_command(f"git -C {git_repo} status -s") if uncommited_files != [""]: print( colorize_text( f"{git_repo} @ {branch_name} has uncommitted files", "red")) for uncommited_file in uncommited_files: print(uncommited_file.split()[1]) print() dirty_repos += 1 continue unpushed_branches = run_command( f"git -C {git_repo} branch -v | grep ahead || true") if unpushed_branches != [""]: print(colorize_text(f"{git_repo} has unpushed branches", "red")) for unpushed_branch in unpushed_branches: print(unpushed_branch.replace("* ", "").split()[0]) print() dirty_repos += 1 if dirty_repos == 0: print( colorize_text(f"Checked {len(git_repos)} repositiories.", "green")) else: print( colorize_text( f"Checked {len(git_repos)} repositiories. Found {dirty_repos} dirty repositories.", "red", )) exit(1)
def find_missing_init(root_folder: str, create: bool): # TODO use https://github.com/lk16/detect-missing-init to not replicate code between repositories os.chdir(root_folder) try: run_command("git rev-parse --show-toplevel 2>/dev/null") except CalledProcessError: print(f"This is not a git repository: {root_folder}") exit(1) # get absolute path of all non-root folders with git-tracked files folders_raw = run_command( r"git ls-files | xargs -n 1 dirname | sort | uniq | grep -v '^\.$' | xargs realpath" ) folders = [Path(folder) for folder in folders_raw] missing_init_files: List[Path] = [] for folder in folders: init_path = folder / "__init__.py" if not init_path.exists() and contains_python_file(folder): missing_init_files.append(init_path) if create: for file in missing_init_files: file.touch(mode=0o644) print(f"Created {len(missing_init_files)} missing __init__.py files.") return for file in sorted(missing_init_files): print(file.resolve()) print(f"Found {len(missing_init_files)} missing __init__.py files.") if missing_init_files: exit(1)
def _run_in_current_dir(self): run_command('acdc-camltracer trace --json {} .'.format( os.path.join(self.__dir, 'tests.py')))
def git_update(): run_command("git stash") res = run_command("git pull") if res.returncode is not 0: raise GitException("Cannot pull from " + os.getcwd())
def git_push_tags(): if run_command('git push --tags').returncode is not 0: raise GitException("Cannot push tags")
def git_tag(name): if run_command('git tag ' + name).returncode is not 0: raise GitException("Cannot add tag " + name)
def git_checkout_tag(tag): run_command("git stash") res = run_command("git checkout " + str(tag)) if res.returncode is not 0: raise GitException("Cannot checkout " + str(tag))