def __build_extra_headers(msg, msg_id, ref_id=None): """Build extra email headers and encoding""" del msg['Date'] msg['Date'] = email.utils.formatdate(localtime=True) msg['Message-ID'] = msg_id if ref_id: # make sure the ref id has the angle brackets ref_id = '<%s>' % ref_id.strip(' \t\n<>') msg['In-Reply-To'] = ref_id msg['References'] = ref_id msg['User-Agent'] = 'StGit/%s' % version.get_version() # update other address headers __update_header(msg, 'Reply-To') __update_header(msg, 'Mail-Reply-To') __update_header(msg, 'Mail-Followup-To')
def _main(): """The main function """ global prog sys.argv = list(map(fsdecode_utf8, sys.argv)) prog = os.path.basename(sys.argv[0]) if len(sys.argv) < 2: print('usage: %s <command>' % prog, file=sys.stderr) print(' Try "%s --help" for a list of supported commands' % prog, file=sys.stderr) sys.exit(utils.STGIT_GENERAL_ERROR) cmd = sys.argv[1] if cmd in ['-h', '--help']: if len(sys.argv) >= 3: cmd = commands.canonical_cmd(sys.argv[2]) sys.argv[2] = '--help' else: print_help() sys.exit(utils.STGIT_SUCCESS) if cmd == 'help': if len(sys.argv) == 3 and not sys.argv[2] in ['-h', '--help']: cmd = commands.canonical_cmd(sys.argv[2]) sys.argv[0] += ' %s' % cmd command = commands[cmd] parser = argparse.make_option_parser(command) if is_cmd_alias(command): parser.remove_option('-h') pager(parser.format_help().encode()) else: print_help() sys.exit(utils.STGIT_SUCCESS) if cmd in ['-v', '--version', 'version']: from stgit.version import get_version print('Stacked GIT %s' % get_version()) os.system('git --version') print('Python version %s' % sys.version) sys.exit(utils.STGIT_SUCCESS) if cmd in ['copyright']: print(__copyright__) sys.exit(utils.STGIT_SUCCESS) # re-build the command line arguments cmd = commands.canonical_cmd(cmd) sys.argv[0] += ' %s' % cmd del sys.argv[1] command = commands[cmd] if is_cmd_alias(command): sys.exit(command.func(sys.argv[1:])) parser = argparse.make_option_parser(command) directory = command.directory # These modules are only used from this point onwards and do not # need to be imported earlier try: from configparser import ParsingError, NoSectionError except ImportError: from ConfigParser import ParsingError, NoSectionError from stgit.exception import StgException from stgit.config import config_setup from stgit.stack import Series try: debug_level = int(environ_get('STGIT_DEBUG_LEVEL', 0)) except ValueError: out.error('Invalid STGIT_DEBUG_LEVEL environment variable') sys.exit(utils.STGIT_GENERAL_ERROR) try: (options, args) = parser.parse_args() directory.setup() config_setup() # Some commands don't (always) need an initialized series. if directory.needs_current_series: if hasattr(options, 'branch') and options.branch: command.crt_series = Series(options.branch) else: command.crt_series = Series() ret = command.func(parser, options, args) except (StgException, IOError, ParsingError, NoSectionError) as err: directory.write_log(cmd) if debug_level > 0: traceback.print_exc(file=sys.stderr) out.error(str(err), title='%s %s' % (prog, cmd)) sys.exit(utils.STGIT_COMMAND_ERROR) except SystemExit: # Triggered by the option parser when it finds bad commandline # parameters. sys.exit(utils.STGIT_COMMAND_ERROR) except KeyboardInterrupt: sys.exit(utils.STGIT_GENERAL_ERROR) except BaseException: out.error('Unhandled exception:') traceback.print_exc(file=sys.stderr) sys.exit(utils.STGIT_BUG_ERROR) directory.write_log(cmd) sys.exit(ret or utils.STGIT_SUCCESS)