-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymlink_picture_list.py
executable file
·153 lines (127 loc) · 5.66 KB
/
symlink_picture_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
""" symlink_picture_list.py
Creates symlinks to pictures out of a list in a file"""
# The MIT License (MIT)
#
# Copyright (c) 2017 Georg Lutz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Standard library imports:
import argparse
import logging
import os
import sys
def get_args():
'''Configures command line parser and returns parsed parameters'''
parser = argparse.ArgumentParser(
description="Creates symlinks to pictures out of a list in a file")
parser.add_argument(
"-v", "--verbose", dest="debuglevel", action="store_const",
const=logging.DEBUG, default=logging.INFO,
help="Enables verbose/debug output.")
parser.add_argument(
"indir",
help="directory structure with filelist.txt files, one path name per line")
parser.add_argument(
"outdir",
help="folder name for writing the symlinks")
return parser.parse_args()
def read_list(filepath):
'''Reads the list from filepath and returns an array with filenames'''
result = []
file_ = open(filepath, "r")
for line in file_:
if len(line) <= 1 or line.startswith("#"): # Skip empty and comment lines
pass
else:
result.append(line.strip())
return result
def check_and_create_symlink(source, symlink_file):
'''Checks and creates a symlink if possible
Arguments:
source: where the symlink points to
symlink_file: the filesystem entry representing the symlink
Calls sys.exit(1) in case that a symlink exists, but points to another source.
'''
if os.path.lexists(symlink_file):
if os.path.islink(symlink_file) and os.readlink(symlink_file) == source:
logging.debug("Symlink %s to %s already exists. Skip", symlink_file, source)
else:
logging.error("%s exists, but does not point to %s", symlink_file, source)
sys.exit(1)
else:
logging.info("Symlink " + source + " to " + symlink_file)
os.symlink(source, symlink_file)
def symlink_files(filelist, outdir):
'''Symlinks the files in list to outdir.
All filenames are pretended by 000_, 001_, 002_ etc.'''
counter = 0
for entry in filelist:
old_filename = os.path.basename(entry)
new_filename = "%03d_%s" % (counter, old_filename)
new_pathname = os.path.join(outdir, new_filename)
check_and_create_symlink(entry, new_pathname)
counter = counter + 1
def get_dest_path(root, dir_entry, indir, outdir):
'''Helper function for generate_folder_structure, returns file/folder path in outdir'''
full_path = os.path.join(root, dir_entry)
rel_path = os.path.relpath(full_path, indir)
dest_path = os.path.join(outdir, rel_path)
return dest_path
def generate_folder_structure(indir, outdir):
'''Iterates over folder structure in indir and generates folder structure in outdir
* any subfolder in indir will be also created in outdir
* filelist.txt files are parsed and the containing file names are symlinked
* any other file will be symlinked to indir
'''
for root, dirs, files in os.walk(indir):
for file_ in files:
full_path = os.path.join(root, file_)
if file_ == "filelist.txt":
# Read content of filelist.txt and create symlinks for all the files
logging.debug("found filelist at %s", full_path)
filelist = read_list(full_path)
symlink_files(filelist, get_dest_path(root, "", indir, outdir))
else:
# Symlink single file
logging.debug("found other file %s", full_path)
dest_path = get_dest_path(root, file_, indir, outdir)
check_and_create_symlink(os.path.abspath(full_path), dest_path)
for dir_ in dirs:
full_path = os.path.join(root, dir_)
logging.debug("found dir %s", full_path)
dest_path = get_dest_path(root, dir_, indir, outdir)
if not os.path.exists(dest_path):
logging.info(" Create dir %s", dest_path)
os.mkdir(dest_path)
else:
logging.debug(" Dir %s already exists. Skip", dest_path)
def main():
'''main function, called when script file is executed directly'''
args = get_args()
logging.basicConfig(format="%(message)s", level=args.debuglevel)
if not os.path.isdir(args.indir):
logging.error("Given argument is not a directory: %s. Exit.", args.indir)
sys.exit(1)
if not os.path.isdir(args.outdir):
os.mkdir(args.outdir)
generate_folder_structure(args.indir, args.outdir)
if __name__ == "__main__":
main()