Flutter Engine
The Flutter Engine
copy.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright 2013 The Flutter Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Emulation of `rm -f out && cp -af` in out. This is necessary on Mac in order
7to preserve nanoseconds of mtime. See https://fxbug.dev/56376#c5."""
8
9import os
10import shutil
11import sys
12
13
14def main():
15 if len(sys.argv) != 3:
16 print('usage: copy.py source dest', file=sys.stderr)
17 return 1
18 source = sys.argv[1]
19 dest = sys.argv[2]
20
21 if os.path.isdir(source):
22 print(f'{source} is a directory, tool "copy" does not support directory copies')
23 return 1
24
25 if os.path.exists(dest):
26 if os.path.isdir(dest):
27
28 def _on_error(fn, path, dummy_excinfo):
29 # The operation failed, possibly because the file is set to
30 # read-only. If that's why, make it writable and try the op
31 # again.
32 if not os.access(path, os.W_OK):
33 os.chmod(path, stat.S_IWRITE)
34 fn(path)
35
36 shutil.rmtree(dest, onerror=_on_error)
37 else:
38 if not os.access(dest, os.W_OK):
39 # Attempt to make the file writable before deleting it.
40 os.chmod(dest, stat.S_IWRITE)
41 os.unlink(dest)
42
43 shutil.copy2(source, dest)
44
45
46if __name__ == '__main__':
47 main()
def main()
Definition: copy.py:14
Definition: main.py:1
def print(*args, **kwargs)
Definition: run_tests.py:49