2 # -*- coding: utf-8 -*-
7 Copyright (c) 2012 Frédéric Massart - FMCorz.net
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 http://github.com/FMCorz/mdk
36 from .config
import Conf
42 i
= raw_input('%s (y/n) ' %
(q
)).strip().lower()
49 def question(q
, default
=None, options
=None, password
=False):
50 """Asks the user a question, and return the answer"""
53 text
= text
+ ' [%s]' % str
(default
)
56 i
= getpass
.getpass('%s\n ' % text
)
58 i
= raw_input('%s\n ' % text
)
63 if options
!= None and i
not in options
:
64 return question(q
, default
, options
)
68 def chmodRecursive(path
, chmod
):
70 for (dirpath
, dirnames
, filenames
) in os
.walk(path
):
72 dir = os
.path
.join(dirpath
, d
)
75 file = os
.path
.join(dirpath
, f
)
79 def getMDLFromCommitMessage(message
):
80 """Return the MDL-12345 number from a commit message"""
82 match
= re
.match(r
'MDL(-|_)([0-9]+)', message
, re
.I
)
84 mdl
= 'MDL-%s' %
(match
.group(2))
88 def get_current_user():
89 """Attempt to get the currently logged in user"""
92 username
= os
.getlogin()
96 username
= getpass
.getuser()
102 def launchEditor(filepath
=None, suffix
='.tmp'):
103 """Launchs up an editor
105 If filepath is passed, the content of the file is used to populate the editor.
107 This returns the path to the saved file.
109 editor
= resolveEditor()
111 raise Exception('Could not locate the editor')
112 with tempfile
.NamedTemporaryFile(suffix
=suffix
, delete
=False) as tmpfile
:
113 with
open(filepath
, 'r') as f
:
114 tmpfile
.write(f
.read())
116 subprocess
.call([editor
, tmpfile
.name
])
120 def getText(suffix
='.md', currenttext
=None):
122 with tempfile
.NamedTemporaryFile(suffix
=suffix
, delete
=False) as tmpfile
:
124 tmpfile
.write(currenttext
)
127 tmpfile
= launchEditor(suffix
=suffix
, filepath
=tmpfile
.name
)
129 with
open(tmpfile
, 'r') as f
:
134 logging
.error('I could not detect any file content. Did you save properly?')
135 if yesOrNo('Would you like to continue editing? If not the changes will be discarded.'):
142 def md5file(filepath
):
143 """Return the md5 sum of a file
144 This is terribly memory inefficient!"""
145 return hashlib
.md5(open(filepath
).read()).hexdigest()
148 def mkdir(path
, perms
=0755):
149 """Creates a directory ignoring the OS umask"""
150 oldumask
= os
.umask(0000)
151 os
.mkdir(path
, perms
)
155 def parseBranch(branch
):
156 pattern
= re
.compile(C
.get('wording.branchRegex'), flags
=re
.I
)
157 result
= pattern
.search(branch
)
162 'issue': result
.group(pattern
.groupindex
['issue']),
163 'version': result
.group(pattern
.groupindex
['version'])
166 parsed
['suffix'] = result
.group(pattern
.groupindex
['suffix'])
168 parsed
['suffix'] = None
172 def process(cmd
, cwd
=None, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
):
173 if type(cmd
) != list:
174 cmd
= shlex
.split(str(cmd
))
175 logging
.debug(' '.join(cmd
))
177 proc
= subprocess
.Popen(cmd
, cwd
=cwd
, stdout
=stdout
, stderr
=stderr
)
178 (out
, err
) = proc
.communicate()
179 except KeyboardInterrupt as e
:
182 return (proc
.returncode
, out
, err
)
186 """Try to resolve the editor that the user would want to use.
187 This does actually checks if it is executable"""
188 editor
= C
.get('editor')
190 editor
= os
.environ
.get('EDITOR')
192 editor
= os
.environ
.get('VISUAL')
193 if not editor
and os
.path
.isfile('/usr/bin/editor'):
194 editor
= '/usr/bin/editor'
198 def downloadProcessHook(count
, size
, total
):
199 """Hook to report the downloading a file using urllib.urlretrieve"""
202 downloaded
= int((count
* size
) / (1024))
203 total
= int(total
/ (1024)) if total
!= 0 else '?'
204 if downloaded
> total
:
206 sys
.stderr
.write("\r %sKB / %sKB" %
(downloaded
, total
))
210 def stableBranch(version
):
211 if version
== 'master':
213 return 'MOODLE_%d_STABLE' % int
(version
)
216 class ProcessInThread(threading
.Thread
):
217 """Executes a process in a separate thread"""
226 def __init__(self
, cmd
, cwd
=None, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
):
227 threading
.Thread
.__init__(self
)
228 if type(cmd
) != 'list':
229 cmd
= shlex
.split(str(cmd
))
236 os
.kill(self
._pid
, signal
.SIGKILL
)
239 logging
.debug(' '.join(self
.cmd
))
240 proc
= subprocess
.Popen(self
.cmd
, cwd
=self
.cwd
, stdout
=self
.stdout
, stderr
=self
.stderr
)
246 # Reading the output seems to prevent the process to hang.
247 if self
.stdout
== subprocess
.PIPE
: