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 ###############################################################################
20 return subprocess
.check_output(["cygpath", "-m", file]).strip()
22 # ldd does not work well on Windows 10 and is not supported on mingw32
24 ldd_output
= subprocess
.check_output(["ntldd", "-R"] + files
)
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/.*"
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
}
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"
53 dependencies
= ldd(args
.files
)
54 print "Found " + str(len(dependencies
)) + " new dependencies"
60 # copy dependencies to destination dir
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
67 if not os
.path
.isfile(dest_file
):
68 print "Copying " + file + " to " + args
.dest
69 shutil
.copy2(file, args
.dest
)
71 print "Copied " + str(copy_count
) + " dependencies to " + str(args
.dest
)