]> git.draconx.ca Git - scripts.git/commitdiff
caa-fetcher: Create downloaded files with correct permissions.
authorNick Bowler <nbowler@draconx.ca>
Wed, 8 May 2019 01:26:29 +0000 (21:26 -0400)
committerNick Bowler <nbowler@draconx.ca>
Wed, 8 May 2019 01:34:43 +0000 (21:34 -0400)
python's NamedTemporaryFile hardcodes a mode of 0600, which is wrong
for what we are trying to accomplish.  Downloaded files should be
created with default permissions (respecting umask, acls, etc.)
It's a real pain to fix up permissions after the fact so we'll
have to cook up our own version instead...

caa-fetcher.py

index 5c23107d6c72cb0580735116e55c821d724abd36..7f90f2c34b4fc61aa1935627c2057e7712152d58 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #
-# Copyright © 2018 Nick Bowler
+# Copyright © 2018-2019 Nick Bowler
 #
 # Tool to fetch album art for a release from the Cover Art Archive.
 #
@@ -15,20 +15,35 @@ import tempfile
 import sys
 import os
 
+umask = os.umask(0)
+os.umask(umask)
+
 progname = "caa-fetcher"
-version = "0"
+version = "1"
 musicbrainzngs.set_useragent(progname, version)
 
 parser = argparse.ArgumentParser(
     description='Download album artwork from the Cover Art Archive'
 )
 
+# NamedTemporaryFile workalike which allows control of the file mode...
+def open_tmp(prefix="tmp", suffix="", dir=".", mode=0o600):
+    names = tempfile._get_candidate_names()
+    for seq in range(100):
+        name = os.path.join(dir, "%s%s%s" % (prefix, next(names), suffix))
+        try:
+            f = open(name, "x+", mode)
+        except FileExistsError:
+            continue
+        return tempfile._TemporaryFileWrapper(f, name)
+    return None
+
 class VersionAction(argparse.Action):
     def __init__(self, **kw):
         super().__init__(nargs=0, help="show a version message and exit", **kw)
     def __call__(self, parser, namespace, values, option_string=None):
         print("%s %s" % (progname, version))
-        print('''Copyright © 2018 Nick Bowler
+        print('''Copyright © 2019 Nick Bowler
 License WTFPL2: Do What The Fuck You Want To Public License, version 2.
 This is free software: you are free to do what the fuck you want to.
 There is NO WARRANTY, to the extent permitted by law.''')
@@ -64,10 +79,11 @@ for c in covers["images"]:
         print("%s already exists, skipping..." % outname)
         continue
 
-    outfile = tempfile.NamedTemporaryFile(suffix=ext, dir=".")
+    outfile = open_tmp(suffix=ext, dir=".", mode=0o666)
     rc = os.spawnlp(os.P_WAIT, 'wget', 'wget', '-O', outfile.name, c["image"])
     if rc:
         failed = True
+
     os.link(outfile.name, outname)
     outfile.close()