sync svn:keywords...

Code-Stücke können hier veröffentlicht werden.
Benutzeravatar
jens
Moderator
Beiträge: 8458
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

sync svn:keywords...

Beitragvon jens » Mittwoch 31. Januar 2007, 09:03

Hab mir ein Kleines Skript geschrieben, um die svn:keywords automatisch setzten zu lassen ;)

Passend dazu: http://www.python-forum.de/post-51330.html#51330

Code: Alles auswählen

#!/usr/bin/python
# -*- coding: UTF-8 -*-

""" PyLucid SVN tool

    - sync svn:keywords


Last commit info:
----------------------------------
$LastChangedDate:$
$Rev:$
$Author: jensdiemer $

Created by Jens Diemer

license:
    GNU General Public License v2 or above
    http://www.opensource.org/licenses/gpl-license.php
"""


#_____________________________________________________________________________
# settings:

REPOSITORY = "/home/jens/repos/pylucid/trunk"

SIMULATION = False
#~ SIMULATION = True

# Dateien die keine svn:keywords haben dürfen:
NO_KEYWORD_FILE_EXT = (".zip")

#_____________________________________________________________________________


import os, sys, re, pprint

try:
    import pysvn
except ImportError, e:
    print "Import Error: ", e
    print "You must install pysvn from:"
    print "http://pysvn.tigris.org"
    sys.exit()


client = pysvn.Client()

re_keywords = re.compile("\$(.*?):(.*?)\$")


#_____________________________________________________________________________


def print_status():
    """
    shows the SVN status
    """
    def print_list(changes, status):
        print "%s files:" % status
        wc_status = getattr(pysvn.wc_status_kind, status)
        count = 0
        for f in changes:
            if f.text_status != wc_status:
                continue
            count +=1
            print "    ", f.path
        print "  %s file(s)" % count

    print "SVN status:"
    print "- "*39
    changes = client.status(REPOSITORY)

    for status in ("added","deleted","modified","conflicted","unversioned"):
        print_list(changes, status)

    print "- "*39

#_____________________________________________________________________________

def walk():
    """
    os.walk durch das repro-Verz.
    Liefert den absoluten Pfad als generator zurück
    """
    svn_dir = os.sep + ".svn"
    for dir,_,files in os.walk(REPOSITORY):
        if svn_dir in dir:
            # Versteckte .svn Verzeichnisse auslassen
            continue

        for fn in files:
            abs_path = os.path.join(dir, fn)
            yield abs_path

#_____________________________________________________________________________

def get_file_keywords(fn):
    """
    Fischt aus dem DateiInhalt vorhandene keywords per re raus
    """
    f = file(fn, "r")
    content = f.read()
    f.close()

    keywords = re_keywords.findall(content)
    if keywords == []:
        return set()

    return set([i[0] for i in keywords])

def get_svn_keywords(proplist):
    """
    Liefert eine set() der vorhanden svn:keywords zurück.
    proplist = pysvn.Client().proplist(filename)
    """
    try:
        props = proplist[0][1]["svn:keywords"]
    except (IndexError, KeyError):
        # Hat keine keywords
        return set()
    else:
        return set(props.split(" "))

#_____________________________________________________________________________

def delete_svn_keywords(filename, proplist):
    """
    Löscht aus den svn-properties den Eintrag "svn:keywords", wenn vorhanden.
    """
    svn_keywords = get_svn_keywords(proplist)
    if len(svn_keywords) == 0:
        # Hat keine Keywords die man löschen könnte
        print "File has no svn:keywords, OK"
        return

    print ">>>", svn_keywords
    print "Delete svn_keywords"

    client.propdel("svn:keywords", filename)

def set_svn_keywords(filename, keywords):
    """
    Setzt svn:keywords
    """
    keywords = " ".join(keywords)
    print ">>> set svn_keywords to:", keywords
    if SIMULATION:
        print "simulation on, no change made."
        return

    client.propset("svn:keywords", keywords, filename)

#_____________________________________________________________________________

def sync_keywords():
    """
    syncronisiert keywords aus allen Dateien.
    """
    for fn in walk():
        print "-"*40
        print fn
        try:
            proplist = client.proplist(fn)
        except pysvn._pysvn.ClientError, e:
            print "Error:", e
            continue

        ext = os.path.splitext(fn)[1]
        if ext in NO_KEYWORD_FILE_EXT:
            # Diese Datei sollte keine Keywords haben!
            delete_svn_keywords(fn, proplist)
            continue

        #~ pprint.pprint(proplist)
        file_keywords = get_file_keywords(fn)
        svn_keywords = get_svn_keywords(proplist)

        print "keywords in file..: >%s<" % " ".join(sorted(file_keywords))
        print "svn:keywords......: >%s<" % " ".join(sorted(svn_keywords))

        if file_keywords != svn_keywords:
            print "keywords are different -> change it:"
            if len(file_keywords) == 0:
                delete_svn_keywords(fn, proplist)
            else:
                set_svn_keywords(fn, list(file_keywords))
        else:
            print "keywords are the same, ok"


#_____________________________________________________________________________


if __name__ == "__main__":
    print_status()
    sync_keywords()
    print_status()


Ich hab es noch nicht vollkommen gestetet!

EDIT: Ach, Links die mir geholfen haben:
http://pysvn.tigris.org/docs/pysvn_prog_guide.html
http://pysvn.tigris.org/docs/pysvn_prog_ref.html
https://apestaart.org/thomas/trac/brows ... /client.py

CMS in Python: http://www.pylucid.org
GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Benutzeravatar
jens
Moderator
Beiträge: 8458
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Beitragvon jens » Donnerstag 1. Februar 2007, 15:46


CMS in Python: http://www.pylucid.org
GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder