diff --git a/link-tool.py b/link-tool.py index 312fe5e..2905a41 100644 --- a/link-tool.py +++ b/link-tool.py @@ -7,7 +7,6 @@ import logging from typing import List, Tuple, Union - logger = logging.getLogger(__name__) @dataclasses.dataclass(repr=True) @@ -66,28 +65,37 @@ def load_config(conf_path: str) -> Tuple[Conf, List[Link]]: return conf, links -def make_link(links: List[Link]): +def make_link(links: List[Link], dry_run=False): for link in links: if os.path.exists(link.dst): logger.warning('dst exists, removing: {}'.format(link.dst)) - os.remove(link.dst) + if not dry_run: + os.remove(link.dst) dst_dir = os.path.dirname(link.dst) if not os.path.exists(dst_dir): logger.info('dst directory not exists, creating: {}'.format(dst_dir)) - os.makedirs(dst_dir) + if not dry_run: + os.makedirs(dst_dir) - os.link(link.src, link.dst) + logger.info('creating link: {}'.format(link.dst)) + if not dry_run: + os.link(link.src, link.dst) def main(): parser = argparse.ArgumentParser() - parser.add_argument('-c', '--config', required=True, help='configuration file') - parser.add_argument('-v', '--verbose', action='store_true' ,help='more verbose log') + parser.add_argument('-c', '--config', required=True, + help='configuration file') + parser.add_argument('-n', '--dry-run', action='store_true', + help='dry run without actually create link') + parser.add_argument('-v', '--verbose', action='store_true', + help='more verbose log') args = parser.parse_args() config_path = args.config verbose = args.verbose + dry_run = args.dry_run logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) @@ -100,7 +108,14 @@ def main(): for link in links: logger.info('loaded link: {}'.format(link)) - make_link(links) + assert(config.cwd is not None) + config_path_dir = os.path.dirname(config_path) + chdir_target = os.path.join(config_path_dir, config.cwd) + logger.info('changing CWD: {}/{}'.format(config_path_dir, config.cwd)) + logger.info('the CWD realpath: {}'.format(os.path.realpath(chdir_target))) + os.chdir(chdir_target) + + make_link(links, dry_run=dry_run) if __name__ == '__main__': main() diff --git a/test/src/link1.conf b/test/src/link1.conf index 2142be8..0c94bb4 100644 --- a/test/src/link1.conf +++ b/test/src/link1.conf @@ -1,6 +1,6 @@ # cwd is used for the pwd of src and dst defined below. # the cwd is relative to the its config file, such as this file -cwd = '.' +cwd = ../.. # this is comment test/src/file1.txt -> test/dst/file1.txt