Salome HOME
Add config files and some code update for compilation of SALOME-7.8.0
[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):
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     :return: True if the extraction is successful
62     :rtype: boolean
63     '''
64     if not where.exists():
65         where.make()
66     if tag == "master" or tag == "HEAD":
67         command = "git clone %(remote)s %(where)s" % \
68                     { 'remote': from_what, 'tag': tag, 'where': str(where) }
69     else:
70         # NOTICE: this command only works with recent version of git
71         #         because --work-tree does not work with an absolute path
72         where_git = os.path.join( str(where), ".git" )
73         command = "rmdir %(where)s && git clone %(remote)s %(where)s && " + \
74                   "git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
75         command = command % {'remote': from_what, 
76                              'tag': tag, 
77                              'where': str(where), 
78                              'where_git': where_git }
79
80     logger.write(command + "\n", 5)
81
82     logger.logTxtFile.write("\n" + command + "\n")
83     logger.logTxtFile.flush()
84     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
85                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
86     return (res == 0)
87
88 def archive_extract(from_what, where, logger):
89     '''Extracts sources from an archive.
90     
91     :param from_what str: The path to the archive.
92     :param where str: The path where to extract.
93     :param logger Logger: The logger instance to use.
94     :return: True if the extraction is successful
95     :rtype: boolean
96     '''
97     try:
98         archive = tarfile.open(from_what)
99         for i in archive.getmembers():
100             archive.extract(i, path=str(where))
101         return True, os.path.commonprefix(archive.getnames())
102     except Exception as exc:
103         logger.write("archive_extract: %s\n" % exc)
104         return False, None
105
106 def cvs_extract(protocol, user, server, base, tag, product, where,
107                 logger, checkout=False):
108     '''Extracts sources from a cvs repository.
109     
110     :param protocol str: The cvs protocol.
111     :param user str: The user to be used.
112     :param server str: The remote cvs server.
113     :param base str: .
114     :param tag str: The tag.
115     :param product str: The product.
116     :param where str: The path where to extract.
117     :param logger Logger: The logger instance to use.
118     :param checkout boolean: If true use checkout cvs.
119     :return: True if the extraction is successful
120     :rtype: boolean
121     '''
122
123     opttag = ''
124     if tag is not None and len(tag) > 0:
125         opttag = '-r ' + tag
126
127     cmd = 'export'
128     if checkout:
129         cmd = 'checkout'
130     elif len(opttag) == 0:
131         opttag = '-DNOW'
132     
133     if len(protocol) > 0:
134         root = "%s@%s:%s" % (user, server, base)
135         command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
136             { 'protocol': protocol, 'root': root, 'where': str(where.base()),
137               'tag': opttag, 'product': product, 'command': cmd }
138     else:
139         command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
140             { 'root': server, 'base': base, 'where': str(where.base()),
141               'tag': opttag, 'product': product, 'command': cmd }
142
143     logger.write(command + "\n", 5)
144
145     if not where.dir().exists():
146         where.dir().make()
147
148     logger.logTxtFile.write("\n" + command + "\n")
149     logger.logTxtFile.flush()        
150     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
151                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
152     return (res == 0)
153
154 def svn_extract(user, from_what, tag, where, logger, checkout=False):
155     '''Extracts sources from a svn repository.
156     
157     :param user str: The user to be used.
158     :param from_what str: The remote git repository.
159     :param tag str: The tag.
160     :param where str: The path where to extract.
161     :param logger Logger: The logger instance to use.
162     :param checkout boolean: If true use checkout svn.
163     :return: True if the extraction is successful
164     :rtype: boolean
165     '''
166     if not where.exists():
167         where.make()
168
169     if checkout:
170         command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
171             { 'remote': from_what, 'user' : user, 'where': str(where) }
172     else:
173         command = ""
174         if os.path.exists(str(where)):
175             command = "/bin/rm -rf %(where)s && " % \
176                 { 'remote': from_what, 'where': str(where) }
177         
178         if tag == "master":
179             command += "svn export --username %(user)s %(remote)s %(where)s" % \
180                 { 'remote': from_what, 'user' : user, 'where': str(where) }       
181         else:
182             command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
183                 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
184     
185     logger.logTxtFile.write(command + "\n")
186     
187     logger.write(command + "\n", 5)
188     logger.logTxtFile.write("\n" + command + "\n")
189     logger.logTxtFile.flush()
190     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
191                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
192     return (res == 0)