Salome HOME
sat source: Adding the application environment when getting sources of a product
[tools/sat.git] / src / system.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 '''
20 In this file : all functions that do a system call, 
21 like open a browser or an editor, or call a git command
22 '''
23
24 import subprocess
25 import os
26 import tarfile
27
28 from . import printcolors
29
30 def show_in_editor(editor, filePath, logger):
31     '''open filePath using editor.
32     
33     :param editor str: The editor to use.
34     :param filePath str: The path to the file to open.
35     '''
36     # default editor is vi
37     if editor is None or len(editor) == 0:
38         editor = 'vi'
39     
40     if '%s' not in editor:
41         editor += ' %s'
42
43     try:
44         # launch cmd using subprocess.Popen
45         cmd = editor % filePath
46         logger.write('Launched command:\n' + cmd + '\n', 5)
47         p = subprocess.Popen(cmd, shell=True)
48         p.communicate()
49     except:
50         logger.write(printcolors.printcError(_("Unable to edit file %s\n") 
51                                              % filePath), 1)
52
53
54 def git_extract(from_what, tag, where, logger, environment=None):
55     '''Extracts sources from a git repository.
56     
57     :param from_what str: The remote git repository.
58     :param tag str: The tag.
59     :param where str: The path where to extract.
60     :param logger Logger: The logger instance to use.
61     :param environment src.environment.Environ: The environment to source when
62                                                 extracting.
63     :return: True if the extraction is successful
64     :rtype: boolean
65     '''
66     if not where.exists():
67         where.make()
68     if tag == "master" or tag == "HEAD":
69         command = "git clone %(remote)s %(where)s" % \
70                     { 'remote': from_what, 'tag': tag, 'where': str(where) }
71     else:
72         # NOTICE: this command only works with recent version of git
73         #         because --work-tree does not work with an absolute path
74         where_git = os.path.join( str(where), ".git" )
75         command = "rmdir %(where)s && git clone %(remote)s %(where)s && " + \
76                   "git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
77         command = command % {'remote': from_what, 
78                              'tag': tag, 
79                              'where': str(where), 
80                              'where_git': where_git }
81
82     logger.write(command + "\n", 5)
83
84     logger.logTxtFile.write("\n" + command + "\n")
85     logger.logTxtFile.flush()
86     res = subprocess.call(command,
87                           cwd=str(where.dir()),
88                           env=environment.environ.environ,
89                           shell=True,
90                           stdout=logger.logTxtFile,
91                           stderr=subprocess.STDOUT)
92     return (res == 0)
93
94 def archive_extract(from_what, where, logger):
95     '''Extracts sources from an archive.
96     
97     :param from_what str: The path to the archive.
98     :param where str: The path where to extract.
99     :param logger Logger: The logger instance to use.
100     :return: True if the extraction is successful
101     :rtype: boolean
102     '''
103     try:
104         archive = tarfile.open(from_what)
105         for i in archive.getmembers():
106             archive.extract(i, path=str(where))
107         return True, os.path.commonprefix(archive.getnames())
108     except Exception as exc:
109         logger.write("archive_extract: %s\n" % exc)
110         return False, None
111
112 def cvs_extract(protocol, user, server, base, tag, product, where,
113                 logger, checkout=False, environment=None):
114     '''Extracts sources from a cvs repository.
115     
116     :param protocol str: The cvs protocol.
117     :param user str: The user to be used.
118     :param server str: The remote cvs server.
119     :param base str: .
120     :param tag str: The tag.
121     :param product str: The product.
122     :param where str: The path where to extract.
123     :param logger Logger: The logger instance to use.
124     :param checkout boolean: If true use checkout cvs.
125     :param environment src.environment.Environ: The environment to source when
126                                                 extracting.
127     :return: True if the extraction is successful
128     :rtype: boolean
129     '''
130
131     opttag = ''
132     if tag is not None and len(tag) > 0:
133         opttag = '-r ' + tag
134
135     cmd = 'export'
136     if checkout:
137         cmd = 'checkout'
138     elif len(opttag) == 0:
139         opttag = '-DNOW'
140     
141     if len(protocol) > 0:
142         root = "%s@%s:%s" % (user, server, base)
143         command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
144             { 'protocol': protocol, 'root': root, 'where': str(where.base()),
145               'tag': opttag, 'product': product, 'command': cmd }
146     else:
147         command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
148             { 'root': server, 'base': base, 'where': str(where.base()),
149               'tag': opttag, 'product': product, 'command': cmd }
150
151     logger.write(command + "\n", 5)
152
153     if not where.dir().exists():
154         where.dir().make()
155
156     logger.logTxtFile.write("\n" + command + "\n")
157     logger.logTxtFile.flush()        
158     res = subprocess.call(command,
159                           cwd=str(where.dir()),
160                           env=environment.environ.environ,
161                           shell=True,
162                           stdout=logger.logTxtFile,
163                           stderr=subprocess.STDOUT)
164     return (res == 0)
165
166 def svn_extract(user,
167                 from_what,
168                 tag,
169                 where,
170                 logger,
171                 checkout=False,
172                 environment=None):
173     '''Extracts sources from a svn repository.
174     
175     :param user str: The user to be used.
176     :param from_what str: The remote git repository.
177     :param tag str: The tag.
178     :param where str: The path where to extract.
179     :param logger Logger: The logger instance to use.
180     :param checkout boolean: If true use checkout svn.
181     :param environment src.environment.Environ: The environment to source when
182                                                 extracting.
183     :return: True if the extraction is successful
184     :rtype: boolean
185     '''
186     if not where.exists():
187         where.make()
188
189     if checkout:
190         command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
191             { 'remote': from_what, 'user' : user, 'where': str(where) }
192     else:
193         command = ""
194         if os.path.exists(str(where)):
195             command = "/bin/rm -rf %(where)s && " % \
196                 { 'remote': from_what, 'where': str(where) }
197         
198         if tag == "master":
199             command += "svn export --username %(user)s %(remote)s %(where)s" % \
200                 { 'remote': from_what, 'user' : user, 'where': str(where) }       
201         else:
202             command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
203                 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
204     
205     logger.logTxtFile.write(command + "\n")
206     
207     logger.write(command + "\n", 5)
208     logger.logTxtFile.write("\n" + command + "\n")
209     logger.logTxtFile.flush()
210     res = subprocess.call(command,
211                           cwd=str(where.dir()),
212                           env=environment.environ.environ,
213                           shell=True,
214                           stdout=logger.logTxtFile,
215                           stderr=subprocess.STDOUT)
216     return (res == 0)