Merged in filnet/librepilot/LP-567_uavobjectbrowser_highlight_handling (pull request...
[librepilot.git] / make / copy_dependencies.py
blobe89ebd4ca7e3ecc0842feeeb3073a043d956e300
1 #!/usr/bin/python2
3 ###############################################################################
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.
9 ###############################################################################
11 import argparse
12 import glob
13 import os
14 import re
15 import shutil
16 import subprocess
17 import sys
19 def cygpath(file):
20 return subprocess.check_output(["cygpath", "-m", file]).strip()
22 # ldd does not work well on Windows 10 and is not supported on mingw32
23 def ldd(files):
24 ldd_output = subprocess.check_output(["ntldd", "-R"] + files)
25 # sanitize output
26 ldd_output = ldd_output.strip()
27 ldd_output = ldd_output.replace('\\', '/')
28 # split output into lines
29 ldd_lines = ldd_output.split(os.linesep)
30 # parse lines that match this format : <file name> ==> <file path> (<memory address>)
31 file_pattern = cygpath("/") + "mingw(32|64)/bin/.*"
32 print file_pattern
33 pattern = "(.*) => (" + file_pattern + ") \((.*)\)";
34 regex = re.compile(pattern)
35 dependencies = {m.group(2) for m in [regex.match(line.strip()) for line in ldd_lines] if m}
36 return dependencies
38 parser = argparse.ArgumentParser(description='Find and copy dependencies to a destination directory.')
39 parser.add_argument('-n', '--no-copy', action='store_true', help='don\'t copy dependencies to destination dir')
40 parser.add_argument('-v', '--verbose', action='store_true', help='enable verbose mode')
41 parser.add_argument('-d', '--dest', type=str, default='.', help='destination directory (defaults to current directory)')
42 parser.add_argument('-f', '--files', metavar='FILE', nargs='+', required=True, help='a file')
43 parser.add_argument('-e', '--excludes', metavar='FILE', nargs='+', help='a file')
45 args = parser.parse_args()
47 # check that args.dest exists and is a directory
48 if not os.path.isdir(args.dest):
49 print "Error: destination " + str(args.dest) + " is not a directory"
50 exit(1)
52 # find dependencies
53 dependencies = ldd(args.files)
54 print "Found " + str(len(dependencies)) + " new dependencies"
56 # no copy, exit now
57 if args.no_copy:
58 exit(0)
60 # copy dependencies to destination dir
61 copy_count = 0
62 for file in dependencies:
63 dest_file = args.dest + "/" + os.path.basename(file)
64 if args.excludes and os.path.basename(file) in args.excludes:
65 print "Ignoring " + file
66 continue
67 if not os.path.isfile(dest_file):
68 print "Copying " + file + " to " + args.dest
69 shutil.copy2(file, args.dest)
70 copy_count += 1
71 print "Copied " + str(copy_count) + " dependencies to " + str(args.dest)
73 exit(0)