Read about our upcoming Code of Conduct on this issue

Commit d643ea22 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

convert-git-tags-push: Helper class for simulation mode

Moving these two functions to a separate class will make it
easier to provide a script entrypoint.
parent 69e94b92aef2
......@@ -8,6 +8,7 @@
from subprocess import check_call, check_output
logger = logging.getLogger(os.path.basename(sys.argv[0]))
GIT_TAGS_BRANCH = 'git-tags'
os.environ['HGPLAIN'] = 'yes'
......@@ -11,6 +12,29 @@
GIT_TAGS_BRANCH = 'git-tags'
os.environ['HGPLAIN'] = 'yes'
class Helper:
"""Takes care of some logging and subprocess calling
Notably takes simulation mode into account
"""
def __init__(self, simulate):
self.simulate = simulate
def check_call(self, cmd):
if self.simulate:
logger.info("Simulation mode: would have run %r", cmd)
else:
check_call(cmd)
def log_info(self, msg, *a, **kw):
if self.simulate:
logger.info("Simulation mode: would (be) " + msg, *a, **kw)
else:
logger.info(msg.capitalize(), *a, **kw)
parser = argparse.ArgumentParser(
description="Copy Git tags in Mercurial global tags."
)
......@@ -25,7 +49,6 @@
repo = cl_args.repository
simulate = cl_args.simulate
logging.basicConfig(level=getattr(logging, cl_args.logging_level.upper()))
logger = logging.getLogger(os.path.basename(sys.argv[0]))
os.chdir(repo)
dirty = check_output(('hg', 'status', '-mard')).strip()
......@@ -62,23 +85,9 @@
sys.exit(0)
def check_call_with_simulate(cmd):
if simulate:
logger.info("Simulation mode: would have run %r", cmd)
else:
check_call(cmd)
def info_with_simulate(msg, *a, **kw):
if simulate:
logger.info("Simulation mode: would (be) " + msg, *a, **kw)
else:
logger.info(msg.capitalize(), *a, **kw)
if simulate:
logger.info("Simulation mode: reverting .hgtags")
revert()
logger.info("Simulation mode: current working directory is %s",
os.getcwd())
......@@ -79,10 +88,12 @@
if simulate:
logger.info("Simulation mode: reverting .hgtags")
revert()
logger.info("Simulation mode: current working directory is %s",
os.getcwd())
info_with_simulate("committing the changes")
check_call_with_simulate(
helper = Helper(simulate)
helper.log_info("committing the changes")
helper.check_call(
('hg', 'commit', '-m', "Updated Mercurial tags from Git tags", '.hgtags'))
......@@ -87,7 +98,7 @@
('hg', 'commit', '-m', "Updated Mercurial tags from Git tags", '.hgtags'))
info_with_simulate("pushing")
check_call_with_simulate(('hg', 'push', '-r', '.'))
helper.log_info("pushing")
helper.check_call(('hg', 'push', '-r', '.'))
if simulate:
logger.warning("End of simulation, hope you enjoyed it. Bye")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment