File: ext_copy.py

package info (click to toggle)
lyx 2.5.0~RC2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 138,212 kB
  • sloc: cpp: 244,227; ansic: 106,398; xml: 72,791; python: 39,384; sh: 7,666; makefile: 6,586; pascal: 2,143; perl: 2,101; objc: 1,084; tcl: 163; sed: 16
file content (108 lines) | stat: -rw-r--r-- 3,548 bytes parent folder | download
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
# file ext_copy.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.

# author Richard Kimberly Heck, Alex Fernandez, Uwe Stöhr

# Full author contact details are available in file CREDITS

# Usage:
# ext_copy.py [-d] [-e ext1,ext2,...] [-t target_ext] from_file to_file

# This script is to be used as a "copier" script in the sense needed by
# the converter definitions. Given a from_file and to_file, it will copy
# all files in the directory in which from_file is found that have the
# extensions given in the -e argument, or all files in that directory if no
# such argument is given. So, for example, we can do:
#   python ext_copy.py -e png,html,css /path/from/file.html /path/to/file.html
# and all html, png, and css files in /path/from/ will be copied to the
# (possibly new) directory /path/to/file.html.LyXconv/.
# The -t argument determines the extension added, the default being "LyXconv".
# If just . is given, no extension is added.
# If the -d option is given then the document directory /path/to/ will be used
# to copy the files no subdirectory will be created/used.

import getopt, os, shutil, sys
from lyxpreview_tools import error


def usage(prog_name):
    msg = "Usage: %s [-d] [-e extensions] [-t target extension] from_file to_file"
    return msg % prog_name


def main(argv):
    progname = argv[0]

    exts = [] #list of extensions for which we're checking
    use_doc_dir = False
    targext = "LyXconv" #extension for target directory
    opts, args = getopt.getopt(sys.argv[1:], "de:t:")
    for o, v in opts:
      if o == "-e":
        exts = v.split(',')
      if o == "-t":
        targext = v
      if o == "-d":
        use_doc_dir = True

    # input directory
    if len(args) != 2:
      error(usage(progname))
    abs_from_file = args[0]
    if not os.path.isabs(abs_from_file):
      error(f"{abs_from_file} is not an absolute file name.\n{usage(progname)}")
    from_dir = os.path.dirname(abs_from_file)

    # output directory
    if use_doc_dir:
      to_dir = os.path.dirname(args[1])
    else:
      to_dir = args[1]
      if targext != '.':
        to_dir += "." + targext
      if not os.path.isabs(to_dir):
        error(f"{to_dir} is not an absolute file name.\n{usage(progname)}")

    if not copy_all(from_dir, to_dir, exts):
      # some kind of failure
      return 1
    return 0


def copy_all(from_dir, to_dir, exts):
    "Copy all matching files in from_dir to to_dir"
    for file in os.listdir(from_dir):
      if os.path.isdir(os.path.join(from_dir, file)):
        copy_all(os.path.join(from_dir, file), os.path.join(to_dir, file), exts)
        continue
      junk, ext = os.path.splitext(os.path.basename(file))
      ext = ext.lower()[1:] #strip the leading dot
      # only create a directory and copy files when either
      # exts is empty or when ext is in the exts list
      if (exts) and (ext not in exts):
        continue
      if not create_dir(to_dir):
        return False
      from_file = os.path.join(from_dir, file)
      to_file  = os.path.join(to_dir, file)
      shutil.copyfile(from_file, to_file)
      try:
        shutil.copymode(from_file, to_file)
      except:
        pass
    return True


def create_dir(new_dir):
    "Try to create the output directory if it doesn't exist"
    if not os.path.isdir(new_dir):
      try:
        os.makedirs(new_dir)
      except:
        error("Unable to create %s" % new_dir)
        return False
    return True

if __name__ == "__main__":
    main(sys.argv)