--- a +++ b/trunk/cforge/cforge/Package/CFORGE/Scripts/pysvn.py @@ -0,0 +1,66 @@ +#!/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='strict'): + entries = output.decode(encoding='utf-8', errors='strict').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() + +