[autotest] pack_audio_quality: Add git SHA-1 to packed file name

It will be helpful to see the packed file is different than the older
one.

BUG=chromium:714066
TEST=Run pack_audio_quality when there is uncommited changes in autotest
repo. Check the output file is like audio_quality_f4610bdd3_dirty.zip.
TEST=Run pack_audio_quality when there is not changes in autotest
repo. Check the output file is like audio_quality_f4610bdd3.zip.

Change-Id: I3140130fac7627cdab38b548044f3c8f8fc8edb9
Reviewed-on: https://chromium-review.googlesource.com/520827
Commit-Ready: Cheng-Yi Chiang <cychiang@chromium.org>
Tested-by: Cheng-Yi Chiang <cychiang@chromium.org>
Reviewed-by: Wai-Hong Tam <waihong@google.com>
diff --git a/client/cros/audio/pack_audio_quality.py b/client/cros/audio/pack_audio_quality.py
index 6b958a9..2b90894 100755
--- a/client/cros/audio/pack_audio_quality.py
+++ b/client/cros/audio/pack_audio_quality.py
@@ -86,6 +86,47 @@
         os.unlink(ENTRY)
 
 
+def repo_is_dirty():
+    """Checks if a repo is dirty by git diff command.
+
+    @returns: True if there are uncommitted changes. False otherwise.
+
+    """
+    try:
+        subprocess.check_call(['git', 'diff', '--quiet'])
+        subprocess.check_call(['git', 'diff', '--cached', '--quiet'])
+    except subprocess.CalledProcessError:
+        return True
+    return False
+
+
+def get_git_sha1():
+    """Returns git SHA-1 hash of HEAD.
+
+    @returns: git SHA-1 has of HEAD with minimum length 9.
+
+    """
+    return subprocess.check_output(
+            ['git', 'rev-parse', '--short', 'HEAD']).strip()
+
+
+def append_name_with_git_hash(out_file):
+    """Append the file with git SHA-1 hash.
+
+    For out_file like ABC.xyz, append the name ABC with git SHA-1 of HEAD, like
+    ABC_f4610bdd3.xyz.
+    If current repo contains uncommitted changes, it will be
+    ABC_f4610bdd3_dirty.xyz.
+
+    """
+    basename, ext = os.path.splitext(out_file)
+    basename += '_'
+    basename += get_git_sha1()
+    if repo_is_dirty():
+        basename += '_dirty'
+    return basename + ext
+
+
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(
         description='Pack audio related modules into a zip file.')
@@ -97,7 +138,7 @@
     format = '%(asctime)-15s:%(levelname)s:%(pathname)s:%(lineno)d: %(message)s'
     logging.basicConfig(format=format, level=level)
 
-    out_file = args.out
+    out_file = append_name_with_git_hash(args.out)
 
     try:
         create_link()