Från Perl till Python

Jag har roat mig med att skriva om ett Perl skript jag skrev för att ta backup på alla mina subversion repos till Python. Så här ser det ut nu. Det är inte riktigt färdigt än men nu känns mycket bättre helt plötsligt 🙂

#!/usr/bin/perl -w
use strict;

my $repoParentPath = "/var/svn/repos/";
my $copyToPath = "/home/backup/";

sub isRepo {
	my $dirPath = $_[0];
	my $pathToFormatFileInRepo = appendFileNameToPath($_[0],"format") ;
	my $cmd = "file -b ".$pathToFormatFileInRepo." 2>/dev/null";
	my $statResult = `$cmd`;
	chop($statResult);

	if($statResult eq "ASCII text"){
		return 1;
	}
	return 0;
}

sub doSvnDump {
	my $pathToRepo = $_[0];
	my $pathToDumpFile = $_[1];
	print "svnadmin dump ".$pathToRepo." > ".$pathToDumpFile."\n";

	my $statResult = system("svnadmin dump ".$pathToRepo." > ".$pathToDumpFile);
}

sub appendFileNameToPath {
	my $path = $_[0];
	my $fileName = $_[1];

	if(length($path) > 0){
		$path = substr($path, 0, (length($path) -1) );
		$path = $path.$fileName."\"";
	}
	return $path;
}

sub isDir {
	my $dirPath = $_[0];	
	my $cmd = "file -b ".$dirPath." 2>/dev/null";
	my $statResult = `$cmd`;
	chop($statResult);

	if($statResult eq "directory"){
		return 1;
	}
	return 0;
}

sub readDir {
	my $repoParentPath = $_[0];
	my @repos=();
	my $dirSeparator="/";

	opendir(my $DIR, $repoParentPath) or die ("Kunde inte öppna mappen");
	@repos =  readdir($DIR) or die ("Kunde inte läsa mappen");
	closedir($DIR);

	foreach my $repo(@repos) {
		if(-d $repoParentPath.$repo and $repo ne ".." and $repo ne "." ){
			my $currentRepo = "\"".$repoParentPath.$repo.$dirSeparator."\"";					
			if(isRepo($currentRepo) == 1){
				my $fileNameToDumpTo = appendFileNameToPath("\"".$copyToPath."\"",$repo.".dump");
				doSvnDump($currentRepo,$fileNameToDumpTo);
			}
		}
	}
}

sub main {
	my $repoParentPath = $_[0];
	if(isDir($repoParentPath) == 1 and isDir($copyToPath) == 1){
		readDir($repoParentPath);
	}else{
		die("Fel på sökvägar");
	}
}
main($repoParentPath);
'''
Created on 2010-08-14
@author: Peter Lindblom 
@contact: http://plweb.se
'''

import os, subprocess

class SubversionBackup:
    
    __fromDirPath = ""
    __toDirPath = ""
    __repos = [] 
      
    

    def doSvnDump(self):
        """ execute svn dump command on found repos """
        for repo in self.__repos:
            cmd = "svnadmin dump ""%s"" > ""%s""" % repo
            print cmd
            p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            for line in p.stdout.readlines():
                print line,
            p.wait()

    def findReposInPath(self):
        """ find svn repos in fromDirPath """
        if(os.path.isdir(self.__fromDirPath)):
            files = os.listdir(self.__fromDirPath)
            for file in files:
                if(self.__isRepo(os.path.join(self.__fromDirPath, file))):                    
                    self.__repos.append( ( os.path.join(self.__fromDirPath, file), self.__makeToFilePath(file)))
    
    def setFromDirPath(self, fromDirPath):
        """ sets the directory from where look for svn repos """
        self.__fromDirPath = fromDirPath    

    def setToDirPath(self, toDirPath):
        """ sets the directory to where svn dumps while be created """
        self.__toDirPath = toDirPath    

    def resetRepos(self):
        """ sets repos to an empty list """      
        self.__repos = []    
    
    def reset(self):
        """ sets fromDirPath and toDirPath to an empty string and sets repos to an empty list  """
        self.__fromDirPath = ""
        self.__toDirPath = ""
        self.__repos = []
                                    
    def __isRepo(self, dirPath):
        """ check to see if the path is folder and the folder contains a format file """ 
        formatFilePath = os.path.join(dirPath, "format") 
        if (os.path.isdir(dirPath) and os.path.isfile(formatFilePath)): 
            return True
        return False

    def __makeToFilePath(self, file):
        """ ads .dump to file """
        file = file + ".dump"
        return os.path.join(self.__toDirPath, file)                
                                      
    def __init__(self, fromDirPath, toDirPath):
        """ make as an instance of SubversionBackup take """
        self.__fromDirPath = fromDirPath
        self.__toDirPath = toDirPath

    def __str__(self):
        return  "fromDir:"+ self.__fromDirPath + "\ntoDir:" + self.__toDirPath +"\nfoundRepos:" + self.__repos.__str__()  

min loopia dns sync python modul

Laddade precis upp min python modul jag använder för synka några av mina domäner till http://pypi.python.org/pypi.

http://pypi.python.org/pypi/loopiadnssync/1.0.0/
https://github.com/plwebse/loopiadnssync

from loopiadnssync import LoopiaDnsSync

loopiadnsSync = LoopiaDnsSync("loopiaUserName", "loopiaPassword")

loopiadnsSync.addDomain("www.test.com");
loopiadnsSync.addDomain("test.com");
loopiadnsSync.syncDomains()

Och om kör koden ovanför så ser resultat ut ungefär så här:

python loopiadns.py 
loopiadnssync: Sat, 16 Apr 2011 19:12:12
www.test.com, 127.0.0.1, nochg
test.com, 127.0.0.1, nochg