def main(): util.SetupDefaultLoggingConfig() parser = _CreateArgumentParser() args = parser.parse_args() target_path, link_path = args.link if args.force: Rmtree(link_path) if args.use_absolute_path: target_path = os.path.abspath(target_path) elif args.use_relative_path: # os.path.relpath() requires its second parameter be a directory. If our # target is a file, i.e. the link we will be creating should be to a file, # we need to calculate the relative path between our target and the # directory that our link will reside in, not the full path to the link # itself. If we do not, our relative path will ascend one level too far. # This is visible in the following examples. # # os.path.relpath( # '/target/file.txt', '/link/file.txt') = '../../target/file.txt' [bad] # # os.path.relpath( # '/target/file.txt', '/link') = '../target/file.txt' [good] if os.path.isfile(target_path): relative_link_path = os.path.dirname(link_path) else: relative_link_path = link_path target_path = os.path.relpath(target_path, relative_link_path) MakeSymLink(target_path=target_path, link_path=link_path)
def main(): util.SetupDefaultLoggingConfig() parser = _CreateArgumentParser() args = parser.parse_args() folder_path, link_path = args.link if '.' in folder_path: d1 = os.path.abspath(folder_path) else: d1 = os.path.abspath(os.path.join(link_path, folder_path)) if not os.path.isdir(d1): logging.warning('%s is not a directory.', d1) MakeSymLink(from_folder=folder_path, link_folder=link_path)
try: external_temp_file = os.path.join(external_temp_dir, 'test.txt') with open(external_temp_file, 'w') as fd: fd.write('HI') link_dir = os.path.join(self.tmp_dir, 'foo', 'link_dir') MakeSymLink(external_temp_file, link_dir) win_symlink._RmtreeOsWalk(self.tmp_dir) # The target file should still exist self.assertTrue(os.path.isfile(external_temp_file)) finally: shutil.rmtree(external_temp_dir, ignore_errors=True) def testRmtreeCmdShellDoesNotFollowSymlinks(self): """_RmtreeShellCmd(...) will delete the symlink and not the target.""" external_temp_dir = tempfile.mkdtemp() try: external_temp_file = os.path.join(external_temp_dir, 'test.txt') with open(external_temp_file, 'w') as fd: fd.write('HI') link_dir = os.path.join(self.tmp_dir, 'foo', 'link_dir') MakeSymLink(external_temp_file, link_dir) win_symlink._RmtreeShellCmd(self.tmp_dir) # The target file should still exist self.assertTrue(os.path.isfile(external_temp_file)) finally: shutil.rmtree(external_temp_dir, ignore_errors=True) util.SetupDefaultLoggingConfig() unittest.main(verbosity=2)