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 (dir ):
27+ os .chdir (dir )
28+ process = subprocess .Popen (
29+ #"mvn clean install -DskipTests=true"
30+ "mvn clean install" ,
31+ shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE
32+ )
33+ out , err = process .communicate ()
34+ #log(out, dir)
35+ #print(out)
36+ errcode = process .returncode
37+ return (out , err , errcode )
38+
39+
40+ try :
41+ clonedprojectfolder = sys .argv [1 ]
42+ #mavenargumenr=sys.argv[2]
43+ except :
44+ print (" Plese provide proper arguments in the form python BuildProjects <FOLDE PATH where projects are cloned <MAVEN command to run>" )
45+
46+
47+ projectstobuild = os .listdir (clonedprojectfolder )
48+ logfile = open ('Build_log_' + str (datetime .datetime .now ()),'a' )
49+
50+ print ("Processing projects in folder ::" + clonedprojectfolder )
51+ projcount = len (projectstobuild )
52+ failedprojcount = 0
53+ processedprojcount = 0
54+ #print("Total projects to build "+ str(projcount))
55+ #logfile.write("Total projects to build "+ str(projcount)+"\n")
56+
57+
58+ for dir in projectstobuild :
59+ path = dir
60+ fullpath = os .path .join (clonedprojectfolder + path )
61+ #print(fullpath)
62+ if (os .path .isdir (fullpath )):
63+ currfilelog = open ('path.log' ,'a' )
64+ try :
65+ #https://stackoverflow.com/questions/21377520/do-a-maven-build-in-a-python-script
66+ if (not (dir .startswith ('.' ))):
67+ print ("Procesiing " + path )
68+ opofbuild = mvnbuild (fullpath )
69+ if ("BUILD SUCCESS" in opofbuild [0 ]):
70+ print ("Build done for project " + path + "\n " )
71+ logfile .write ("Build done for project " + path + "\n " )
72+ else :
73+ print ("Build failed for project " + path + "\n " )
74+ logfile .write ("Build failed for project " + path + "\n " )
75+ '''
76+ with changeDir(fullpath):
77+ # ****** NOTE ******: using shell=True is strongly discouraged since it possesses security risks
78+ #subprocess.call(["mvn", "clean", "install"], shell=True)
79+ #opofbuild=build("clean", "install")
80+ #opofbuild=build("Compile")
81+
82+ #build("Compile")
83+ #logfile.write("Compiled project " + path+"\n ")
84+ '''
85+
86+ except :
87+ print ("Exception durin build or compile for project " + path + "\n " )
88+ logfile .write ("Exception durin build or compile for project " + path + "\n " )
89+
90+
91+
92+
93+ logfile .close ( )
0 commit comments