Skip to content

Commit 05567e1

Browse files
author
Vladimir Kotal
committed
fix python code comments from timf
1 parent 020ef19 commit 05567e1

File tree

14 files changed

+126
-86
lines changed

14 files changed

+126
-86
lines changed

tools/sync/command.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ def fileno(self):
9898
def close(self):
9999
os.close(self.write_fd)
100100

101+
orig_work_dir = None
101102
if self.work_dir:
103+
orig_work_dir = os.getcwd()
102104
try:
103105
os.chdir(self.work_dir)
104106
except OSError as e:
105107
self.state = Command.ERRORED
106108
self.logger.error("Cannot change working directory to {}".
107-
format(self.work_dir))
109+
format(self.work_dir))
108110
return
109111

110112
othr = OutputThread()
@@ -135,6 +137,15 @@ def close(self):
135137
othr.close()
136138
self.out = othr.getoutput()
137139

140+
if orig_work_dir:
141+
try:
142+
os.chdir(orig_work_dir)
143+
except OSError as e:
144+
self.state = Command.ERRORED
145+
self.logger.error("Cannot change working directory back to {}".
146+
format(orig_work_dir))
147+
return
148+
138149
def fill_arg(self, args_append=None, args_subst=None):
139150
"""
140151
Replace argument names with actual values or append arguments

tools/sync/commands.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,29 @@ def run(self):
8888

8989
def check(self, ignore_errors):
9090
"""
91-
Check the output of the commands and perform logging.
92-
"""
91+
Check the output of the commands and perform logging.
92+
"""
9393

9494
self.logger.debug("Output from {}:".format(self.name))
9595
for cmd in self.outputs.keys():
9696
if self.outputs[cmd] and len(self.outputs[cmd]) > 0:
9797
self.logger.debug("{}: {}".
98-
format(cmd, self.outputs[cmd]))
98+
format(cmd, self.outputs[cmd]))
9999

100100
if self.name in ignore_errors:
101101
return
102102

103103
if any(rv != 0 for rv in self.retcodes.values()):
104104
self.logger.error("processing of selfect {} failed".
105-
format(self))
105+
format(self))
106106
indent = " "
107107
self.logger.error("{}failed commands:".format(indent))
108108
failed_cmds = {k: v for k, v in
109109
self.retcodes.items() if v != 0}
110110
indent = " "
111111
for cmd in failed_cmds.keys():
112112
self.logger.error("{}'{}': {}".
113-
format(indent, cmd, failed_cmds[cmd]))
113+
format(indent, cmd, failed_cmds[cmd]))
114114
out = self.get_cmd_output(cmd,
115115
indent=indent + " ")
116116
if out:
@@ -121,7 +121,7 @@ def check(self, ignore_errors):
121121
if "error" in str(v).lower()}
122122
if len(errored_cmds) > 0:
123123
self.logger.error("Command output in selfect {}"
124-
" contains errors:".format(self.name))
124+
" contains errors:".format(self.name))
125125
indent = " "
126126
for cmd in errored_cmds.keys():
127127
self.logger.error("{}{}".format(indent, cmd))

tools/sync/cvs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from command import Command
2525
from repository import Repository
26-
from utils import which
26+
from shutil import which
2727

2828

2929
class CVSRepository(Repository):

tools/sync/git.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
from command import Command
2525
from repository import Repository
26-
from utils import which
26+
from shutil import which
27+
2728

2829
class GitRepository(Repository):
2930
def __init__(self, logger, path, project, command, env, hooks):
@@ -46,7 +47,7 @@ def reposync(self):
4647
self.logger.info(cmd.getoutputstr())
4748
if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED:
4849
self.logger.error("failed to perform pull for {}".
49-
format(self.path))
50+
format(self.path))
5051
return 1
5152

5253
return 0

tools/sync/hook.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from command import Command
2626
import logging
2727

28+
2829
def run_hook(logger, script, path):
2930
"""
3031
Change a working directory to specified path, run a command
@@ -33,15 +34,7 @@ def run_hook(logger, script, path):
3334
Return 0 on success, 1 on failure.
3435
"""
3536

