/usr/bin/grok-dumb-pull is in grokmirror 0.3.5-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | #! /usr/bin/python -tt
# Copyright (C) 2013 by The Linux Foundation and contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import grokmirror
import logging
import time
import fnmatch
import subprocess
import shutil
import calendar
from fcntl import lockf, LOCK_EX, LOCK_UN, LOCK_NB
from git import Repo
logger = logging.getLogger(__name__)
def git_remote_update(args, env):
logger.debug('Running: GIT_DIR=%s %s' % (env['GIT_DIR'], ' '.join(args)))
(output, error) = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env).communicate()
error = error.strip()
if error:
# Put things we recognize into debug
debug = []
warn = []
for line in error.split('\n'):
if line.find('From ') == 0:
debug.append(line)
elif line.find('-> ') > 0:
debug.append(line)
else:
warn.append(line)
if debug:
logger.debug('Stderr: %s' % '\n'.join(debug))
if warn:
logger.warning('Stderr: %s' % '\n'.join(warn))
def dumb_pull_repo(gitdir, remotes, svn=False):
# verify it's a git repo and fetch all remotes
logger.debug('Will pull %s with following remotes: %s' % (gitdir, remotes))
try:
repo = Repo(gitdir)
assert repo.bare == True
except:
logger.critical('Error opening %s.' % gitdir)
logger.critical('Make sure it is a bare git repository.')
sys.exit(1)
try:
grokmirror.lock_repo(gitdir, nonblocking=True)
except IOError, ex:
logger.info('Could not obtain exclusive lock on %s' % gitdir)
logger.info('\tAssuming another process is running.')
return False
env = {'GIT_DIR': gitdir}
if svn:
logger.debug('Using git-svn for %s' % gitdir)
for remote in remotes:
# arghie-argh-argh
if remote == '*':
remote = '--all'
logger.info('Running git-svn fetch %s in %s' % (remote, gitdir))
args = ['/usr/bin/git', 'svn', 'fetch', remote]
git_remote_update(args, env)
grokmirror.unlock_repo(gitdir)
return True
# Not an svn remote
hasremotes = repo.git.remote()
if not len(hasremotes.strip()):
logger.info('Repository %s has no defined remotes!' % gitdir)
return False
didwork = False
logger.debug('existing remotes: %s' % hasremotes)
for remote in remotes:
remotefound = False
for hasremote in hasremotes.split('\n'):
if fnmatch.fnmatch(hasremote, remote):
remotefound = True
logger.debug('existing remote %s matches requested %s' % (
hasremote, remote))
args = ['/usr/bin/git', 'remote', 'update', hasremote]
logger.info('Updating remote %s in %s' % (hasremote, gitdir))
git_remote_update(args, env)
didwork = True
if not remotefound:
logger.info('Could not find any remotes matching %s in %s' % (
remote, gitdir))
grokmirror.unlock_repo(gitdir)
return didwork
def run_post_update_hook(hookscript, gitdir):
if hookscript == '':
return
if not os.access(hookscript, os.X_OK):
logger.warning('post_update_hook %s is not executable' % hookscript)
return
args = [hookscript, gitdir]
logger.debug('Running: %s' % ' '.join(args))
(output, error) = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
error = error.strip()
output = output.strip()
if error:
# Put hook stderror into warning
logger.warning('Hook Stderr: %s' % error)
if output:
# Put hook stdout into info
logger.info('Hook Stdout: %s' % output)
if __name__ == '__main__':
from optparse import OptionParser
usage = '''usage: %prog [options] /path/to/repos
Bluntly fetch remotes in specified git repositories.
'''
parser = OptionParser(usage=usage, version=grokmirror.VERSION)
parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
default=False,
help='Be verbose and tell us what you are doing')
parser.add_option('-s', '--svn', dest='svn', action='store_true',
default=False,
help='The remotes for these repositories are Subversion')
parser.add_option('-r', '--remote-names', dest='remotes', action='append',
default=[],
help='Only fetch remotes matching this name (accepts globbing, '
'can be passed multiple times)')
parser.add_option('-u', '--post-update-hook', dest='posthook',
default='',
help='Run this hook after each repository is updated. Passes '
'full path to the repository as the sole argument.')
parser.add_option('-l', '--logfile', dest='logfile',
default=None,
help='Put debug logs into this file')
(opts, args) = parser.parse_args()
if not len(args):
parser.error('You must provide at least a path to the repos to pull')
if not len(opts.remotes):
opts.remotes = ['*']
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
if opts.verbose:
ch.setLevel(logging.INFO)
else:
ch.setLevel(logging.CRITICAL)
logger.addHandler(ch)
if opts.logfile is not None:
ch = logging.FileHandler(opts.logfile)
formatter = logging.Formatter("[%(process)d] %(asctime)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
# push our logger into grokmirror to override the default
grokmirror.logger = logger
# Find all repositories we are to pull
for entry in args:
if entry[-4:] == '.git':
if not os.path.exists(entry):
logger.critical('%s does not exist' % entry)
continue
logger.debug('Found %s' % entry)
didwork = dumb_pull_repo(entry, opts.remotes, svn=opts.svn)
if didwork:
run_post_update_hook(opts.posthook, entry)
else:
logger.debug('Finding all git repos in %s' % entry)
for founddir in grokmirror.find_all_gitdirs(entry):
didwork = dumb_pull_repo(founddir, opts.remotes, svn=opts.svn)
if didwork:
run_post_update_hook(opts.posthook, founddir)
|