]> git.lyx.org Git - lyx.git/blob - development/tools/lyxpak.py
cmake: also install locale into the resource dir on windows
[lyx.git] / development / tools / lyxpak.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file lyxpak.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Enrico Forestieri
9
10 # Full author contact details are available in file CREDITS
11
12 # This script creates a tar or zip archive with a lyx file and all included
13 # files (graphics and so on). A zip archive is created only if tar is not
14 # found in the path. The tar archive is then compressed with gzip or bzip2.
15
16 import os, re, string, sys
17 from sets import Set
18
19 # Replace with the actual path to the 1.5.x or 1.6.x lyx2lyx.
20 # If left undefined and the LyX executable is in the path, the script will
21 # try to locate lyx2lyx by querying LyX about the system dir.
22 # Example for *nix:
23 # lyx2lyx = /usr/share/lyx/lyx2lyx/lyx2lyx
24 lyx2lyx = None
25
26 # Pre-compiled regular expressions.
27 re_lyxfile = re.compile("\.lyx$")
28 re_input = re.compile(r'^(.*)\\(input|include){(\s*)(\S+)(\s*)}.*$')
29 re_package = re.compile(r'^(.*)\\(usepackage){(\s*)(\S+)(\s*)}.*$')
30 re_class = re.compile(r'^(\\)(textclass)(\s+)(\S+)$')
31 re_norecur = re.compile(r'^(.*)\\(verbatiminput|lstinputlisting|includegraphics\[*.*\]*){(\s*)(\S+)(\s*)}.*$')
32 re_filename = re.compile(r'^(\s*)(filename)(\s+)(\S+)$')
33 re_options = re.compile(r'^(\s*)options(\s+)(\S+)$')
34 re_bibfiles = re.compile(r'^(\s*)bibfiles(\s+)(\S+)$')
35
36
37 def usage(prog_name):
38     return "Usage: %s file.lyx [output_dir]\n" % prog_name
39
40
41 def error(message):
42     sys.stderr.write(message + '\n')
43     sys.exit(1)
44
45
46 def run_cmd(cmd):
47     handle = os.popen(cmd, 'r')
48     cmd_stdout = handle.read()
49     cmd_status = handle.close()
50     return cmd_status, cmd_stdout
51
52
53 def find_exe(candidates, extlist, path):
54     for prog in candidates:
55         for directory in path:
56             for ext in extlist:
57                 full_path = os.path.join(directory, prog + ext)
58                 if os.access(full_path, os.X_OK):
59                     return prog, full_path
60     return None, None
61
62
63 def abspath(name):
64     " Resolve symlinks and returns the absolute normalized name."
65     newname = os.path.normpath(os.path.abspath(name))
66     if os.name != 'nt':
67         newname = os.path.realpath(newname)
68     return newname
69
70
71 def gather_files(curfile, incfiles):
72     " Recursively gather files."
73     curdir = os.path.dirname(abspath(curfile))
74     is_lyxfile = re_lyxfile.search(curfile)
75     if is_lyxfile:
76         lyx2lyx_cmd = 'python "%s" "%s"' % (lyx2lyx, curfile)
77         l2l_status, l2l_stdout = run_cmd(lyx2lyx_cmd)
78         if l2l_status != None:
79             error('%s failed to convert "%s"' % (lyx2lyx, curfile))
80         lines = l2l_stdout.splitlines()
81     else:
82         input = open(curfile, 'rU')
83         lines = input.readlines()
84         input.close()
85
86     i = 0
87     while i < len(lines):
88         # Gather used files.
89         recursive = True
90         extlist = ['']
91         match = re_filename.match(lines[i])
92         if not match:
93             match = re_input.match(lines[i])
94             if not match:
95                 match = re_package.match(lines[i])
96                 extlist = ['.sty']
97                 if not match:
98                     match = re_class.match(lines[i])
99                     extlist = ['.cls']
100                     if not match:
101                         match = re_norecur.match(lines[i])
102                         extlist = ['', '.eps', '.pdf', '.png', '.jpg']
103                         recursive = False
104         if match:
105             file = match.group(4).strip('"')
106             if not os.path.isabs(file):
107                 file = os.path.join(curdir, file)
108             file_exists = False
109             for ext in extlist:
110                 if os.path.exists(file + ext):
111                     file = file + ext
112                     file_exists = True
113                     break
114             if file_exists:
115                 incfiles.append(abspath(file))
116                 if recursive:
117                     gather_files(file, incfiles)
118             i += 1
119             continue
120
121         if not is_lyxfile:
122             i += 1
123             continue
124
125         # Gather bibtex *.bst files.
126         match = re_options.match(lines[i])
127         if match:
128             file = match.group(3).strip('"')
129             if not os.path.isabs(file):
130                 file = os.path.join(curdir, file + '.bst')
131             if os.path.exists(file):
132                 incfiles.append(abspath(file))
133             i += 1
134             continue
135
136         # Gather bibtex *.bib files.
137         match = re_bibfiles.match(lines[i])
138         if match:
139             bibfiles = match.group(3).strip('"').split(',')
140             j = 0
141             while j < len(bibfiles):
142                 if os.path.isabs(bibfiles[j]):
143                     file = bibfiles[j]
144                 else:
145                     file = os.path.join(curdir, bibfiles[j] + '.bib')
146                 if os.path.exists(file):
147                     incfiles.append(abspath(file))
148                 j += 1
149             i += 1
150             continue
151
152         i += 1
153
154     return 0
155
156
157 def main(argv):
158
159     if len(argv) >= 2 and len(argv) <= 3:
160         lyxfile = argv[1]
161         if not os.path.exists(lyxfile):
162             error('File "%s" not found.' % lyxfile)
163
164         # Check that it actually is a LyX document
165         input = open(lyxfile, 'rU')
166         line = input.readline()
167         input.close()
168         if not (line and line.startswith('#LyX')):
169             error('File "%s" is not a LyX document.' % lyxfile)
170
171         # Either tar or zip must be available
172         extlist = ['']
173         if os.environ.has_key("PATHEXT"):
174             extlist = extlist + os.environ["PATHEXT"].split(os.pathsep)
175         path = string.split(os.environ["PATH"], os.pathsep)
176         archiver, full_path = find_exe(["tar", "zip"], extlist, path)
177
178         if archiver == "tar":
179             ar_cmd = "tar cf"
180             ar_name = re_lyxfile.sub(".tar", abspath(lyxfile))
181             # Archive will be compressed if either gzip or bzip2 are available
182             compress, full_path = find_exe(["gzip", "bzip2"], extlist, path)
183             if compress == "gzip":
184                 ext = ".gz"
185             elif compress == "bzip2":
186                 ext = ".bz2"
187         elif archiver == "zip":
188             ar_cmd = "zip"
189             ar_name = re_lyxfile.sub(".zip", abspath(lyxfile))
190             compress = None
191         else:
192             error("Unable to find neither tar nor zip.")
193
194         if len(argv) == 3:
195             outdir = argv[2]
196             if not os.path.isdir(outdir):
197                 error('Error: "%s" is not a directory.' % outdir)
198             ar_name = os.path.join(abspath(outdir), os.path.basename(ar_name))
199     else:
200         error(usage(argv[0]))
201
202     # Try to find the location of the lyx2lyx script
203     global lyx2lyx
204     if lyx2lyx == None:
205         lyx_exe, full_path = find_exe(["lyxc", "lyx"], extlist, path)
206         if lyx_exe == None:
207             error('Cannot find the LyX executable in the path.')
208         else:
209             cmd_status, cmd_stdout = run_cmd("%s -version 2>&1" % lyx_exe)
210             if cmd_status != None:
211                 error('Cannot query LyX about the lyx2lyx script.')
212             re_msvc = re.compile(r'^(\s*)(Host type:)(\s+)(win32)$')
213             re_sysdir = re.compile(r'^(\s*)(LyX files dir:)(\s+)(\S+)$')
214             lines = cmd_stdout.splitlines()
215             for line in lines:
216                 match = re_msvc.match(line)
217                 if match:
218                     # The LyX executable was built with MSVC, so the
219                     # "LyX files dir:" line is unusable
220                     basedir = os.path.dirname(os.path.dirname(full_path))
221                     lyx2lyx = os.path.join(basedir, 'Resources', 'lyx2lyx', 'lyx2lyx')
222                     break
223                 match = re_sysdir.match(line)
224                 if match:
225                     lyx2lyx = os.path.join(match.group(4), 'lyx2lyx', 'lyx2lyx')
226                     break
227             if not os.access(lyx2lyx, os.X_OK):
228                 error('Unable to find the lyx2lyx script.')
229
230     # Initialize the list with the specified LyX file and recursively
231     # gather all required files (also from child documents).
232     incfiles = [abspath(lyxfile)]
233     gather_files(lyxfile, incfiles)
234
235     # Find the topmost dir common to all files
236     if len(incfiles) > 1:
237         topdir = os.path.commonprefix(incfiles)
238     else:
239         topdir = os.path.dirname(incfiles[0]) + os.path.sep
240
241     # Remove the prefix common to all paths in the list
242     i = 0
243     while i < len(incfiles):
244         incfiles[i] = string.replace(incfiles[i], topdir, '')
245         i += 1
246
247     # Remove duplicates and sort the list
248     incfiles = list(Set(incfiles))
249     incfiles.sort()
250
251     # Build the archive command
252     ar_cmd = '%s "%s"' % (ar_cmd, ar_name)
253     for file in incfiles:
254         print file
255         ar_cmd = ar_cmd + ' "' + file + '"'
256
257     # Create the archive
258     if topdir != '':
259         os.chdir(topdir)
260     cmd_status, cmd_stdout = run_cmd(ar_cmd)
261     if cmd_status != None:
262         error('Failed to create LyX archive "%s"' % ar_name)
263
264     # If possible, compress the archive
265     if compress != None:
266         compress_cmd = '%s "%s"' % (compress, ar_name)
267         cmd_status, cmd_stdout = run_cmd(compress_cmd)
268         if cmd_status != None:
269             error('Failed to compress LyX archive "%s"' % ar_name)
270         ar_name = ar_name + ext
271
272     print 'LyX archive "%s" created successfully.' % ar_name
273     return 0
274
275
276 if __name__ == "__main__":
277     main(sys.argv)