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
|
#!/usr/bin/env python
# Copyright (C) 2013 Linaro Ltd.
#
# 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 argparse
import os
import re
import subprocess
# Match a directory that ends with .git, but not only .git
GIT_DIRECTORY_ENDS = re.compile(".+\.git$")
# Default path to the git repo.
DEFAULT_GIT_REPOS = "/opt/rhodecode/git_repos"
DEFAULT_USER = "rhodecode"
def args_parser():
"""Sets up the argument parser."""
parser = argparse.ArgumentParser()
parser.add_argument("--repos-dir",
default=DEFAULT_GIT_REPOS,
help="The directory where repositories are stored.")
parser.add_argument("--user",
default=DEFAULT_USER,
help="User to run the commands as.")
return parser
def update_server_info(repos_dir, user):
"""Runs command 'git update-server-info' on the repositories.
:param repos_dir: The directory where repositories are located.
:param user: The user to run the command as.
"""
for root, dirs, files in os.walk(os.path.abspath(repos_dir)):
if GIT_DIRECTORY_ENDS.match(root):
if files:
# We really are in a git repository.
cmd_args = []
if user:
cmd_args = ["sudo", "-u", user, "-H"]
cmd_args += ["git", "update-server-info"]
process = subprocess.Popen(cmd_args,
cwd=root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p_out, p_err = process.communicate()
if process.returncode != 0:
print "Error executing command on directory %s." % root
else:
# git repositories always have a HEAD file, or it means they
# are empty.
continue
else:
# Directory looks like is not a git one.
continue
if __name__ == '__main__':
parser = args_parser()
args = parser.parse_args()
update_server_info(args.repos_dir, args.user)
|