Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / build / buildman / bin / bmplugins / DepManCreatePlugIn.py @ 27642

History | View | Annotate | Download (6.85 KB)

1
#depman create plugin
2

    
3
from bmbase.IPlugIn import IPlugIn
4
from bmbase.PlugInManager import PlugInManager
5
from bmcore.BMUtil import *
6
from bmcore.ftpslib import *
7
from xml.dom import minidom
8
import string
9
import shutil
10
import os
11
import sys
12
import subprocess
13

    
14
class DepManCreatePlugIn(IPlugIn):
15
        
16
        def __init__(self):
17
                IPlugIn.__init__(self)
18
                
19
                self._path="."
20
                self._upload=False
21
                self._is_Interactive=False
22
                self._dependency = None
23
                
24
                self._default_ftp="downloads.gvsig.org"
25
                self._default_login="maven"
26
                self._default_destdir = "/anon/pub/gvSIG-desktop/buildman-repository"
27
                
28
                
29
        def init(self):
30
                self.addGoal("create", "Create an artifact")
31
                self.addGoalOption("create","--path", "Specifies source directory")
32
                self.addGoalOption("create","--upload", "Whenever the artifact must be uploaded or not")
33
                self.addGoalOption("create","--interactive", "ask for server or artifact deployment configuration")
34
                self.addPreGoal("create", "depman")        
35
                self._dmplugin = PlugInManager().getPlugInInstance("DepManPlugIn")
36

    
37
                if self._dmplugin == None:
38
                        self.reportError("PlugIn `depman` not found")
39
                        return False
40

    
41
        
42
                if self._arguments.read("create"):
43
                        self.setExecute(True)
44
                
45
                args=[""]
46
                if self._arguments.read("--path",args):
47
                        self._path=args[0]
48
                
49
                if self._arguments.read("--upload"):
50
                        self._upload=True        
51

    
52
                if self._arguments.read("--interactive"):
53
                        self._is_Interactive=True        
54

    
55

    
56
        def initFromXML(self,node):
57
                for n in node.childNodes:
58
                        if n.localName=="package":
59
                                processPackage = True
60
                                if n.hasAttributes():
61
                                        if n.attributes.has_key("platform"):
62
                                                values = n.attributes.get("platform").value.split(",")
63
                                                if self._dmplugin.getOSPlatform() in values:
64
                                                        processPackage = True
65
                                                else:
66
                                                        processPackage = False
67
                                if processPackage:
68
                                        print "Processing for platform..."
69
                                        self._dependency = DepManDependency(self._dmplugin)
70
                                        for p in n.childNodes:
71
                                                if p.localName=="group":
72
                                                        self._dependency.group=p.childNodes[0].nodeValue
73
                                                if p.localName=="artifact":
74
                                                        self._dependency.artifact=p.childNodes[0].nodeValue
75
                                                if p.localName=="version":
76
                                                        self._dependency.version=p.childNodes[0].nodeValue
77
                                                if p.localName=="platform":
78
                                                        self._dependency.platform=p.childNodes[0].nodeValue
79
                                                if p.localName=="distribution":
80
                                                        self._dependency.distribution=p.childNodes[0].nodeValue
81
                                                if p.localName=="compiler":
82
                                                        self._dependency.compiler=p.childNodes[0].nodeValue
83
                                                if p.localName=="arch":
84
                                                        self._dependency.arch=p.childNodes[0].nodeValue
85
                                                if p.localName=="libraryType":
86
                                                        self._dependency.libraryType=p.childNodes[0].nodeValue
87
                                        
88
                                        print "Creating Artifact:" + str(self._dependency)        
89
                                                #if p.localName =="upload":
90
                                                        #TODO: Maybe upload should be an external plugin
91
                                                #        for k in p.childNodes:
92
                                                #                if k.localName == "sshserver":
93
                                                #                        self._default_ssh = k.childNodes[0].nodeValue
94
                                                #                if k.localName == "destdir":
95
                                                #                        self._default_destdir = k.childNodes[0].nodeValue
96
                                                #                if k.localName == "username":
97
                                                #                        self._default_login = k.childNodes[0].nodeValue
98
        
99

    
100
                
101
        def execute(self):
102
                print "Executing Plugin:" + str(self.__class__)
103
                return self.create()
104
        
105
        def create(self):
106
                   self.initFromXML(self._dmplugin.getNode())        
107
                #user questions
108
                if self._dependency==None:
109
                        self._dependency = DepManDependency(self._dmplugin)
110
                        self._is_Interactive = True
111
                        self._dependency.raw_input()
112
                                        
113
                        upload_response = raw_input("Upload to server? (y/n): ")
114
                        if upload_response == "y" or upload_response == "yes":
115
                                self._upload=True
116
        
117
                if not self._dmplugin.validateDependency(self._dependency):
118
                        return False
119
                                
120

    
121
                #artifact and md5 generation
122
                file_name=self._dependency.getDepManFileName()
123
                file_path=self._dependency.getDepManUrlPath()
124

    
125
                tarname=file_name+".tar.gz"
