annotate september.py @ 0:ee98303e24b8

Initial commit.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Mar 2015 23:33:07 -0800
parents
children 6bbebb01f614
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import re
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import sys
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import json
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import os.path
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 import logging
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 import argparse
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 import subprocess
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 import configparser
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 from urllib.parse import urlparse
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 logger = logging.getLogger(__name__)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 class IRepo(object):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 def clone(self, repo_url, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 raise NotImplementedError()
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 def pull(self, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 raise NotImplementedError()
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 def getTags(self, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 raise NotImplementedError()
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 def update(self, repo_path, rev_id):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 raise NotImplementedError()
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 class GitRepo(object):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 def clone(self, repo_url, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 subprocess.check_call(['git', 'clone', repo_url, repo_path])
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 def pull(self, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 subprocess.check_call(['git', 'pull', 'origin', 'master'])
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 def getTags(self, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 output = subprocess.check_output(['git', 'show-ref', '--tags'])
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 pat = re.compile(r'^(?P<id>[0-9a-f]+) (?P<tag>.+)$')
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 for line in output.split('\n'):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 m = pat.match(line)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 if m:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 yield (m.group('tag'), m.group('id'))
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 def update(self, repo_path, rev_id):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 rev_id = rev_id or 'master'
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 subprocess.check_call(['git', 'checkout', rev_id])
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 class MercurialRepo(object):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 def clone(self, repo_url, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 subprocess.check_call(['hg', 'clone', repo_url, repo_path])
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 def pull(self, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 subprocess.check_call(['hg', 'pull'],
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 stderr=subprocess.STDOUT)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 def getTags(self, repo_path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 output = subprocess.check_output(
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 'hg log -r "tag()" --template "{tags} {node}\\n"',
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 stderr=subprocess.STDOUT,
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 universal_newlines=True,
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 shell=True)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 pat = re.compile(r'^(?P<tag>.+) (?P<id>[0-9a-f]+)$')
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 for line in output.split('\n'):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 m = pat.match(line)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 if m:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 yield (m.group('tag'), m.group('id'))
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 def update(self, repo_path, rev_id):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 rev_id = rev_id or 'default'
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 subprocess.check_call(['hg', 'update', rev_id],
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 stderr=subprocess.STDOUT)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 repo_class = {
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 'git': GitRepo,
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 'hg': MercurialRepo}
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 def guess_repo_type(repo):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 # Parse repo as an URL: scheme://netloc/path;parameters?query#fragment
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 scheme, netloc, path, params, query, fragment = urlparse(repo)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 if scheme == 'ssh':
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 if netloc.startswith('git@'):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 return 'git'
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 if netloc.startswith('hg@'):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 return 'hg'
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 elif scheme == 'https':
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 if path.endswith('.git'):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 return 'git'
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 elif scheme == '' and netloc == '' and os.path.isdir(path):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 if os.path.isdir(os.path.join(path, '.git')):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 return 'git'
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 if os.path.isdir(os.path.join(path, '.hg')):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 return 'hg'
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 return None
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 def main():
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 # Setup the argument parser.
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 parser = argparse.ArgumentParser(
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 prog='september',
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 description=("An utility that goes back in time and does "
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 "something in the background."))
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 parser.add_argument(
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 'repo',
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 help="The repository to observe and process")
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 parser.add_argument(
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 '-t', '--tmp-dir',
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 help="The temporary directory in which to clone the repository.")
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 parser.add_argument(
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 '--scm',
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 default='guess',
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 choices=['guess', 'git', 'mercurial'],
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 help="The type of source control system handling the repository.")
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 parser.add_argument(
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 '--config',
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 help="The configuration file to use.")
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 parser.add_argument(
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 '--command',
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 help="The command to run on each tag.")
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 # Parse arguments, guess repo type.
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 res = parser.parse_args()
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 repo_type = res.scm
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 if not repo_type or repo_type == 'guess':
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 repo_type = guess_repo_type(res.repo)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 if not repo_type:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 logger.error("Can't guess the repository type. Please use the "
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 "--scm option to specify it.")
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 sys.exit(1)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 if repo_type not in repo_class:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 logger.error("Unknown repository type: %s" % repo_type)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 sys.exit(1)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 # Create the repo handler.
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 repo = repo_class[repo_type]()
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 # Clone or update/checkout the repository in the temp directory.
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 clone_dir = os.path.join(res.tmp_dir, 'clone')
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 if not os.path.exists(clone_dir):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 logger.info("Cloning '%s' into: %s" % (res.repo, clone_dir))
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 repo.clone(res.repo, clone_dir)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 else:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146 os.chdir(clone_dir)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 logger.info("Pulling changes from '%s'." % res.repo)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 repo.update(res.repo, None)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150 os.chdir(clone_dir)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 # Find the configuration file in the repository clone.
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153 config_file = res.config or os.path.join(clone_dir, '.september.yml')
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 config = configparser.ConfigParser(interpolation=None)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 if os.path.exists(config_file):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 logger.info("Loading configuration file: %s" % config_file)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 config.read(config_file)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 if not config.has_section('september'):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 config.add_section('september')
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161 config_sec = config['september']
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162 if res.command:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 config_sec['command'] = res.command
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
164
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
165 if not config.has_option('september', 'command'):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166 logger.error("There is no 'command' configuration setting under the "
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 "'september' section, and no command was passed as an "
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 "option.")
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 sys.exit(1)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 # Find the cache file in the temp directory.
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 cache_file = os.path.join(res.tmp_dir, 'september.json')
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 if os.path.exists(cache_file):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 with open(cache_file, 'r') as fp:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 cache = json.load(fp)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 else:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 cache = {'tags': {}}
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179 # Update the cache: get any new/moved tags.
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 first_tag = config_sec.get('first_tag', None)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 tag_pattern = config_sec.get('tag_pattern', None)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 tag_re = None
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183 if tag_pattern:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 tag_re = re.compile(tag_pattern)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 previous_tags = cache['tags']
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 tags = repo.getTags(clone_dir)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 for t, i in tags:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 if not tag_re or tag_re.search(t):
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 if t not in previous_tags:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 logger.info("Adding tag '%s'." % t)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 previous_tags[t] = {'id': i, 'processed': False}
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 elif previous_tags[t]['id'] != i:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 logger.info("Moving tag '%s'." % t)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 previous_tags[t] = {'id': i, 'processed': False}
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 if first_tag and first_tag == t:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 break
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 logger.info("Updating cache file '%s'." % cache_file)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201 with open(cache_file, 'w') as fp:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 json.dump(cache, fp)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204 # Process tags!
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 use_shell = config_sec.get('use_shell') in ['1', 'yes', 'true']
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 for tn, ti in cache['tags'].items():
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 if ti['processed']:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 logger.info("Skipping '%s'." % tn)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 continue
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211 logger.info("Updating repo to '%s'." % tn)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 repo.update(clone_dir, ti['id'])
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 command = config_sec['command'] % {
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215 'rev_id': ti['id'],
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216 'root_dir': clone_dir,
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217 'tag': tn}
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218 logger.info("Running: %s" % command)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 subprocess.check_call(command, shell=use_shell)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 ti['processed'] = True
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 with open(cache_file, 'w') as fp:
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 json.dump(cache, fp)
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 if __name__ == '__main__':
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 logging.basicConfig(level=logging.INFO, format='%(message)s')
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 main()
ee98303e24b8 Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229