36-
orig_cwd = os.getcwd()
37-
38-
try:
39-
os.chdir(path)
40-
except:
41-
logger.error("Cannot chdir to {}".format(path))
42-
return 1
43-
44-
cmd = Command([script])
37+
cmd = Command([script], work_dir=path)
4538
cmd.execute()
4639
if cmd.state is not "finished" or cmd.getretcode() != 0:
4740
logger.error("failed to execute {}".format(cmd))
@@ -50,10 +43,4 @@ def run_hook(logger, script, path):
5043

5144
logger.info(cmd.getoutput())
5245

53-
try:
54-
os.chdir(orig_cwd)
55-
except:
56-
logger.error("Cannot chdir to {}".format(orig_cwd))
57-
return 1
58-
5946
return 0

tools/sync/mercurial.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from command import Command
2525
from repository import Repository
26-
from utils import which
26+
from shutil import which
2727

2828

2929
class MercurialRepository(Repository):
@@ -69,10 +69,10 @@ def reposync(self):
6969
cmd = Command(hg_command, work_dir=self.path, env_vars=self.env)
7070
cmd.execute()
7171
self.logger.info(cmd.getoutputstr())
72-
#
73-
# 'hg incoming' will return 1 if there are no incoming changesets,
74-
# so do not check the return value.
75-
#
72+
#
73+
# 'hg incoming' will return 1 if there are no incoming changesets,
74+
# so do not check the return value.
75+
#
7676
if cmd.getstate() != Command.FINISHED:
7777
self.logger.error("failed to run 'hg incoming'")
7878
return 1

tools/sync/mirror.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@
4747
from repository import Repository
4848
from mercurial import MercurialRepository
4949
from repofactory import get_repository
50-
from utils import which, is_exe, check_create_dir, get_dict_val
50+
from utils import is_exe, check_create_dir
5151
from hook import run_hook
5252
from readconfig import read_config
5353
from opengrok import get_repos, get_config_value, get_repo_type
54+
from shutil import which
5455

5556

5657
major_version = sys.version_info[0]
@@ -128,7 +129,7 @@
128129
# The project has no config, that's fine - defaults will be used.
129130
pass
130131

131-
hookdir = get_dict_val(config, 'hookdir')
132+
hookdir = config.get('hookdir')
132133
if hookdir:
133134
logger.debug("Hook directory = {}".format(hookdir))
134135

@@ -137,8 +138,8 @@
137138
if project_config:
138139
try:
139140
if not hookdir:
140-
logger.error("Need to have 'hookdir' in the configuration " +
141-
"to run hooks")
141+
logger.error("Need to have 'hookdir' in the configuration "
142+
"to run hooks")
142143
sys.exit(1)
143144

144145
if not os.path.isdir(hookdir):
@@ -155,12 +156,12 @@
155156
logger.debug("post-hook = {}".format(posthook))
156157
else:
157158
logger.error("Unknown hook name {} for project {}".
158-
format(hookname, args.project))
159+
format(hookname, args.project))
159160
sys.exit(1)
160161

161162
if not is_exe(hookpath):
162-
logger.error("hook file {} does not exist or not executable".
163-
format(hookpath))
163+
logger.error("hook file {} does not exist or not "
164+
"executable".format(hookpath))
164165
sys.exit(1)
165166
except KeyError:
166167
pass
@@ -181,18 +182,20 @@
181182
for handler in logging.root.handlers[:]:
182183
logging.root.removeHandler(handler)
183184

184-
logging.basicConfig(filename = os.path.join(logdir,
185-
args.project + ".log"), filemode = 'a',
186-
format = '%(asctime)s - %(levelname)s: %(message)s',
187-
datefmt = '%m/%d/%Y %I:%M:%S %p',
188-
level = logging.DEBUG if args.debug else logging.INFO)
185+
logging.basicConfig(filename=os.path.join(logdir,
186+
args.project + ".log"), filemode='a',
187+
format='%(asctime)s - %(levelname)s: %(message)s',
188+
datefmt='%m/%d/%Y %I:%M:%S %p',
189+
level=logging.DEBUG if args.debug
190+
else logging.INFO)
189191
logger = logging.getLogger(os.path.basename(sys.argv[0]))
190192

