Fix potential symlink loop in _compress_text() 74/63474/1
authorEric Ball <eball@linuxfoundation.org>
Fri, 20 Mar 2020 23:26:47 +0000 (16:26 -0700)
committerEric Ball <eball@linuxfoundation.org>
Fri, 20 Mar 2020 23:26:47 +0000 (16:26 -0700)
By checking if os.path.exists can find the path, we stop the method
from attempting to traverse symlinked directories.
This also pins a more recent version of pre-commit, as the older
version was making a pre-commit file with errors.

Issue: RELENG-2576
Change-Id: I2265c9895444892877f684b807287ebe8319083a
Signed-off-by: Eric Ball <eball@linuxfoundation.org>
lftools/deploy.py
requirements.txt

index 70b9102..fa6bd36 100755 (executable)
@@ -50,9 +50,14 @@ def _compress_text(dir):
         paths.extend(glob.glob(search, recursive=True))
 
     for _file in paths:
-        with open(_file, "rb") as src, gzip.open("{}.gz".format(_file), "wb") as dest:
-            shutil.copyfileobj(src, dest)
-            os.remove(_file)
+        # glob may follow symlink paths that open can't find
+        if os.path.exists(_file):
+            log.debug("Compressing file {}".format(_file))
+            with open(_file, "rb") as src, gzip.open("{}.gz".format(_file), "wb") as dest:
+                shutil.copyfileobj(src, dest)
+                os.remove(_file)
+        else:
+            log.info("Could not open path from glob {}".format(_file))
 
     os.chdir(save_dir)
 
index 8617a30..b1343a6 100644 (file)
@@ -23,7 +23,7 @@ multi-key-dict==2.0.3
 nodeenv==1.3.5
 oauth2client==4.1.3
 pbr==5.4.4
-pre-commit==1.21.0
+pre-commit~=2.2.0
 pyasn1==0.4.8
 pyasn1-modules==0.2.8
 pygerrit2==2.0.10