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