git » galala » commit d6ec903

Add link_to_orig subcommand, to create links to the original files

author Alberto Bertogli
2017-08-24 04:56:20 UTC
committer Alberto Bertogli
2017-08-24 04:56:20 UTC
parent a71154cfc146b4334348b3f324808a4970099e0e

Add link_to_orig subcommand, to create links to the original files

This patch adds a new link_to_orig subcommand, which creates symlinks to
the original files in the given directory.

This can be used, for example, to back up all images used in galala
albums.

galala +34 -0

diff --git a/galala b/galala
index 24d4473..01abdfd 100755
--- a/galala
+++ b/galala
@@ -191,6 +191,29 @@ def rescan_album(page, shortname):
     return True
 
 
+def _link(src, dst):
+    # create destination directory if it does not exist
+    dn = os.path.dirname(dst)
+    if not os.path.exists(dn):
+        os.makedirs(dn)
+
+    if not os.path.lexists(dst):
+        print("%s -> %s"% (src, dst))
+        os.symlink(src, dst)
+
+def link_to_orig(page, dst_dir):
+    for a in page.albums:
+        print a.shortname
+        for i in a.images:
+            # hierarchical, to dst/album/file.jpg
+            _link(i.realpath, dst_dir + "/" + i.albumpath)
+
+            # flat, to dst/_flat/album-file.jpg
+            dst_fname = "%s/_flat/%s-%s" % (dst_dir, a.shortname,
+                    os.path.basename(i.realpath))
+            _link(i.realpath, dst_fname)
+
+
 HELP = '''
 Use one of:
 
@@ -214,6 +237,10 @@ Use one of:
 
     galala generate FILENAME OUT_PATH
         Generates static HTML for the page given in FILENAME to OUT_PATH.
+
+    galala link_to_orig FILENAME OUT_PATH
+        Create symlinks to the original images on the page given in FILENAME
+        in OUT_PATH.
 '''
 
 
@@ -250,6 +277,13 @@ if __name__ == '__main__':
 
         if success:
             common.page_save(page, sys.argv[2])
+    elif cmd == "link_to_orig":
+        if len(sys.argv) != 4:
+            print HELP
+            sys.exit(1)
+        page = common.page_load(sys.argv[2])
+        dst_dir = sys.argv[3]
+        link_to_orig(page, dst_dir)
     else:
         print "ERROR: unknown command"
         sys.exit(1)