# Copyright 2017 The Chromium Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file.
"""Recursively create hardlinks to targets at output."""
import argparse import os import shutil import sys
def CreateHardlinkHelper(target, output): """
Creates hardlink to `target` at `output`.
If `target` is a directory, the directory structure will be copied and
each file will be hardlinked independently. If `target` is a symlink,
a new symlink will be created.
The parent directory of `output` must exists or the function will fail. """ if os.path.islink(target):
os.symlink(os.readlink(target), output) elif os.path.isfile(target): try:
os.link(target, output) except:
shutil.copy(target, output) else:
os.mkdir(output) for name in os.listdir(target):
CreateHardlinkHelper(
os.path.join(target, name),
os.path.join(output, name))
def CreateHardlink(target, output): """
Creates hardlink to `target` at `output`.
If `target` is a directory, the directory structure will be copied and
each file will be hardlinked independently. If `target` is a symlink,
a new symlink will be created.
If `output` already exists, it is first deleted. The parent directory
of `output` is created if it does not exists. """ if os.path.exists(output): if os.path.isdir(output):
shutil.rmtree(output) else:
os.unlink(output)
dirname = os.path.dirname(output) ifnot os.path.isdir(dirname):
os.makedirs(dirname)
CreateHardlinkHelper(target, output)
def CreateHardlinks(output_dir, relative_to, targets): """
Creates hardlinks to `targets` in `output_dir`.
The `targets` should starts with `relative_to` and the hardlink will
be created at `{output_dir}/{os.path.relpath(sources, relative_to)}`.
Fails with an error if any file in `targets` not located inside the
`relative_to` directory orif creating any of the hardlinks fails. """ for target in targets: ifnot target.startswith(relative_to):
print(f'error: "{target}" not relative to "{relative_to}',
file=sys.stderr)
sys.exit(1)
for target in targets:
output = os.path.join(output_dir, os.path.relpath(target, relative_to))
CreateHardlink(target, output)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.