Add exist_ok and use git rev-parse to process revisions

Signed-off-by: Xiaofei Bai <xiaofei.bai@arm.com>
This commit is contained in:
Xiaofei Bai 2021-10-26 09:23:42 +00:00
parent 2400b50250
commit 184e8b6a36

View file

@ -41,12 +41,10 @@ class CodeSizeComparison:
""" """
self.repo_path = "." self.repo_path = "."
self.result_dir = os.path.abspath(result_dir) self.result_dir = os.path.abspath(result_dir)
if os.path.exists(self.result_dir) is False: os.makedirs(self.result_dir, exist_ok=True)
os.makedirs(self.result_dir)
self.csv_dir = os.path.abspath("code_size_records/") self.csv_dir = os.path.abspath("code_size_records/")
if os.path.exists(self.csv_dir) is False: os.makedirs(self.csv_dir, exist_ok=True)
os.makedirs(self.csv_dir)
self.old_rev = old_revision self.old_rev = old_revision
self.new_rev = new_revision self.new_rev = new_revision
@ -60,20 +58,20 @@ class CodeSizeComparison:
@staticmethod @staticmethod
def validate_revision(revision): def validate_revision(revision):
result = subprocess.run(["git", "cat-file", "-e", revision], check=False) result = subprocess.run(["git", "rev-parse", "--verify", revision],
return result.returncode check=False, stdout=subprocess.PIPE)
return result
def _create_git_worktree(self, revision): def _create_git_worktree(self, revision):
"""Make a separate worktree for revision. """Make a separate worktree for revision.
Do not modify the current worktree.""" Do not modify the current worktree."""
if revision == "HEAD": if revision == "current":
print("Using current work directory.") print("Using current work directory.")
git_worktree_path = self.repo_path git_worktree_path = self.repo_path
else: else:
print("Creating git worktree for", revision) print("Creating git worktree for", revision)
rev_dirname = revision.replace("/", "_") git_worktree_path = os.path.join(self.repo_path, "temp-" + revision)
git_worktree_path = os.path.join(self.repo_path, "temp-" + rev_dirname)
subprocess.check_output( subprocess.check_output(
[self.git_command, "worktree", "add", "--detach", [self.git_command, "worktree", "add", "--detach",
git_worktree_path, revision], cwd=self.repo_path, git_worktree_path, revision], cwd=self.repo_path,
@ -93,8 +91,11 @@ class CodeSizeComparison:
def _gen_code_size_csv(self, revision, git_worktree_path): def _gen_code_size_csv(self, revision, git_worktree_path):
"""Generate code size csv file.""" """Generate code size csv file."""
csv_fname = revision.replace("/", "_") + ".csv" csv_fname = revision + ".csv"
print("Measuring code size for", revision) if revision == "current":
print("Measuring code size in current work directory.")
else:
print("Measuring code size for", revision)
result = subprocess.check_output( result = subprocess.check_output(
["size library/*.o"], cwd=git_worktree_path, shell=True ["size library/*.o"], cwd=git_worktree_path, shell=True
) )
@ -118,8 +119,8 @@ class CodeSizeComparison:
"""Generate code size csv file for the specified git revision.""" """Generate code size csv file for the specified git revision."""
# Check if the corresponding record exists # Check if the corresponding record exists
csv_fname = revision.replace("/", "_") + ".csv" csv_fname = revision + ".csv"
if (revision != "HEAD") and \ if (revision != "current") and \
os.path.exists(os.path.join(self.csv_dir, csv_fname)): os.path.exists(os.path.join(self.csv_dir, csv_fname)):
print("Code size csv file for", revision, "already exists.") print("Code size csv file for", revision, "already exists.")
else: else:
@ -133,13 +134,11 @@ class CodeSizeComparison:
old and new. Measured code size results of these two revisions old and new. Measured code size results of these two revisions
must be available.""" must be available."""
old_file = open(os.path.join(self.csv_dir, \ old_file = open(os.path.join(self.csv_dir, self.old_rev + ".csv"), "r")
self.old_rev.replace("/", "_") + ".csv"), "r") new_file = open(os.path.join(self.csv_dir, self.new_rev + ".csv"), "r")
new_file = open(os.path.join(self.csv_dir, \ res_file = open(os.path.join(self.result_dir, "compare-" + self.old_rev
self.new_rev.replace("/", "_") + ".csv"), "r") + "-" + self.new_rev + ".csv"), "w")
res_file = open(os.path.join(self.result_dir, \
"compare-" + self.old_rev.replace("/", "_") + "-" \
+ self.new_rev.replace("/", "_") + ".csv"), "w")
res_file.write("file_name, this_size, old_size, change, change %\n") res_file.write("file_name, this_size, old_size, change, change %\n")
print("Generating comparision results.") print("Generating comparision results.")
@ -194,12 +193,13 @@ def main():
default is comparison", default is comparison",
) )
parser.add_argument( parser.add_argument(
"-o", "--old-rev", type=str, help="old revision for comparison.(prefer commit ID)", "-o", "--old-rev", type=str, help="old revision for comparison.",
required=True, required=True,
) )
parser.add_argument( parser.add_argument(
"-n", "--new-rev", type=str, default="HEAD", "-n", "--new-rev", type=str, default=None,
help="new revision for comparison, default is current work directory." help="new revision for comparison, default is the current work \
directory, including uncommited changes."
) )
comp_args = parser.parse_args() comp_args = parser.parse_args()
@ -207,15 +207,18 @@ def main():
print("Error: {} is not a directory".format(comp_args.result_dir)) print("Error: {} is not a directory".format(comp_args.result_dir))
parser.exit() parser.exit()
validate_result = CodeSizeComparison.validate_revision(comp_args.old_rev) validate_res = CodeSizeComparison.validate_revision(comp_args.old_rev)
if validate_result != 0: if validate_res.returncode != 0:
sys.exit(validate_result) sys.exit(validate_res.returncode)
old_revision = comp_args.old_rev old_revision = validate_res.stdout.decode().replace("\n", "")
validate_result = CodeSizeComparison.validate_revision(comp_args.new_rev) if comp_args.new_rev is not None:
if validate_result != 0: validate_res = CodeSizeComparison.validate_revision(comp_args.new_rev)
sys.exit(validate_result) if validate_res.returncode != 0:
new_revision = comp_args.new_rev sys.exit(validate_res.returncode)
new_revision = validate_res.stdout.decode().replace("\n", "")
else:
new_revision = "current"
result_dir = comp_args.result_dir result_dir = comp_args.result_dir
size_compare = CodeSizeComparison(old_revision, new_revision, result_dir) size_compare = CodeSizeComparison(old_revision, new_revision, result_dir)