Add logic of cwd, dry_run

This commit is contained in:
leafee98 2025-09-21 17:17:51 +08:00
parent 2e014f141d
commit 2605660817
2 changed files with 24 additions and 9 deletions

View file

@ -7,7 +7,6 @@ import logging
from typing import List, Tuple, Union from typing import List, Tuple, Union
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@dataclasses.dataclass(repr=True) @dataclasses.dataclass(repr=True)
@ -66,28 +65,37 @@ def load_config(conf_path: str) -> Tuple[Conf, List[Link]]:
return conf, links return conf, links
def make_link(links: List[Link]): def make_link(links: List[Link], dry_run=False):
for link in links: for link in links:
if os.path.exists(link.dst): if os.path.exists(link.dst):
logger.warning('dst exists, removing: {}'.format(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) dst_dir = os.path.dirname(link.dst)
if not os.path.exists(dst_dir): if not os.path.exists(dst_dir):
logger.info('dst directory not exists, creating: {}'.format(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(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', required=True, help='configuration file') parser.add_argument('-c', '--config', required=True,
parser.add_argument('-v', '--verbose', action='store_true' ,help='more verbose log') 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() args = parser.parse_args()
config_path = args.config config_path = args.config
verbose = args.verbose verbose = args.verbose
dry_run = args.dry_run
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
@ -100,7 +108,14 @@ def main():
for link in links: for link in links:
logger.info('loaded link: {}'.format(link)) 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__': if __name__ == '__main__':
main() main()

View file

@ -1,6 +1,6 @@
# cwd is used for the pwd of src and dst defined below. # 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 # the cwd is relative to the its config file, such as this file
cwd = '.' cwd = ../..
# this is comment # this is comment
test/src/file1.txt -> test/dst/file1.txt test/src/file1.txt -> test/dst/file1.txt