Debian Codegraph @

tools/pycdlib-extract-files

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Copyright (C) 2018-2021 Chris Lalancette <[email protected]>
4
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation;
8 # version 2.1 of the License.
9
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 """
20 The main code for the pycdlib-extract-files tool, which can extract all or a
21 subset of files from an ISO.
22 """
23
24 from __future__ import print_function
25
26 import argparse
27 import collections
28 import os
29 import sys
30
31 import pycdlib
32
33
34 def parse_arguments():
35 """
36 A function to parse all of the arguments passed to the executable.
37
38 Parameters:
39 None.
40 Returns:
41 An ArgumentParser object with the parsed command-line arguments.
42 """
43 parser = argparse.ArgumentParser()
44 parser.add_argument('-path-type', help='Which path type to use for extraction', action='store', choices=['auto', 'iso', 'joliet', 'rockridge', 'udf'], default='auto')
45 parser.add_argument('-start-path', help='Path on ISO to start extraction from', action='store', default='/')
46 parser.add_argument('-extract-to', help='Path to extract ISO contents to', action='store', default='.')
47 parser.add_argument('iso', help='ISO to open', action='store')
48 return parser.parse_args()
49
50
51 def main():
52 """
53 The main function for this executable that does the work of extracting
54 files from an ISO given the parameters specified by the user.
55 """
56 args = parse_arguments()
57
58 iso = pycdlib.PyCdlib()
59 print('Opening %s' % (args.iso))
60 iso.open(args.iso)
61
62 if args.path_type == 'auto':
63 if iso.has_udf():
64 pathname = 'udf_path'
65 elif iso.has_rock_ridge():
66 pathname = 'rr_path'
67 elif iso.has_joliet():
68 pathname = 'joliet_path'
69 else:
70 pathname = 'iso_path'
71 elif args.path_type == 'rockridge':
72 if not iso.has_rock_ridge():
73 print('Can only extract Rock Ridge paths from a Rock Ridge ISO')
74 return 1
75 pathname = 'rr_path'
76 elif args.path_type == 'joliet':
77 if not iso.has_joliet():
78 print('Can only extract Joliet paths from a Joliet ISO')
79 return 2
80 pathname = 'joliet_path'
81 elif args.path_type == 'udf':
82 if not iso.has_udf():
83 print('Can only extract UDF paths from a UDF ISO')
84 return 3
85 pathname = 'udf_path'
86 else:
87 pathname = 'iso_path'
88
89 print("Using path type of '%s'" % (pathname))
90
91 root_entry = iso.get_record(**{pathname: args.start_path})
92
93 dirs = collections.deque([root_entry])
94 while dirs:
95 dir_record = dirs.popleft()
96 ident_to_here = iso.full_path_from_dirrecord(dir_record,
97 rockridge=pathname == 'rr_path')
98 relname = ident_to_here[len(args.start_path):]
99 if relname and relname[0] == '/':
100 relname = relname[1:]
101 print(relname)
102 if dir_record.is_dir():
103 if relname != '':
104 os.makedirs(os.path.join(args.extract_to, relname))
105 child_lister = iso.list_children(**{pathname: ident_to_here})
106
107 for child in child_lister:
108 if child is None or child.is_dot() or child.is_dotdot():
109 continue
110 dirs.append(child)
111 else:
112 if dir_record.is_symlink():
113 fullpath = os.path.join(args.extract_to, relname)
114 local_dir = os.path.dirname(fullpath)
115 local_link_name = os.path.basename(fullpath)
116 old_dir = os.getcwd()
117 os.chdir(local_dir)
118 os.symlink(dir_record.rock_ridge.symlink_path(), local_link_name)
119 os.chdir(old_dir)
120 else:
121 iso.get_file_from_iso(os.path.join(args.extract_to, relname), **{pathname: ident_to_here})
122
123 iso.close()
124 return 0
125
126
127 if __name__ == '__main__':
128 sys.exit(main())