def dupe2link4PFarrayOfSame(arrayOfSomePF, onlyInfo=False): acc1 = array(arrayOfSomePF) forig = os.sep.join(acc1[0, :2]) print "\norig:\t", forig for x in acc1[1:]: fsame = os.sep.join(x[:2]) print fsame, if hardlink.samefile(forig, fsame): print "already hardlink" continue else: print if not onlyInfo: os.remove(fsame) try: hardlink.create(forig, fsame) except: try: print "can't create hardlink. try create symlink" makesymlinkViaCmd(forig, fsame) except: print "can't create symlink. sorry. try just copy" try: shutil.copyfile(forig, fsame) except: print "can't copy. please do something manualy!" raw_input() print "created hardlink" else: print "need to created hardlink"
def create_links(self, root): seed_path = os.path.join(options.seed_folder, root) if os.path.isfile(seed_path): cur_file = os.path.join(seed_path, root) new_file = cur_file.replace(options.seed_folder, options.link_folder) if not os.path.exists(new_file): print 'Linking:', new_file hardlink.create(cur_file, new_file) else: print "File Exists:", new_file else: for start_dir, subdirs, files in os.walk(root): new_dir = start_dir.replace(options.seed_folder, options.link_folder) if not os.path.exists(new_dir): os.makedirs(new_dir) else: print 'Folder Exists:', new_dir for f in files: cur_file = os.path.join(start_dir, f) new_file = cur_file.replace(options.seed_folder, options.link_folder) if not os.path.exists(new_file): print 'Linking:', new_file hardlink.create(cur_file, new_file) else: print "File Exists:", new_file
def test(self): link = 'hardlink' contents = 'bar' create(self.FOO, link) self.assertTrue(samefile(self.FOO, link)) with open(link, 'r') as fd: self.assertEqual(fd.read(), self.FOO_CONTENTS) with open(self.FOO, 'w') as fd: fd.write(contents) with open(link, 'r') as fd: self.assertEqual(fd.read(), contents)
def create_link(file_origin, file_destination, mode='symlink'): try: if not file_destination.parent.exists(): os.makedirs(os.path.dirname(str(file_destination))) # file_destination.mkdir(parents=True) print('Folder already exists, just creating {}\n'.format(mode)) if mode == 'symlink': os.symlink(str(file_origin), str(file_destination)) # file_origin.symlink_to(file_destination, target_is_directory=True) else: hardlink.create(file_origin, file_destination) print("File: {} linked to {}\n ({})\n".format( file_origin, file_destination, mode)) except FileExistsError: print( "File {} already exists. Skipping it\n".format(file_destination)) pass
def hardlink(source, link_name): if System.is_unix(): return os.link(source, link_name) return winlink.create(source, link_name)
def new_symlink(source, link_name): if os.path.isdir(source): return junction.create(source, link_name) return hardlink.create(source, link_name)