PyXMake Developer Guide 1.0
PyXMake
Loading...
Searching...
No Matches
stm_svn2git.py
1# -*- coding: utf-8 -*-
2# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3# % PyXMake - Build environment for PyXMake %
4# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5"""
6Translate a given SVN repository to a GIT repository.
7
8@version: 1.0
9----------------------------------------------------------------------------------------------
10@requires:
11 -
12
13@change:
14 -
15
16@author: garb_ma [DLR-FA,STM Braunschweig]
17----------------------------------------------------------------------------------------------
18"""
19
20import os, sys
21import git
22import glob
23import time
24import shlex
25import shutil
26import argparse
27import subprocess
28import svn.remote
29
30from PyXMake.Tools import Utility
31from PyXMake.VTL import Scratch
32
33__url_delimn = "/"
34__svn_repo = "https://svn.dlr.de/STM-Routines"
35__git_repo = "stm_routines"
36
37def main(git_repo, svn_repo, output=os.getcwd()):
38 """
39 Main function to execute the script.
40 """
41 # Operate in a full temporary directory
43 # Create a temporary source code folder.
44 __temp_path = os.path.join(os.getcwd(),Utility.GetTemporaryFileName(extension=""))
45 # Print an informative message
46 print("==================================")
47 print("Processing %s" % git_repo.upper())
48 print("==================================")
49 svn.remote.RemoteClient(svn_repo).checkout(__temp_path)
50 with Utility.ChangedWorkingDirectory(__temp_path):
51 # Get current bash executable from local GIT installation
52 bash = os.path.join(os.path.dirname(os.path.dirname(Utility.GetExecutable(git.Git.GIT_PYTHON_GIT_EXECUTABLE, get_path=True)[-1])),"bin","bash.exe")
53 # Search for all authors who contributed to this path
54 command = "svn log -q | awk -F"+" '|' '/^r/ "+'{sub("^ ", "", $2); sub(" $", "", $2); ' + 'print $2" = "$2" <"$2">"}'+"' | sort -u > authors-git.txt"
55 with open("authors.sh","w") as script: script.write(command)
56 command = " ".join(['"'+bash+'"',"-c","./authors.sh"])
57 # Create a bash script to execute a bash command with both types of quotation
58 subprocess.Popen(command, shell=True);
59 while True:
60 time.sleep(1) # Attempt to rename result. Avoid race condition
61 if os.path.exists(os.path.join(os.getcwd(),"authors-git.txt")):
62 shutil.move(os.path.join(os.getcwd(),"authors-git.txt"),os.path.join(Scratch,"authors-git.txt"))
63 break
64 # Again, avoid race condition. If it is still happening, retry again until success.
65 while True:
66 time.sleep(1)
67 try: Utility.DeleteRedundantFolders(__temp_path, ignore_readonly=True); break
68 except: pass
69 source = os.path.dirname(svn_repo)
70 trunk = Utility.PathLeaf(svn_repo)
71
72 # Create a new local repository
73 g = git.Repo.init(os.path.join(os.getcwd(),git_repo))
74 if Utility.GetPlatform() == "windows": g.git.execute("git config --global core.longpaths true")
75
76 # Assemble GIT command
77 command = "git svn clone "+source+" --no-metadata --no-minimize-url -T "+trunk+" --authors-file="+str(os.path.join('"'+os.getcwd(),"authors-git.txt"+'"'))+" "+"."
78
79 # Never surrender to GIT. Wait until the requested repository is non-empty
80 while not glob.glob(os.path.join(os.getcwd(),git_repo, '*')):
81 try:
82 time.sleep(0.2) # Again, avoid race conditions
83 g.git.execute(shlex.split(command,posix=not os.name.lower() in ["nt"]))
84 except Exception as e:
85 # Present exception error
86 print(e)
87 # Issue a notification
88 print("==================================")
89 print("This error is deemed non-critical. Ignoring")
90 print("==================================")
91 pass
92
93 # Delete files with no further use
94 Utility.DeleteFilesbyEnding("authors-git.txt")
95 if os.getcwd() != output: Utility.MoveIO(git_repo, os.path.join(output,git_repo))
96
97if __name__ == '__main__':
98# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99# % Access command line inputs %
100# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 parser = argparse.ArgumentParser(description="Translate a given SVN repository to a GIT repository.")
102
103 try:
104 # Access user command line arguments. Do nothing if not given.
105 _ = sys.argv[1]
106 args, _ = parser.parse_known_args()
107 except:
108 pass
109
110 try:
111 # User input information from command line
112 _ = args.user[0]
113 except:
114 # Translate
115 main(__git_repo, __svn_repo);
116 else:
117 raise NotImplementedError
118
119 # Finish translation job
120 print("==================================")
121 print("Finished translation")
122 print("==================================")
123 sys.exit()
Class to create 2to3 compatible pickling dictionary.
Module containing basic functionalities defined for convenience.
Definition __init__.py:1
main(git_repo, svn_repo, output=os.getcwd())