]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpaperview.py
remerge he.po
[lyx.git] / lib / scripts / lyxpaperview.py
1 #! /usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # file lyxpaperview.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Jürgen Spitzmüller
9 # This draws on a bash script and conceptual idea by Pavel Sanda
10 # Full author contact details are available in file CREDITS
11
12 # This script searches the home directory for a PDF or PS
13 # file with a name containing specific keywords (year and author by default).
14 # If found, it returns the path(s), separated by \n.
15
16 import os, sys, subprocess
17
18 def message(message):
19     sys.stderr.write("lyxpaperview: %s\n" % message)
20
21 def error(message):
22     sys.stderr.write("lyxpaperview error: %s\n" % message)
23     exit(1)
24
25 def usage(prog_name):
26     msg = "Usage: %s titletoken-1 [titletoken-2] ... [titletoken-n]\n" \
27           "    Each title token must occur in the filename (at an arbitrary position).\n" \
28           "    You might use quotes to enter multi-word tokens"
29     return  msg % prog_name
30
31 # Copied from lyxpreview_tools.py
32 # PATH and PATHEXT environment variables
33 path = os.environ["PATH"].split(os.pathsep)
34 extlist = ['']
35 if "PATHEXT" in os.environ:
36     extlist += os.environ["PATHEXT"].split(os.pathsep)
37 extlist.append('.py')
38
39 def find_exe(candidates):
40     global extlist, path
41
42     for command in candidates:
43         prog = command.split()[0]
44         for directory in path:
45             for ext in extlist:
46                 full_path = os.path.join(directory, prog + ext)
47                 if os.access(full_path, os.X_OK):
48                     # The thing is in the PATH already (or we wouldn't
49                     # have found it). Return just the basename to avoid
50                     # problems when the path to the executable contains
51                     # spaces.
52                     if full_path.lower().endswith('.py'):
53                         return command.replace(prog, '"%s" "%s"'
54                             % (sys.executable, full_path))
55                     return command
56
57     return None
58
59
60 def find(args, path):
61     if os.name != 'nt':
62         # use locate if possible (faster)
63         if find_exe(['locate']):
64             p1 = subprocess.Popen(['locate', '-i', args[0].lower()], stdout=subprocess.PIPE)
65             px = subprocess.Popen(['grep', '-Ei', r'\.pdf$|\.ps$'], stdin=p1.stdout, stdout=subprocess.PIPE)
66             for arg in args:
67                if arg == args[0]:
68                    # have this already
69                    continue
70                px = subprocess.Popen(['grep', '-i', arg], stdin=px.stdout, stdout=subprocess.PIPE)
71             p1.stdout.close()
72             output = px.communicate()
73             return output[0].decode("utf8").strip('\n')
74      # FIXME add something for windows as well?
75      # Maybe dir /s /b %WINDIR%\*author* | findstr .*year.*\."ps pdf"
76
77     for root, dirs, files in os.walk(path):
78         for fname in files:
79             lfname = fname.lower()
80             if lfname.endswith(('.pdf', '.ps')):
81                 caught = True
82                 for arg in args:
83                     if lfname.find(arg.lower()) == -1:
84                         caught = False
85                         break
86                 if caught:
87                     return os.path.join(root, fname)
88     return ""
89
90 def main(argv):
91     progname = argv[0]
92     
93     args = sys.argv[1:]
94     
95     if len(args) < 1:
96       error(usage(progname))
97
98     result = find(args, path = os.environ["HOME"])
99      
100     print(result)
101     exit(0)
102
103 if __name__ == "__main__":
104     main(sys.argv)