#! /usr/bin/env python3# linktree## Make a copy of a directory tree with symbolic links to all files in the# original tree.# All symbolic links go to a special symbolic link at the top, so you# can easily fix things if the original source tree moves.# See also "mkreal".## usage: mklinks oldtree newtreeimport sys, osLINK = '.LINK' # Name of special symlink at the top.debug = 0def main():if not 3 <= len(sys.argv) <= 4:print('usage:', sys.argv[0], 'oldtree newtree [linkto]')return 2oldtree, newtree = sys.argv[1], sys.argv[2]if len(sys.argv) > 3:link = sys.argv[3]link_may_fail = 1else:link = LINKlink_may_fail = 0if not os.path.isdir(oldtree):print(oldtree + ': not a directory')return 1try:os.mkdir(newtree, 0o777)except OSError as msg:print(newtree + ': cannot mkdir:', msg)return 1linkname = os.path.join(newtree, link)try:os.symlink(os.path.join(os.pardir, oldtree), linkname)except OSError as msg:if not link_may_fail:print(linkname + ': cannot symlink:', msg)return 1else:print(linkname + ': warning: cannot symlink:', msg)linknames(oldtree, newtree, link)return 0def linknames(old, new, link):if debug: print('linknames', (old, new, link))try:names = os.listdir(old)except OSError as msg:print(old + ': warning: cannot listdir:', msg)returnfor name in names:if name not in (os.curdir, os.pardir):oldname = os.path.join(old, name)linkname = os.path.join(link, name)newname = os.path.join(new, name)if debug > 1: print(oldname, newname, linkname)if os.path.isdir(oldname) and \not os.path.islink(oldname):try:os.mkdir(newname, 0o777)ok = 1except:print(newname + \': warning: cannot mkdir:', msg)ok = 0if ok:linkname = os.path.join(os.pardir,linkname)linknames(oldname, newname, linkname)else:os.symlink(linkname, newname)if __name__ == '__main__':sys.exit(main())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。