Skip to content

Instantly share code, notes, and snippets.

@Soft
Created April 17, 2010 19:09
Show Gist options
  • Save Soft/369749 to your computer and use it in GitHub Desktop.
Save Soft/369749 to your computer and use it in GitHub Desktop.
from install import *
packages= [
Package(name="Vim", posix=[
SymlinkTask("./.vimrc", "~/.vimrc")
])
]
install(packages)
#!/usr/bin/env python
import os
from os.path import *
DEFAULT_INSTALL = "deploy.py"
class RunnableList(list):
def run(self):
for item in self:
item.run()
class Package(object):
def __init__(self, name, common=[], posix=[], nt=[]):
self.name = name
self.common = RunnableList(common)
self.posix = RunnableList(posix)
self.nt = RunnableList(nt)
def run(self):
print "Installing package '%s'" % self.name
self.common.run()
if os.name is "posix":
self.posix.run()
elif os.name is "nt":
self.nt.run()
class TaskError(Exception):
pass
class SymlinkTask(object):
def __init__(self, source, name):
self.source = normpath(expanduser(source))
self.name = (expanduser(name))
@staticmethod
def symlink_ntfs(source, name):
import ctypes
flags = 1 if isdir(source) else 0
kdll = ctypes.windll.LoadLibrary("kernel32.dll")
if kdll.CreateSymbolicLinkA(name, source, flags) is 0:
raise TaskError("Cannot create symbolic link: %s" % ctypes.FormatError())
@staticmethod
def symlink(source, name):
if os.name is "posix":
os.symlink(source, name)
elif os.name is "nt":
SymlinkTask.symlink_ntfs(source, name)
else:
raise TaskError("Unsupported system")
def run(self):
SymlinkTask.symlink(self.source, self.name)
def module_name(path):
return splitext(path)[0]
def parse_options():
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-a", "--all", action="store_true", dest="all")
parser.add_option("-p", "--packages", action="store_true", dest="packages")
return parser.parse_args()
def install(tasks):
tasks = RunnableList(tasks)
options, args = parse_options()
if options.all:
tasks.run()
if options.packages:
for package in filter(lambda p: p.name in args, tasks):
package.run()
if not (options.all or options.packages):
print "Installable packages:"
for package in tasks:
print package.name
if __name__ == "__main__":
if isfile(DEFAULT_INSTALL):
__import__(module_name(DEFAULT_INSTALL), globals(), locals())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment