11def copy_git_directory(src, dst, out=None):
13 Makes a copy of `src` directory in `dst` directory. If files already exist
14 and are identical, do not touch them. If extra files or directories exist,
15 remove them. Assume that `src` is a git directory so that `git ls-files` can
16 be used to enumerate files. This has the added benefit of ignoring files
17 not tracked by git. Also, if out is not None, write summary of actions to out.
18 If `dst` is a top-level git directory, the `.git` directory will be removed.
20 if not os.path.isdir(src):
21 raise Exception(
'Directory "%s" does not exist.' % src)
22 if not os.path.isdir(dst):
24 ls_files = subprocess.check_output([
25 'git',
'ls-files',
'-z',
'.'], cwd=src).
decode(
'utf-8')
26 src_files = set(p
for p
in ls_files.split(
'\0')
if p)
27 abs_src = os.path.abspath(src)
31 def output(out, sym, dst, path):
33 out.write(
'%s %s%s%s\n' % (sym, dst, os.sep, path))
34 for dirpath, dirnames, filenames
in os.walk(
'.', topdown=
False):
35 for filename
in filenames:
36 path = os.path.normpath(os.path.join(dirpath, filename))
37 if path
not in src_files:
38 output(out,
'-', dst, path)
40 for filename
in dirnames:
41 path = os.path.normpath(os.path.join(dirpath, filename))
42 if not os.listdir(path):
43 output(out,
'-', dst, path + os.sep)
45 for path
in src_files:
46 src_path = os.path.join(abs_src, path)
47 if os.path.exists(path):
48 with open(path)
as f1:
49 with open(src_path)
as f2:
50 if f1.read() == f2.read():
52 output(out,
'+', dst, path)
53 shutil.copy2(src_path, path)