view symlink.py @ 1:c8c579f62d99

add rc files that have relation with mailer. and symlink.py was modified as adding subroutin that make hardlink for fetchmailrc.
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Sat, 15 Nov 2008 00:57:04 +0900
parents 6d5c73fe5744
children 0f43fb312bd6
line wrap: on
line source

#!/usr/bin/env python
# vim:fileencoding=utf-8

import re
import os
import sys
import ConfigParser

home = os.environ['HOME']
etcdir = os.getcwd()
dir = re.match( home+"/(.*)", etcdir).group(1)
force = False

def main():
	# read configuration file
	config = ConfigParser.ConfigParser()
	config.read( 'install.cfg')
	opts = config.items('InstallFiles', 0)

	# loop all options
	for opt in opts:
		value = opt[1].split(':')
		if value[0] == "SYM":
			if len(value) > 1:
				makeSymlink(opt[0], value[1])
			else:
				makeSymlink(opt[0])
		elif value[0] == "LNK":
			if len(value) > 1:
				makeHardlink(opt[0], value[1])
			else:
				makeHardlink(opt[0])


### make symbolic link dst0 relating with src0
def makeSymlink(src0, dst0=None):
	# correct filename of source and destination
	if dst0==None:
		dst0 = "."+src0
	src = dir+"/"+src0
	dst = home+"/"+dst0

	# check destination file
	if os.path.exists(dst):
		if force:
			os.remove(dst)
		else:
			print "file "+dst+" already existed!"
			return

	# make symbolic link
	print dst+" => "+src
	os.symlink(src, dst)

### make hardlink dst0 relating with src0
def makeHardlink(src0, dst0=None):
	# correct filename of source and destination
	if dst0==None:
		dst0 = "."+src0
	src = src0
	dst = home+"/"+dst0

	# check destination file
	if os.path.exists(dst):
		if force:
			os.remove(dst)
		else:
			print "file "+dst+" already existed!"
			return

	# make symbolic link
	print dst+" => "+src
	os.link(src, dst)

main()






### てすとるーちん ###
def listingFromArgs():
	del sys.argv[0]
	for file in sys.argv:
		makeSymlink(file)

def listingFromDir():
	files = os.listdir(".")
	for file in files:
		correctFilename(file)

def correctFilename(filename):
	result = re.match( "DoT-(.*)", filename);
	if result!=None:
		dst = result.group(1)
		print filename+" => "+dst
		os.rename(filename, dst)
	
#listingFromDir()
#listingFromArgs()



# test

#master = [10, 20, 30]
#listing( master, 20 )
#listing( master, 20 )
def listing(a, b):
	i=0;
	print "b = "+str(b)
	print "len(a) = "+str(len(a))
	while i<len(a):
		print "a["+str(i)+"] = "+str(a[i])
		a[i] = a[i] + 5
		i = i+1