|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import datetime |
| 4 | +import subprocess |
| 5 | + |
| 6 | +#https://stackoverflow.com/questions/21377520/do-a-maven-build-in-a-python-script |
| 7 | +class changeDir: |
| 8 | + def __init__(self, newPath): |
| 9 | + self.newPath = os.path.expanduser(newPath) |
| 10 | + |
| 11 | + # Change directory with the new path |
| 12 | + def __enter__(self): |
| 13 | + self.savedPath = os.getcwd() |
| 14 | + os.chdir(self.newPath) |
| 15 | + |
| 16 | + # Return back to previous directory |
| 17 | + def __exit__(self, etype, value, traceback): |
| 18 | + os.chdir(self.savedPath) |
| 19 | + |
| 20 | + |
| 21 | +def build(*args): |
| 22 | + return subprocess.check_call(['mvn'] + list(args),shell=True) |
| 23 | + #return subprocess.check_output(['mvn'] + list(args),shell=True) |
| 24 | + |
| 25 | +#https://coderwall.com/p/tjc9oa/for-those-who-build-multiple-maven-projects-at-once |
| 26 | +def mvnbuild(cdir): |
| 27 | + cwd=os.getcwd() |
| 28 | + os.chdir(cdir) |
| 29 | + process = subprocess.Popen( |
| 30 | + #"mvn clean install -DskipTests=true" |
| 31 | + "mvn clean install", |
| 32 | + shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE |
| 33 | + ) |
| 34 | + out, err = process.communicate() |
| 35 | + #log(out, dir) |
| 36 | + #print(out) |
| 37 | + errcode = process.returncode |
| 38 | + os.chdir(cwd) |
| 39 | + return (out, err, errcode) |
| 40 | + |
| 41 | + |
| 42 | +try: |
| 43 | + clonedprojectfolder= sys.argv[1] |
| 44 | + #mavenargumenr=sys.argv[2] |
| 45 | + projectstobuild=[ i for i in os.listdir(clonedprojectfolder) if(os.path.isdir(os.path.join(clonedprojectfolder+i)))] |
| 46 | + logfile=open('Build_log_'+str(datetime.datetime.now()),'a') |
| 47 | + |
| 48 | + print("Processing projects in folder ::" + clonedprojectfolder) |
| 49 | + projcount=len(projectstobuild) |
| 50 | + failedprojcount=0 |
| 51 | + processedprojcount=0 |
| 52 | + print("Total projects to build "+ str(projcount)) |
| 53 | + #logfile.write("Total projects to build "+ str(projcount)+"\n") |
| 54 | + |
| 55 | + |
| 56 | + for p in projectstobuild: |
| 57 | + path=p |
| 58 | + fullpath=os.path.join(clonedprojectfolder+path) |
| 59 | + #print(fullpath) |
| 60 | + if(os.path.isdir(fullpath)): |
| 61 | + currfilelog=open('path.log','a') |
| 62 | + try: |
| 63 | + #https://stackoverflow.com/questions/21377520/do-a-maven-build-in-a-python-script |
| 64 | + if(not(p.startswith('.'))): |
| 65 | + print("Procesiing " + path) |
| 66 | + opofbuild=mvnbuild(fullpath) |
| 67 | + if("BUILD SUCCESS" in opofbuild[0]): |
| 68 | + print("Build done for project " + path+"\n") |
| 69 | + logfile.write("Build done for project " + path+"\n") |
| 70 | + else: |
| 71 | + print("Build failed for project " + path+"\n") |
| 72 | + logfile.write("Build failed for project " + path+"\n") |
| 73 | + #logfile..write(op) |
| 74 | + ''' |
| 75 | + with changeDir(fullpath): |
| 76 | + # ****** NOTE ******: using shell=True is strongly discouraged since it possesses security risks |
| 77 | + #subprocess.call(["mvn", "clean", "install"], shell=True) |
| 78 | + #opofbuild=build("clean", "install") |
| 79 | + #opofbuild=build("Compile") |
| 80 | + |
| 81 | + #build("Compile") |
| 82 | + #logfile.write("Compiled project " + path+"\n") |
| 83 | + ''' |
| 84 | + |
| 85 | + except: |
| 86 | + print("Exception durin build or compile for project " + path+"\n") |
| 87 | + logfile.write("Exception durin build or compile for project " + path+"\n") |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + logfile.close( ) |
| 93 | +except: |
| 94 | + print(" Plese provide proper arguments in the form python BuildProjects <FOLDER PATH where projects are cloned <MAVEN command to run>") |
| 95 | + |
| 96 | + |
0 commit comments