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
|
import os
import sys
import time
from gettext import gettext as _
from fiveaday import files, lpteams
try:
import bzrlib
except:
print >> sys.stderr, _("You need to install the bzr package.")
sys.exit(1)
def remote_branch():
return "bzr+ssh://%s@bazaar.launchpad.net/~5-a-day/5-a-day-data/main" % files.get_lpid()
def branch_dir():
return os.path.expanduser("~/.5-a-day-data")
def checkout_branch():
err = os.system("bzr checkout %s %s" % (remote_branch(), branch_dir()))
return err #return 0 if 'bzr checkout' was successfull or the bzr-error otherwise
def update_branch():
if not os.path.exists(branch_dir()):
err = checkout_branch()
if err: return err #if unable to checkout the branch return bzr-error
cwd = os.getcwd()
err = os.system("cd %s; bzr update" % (branch_dir()))
os.system("cd %s" % cwd) #always cd back to working-dir, this should not fail
return err #return 0 if 'bzr update' was successfull or the bzr-error otherwise
def commit_changes(force):
# check if committed already in the last hour:
if not force:
from bzrlib import branch
b = branch.Branch.open(branch_dir())
last_rev = b.repository.get_revision(b.last_revision())
seconds_since_last_commit = time.time() - last_rev.timestamp
if seconds_since_last_commit < 3600 and last_rev.committer == b.get_config().username():
print >> sys.stdout, _("Bugs have been added, but not committed yet (already committed %d minutes ago).") % (seconds_since_last_commit/60)
return 0 # This is no error
err = update_branch()
if err:
# stop if creating/updating branch was not successful
print >> sys.stderr, _("bzr failed with error code"), err
return 103 #103: error while updating bzr branch
os.system("cd %s; bzr add %s" % (branch_dir(), files.get_lpid())) #'bzr add' always returns with error-code 0
# Make sure there is something to commit (when forcing update/commit),
# otherwise "bzr commit" will fail, if there's nothing to commit.
if force:
import subprocess
if not subprocess.Popen(["bzr", "status", branch_dir()], stdout=subprocess.PIPE).communicate()[0]:
return 0
err = os.system("cd %s; bzr commit -m 'updated log for %s'" % (branch_dir(), files.get_lpid()))
return err #return 0 if 'bzr commit' was successfull or the bzr-error otherwise
def update_team_file():
tf = files.TeamFile()
tof = files.TeamOverviewFile()
username = files.get_lpid()
if tf.teams and tof.overview[files.get_lpid()] != tf.teams:
t = filter(lambda a: a not in lpteams.get_teams(username), tf.teams)
if t:
print >> sys.stderr, \
_("'%s' is not member of %s.") % (username, ", ".join(t))
sys.exit(1)
tof.add(files.get_lpid(), tf.teams)
os.system("cd %s; bzr add %s" % (bzr.branch_dir(),
files.team_overview_file()))
def add(args):
update_team_file()
log = files.LogFile()
bugs = filter(lambda a: a not in ["-f", "--force"], args)
err = log.add(time.time(), bugs)
if err:
return err
if log.tags:
print _("'%s' tagged as '%s'.") % (" ". join(bugs),
" ". join(log.tags))
err = commit_changes(bool(filter(lambda a: a in ["-f", "--force"], args)))
if err:
print >> sys.stderr, _("bzr failed with error code"), err
return 104 #104: error while committing changes
return 0
|