191193
# We want this to be logged to the log file (if any).
192194
if project_config:
193195
try:
194196
if project_config['disabled']:
195-
logger.info("Project {} disabled, exiting".format(args.project))
197+
logger.info("Project {} disabled, exiting".
198+
format(args.project))
196199
sys.exit(0)
197200
except KeyError:
198201
pass
@@ -202,7 +205,8 @@
202205
try:
203206
with lock.acquire(timeout=0):
204207
if prehook and run_hook(logger, prehook,
205-
os.path.join(source_root, args.project)) != 0:
208+
os.path.join(source_root,
209+
args.project)) != 0:
206210
logger.error("pre hook failed")
207211
logging.shutdown()
208212
sys.exit(1)
@@ -225,7 +229,7 @@
225229
source_root + repo_path,
226230
repo_type,
227231
args.project,
228-
get_dict_val(config, 'commands'),
232+
config.get('commands'),
229233
config['proxy'] if use_proxy else None,
230234
None)
231235
if not repo:
@@ -234,12 +238,13 @@
234238
ret = 1
235239
else:
236240
if repo.sync() != 0:
237-
logger.debug("failed to sync repository {}".
238-
format(repo_path))
241+
logger.error("failed to sync repository {}".
242+
format(repo_path))
239243
ret = 1
240244

241245
if posthook and run_hook(logger, posthook,
242-
os.path.join(source_root, args.project)) != 0:
246+
os.path.join(source_root,
247+
args.project)) != 0:
243248
logger.error("post hook failed")
244249
logging.shutdown()
245250
sys.exit(1)

tools/sync/opengrok.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ def get_repo_type(logger, path, messages_file):
9191
line = get_first_line(logger, [messages_file, '-n', 'repository', '-t',
9292
path, 'get-repo-type'])
9393
idx = line.rfind(":")
94-
return line[idx + 1]
94+
return line[idx + 1:]

tools/sync/readconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import yaml
2727
import sys
2828

29+
2930
def read_config(logger, inputfile):
3031
"""
3132
Try to interpret inputfile as either JSON or YAML file,
@@ -62,4 +63,3 @@ def read_config(logger, inputfile):
6263
logger.error("cannot open '{}'".format(inputfile))
6364

6465
return cfg
65-

tools/sync/repofactory.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from cvs import CVSRepository
2727
from svn import SubversionRepository
2828
from git import GitRepository
29-
from utils import get_dict_val
3029

3130

3231
def get_repository(logger, path, repo_type, project, commands, env, hooks):
@@ -41,23 +40,23 @@ def get_repository(logger, path, repo_type, project, commands, env, hooks):
4140

4241
if repo_lower in ["mercurial", "hg"]:
4342
return MercurialRepository(logger, path, project,
44-
get_dict_val(commands, "hg"),
43+
commands.get("hg"),
4544
env, hooks)
4645
elif repo_lower in ["teamware", "sccs"]:
4746
return TeamwareRepository(logger, path, project,
48-
get_dict_val(commands, "teamware"),
47+
commands.get("teamware"),
4948
env, hooks)
5049
elif repo_lower.lower() == "cvs":
5150
return CVSRepository(logger, path, project,
52-
get_dict_val(commands, "cvs"),
51+
commands.get("cvs"),
5352
env, hooks)
5453
elif repo_lower == "svn":
5554
return SubversionRepository(logger, path, project,
56-
get_dict_val(commands, "svn"),
55+
commands.get("svn"),
5756
env, hooks)
5857
elif repo_lower == "git":
5958
return GitRepository(logger, path, project,
60-
get_dict_val(commands, "git"),
59+
commands.get("git"),
6160
env, hooks)
6261
else:
6362
return None

0 commit comments

Comments
 (0)