How to create a symbolic link with SCons?

Viewed 3828

I'm using SCons for building a project and need to add a symbolic link to a file it is installing via env.Install. What command(s) will make a link that's the equivalent of running ln -s on the command line?

5 Answers

If you wanted to issue the command directly to the shell and know the OS, subprocess can be used as well.

E.g.: subprocess.call(['ln', '-s', '</src/path>', '</dest/path>'])

In addition to Nicks solution, you can add a directory symlink by using a file as a directory name carrier. It's not the cleanest solution and debugging path names is a pain, but this works well:

def symlink_last(target_source_env):
    src = os.path.basename(os.path.dirname(str(source[0])))
    link = "deliverables/last"
    print "Symlinking "+ src + "as" + link
    os.symlink(src, link)

BUILD_TARGETS.append('link')
install_dir = "deliverables/subdir"
carrier_file = "filename"
builder = Builder(action = symlink_last, chdir=False)
env.Append(BUILDERS={ "Symlink" : builder }) 
env.Alias(target="link", source=env.Symlink(dir="deliverables", source = install_dir + carrier_file)

This will make a link to deliverables/subdir named deliverables/last, provided that a file deliverables/subdir/filename exists.

Related