OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import glob | 6 import glob |
7 import os | 7 import os |
8 import shutil | 8 import shutil |
9 import stat | 9 import stat |
10 import sys | 10 import sys |
11 | 11 |
12 def usage(): | 12 def usage(): |
13 print("""Usage: squashdir.py <dest-dir> <source-dir> ... | 13 print("""Usage: squashdir.py <dest-dir> <source-dir> ... |
14 | 14 |
15 Basic tool to copy a directory heirarchy into a flat space. | 15 Basic tool to copy a directory heirarchy into a flat space. |
16 | 16 |
17 This crawls an arbitrarily deep heirarchy of files and directories, and copies | 17 This crawls an arbitrarily deep heirarchy of files and directories, and copies |
18 each file into the destination directory. The destination file name will | 18 each file into the destination directory. The destination file name will |
19 include the relative path to the source file, with '^^' inserted between each | 19 include the relative path to the source file, with '^^' inserted between each |
20 directory name. | 20 directory name. |
21 | 21 |
22 The resulting directory can then be imported into the file manager test harness, | 22 The resulting directory can then be imported into the file manager test harness, |
23 which will reconstitute the directory structure. | 23 which will reconstitute the directory structure. |
24 | 24 |
25 This is used to work around the fact that the FileList and File objects | 25 This is used to work around the fact that the FileList and File objects |
26 presented by <input type=file multiple> do not allow users to recurse a | 26 presented by <input type=file multiple> do not allow users to recurse a |
27 selected directory, nor do they provide information about directory structure. | 27 selected directory, nor do they provide information about directory structure. |
28 """) | 28 """) |
29 | 29 |
| 30 |
30 def status(msg): | 31 def status(msg): |
31 sys.stderr.write(msg + '\n') | 32 sys.stderr.write(msg + '\n') |
32 | 33 |
| 34 |
33 def scan_path(dest, src, path): | 35 def scan_path(dest, src, path): |
34 abs_src = os.path.join(src, path) | 36 abs_src = os.path.join(src, path) |
35 statinfo = os.stat(abs_src) | 37 statinfo = os.stat(abs_src) |
36 | 38 |
37 basename = os.path.basename(path) | 39 basename = os.path.basename(path) |
38 | 40 |
39 if not stat.S_ISDIR(statinfo.st_mode): | 41 if not stat.S_ISDIR(statinfo.st_mode): |
40 newname = os.path.join(dest, path.replace('/', '^^')) | 42 newname = os.path.join(dest, path.replace('/', '^^')) |
41 status(newname) | 43 status(newname) |
42 shutil.copyfile(abs_src, newname) | 44 shutil.copyfile(abs_src, newname) |
43 else: | 45 else: |
44 for child_path in glob.glob(abs_src + '/*'): | 46 for child_path in glob.glob(abs_src + '/*'): |
45 scan_path(dest, src, child_path[len(src) + 1:]) | 47 scan_path(dest, src, child_path[len(src) + 1:]) |
46 | 48 |
47 if __name__ == '__main__': | 49 |
| 50 def main(): |
48 if len(sys.argv) < 3 or sys.argv[1][0] == '-': | 51 if len(sys.argv) < 3 or sys.argv[1][0] == '-': |
49 usage() | 52 usage() |
50 return | 53 return 1 |
51 | 54 |
52 dest = sys.argv[1] | 55 dest = sys.argv[1] |
53 for src in sys.argv[2:]: | 56 for src in sys.argv[2:]: |
54 abs_src = os.path.abspath(src) | 57 abs_src = os.path.abspath(src) |
55 path = os.path.basename(abs_src) | 58 path = os.path.basename(abs_src) |
56 abs_src = os.path.dirname(abs_src) | 59 abs_src = os.path.dirname(abs_src) |
57 scan_path(dest, abs_src, path) | 60 scan_path(dest, abs_src, path) |
| 61 return 0 |
| 62 |
| 63 |
| 64 if __name__ == '__main__': |
| 65 sys.exit(main()) |
OLD | NEW |