[r85]: / trunk / cforge / cforge / Package / CFORGE / Scripts / pysvn.py  Maximize  Restore  History

Download this file

102 lines (86 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#!/usr/bin/python
#
# This module is a wrapper for a locally installed SVN command line client.
# It can be used to find URLs of CODESYS projects within a repository, or to
# checkout all files outside of CODESYS projects.
#
# - svn_list:
# a wrapper for the "svn list" command, supporting recursive lists
#
# - svn_get_directories_with_codesys_projects / svn_get_directories_without_codesys_projects:
# return directories with or without CODESYS projects. Can be used to find
# CODESYS library URLs in repositories.
#
# - svn_checkout_non_codesys:
# Checkout top-level directory w/o content.
# Then update all directories, which are not containing any CODESYS projects.
#
import subprocess


def svn_list(username, password, url, recursive=False):
    args = ""
    if recursive:
        args += " -R"
    cmd="svn list %s --username=%s --password=%s %s" % (args, username, password, url)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    entries = list()
    if "\n" in output.decode(encoding='utf-8', errors='ignore'):
        entries = output.decode(encoding='utf-8', errors='ignore').strip().replace("\r","").split("\n")
    return entries

def svn_get_directories_with_codesys_projects(username, password, url):
    allfiles = svn_list(username, password, url, True)
    codesys_projects = filter(lambda file: 'meta.profile' in file, allfiles)

    alldirs=list()
    for cp in codesys_projects:
        dirname = cp.replace("/meta.profile", "")
        alldirs.append(dirname)
    return alldirs

def svn_get_directories_without_codesys_projects(username, password, url):
    allfiles = svn_list(username, password, url, True)
    codesys_projects = filter(lambda file: 'meta.profile' in file, allfiles)
    # filter out all subdirectories of the directory containing 'meta.profile',
    # as well as all files (keep only directories)
    alldirs=allfiles
    for cp in codesys_projects:
        dirname = cp.replace("/meta.profile", "")
        alldirs = filter(lambda file: not file.startswith(dirname) and file.endswith('/'), alldirs)
    return alldirs

def svn_checkout_non_codesys(username, password, url, destination):
    dirs = svn_get_directories_without_codesys_projects(username, password, url)
    cmd="svn checkout --depth=empty --username=%s --password=%s %s %s" % (username, password, url, destination)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

    if not err:
        for d in dirs:
            cmd="svn update --depth=files --username=%s --password=%s %s/%s" % (username, password, destination, d)
            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()

def svn_checkout_all(username, password, url, destination):
    cmd="svn checkout --username=%s --password=%s %s %s" % (username, password, url, destination)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

def svn_remove_local(destination):
    cmd="svn update --set-depth exclude %s" % (destination)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()

def svn_get_stat(folder):
    cmd="svn stat %s" % (folder)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (stat, err) = p.communicate()
    return stat
            
def svn_get_url(folder):
    cmd="svn info --show-item url %s" % (folder)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (url, err) = p.communicate()
    return url
            
def svn_update_non_codesys(username, password, folder):
    url = svn_get_url(folder)
    dirs = svn_get_directories_without_codesys_projects(username, password, url)
    
    for d in dirs:
        cmd="svn update --depth=files --username=%s --password=%s %s/%s" % (username, password, folder, d)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        (output, err) = p.communicate()

def svn_commit_non_codesys(username, password, folder, message):
    cmd="svn commit -m \"%s\" --username=%s --password=%s" % (message, username, password)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, cwd=folder)
    (output, err) = p.communicate()