Salome HOME
write the called command to the log file
[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 # Extracts sources from a git repository.
55 def git_extract(from_what, tag, where, logger):
56     if not where.exists():
57         where.make()
58     if tag == "master" or tag == "HEAD":
59         command = "git clone %(remote)s %(where)s" % \
60                     { 'remote': from_what, 'tag': tag, 'where': str(where) }
61     else:
62         # NOTICE: this command only works with recent version of git
63         #         because --work-tree does not work with an absolute path
64         where_git = os.path.join( str(where), ".git" )
65         command = "rmdir %(where)s && git clone %(remote)s %(where)s && " + \
66                   "git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
67         command = command % { 'remote': from_what, 'tag': tag, 'where': str(where), 'where_git': where_git }
68
69     logger.write(command + "\n", 5)
70
71     logger.logTxtFile.write("\n" + command + "\n")
72     logger.logTxtFile.flush()
73     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
74                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
75     return (res == 0)
76
77 def archive_extract(from_what, where, logger):
78     try:
79         archive = tarfile.open(from_what)
80         for i in archive.getmembers():
81             archive.extract(i, path=str(where))
82         return True, os.path.commonprefix(archive.getnames())
83     except Exception as exc:
84         logger.write("archive_extract: %s\n" % exc)
85         return False, None
86
87 def cvs_extract(protocol, user, server, base, tag, product, where,
88                 logger, checkout=False):
89
90     opttag = ''
91     if tag is not None and len(tag) > 0:
92         opttag = '-r ' + tag
93
94     cmd = 'export'
95     if checkout:
96         cmd = 'checkout'
97     elif len(opttag) == 0:
98         opttag = '-DNOW'
99     
100     if len(protocol) > 0:
101         root = "%s@%s:%s" % (user, server, base)
102         command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
103             { 'protocol': protocol, 'root': root, 'where': str(where.base()),
104               'tag': opttag, 'product': product, 'command': cmd }
105     else:
106         command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
107             { 'root': server, 'base': base, 'where': str(where.base()),
108               'tag': opttag, 'product': product, 'command': cmd }
109
110     logger.write(command + "\n", 5)
111
112     if not where.dir().exists():
113         where.dir().make()
114
115     logger.logTxtFile.write("\n" + command + "\n")
116     logger.logTxtFile.flush()        
117     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
118                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
119     return (res == 0)
120
121 def svn_extract(user, from_what, tag, where, logger, checkout=False):
122     if not where.exists():
123         where.make()
124
125     if checkout:
126         command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
127             { 'remote': from_what, 'user' : user, 'where': str(where) }
128     else:
129         command = ""
130         if os.path.exists(str(where)):
131             command = "/bin/rm -rf %(where)s && " % \
132                 { 'remote': from_what, 'where': str(where) }
133         
134         if tag == "master":
135             command += "svn export --username %(user)s %(remote)s %(where)s" % \
136                 { 'remote': from_what, 'user' : user, 'where': str(where) }       
137         else:
138             command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
139                 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
140     
141     logger.logTxtFile.write(command + "\n")
142     
143     logger.write(command + "\n", 5)
144     logger.logTxtFile.write("\n" + command + "\n")
145     logger.logTxtFile.flush()
146     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
147                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
148     return (res == 0)