2 # -*- coding: utf-8 -*-
7 Copyright (c) 2013 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
28 from datetime
import datetime
29 from .. import tools
, jira
, fetch
30 from ..command
import Command
31 from ..tools
import question
34 class PullCommand(Command
):
38 ['-i', '--integration'],
40 'action': 'store_true',
41 'help': 'checkout the stable branch before proceeding to the pull. Short for --mode integration.'
47 'action': 'store_true',
49 'help': 'checkout the remote branch without merging. Short for --mode checkout.'
55 'action': 'store_true',
57 'help': 'only fetches the remote branch, you can then use FETCH_HEAD. Short for --mode fetch.'
63 'action': 'store_true',
64 'help': 'checkout a testing branch before proceeding to the pull. Short for --mode testing.'
71 'choices': ['checkout', 'fetch', 'integration', 'pull', 'testing'],
73 'help': 'define the mode to use'
79 'action': 'store_true',
80 'help': 'prompts the user to choose the patch to download.'
87 'help': 'tracker issue to pull from (MDL-12345, 12345). If not specified, read from current branch.',
93 _description
= 'Pull a branch from a tracker issue'
99 raise Exception('This is not a Moodle instance')
109 elif args
.integration
:
115 # Tracker issue number.
118 parsedbranch
= tools
.parseBranch(M
.currentBranch())
120 raise Exception('Could not extract issue number from %s' % M
.currentBranch())
121 issuenb
= parsedbranch
['issue']
123 issue
= re
.sub(r
'(MDL|mdl)(-|_)?', '', issuenb
)
126 # Reading the information about the current instance.
127 branch
= M
.get('branch')
129 # Get information from Tracker
130 logging
.info('Retrieving information about %s from Moodle Tracker' %
(mdl
))
131 fetcher
= fetch
.FetchTracker(M
)
135 fetcher
.setFromTracker(mdl
, branch
)
136 except (fetch
.FetchTrackerRepoException
, fetch
.FetchTrackerBranchException
) as e
:
140 patches
= self
.pickPatches(mdl
)
142 raise Exception('Could not find any relevant information for a successful pull')
143 fetcher
.usePatches(patches
)
147 elif mode
== 'checkout':
149 elif mode
== 'fetch':
151 elif mode
== 'integration':
152 fetcher
.pull(into
=M
.get('stablebranch'))
153 elif mode
== 'testing':
157 suffix
= 'test' if i
<= 1 else 'test' + str(i
)
158 newBranch
= M
.generateBranchName(issue
, suffix
=suffix
, version
=branch
)
159 if not M
.git().hasBranch(newBranch
):
161 fetcher
.pull(into
=newBranch
, track
=M
.get('stablebranch'))
163 def pickPatches(self
, mdl
):
164 """Prompts the user to pick a patch"""
167 patches
= J
.getAttachments(mdl
)
168 patches
= {k
: v
for k
, v
in patches
.items() if v
.get('filename').endswith('.patch')}
176 for key
in sorted(patches
.keys()):
179 print '{0:<2}: {1:<60} {2}'.format(i
, key
[:60], datetime
.strftime(patch
.get('date'), '%Y-%m-%d %H:%M'))
184 ids
= question('What patches would you like to apply?')
185 if ids
.lower() == 'ankit':
186 logging
.warning('Sorry, I am unable to punch a child at the moment...')
189 ids
= re
.split(r
'\s*[, ]\s*', ids
)
190 toApply
= [mapping
[int(i
)] for i
in ids
if int(i
) in mapping
.keys()]
192 logging
.warning('Error while parsing the list of patches, try a little harder.')