126
                md5name=tarname+".md5"
127
                dmfile=file_name+".xml"
128
                
129
                dmutil = BMUtil()
130
                
131
                tmpdir=self._dmplugin.getDepManPath()+os.path.sep+".cache"+os.path.sep+file_path
132
                dmutil.mkdir(tmpdir)
133
                print tmpdir+os.path.sep+tarname,self._path
134
                dmutil.targz(os.path.join(tmpdir,tarname),self._path)
135
                #print "targz ",tmpdir+os.path.sep+tarname
136
                dmutil.createMD5(tmpdir+os.path.sep+tarname,tmpdir+os.path.sep+md5name)
137

    
138
                if not self._is_Interactive:
139
                        shutil.copyfile(self._dmplugin.getXMLFile(),tmpdir+os.path.sep+dmfile)        
140

    
141
                print "Artifact " + tarname + " created in:\n" + tmpdir
142
                
143
                if self._upload:
144
                        #dmutil.rmdir(".dmn_tmp") # Prevent for uploading bad previous compilations!
145
                        #sshtmpdir=".dmn_tmp"+os.path.sep+file_path
146
                        #dmutil.mkdir(sshtmpdir)
147
                        #shutil.copyfile(tmpdir+os.path.sep+tarname,sshtmpdir+os.path.sep+tarname)
148
                        #shutil.copyfile(tmpdir+os.path.sep+md5name,sshtmpdir+os.path.sep+md5name)
149
                        #if self._xmlfile != "":
150
                        #        shutil.copyfile(tmpdir+os.path.sep+dmfile,sshtmpdir+os.path.sep+dmfile)        
151
                        url = self._default_ftp;
152
                        destdir = self._default_destdir
153
                        login = self._default_login
154
                        pwd = ""                        
155
                        if self._is_Interactive:
156
                                tmstr="FTP Server [*]:"
157
                                url=raw_input(string.replace(tmstr,"*",self._default_ftp))
158
                                if len(url)==0:
159
                                        url=self._default_ftp
160
                                
161
                                tmstr="Destination Directory [*]:"
162
                                destdir=raw_input(string.replace(tmstr,"*",self._default_destdir))
163
                                if len(destdir)==0:
164
                                        destdir=self._default_destdir
165
                        
166
                                tmstr="Login [*]:"
167
                                login=raw_input(string.replace(tmstr,"*",self._default_login))
168
                                if len(login)==0:
169
                                        login=self._default_login
170
                
171
                                import getpass
172
                                pwd = getpass.getpass()
173
                        
174
                        print "* Uploading ",tarname
175
                        print "* Uploading ",md5name
176
                        print "* Uploading ",dmfile
177
                        
178
                        #scp
179
                        '''
180
                        base_ssh=destdir
181
                        url_ssh=url
182
                        scpcommand = "scp"
183
                        pscppath = ""
184
                        if sys.platform == "win32":
185
                                scpcommand=self._dmplugin.getDepManDataPath()+os.path.sep+"win32"+os.path.sep+"pscp.exe"
186
                                scpcommand  = '"'+os.path.normpath(scpcommand)+'"'
187
                        cmdstr=scpcommand +" -r "+".dmn_tmp"+os.path.sep+"*"+" "+login+"@"+url_ssh+":"+base_ssh
188
                        
189
                        print cmdstr
190
                        os.system(cmdstr)
191
                        
192
                        #scp
193
                        '''
194
                        pwd = "*****"
195
                        #ftp
196
                        self.uploadFTPFile(url,login,pwd,destdir,os.path.sep+file_path,tmpdir+os.path.sep+tarname,tarname)
197
                        self.uploadFTPFile(url,login,pwd,destdir,os.path.sep+file_path,tmpdir+os.path.sep+md5name,md5name)
198
                        if not self._is_Interactive:
199
                                self.uploadFTPFile(url,login,pwd,destdir,os.path.sep+file_path,tmpdir+os.path.sep+dmfile,dmfile)
200

    
201
                        #dmutil.rmdir(".dmn_tmp")
202
                        
203
        def uploadFTPFile(self, url, user, pwd, destdir, path, orig_file, filename):
204
                ftp = FTP_TLS(url)
205
                ftp.set_debuglevel( 1 )
206
                ftp.auth_tls();  ftp.prot_p()
207
                ftp.login(user,pwd)
208
                ftp.set_pasv( 1 )
209
                #ftp.retrlines('LIST')
210
                ftp.cwd(destdir)
211
                dirs = string.split(path,os.path.sep)
212
                for dir in dirs:
213
                        print dir
214
                        try:
215
                                ftp.mkd(dir)
216
                        except:
217
                                pass
218
                        try:
219
                                ftp.cwd(dir)
220
                        except:
221
                                pass
222
                f = open(orig_file,"rb")
223
                ftp.storbinary("STOR "+filename, f)
224
                f.close()
225
                ftp.quit()
226

    
227

    
228

    
229

    
230

    
231

    
232

    
233

    
234

    
235
PlugInManager().registerPlugIn("DepManCreatePlugIn",DepManCreatePlugIn())