|
| 1 | +#!/usr/bin/python2 |
| 2 | + |
| 3 | + ############################################################################### |
| 4 | + # |
| 5 | + # The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017. |
| 6 | + # Script to find and copy dependencies (msys2 only). |
| 7 | + # ./copy_dependencies.py -h for usage. |
| 8 | + # |
| 9 | + ############################################################################### |
| 10 | + |
| 11 | +import argparse |
| 12 | +import glob |
| 13 | +import os |
| 14 | +import re |
| 15 | +import shutil |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | + |
| 19 | +def cygpath(file): |
| 20 | + return subprocess.check_output(["cygpath", "-m", file]).strip() |
| 21 | + |
| 22 | +# ldd does not work well on Windows 10 and is not supported on mingw32 |
| 23 | +# ntldd seems to be more reliable but is not recursive (so recursion needs to be done here) |
| 24 | +def ldd(file): |
| 25 | + ldd_output = subprocess.check_output(["ntldd", file]) |
| 26 | + # sanitize output |
| 27 | + ldd_output = ldd_output.strip() |
| 28 | + ldd_output = ldd_output.replace('\\', '/') |
| 29 | + # split output into lines |
| 30 | + ldd_lines = ldd_output.split('\n') |
| 31 | + # parse lines that match this format : <file name> ==> <file path> (<memory address>) |
| 32 | + file_pattern = ".*/mingw../bin/.*" |
| 33 | + pattern = "(.*) => (" + file_pattern + ") \((.*)\)"; |
| 34 | + regex = re.compile(pattern) |
| 35 | + dependencies = {m.group(2).strip() for m in [regex.match(line) for line in ldd_lines] if m} |
| 36 | + return dependencies |
| 37 | + |
| 38 | +def find_dependencies(file, visited): |
| 39 | + print "Analyzing " + file |
| 40 | + visited.add(file) |
| 41 | + dependencies = ldd(file) |
| 42 | + # recurse |
| 43 | + for f in dependencies.copy(): |
| 44 | + if f in visited: |
| 45 | + continue |
| 46 | + if args.excludes and os.path.basename(f) in args.excludes: |
| 47 | + print "Ignoring " + f |
| 48 | + dependencies.remove(f) |
| 49 | + continue |
| 50 | + dependencies = dependencies | find_dependencies(f, visited) |
| 51 | + return dependencies |
| 52 | + |
| 53 | +parser = argparse.ArgumentParser(description='Find and copy dependencies to a destination directory.') |
| 54 | +parser.add_argument('-n', '--no-copy', action='store_true', help='don\'t copy dependencies to destination dir') |
| 55 | +parser.add_argument('-v', '--verbose', action='store_true', help='enable verbose mode') |
| 56 | +parser.add_argument('-d', '--dest', type=str, default='.', help='destination directory (defaults to current directory)') |
| 57 | +parser.add_argument('-s', '--source', type=str, default='.', help='source directory (defaults to current directory)') |
| 58 | +parser.add_argument('-f', '--files', metavar='FILE', nargs='+', required=True, help='a file') |
| 59 | +parser.add_argument('-e', '--excludes', metavar='FILE', nargs='+', help='a file') |
| 60 | + |
| 61 | +args = parser.parse_args() |
| 62 | + |
| 63 | +# check that args.dest exists and is a directory |
| 64 | +if not os.path.isdir(args.dest): |
| 65 | + print "Error: destination " + str(args.dest) + " is not a directory." |
| 66 | + exit(1) |
| 67 | + |
| 68 | +# find dependencies |
| 69 | +dependencies = set() |
| 70 | +visited = set() |
| 71 | +for file in args.files: |
| 72 | + file = os.path.join(args.source, file) |
| 73 | + files = glob.glob(file) |
| 74 | + for f in files: |
| 75 | + dependencies = dependencies | find_dependencies(f, visited) |
| 76 | +print "Found " + str(len(dependencies)) + " dependencies." |
| 77 | + |
| 78 | +# no copy, exit now |
| 79 | +if args.no_copy: |
| 80 | + exit(0) |
| 81 | + |
| 82 | +# copy dependencies to destination dir |
| 83 | +copy_count = 0 |
| 84 | +for file in dependencies: |
| 85 | + dest_file = args.dest + "/" + os.path.basename(file) |
| 86 | + if not os.path.isfile(dest_file): |
| 87 | + print "Copying " + file + " to " + args.dest |
| 88 | + shutil.copy2(file, args.dest) |
| 89 | + copy_count += 1 |
| 90 | +print "Copied " + str(copy_count) + " dependencies to '" + str(args.dest) + "'." |
| 91 | + |
| 92 | +exit(0) |
0 commit comments