Salome HOME
Finalization of source command with commentaries
[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     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
72                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
73     return (res == 0)
74
75 def archive_extract(from_what, where, logger):
76     try:
77         archive = tarfile.open(from_what)
78         for i in archive.getmembers():
79             archive.extract(i, path=str(where))
80         return True, os.path.commonprefix(archive.getnames())
81     except Exception as exc:
82         logger.write("archive_extract: %s\n" % exc)
83         return False, None
84
85 def cvs_extract(protocol, user, server, base, tag, module, where,
86                 logger, checkout=False):
87
88     opttag = ''
89     if tag is not None and len(tag) > 0:
90         opttag = '-r ' + tag
91
92     cmd = 'export'
93     if checkout:
94         cmd = 'checkout'
95     elif len(opttag) == 0:
96         opttag = '-DNOW'
97     
98     if len(protocol) > 0:
99         root = "%s@%s:%s" % (user, server, base)
100         command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(module)s" % \
101             { 'protocol': protocol, 'root': root, 'where': str(where.base()),
102               'tag': opttag, 'module': module, 'command': cmd }
103     else:
104         command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(module)s" % \
105             { 'root': server, 'base': base, 'where': str(where.base()),
106               'tag': opttag, 'module': module, 'command': cmd }
107
108     logger.logTxtFile.write(command + "\n")
109     logger.write(command + "\n", 5)
110
111     if not where.dir().exists():
112         where.dir().make()
113         
114     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
115                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
116     return (res == 0)
117
118 def svn_extract(user, from_what, tag, where, logger, checkout=False):
119     if not where.exists():
120         where.make()
121
122     if checkout:
123         command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
124             { 'remote': from_what, 'user' : user, 'where': str(where) }
125     else:
126         command = ""
127         if os.path.exists(str(where)):
128             command = "/bin/rm -rf %(where)s && " % \
129                 { 'remote': from_what, 'where': str(where) }
130         
131         if tag == "master":
132             command += "svn export --username %(user)s %(remote)s %(where)s" % \
133                 { 'remote': from_what, 'user' : user, 'where': str(where) }       
134         else:
135             command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
136                 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
137     
138     logger.logTxtFile.write(command + "\n")
139     
140     logger.write(command + "\n", 5)
141     res = subprocess.call(command, cwd=str(where.dir()), shell=True,
142                           stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
143     return (res == 0)