Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- mosesdecoder/contrib/arrow-pipelines/READMEAdded+58
- mosesdecoder/contrib/arrow-pipelines/documentation/training-pipeline/moses-pypeline.diaAdded
- mosesdecoder/contrib/arrow-pipelines/pcl/MakefileAdded+23
- mosesdecoder/contrib/arrow-pipelines/pcl/components/MakefileAdded+24
- mosesdecoder/contrib/arrow-pipelines/pcl/components/src_trg_tokeniser.cfgAdded+10
- mosesdecoder/contrib/arrow-pipelines/pcl/components/src_trg_tokeniser.pclAdded+40
- mosesdecoder/contrib/arrow-pipelines/pcl/components/translation_model_training.cfgAdded+15
- mosesdecoder/contrib/arrow-pipelines/pcl/components/translation_model_training.pclAdded+70
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/MakefileAdded+14
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/__init__.pyAdded
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/cleanup/__init__.pyAdded
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/cleanup/cleanup.pyAdded+129
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/__init__.pyAdded
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/data_split.cfgAdded+7
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/data_split.pyAdded+144
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/test_data/data.deAdded+50
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/test_data/data.enAdded+50
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/irstlm_build/__init__.pyAdded
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/irstlm_build/irstlm_build.pyAdded+117
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/mert/__init__.pyAdded
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/mert/mert.pyAdded+98
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/model_training/__init__.pyAdded
- mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/model_training/model_training.pyAdded+103
- mosesdecoder/contrib/arrow-pipelines/pcl/training_pipeline.cfgAdded+21
- mosesdecoder/contrib/arrow-pipelines/pcl/training_pipeline.pclAdded+117
- mosesdecoder/contrib/checkplf/MakefileAdded+2
- mosesdecoder/contrib/lmserver/Makefile.inAdded+645
- mosesdecoder/contrib/lmserver/daemon.cAdded+68
- mosesdecoder/contrib/lmserver/lmserver.cAdded+2140
- mosesdecoder/contrib/lmserver/missingAdded+367
- mosesdecoder/contrib/lmserver/srilm.hAdded+8
- mosesdecoder/contrib/tmcombine/README.mdAdded+88
- mosesdecoder/contrib/tmcombine/argparse.pyAdded+2382
- mosesdecoder/contrib/tmcombine/test/model3/model/lex.counts.e2fAdded+8
- mosesdecoder/contrib/tmcombine/test/model3/model/lex.counts.f2eAdded+8
- mosesdecoder/contrib/tmcombine/test/model3/model/lex.e2fAdded+8
- mosesdecoder/contrib/tmcombine/test/model3/model/lex.f2eAdded+8
- mosesdecoder/contrib/tmcombine/test/model3/model/phrase-tableAdded+8
- mosesdecoder/contrib/tmcombine/test/model5/model/lex.counts.e2fAdded+8
- mosesdecoder/contrib/tmcombine/test/model5/model/lex.counts.f2eAdded+8
- mosesdecoder/contrib/tmcombine/test/model5/model/lex.e2fAdded+8
- mosesdecoder/contrib/tmcombine/test/model5/model/lex.f2eAdded+8
- mosesdecoder/contrib/tmcombine/test/model5/model/phrase-tableAdded+8
- mosesdecoder/contrib/tmcombine/test/model6/model/lex.counts.e2fAdded+8
- mosesdecoder/contrib/tmcombine/test/model6/model/lex.counts.f2eAdded+8
- mosesdecoder/contrib/tmcombine/test/model6/model/lex.e2fAdded+8
- mosesdecoder/contrib/tmcombine/test/model6/model/lex.f2eAdded+8
- mosesdecoder/contrib/tmcombine/test/model6/model/phrase-tableAdded+5
- mosesdecoder/contrib/tmcombine/test/phrase-table_test1Added+8
- mosesdecoder/contrib/tmcombine/test/phrase-table_test10Added+9
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758Arrow Based Moses Training Pipeline===================================
This demonstration implements a training pipeline that is shown in the Dia diagram in documentation/training-pipeline/moses-pypeline.dia.
The demo has been tested with:
- Moses v1.0 - Giza++ v1.0.7 - IRSTLM v5.70.04
Setup-----
To use the demonstration you must first initialise the git submodules for this clone. Return to the top level directory and issue the following command:
$ git submodule update --init --recursive
This will clone PCL, available at Github (git://github.com/ianj-als/pcl.git), and Pypeline submodules, available at GitHub (git://github.com/ianj-als/pypeline.git).
Return to the arrow-pipelines contrib directory:
$ cd contrib/arrow-pipelines
To use the PCL compiler and run-time set the following environment variables (assuming Bash shell):
$ export PATH=$PATH:`pwd`/python/pcl/src/pclc:`pwd`/python/pcl/src/pcl-run$ export PYTHONPATH=$PYTHONPATH:`pwd`/python/pcl/libs/pypeline/src$ export PCL_IMPORT_PATH=`pwd`/python/pcl/src/runtime:`pwd`/pcl
Three environment variables need to be set before the pipeline can be run, they are:
- MOSES_HOME : The directory where Moses has been cloned, or installed, - IRSTLM : The installation directory of your IRSTLM, and - GIZA_HOME : The installation directory of GIZA++.
Building the example training pipeline--------------------------------------
$ cd pcl$ make
Running the example training pipeline-------------------------------------
To execute the training pipeline run the following command:
$ pcl-run.py training_pipeline
Once complete the output of the pipeline can be found in the directories:
- training/tokenisation - training/model - training/lm - training/mert1234567891011121314151617181920212223CC = pclc.pyCFLAGS=-iSOURCES = training_pipeline.pclOBJS = $(SOURCES:.pcl=.py)SUBDIRS = components
all: subdirs build
build: $(OBJS)
%.py: %.pcl $(CC) $(CFLAGS) $<
clean: for dir in $(SUBDIRS); do \ $(MAKE) -C $$dir clean; \ done rm -f *.py *.pyc *.log *~
subdirs: for dir in $(SUBDIRS); do \ $(MAKE) -C $$dir ; \ done123456789101112131415161718192021222324CC = pclc.pyCFLAGS = -iSOURCES = src_trg_tokeniser.pcl translation_model_training.pclOBJS = $(SOURCES:.pcl=.py)SUBDIRS = wrappers
all: subdirs build
build: $(OBJS)
%.py: %.pcl $(CC) $(CFLAGS) $<
clean: for dir in $(SUBDIRS); do \ $(MAKE) -C $$dir clean; \ done rm -f *.py *.pyc *.log *~
subdirs: for dir in $(SUBDIRS); do \ $(MAKE) -C $$dir ; \ done
12345678910[Configuration]tokeniser.src.language = entokeniser.src.tokenisation_dir = test_data/src_trg_tokenizer/tokenisedtokeniser.trg.language = lttokeniser.trg.tokenisation_dir = test_data/src_trg_tokenizer/tokenisedtokeniser.moses.installation = /opt/moses
[Inputs]src_filename = test_data/src_trg_tokenizer/cleantrain.entrg_filename = test_data/src_trg_tokenizer/cleantrain.lt12345678910111213141516171819202122232425262728293031323334353637383940## Import all of the components to be composed#import wrappers.tokenizer.tokenizer as tokeniser
## Component definition## +---------+ +---------+ +---------+ +---------+# src_filename -->+ +--> filename -->+-- src --+--> tokenised_filename -->+---------+--> tokenised_filename -->+ +--> tokenised_src_filename# | | | | | | | |# trg_filename -->+ +--> filename -->+---------+-------> filename ------->+-- trg --+--> tokenised_filename -->+ +--> tokenised_trg_filename# +---------+ +---------+ +---------+ +---------+# Config: {language::String, Config: {language::String,# tokenisation_dir::String, tokenisation_dir::String,# moses_installation_dir::String} moses_installation_dir::String}#component src_trg_tokeniser inputs (src_filename), (trg_filename) outputs (tokenised_src_filename), (tokenised_trg_filename) configuration tokeniser.src.language, tokeniser.src.tokenisation_dir, tokeniser.trg.language, tokeniser.trg.tokenisation_dir, tokeniser.moses.installation declare src_tokeniser := new tokeniser with tokeniser.src.language -> corpus.language, tokeniser.src.tokenisation_dir -> working.directory.root, tokeniser.moses.installation -> moses.installation trg_tokeniser := new tokeniser with tokeniser.trg.language -> corpus.language, tokeniser.trg.tokenisation_dir -> working.directory.root, tokeniser.moses.installation -> moses.installation as wire (src_filename -> corpus.filename), (trg_filename -> corpus.filename) >>> (src_tokeniser *** trg_tokeniser) >>> wire (corpus.tokenised.filename -> tokenised_src_filename), (corpus.tokenised.filename -> tokenised_trg_filename)123456789101112131415[Configuration]model_training.max_segment_length = 20model_training.corpus.development_size = 4500model_training.corpus.evaluation_size = 5000model_training.src.language = enmodel_training.trg.language = ltmodel_training.method.alignment = grow-diag-final-andmodel_training.method.reordering = msd-bidirectional-femodel_training.moses.installation = /opt/mosesmodel_training.giza.installation = /opt/moses/giza++-v1.0.7model_training.translation_model.dir = test_data/translation_model_training/translation_model
[Inputs]src_filename = test_data/translation_model_training/cleantrain.entrg_filename = test_data/translation_model_training/cleantrain.lt12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970## Import all of the components to be composed#import wrappers.cleanup.cleanup as cleanupimport wrappers.data_split.data_split as data_splitimport wrappers.model_training.model_training as model_training
## Component definition## {cleaned_src_filename, {src_filename, {[devel|eval|train]_src_filename, {src_filename, {moses_ini_file,# cleaned_trg_filename} trg_filename} [devel|eval|train]_trg_filename} trg_filename} evaluation_data_filename}# | | | | +-------+ |# +-------+ | | +-------+ | +-------+ V | Model | {moses_ini_file} +-------+ V# | Clean | V V | Data | V | +---------------->+ Train +----------------->+ Merge +-----># {src_filename, -->+ +----->+ +------------->+ Split | +-------+ +---+---+# trg_filename} | Up | | Split | | +---\ Config: {[src|trg]_language::String, ^# +-------+ +-------+ +-------+ | alignment_method::String, |# Config: {segment_length::Int} Config: {development_size::Int, | reordering_method::String, |# evaluation_size::Int} | giza_installation_dir::String, |# | model_directory::String} |# \--------------------------------------------/#component translation_model_training inputs src_filename, trg_filename outputs evaluation_data_filename, moses_ini_filename configuration model_training.max_segment_length, model_training.corpus.development_size, model_training.corpus.evaluation_size, model_training.src.language, model_training.trg.language, model_training.method.alignment, model_training.method.reordering, model_training.moses.installation, model_training.giza.installation, model_training.translation_model.dir declare cleanup := new cleanup with model_training.max_segment_length -> segment_length_limit data_split := new data_split with model_training.corpus.development_size -> development_data_size, model_training.corpus.evaluation_size -> evaluation_data_size model_training := new model_training with model_training.src.language -> source_language, model_training.trg.language -> target_language, model_training.method.alignment -> alignment_method, model_training.method.reordering -> reordering_method, model_training.moses.installation -> moses_installation_dir, model_training.giza.installation -> giza_installation_dir, model_training.translation_model.dir -> translation_model_directory as cleanup >>> wire cleaned_src_filename -> src_filename, cleaned_trg_filename -> trg_filename >>> data_split >>> wire devel_src_filename -> devel_src_filename, eval_src_filename -> evaluation_data_filename, train_trg_filename -> _, train_src_filename -> _, eval_trg_filename -> _, devel_trg_filename -> devel_trg_filename >>> ((wire devel_src_filename -> src_filename, devel_trg_filename -> trg_filename, evaluation_data_filename -> _ >>> model_training) &&& wire evaluation_data_filename -> evaluation_data_filename, devel_src_filename -> _, devel_trg_filename -> _) >>> merge top[moses_ini_filename] -> moses_ini_filename, bottom[evaluation_data_filename] -> evaluation_data_filename1234567891011121314SUBDIRS = tokenizer
all: subdirs
clean: for dir in $(SUBDIRS); do \ $(MAKE) -C $$dir clean; \ done
subdirs: for dir in $(SUBDIRS); do \ $(MAKE) -C $$dir ; \ done
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129def get_name(): return 'cleanup'
def get_inputs(): return ['src_filename', 'trg_filename']
def get_outputs(): return ['cleaned_src_filename', 'cleaned_trg_filename']
def get_configuration(): return ['segment_length_limit']
def configure(args): return {'segment_length' : args['segment_length_limit']}
def initialise(config): def _filter(limit, ifh1, ofh1, ifh2, ofh2): def _short(line): n = 0 for c in line: if c == " ": n += 1 return n < limit
for (l1, l2) in zip(ifh1, ifh2): if _short(l1) and _short(l2): print >>ofh1, l1, print >>ofh2, l2,
def _make_cleaned_filename(filename): bits = filename.split(".") bits.insert(-1, "clean") return ".".join(bits)
def _filter_main(a, s): limit = config['segment_length'] (ifh1, ifh2, ofh1, ofh2) = (None, None, None, None) try: input_src_filename = a['src_filename'] input_trg_filename = a['trg_filename']
print "Cleanup: Cleaning [%s] and [%s]..." % (input_src_filename, input_trg_filename)
ifh1 = open(input_src_filename, "r") ifh2 = open(input_trg_filename, "r")
cleaned_src_filename = _make_cleaned_filename(input_src_filename) cleaned_trg_filename = _make_cleaned_filename(input_trg_filename) ofh1 = open(cleaned_src_filename, "w") ofh2 = open(cleaned_trg_filename, "w")
_filter(limit, ifh1, ofh1, ifh2, ofh2)
return {'cleaned_src_filename': cleaned_src_filename, 'cleaned_trg_filename': cleaned_trg_filename} finally: def _safe_close(fh): if fh is not None: fh.close() _safe_close(ifh1) _safe_close(ifh2) _safe_close(ofh1) _safe_close(ofh2) return _filter_main
if __name__ == '__main__': import os import tempfile import test.test as thelp
from pypeline.helpers.helpers import eval_pipeline
def _test_main(): configuration = {'segment_length_limit': 20}
src_filename = tempfile.mkstemp(suffix = ".src", dir = "/tmp") trg_filename = tempfile.mkstemp(suffix = ".trg", dir = "/tmp")
box_eval = { 'src_filename': src_filename[1], 'trg_filename': trg_filename[1], 'cleaned_src_file_expected': src_filename[1] + ".expected", 'cleaned_trg_file_expected': trg_filename[1] + ".expected"}
try: _prep_files(box_eval) _run_test(configuration, box_eval) finally: _cleanup_files(box_eval)
def _run_test(configuration, box_eval): box_config = configure(configuration) box = initialise(box_config) output = eval_pipeline(box, box_eval, box_config) try: thelp.diff(box_eval['cleaned_src_file_expected'], output['cleaned_src_filename']) thelp.diff(box_eval['cleaned_trg_file_expected'], output['cleaned_trg_filename']) finally: os.unlink(output['cleaned_src_filename']) os.unlink(output['cleaned_trg_filename'])
def _line(line_lengths): def _gen_line(tokens): return " ".join(map(lambda n: "tok" + str(n), range(tokens))) return map(_gen_line, line_lengths)
def _prep_files(box_eval): thelp.cat(box_eval['src_filename'], _line([10, 20, 30, 40, 17, 21])) thelp.cat(box_eval['trg_filename'], _line([40, 30, 20, 10, 20, 21])) thelp.cat(box_eval['cleaned_src_file_expected'], _line([17])) thelp.cat(box_eval['cleaned_trg_file_expected'], _line([20]))
def _cleanup_files(box_eval): try: for key, filename in box_eval.items(): os.unlink(filename) except: pass
_test_main()1234567[Configuration]evaluation_data_size = 7development_data_size = 13
[Inputs]src_filename = test_data/data.entrg_filename = test_data/data.de123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144def get_name(): return 'data_split'
def get_inputs(): return ['src_filename', 'trg_filename']
def get_outputs(): return ['devel_src_filename', 'devel_trg_filename', 'eval_src_filename', 'eval_trg_filename', 'train_src_filename', 'train_trg_filename']
def get_configuration(): return ['evaluation_data_size', 'development_data_size']
def configure(args): result = {} result['evaluate_size'] = args['evaluation_data_size'] result['development_size'] = args['development_data_size'] return result
def initialise(config): def _copy(size, inp, ofh1, ofh2): try: while size != 0: (l1, l2) = inp.next() print >>ofh1, l1, print >>ofh2, l2, size -= 1 except StopIteration: pass
def _make_split_filename(filename, data_set): bits = filename.split(".") bits.insert(-1, data_set)
new_filename = ".".join(bits) return new_filename
def _splitter_main(a, s): (ifh1, ifh2, ofh1, ofh2) = (None, None, None, None) try: input_src_filename = a['src_filename'] input_trg_filename = a['trg_filename']
ifh1 = open(input_src_filename, "r") ifh2 = open(input_trg_filename, "r") inp = iter(zip(ifh1, ifh2))
result = {} for (data_set, size) in [('devel', config['development_size']), ('eval', config['evaluate_size']), ('train', -1)]: output_src_filename = _make_split_filename(input_src_filename, data_set) output_trg_filename = _make_split_filename(input_trg_filename, data_set) ofh1 = open(output_src_filename, "w") ofh2 = open(output_trg_filename, "w")
_copy(size, inp, ofh1, ofh2) result[data_set + '_src_filename'] = output_src_filename result[data_set + '_trg_filename'] = output_trg_filename
return result finally: def _safe_close(fh): if fh is not None: fh.close() _safe_close(ifh1) _safe_close(ifh2) _safe_close(ofh1) _safe_close(ofh2) return _splitter_main
if __name__ == '__main__': import os import tempfile import test.test as thelp
from pypeline.helpers.helpers import eval_pipeline
def _test_main(): configuration = {'evaluation_data_size': 7, 'development_data_size': 13}
src_filename = tempfile.mkstemp(suffix = ".src", dir = "/tmp") trg_filename = tempfile.mkstemp(suffix = ".trg", dir = "/tmp")
box_eval = {'src_filename': src_filename[1], 'trg_filename': trg_filename[1], 'devel_src_expected': src_filename[1] + ".devel.expected", 'devel_trg_expected': trg_filename[1] + ".devel.expected", 'eval_src_expected': src_filename[1] + ".eval.expected", 'eval_trg_expected': trg_filename[1] + ".eval.expected", 'train_src_expected': src_filename[1] + ".train.expected", 'train_trg_expected': trg_filename[1] + ".train.expected"}
try: _prep_files(box_eval) _run_test(configuration, box_eval) finally: _cleanup_files(box_eval)
def _run_test(configuration, box_eval): box_config = configure(configuration) box = initialise(box_config) output = eval_pipeline(box, box_eval, box_config) for data_set in ['devel', 'eval', 'train']: for lang in ['src', 'trg']: filename = output[data_set + '_' + lang + '_filename'] filename_expected = box_eval[data_set + '_' + lang + '_expected'] thelp.diff(filename_expected, filename)
def _line(line_lengths): def _gen_line(tokens): return " ".join(map(lambda n: "tok" + str(n), range(tokens))) return map(_gen_line, line_lengths)
def _prep_files(box_eval): thelp.cat(box_eval['src_filename'], _line(range(50))) thelp.cat(box_eval['trg_filename'], _line(range(50))) #expected output: thelp.cat(box_eval['devel_src_expected'], _line(range(0,13))) thelp.cat(box_eval['devel_trg_expected'], _line(range(0,13))) thelp.cat(box_eval['eval_src_expected'], _line(range(13,20))) thelp.cat(box_eval['eval_trg_expected'], _line(range(13,20))) thelp.cat(box_eval['train_src_expected'], _line(range(20,50))) thelp.cat(box_eval['train_trg_expected'], _line(range(20,50)))
def _cleanup_files(box_eval): try: for key, filename in box_eval.items(): os.unlink(filename) except: pass
_test_main()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
tok0tok0 tok1tok0 tok1 tok2tok0 tok1 tok2 tok3tok0 tok1 tok2 tok3 tok4tok0 tok1 tok2 tok3 tok4 tok5tok0 tok1 tok2 tok3 tok4 tok5 tok6tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44 tok45tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44 tok45 tok46tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44 tok45 tok46 tok47tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44 tok45 tok46 tok47 tok481234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
tok0tok0 tok1tok0 tok1 tok2tok0 tok1 tok2 tok3tok0 tok1 tok2 tok3 tok4tok0 tok1 tok2 tok3 tok4 tok5tok0 tok1 tok2 tok3 tok4 tok5 tok6tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44 tok45tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44 tok45 tok46tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44 tok45 tok46 tok47tok0 tok1 tok2 tok3 tok4 tok5 tok6 tok7 tok8 tok9 tok10 tok11 tok12 tok13 tok14 tok15 tok16 tok17 tok18 tok19 tok20 tok21 tok22 tok23 tok24 tok25 tok26 tok27 tok28 tok29 tok30 tok31 tok32 tok33 tok34 tok35 tok36 tok37 tok38 tok39 tok40 tok41 tok42 tok43 tok44 tok45 tok46 tok47 tok48123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import osimport shutilimport subprocessimport tempfile
def get_name(): return 'irstlm_build'
def get_inputs(): return ['input_filename']
def get_outputs(): return ['add_start_end_filename', 'lm_filename', 'compiled_lm_filename']
def get_configuration(): return ['irstlm_installation_dir', 'irstlm_smoothing_method', 'language_model_directory']
def configure(args): config = dict() config['irstlm_install_directory'] = args['irstlm_installation_dir'] config['smoothing_method'] = args['irstlm_smoothing_method'] config['lm_directory'] = args['language_model_directory'] return config
def initialise(config): def process(a, s): # Create the LM directory if we need to if os.path.exists(config['lm_directory']) is False: os.makedirs(config['lm_directory'])
# The filename of the file to chew through start_end_input_filename = a['input_filename'] if os.path.exists(start_end_input_filename) is False: raise Exception("IRSTLM Build: Input file could not be found at [%s]" % start_end_input_filename)
# Derive the output file name for the add start-end marker processor filename_bits = os.path.basename(start_end_input_filename).split(".") filename_bits[2] = "sb"; start_end_output_filename = os.path.join(config['lm_directory'], ".".join(filename_bits))
# Derive the output file name of the LM build filename_bits[2] = "lm" lm_filename = os.path.join(config['lm_directory'], ".".join(filename_bits))
# Derive the compiled LM file name filename_bits[2] = "arpa" compiled_lm_filename = os.path.join(config['lm_directory'], ".".join(filename_bits))
# First thing to do is add start and end markers start_end_cmdline = [os.path.join(config['irstlm_install_directory'], "bin", "add-start-end.sh")] infile = open(start_end_input_filename, 'r') outfile = open(start_end_output_filename, 'w') print "IRSTLM Build: Invoking [%s]..." % " ".join(start_end_cmdline) return_code = subprocess.check_call(start_end_cmdline, stdin = infile, stdout = outfile) if return_code: raise Exception("IRSTLM add start and end markers failed: input file = [%s], output file = [%s], return code = [%d]" % \ start_end_input_filename, start_end_output_filename, return_code)
# Next build the language model tmp_dir = tempfile.mkdtemp(dir = "/tmp") try: build_lm_cmdline = [os.path.join(config['irstlm_install_directory'], "bin", "build-lm.sh"), "-i", start_end_output_filename, "-t", tmp_dir, "-p", "-s", config['smoothing_method'], "-o", lm_filename] print "IRSTLM Build: Invoking [%s]..." % " ".join(build_lm_cmdline) return_code = subprocess.check_call(build_lm_cmdline) if return_code: raise Exception("IRST language model failed to build: return code = [%d]" % return_code) finally: if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir)
# Compile the LM lm_filename = lm_filename + ".gz" compile_lm_cmdline = [os.path.join(config['irstlm_install_directory'], "bin", "compile-lm"), "--text", "yes", lm_filename, compiled_lm_filename] print "IRSTLM Build: Invoking [%s]..." % " ".join(compile_lm_cmdline) return_code = subprocess.check_call(compile_lm_cmdline) if return_code: raise Exception("IRST language model compilation failed: return code = [%d]" % return_code)
output = {'add_start_end_filename': start_end_output_filename, 'lm_filename': lm_filename, 'compiled_lm_filename': compiled_lm_filename}
print "IRSTLM Build: Output = %s" % output
return output
return process
if __name__ == '__main__': from pypeline.helpers.helpers import eval_pipeline, cons_function_component
lm_dir = os.environ["PWD"] configuration = {'irstlm_root': os.environ["IRSTLM"], 'irstlm_smoothing_method': 'improved-kneser-ney', 'language_model_directory': lm_dir} component_config = configure(configuration) component = initialise(component_config)
value = eval_pipeline(cons_function_component(component), {'input_filename': '/Users/ianjohnson/Dropbox/Documents/MTM2012/tokenised_files/news-commentary-v7.fr-en.tok.en'}, component_config) target = {'add_start_end_filename': os.path.join(lm_dir, 'news-commentary-v7.fr-en.sb.en'), 'lm_filename': os.path.join(lm_dir, 'news-commentary-v7.fr-en.lm.en.gz'), 'compiled_lm_filename': os.path.join(lm_dir, 'news-commentary-v7.fr-en.arpa.en')} print "Target: %s" % target if value != target: raise Exception("Massive fail!")1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import osimport shutilimport subprocess
def get_name(): return 'mert'
def get_inputs(): return ['evaluation_data_filename', 'trg_language_model_filename', 'trg_language_model_order', 'trg_language_model_type', 'moses_ini_filename']
def get_outputs(): return ['moses_ini_filename']
def get_configuration(): return ['source_language', 'target_language', 'moses_installation_dir', 'mert_working_directory', 'mert_max_no_iterations']
def configure(args): result = {} result['src_lang'] = args['source_language'] result['trg_lang'] = args['target_language'] result['moses_installation_dir'] = args['moses_installation_dir'] result['mert_working_dir'] = args['mert_working_directory'] result['max_no_iterations'] = args['mert_max_no_iterations'] return result
def initialise(config): def process(a, s): infilename = os.path.abspath(a['evaluation_data_filename']) infilename = ".".join(infilename.split(".")[:-1]) lm_file = os.path.abspath(a['trg_language_model_filename']) lm_order = int(a['trg_language_model_order']) lm_type = int(a['trg_language_model_type']) max_no_iters = int(config['max_no_iterations']) orig_moses_ini = os.path.abspath(a['moses_ini_filename']) if not os.path.exists(orig_moses_ini): raise Exception, "Error: Input moses.ini does not exist"
workdir = os.path.abspath(config['mert_working_dir']) #simply call the training perl script #remove the workdir if it is already there if os.path.exists(workdir): shutil.rmtree(workdir) os.makedirs(workdir)
#local vars moses_install_dir = os.path.abspath(config['moses_installation_dir']) mert_perl = os.path.join(moses_install_dir, 'scripts', 'training', 'mert-moses.pl') bin_dir = os.path.join(moses_install_dir, 'bin') moses_bin = os.path.join(moses_install_dir, 'bin', 'moses') src_file = infilename + '.' + config['src_lang'] ref_file = infilename + '.' + config['trg_lang'] logfile = os.path.join(workdir, 'log') #change lm configuration in moses ini moses_ini = os.path.join(workdir, 'trained-moses.ini') cmd = r"cat %(orig_moses_ini)s | sed '/\[lmodel-file\]/,/^[[:space:]]*$/c\[lmodel-file\]\n%(lm_type)s 0 %(lm_order)s %(lm_file)s\n' > %(moses_ini)s" cmd = cmd % locals() os.system(cmd) #the command cmd = '%(mert_perl)s --maximum-iterations %(max_no_iters)d --mertdir %(bin_dir)s --working-dir %(workdir)s %(src_file)s %(ref_file)s %(moses_bin)s %(moses_ini)s 2> %(logfile)s' cmd = cmd % locals()
pipe = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell=True) pipe.wait()
#check the moses ini new_mosesini = os.path.join(workdir, 'moses.ini') if not os.path.exists(new_mosesini): raise Exception, 'Failed MERT' return {'moses_ini_filename' : new_mosesini}
return process
if __name__ == '__main__': def __test(): configuration = {'src_lang':'en', 'trg_lang':'lt', 'moses_installation_dir':os.path.abspath('../../../../'), 'mert_working_dir':'../../../../../tuning'} values = {'development_data_filename':'../../../../../corpus/tune', 'moses_ini_file':'../../../../../model/model/moses.ini', 'trg_language_model_filename':'../../../../../corpus/train.lt.lm', 'trg_language_model_type':9, 'trg_language_model_order':4} from pypeline.helpers.helpers import run_pipeline box_config = configure(configuration) box = initialise(configuration) print run_pipeline(box, values, None)
#do some test __test()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import osimport shutilimport subprocess
def get_name(): return 'model_training'
def get_inputs(): return ['src_filename', 'trg_filename']
def get_outputs(): return ['moses_ini_filename']
def get_configuration(): return ['source_language', 'target_language', 'moses_installation_dir', 'giza_installation_dir', 'translation_model_directory', 'alignment_method', 'reordering_method']
# Alignment = grow-diag-final-and# Reordering = msd-bidirectional-fedef configure(args): result = {} result['src_lang'] = args['source_language'] result['trg_lang'] = args['target_language'] result['moses_installation_dir'] = args['moses_installation_dir'] result['external_bin_dir'] = args['giza_installation_dir'] result['model_directory'] = args['translation_model_directory'] result['alignment'] = args['alignment_method'] result['reordering'] = args['reordering_method'] return result
def initialise(config): def process(a, s): get_corpora_name_fn = lambda fn: ".".join(os.path.basename(fn).split('.')[:-1]) src_filename = os.path.abspath(a['src_filename']) trg_filename = os.path.abspath(a['trg_filename']) src_corpora_name = get_corpora_name_fn(src_filename) trg_corpora_name = get_corpora_name_fn(trg_filename) if src_corpora_name != trg_corpora_name: raise Exception, "Mismatch of source [%s] and target [%s] filename" % (src_filename, trg_filename)
infilename = os.path.abspath(os.path.join(os.path.dirname(src_filename), src_corpora_name)) workdir = os.path.abspath(config['model_directory']) #simply call the training perl script #remove the workdir if it is already there if os.path.exists(workdir): shutil.rmtree(workdir) os.makedirs(workdir) #local vars train_model_perl = os.path.abspath(os.path.join(config['moses_installation_dir'], 'scripts', 'training', 'train-model.perl')) src_lang = config['src_lang'].lower() trg_lang = config['trg_lang'].lower() external_bin = os.path.abspath(config['external_bin_dir']) #create a dummy lm file dummy_lmfile = os.path.join(workdir, 'dummy.lm') f = open(dummy_lmfile, 'w') print >> f, "dummy lm file" f.close() logfile = os.path.join(workdir, 'log') #the command alignment_method = config['alignment'] reordering_method = config['reordering'] cmd = '%(train_model_perl)s -root-dir %(workdir)s -corpus %(infilename)s ' \ '-f %(src_lang)s -e %(trg_lang)s -alignment %(alignment_method)s ' \ '-reordering %(reordering_method)s -lm 0:5:%(dummy_lmfile)s:0 ' \ '-external-bin-dir %(external_bin)s 2> %(logfile)s' cmd = cmd % locals()
pipe = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell=True) pipe.wait()
# check the moses ini mosesini = os.path.join(workdir, 'model', 'moses.ini') if not os.path.exists(mosesini): raise Exception, 'Failed training model'
return {'moses_ini_filename' : mosesini}
return process
if __name__ == '__main__': def __test(): configuration = {'src_lang' : 'en', 'trg_lang' : 'lt', 'moses_installation_dir' : os.environ['MOSES_HOME'], 'giza_installation_dir' : os.environ['GIZA_HOME'], 'translation_model_directory' : 'model-dir'} values = {'training_data_filename' : '/Users/ianjohnson/work/MTM-2012/corpus/training/cleantrain'} from pypeline.helpers.helpers import run_pipeline box_config = configure(configuration) box = initialise(box_config) print run_pipeline(box, values, None)
#do some test __test()123456789101112131415161718192021[Configuration]source_language = entarget_language = ltmax_segment_length = 20corpus_development_size = 1000corpus_evaluation_size = 500alignment_method = grow-diag-final-andreordering_method = msd-bidirectional-fesmoothing_method = improved-kneser-neytokenisation_directory = training/tokenisationtranslation_model_directory = training/modellanguage_model_directory = training/lmmert_directory = training/mertmert_max_no_iterations = 10moses_installation_directory = $(MOSES_HOME)giza_installation_directory = $(GIZA_HOME)irstlm_installation_directory = $(IRSTLM)
[Inputs]src_filename = ../test_data/cleantrain.entrg_filename = ../test_data/cleantrain.lt123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117## Import all of the components to be composed#import components.src_trg_tokeniser as tokeniserimport components.translation_model_training as model_trainingimport components.wrappers.irstlm_build.irstlm_build as lang_modelimport components.wrappers.mert.mert as mert
## Component definition## Config: {model_training.max_segment_length,# model_training.corpus.[development_size|evaluation_size],# model_training.[src|trg].language,# model_training.method.[alignment|reordering], {moses_ini_filename,# model_training.giza.installation, evaluation_data_filename}# {src_filename, {tokenised_src_filename, model_training.translation_model.dir} |# trg_filename} tokenised_trg_filename} +-----------------------------------------+ +-------+ | {moses_ini_filename}# | +-------+ +-------+ +-------+ | +-------+ | tokenised_src_filename -> src_filename, | | Model | V +-------+ |# V | +--->+ Src/ +--->+ | V | +-->+ tokenised_trg_filename -> trg_filename +-->+ Train +------>+ | +------+ V# --->+ Split | | Trg | | Merge +--->+ Split | +-----------------------------------------+ +-------+ | Merge +----->+ MERT +---># | +--->+ Token +--->+ | | +--\ +------------------------------------------+ +--------+ | | ^ +------+# +-------+ +-------+ +-------+ +-------+ \->+ tokenised_trg_filename -> input_filename +-->+ IRSTLM +-->+ | |# Config: {tokeniser.[src|trg].language, +------------------------------------------+ +--------+ ^ +-------+ |# tokeniser.[src|trg].tokeniser_dir Config: {irstlm_installation_dir::String, | |# tokeniser.moses.installation} irstlm_smoothing_method::String, | |# language_model_directory} | |# | |# {lm_filename, compiled_lm_filename, add_start_end_filename} |# |# {moses_ini_file, evaluation_data_filename, trg_language_model_filename,# trg_language_model_order, trg_language_model_type}#component training_pipeline inputs src_filename, trg_filename output moses_ini_filename configuration source_language, target_language, max_segment_length, corpus_development_size, corpus_evaluation_size, alignment_method, reordering_method, smoothing_method, tokenisation_directory, translation_model_directory, language_model_directory, mert_directory, mert_max_no_iterations, moses_installation_directory, giza_installation_directory, irstlm_installation_directory declare tokeniser := new tokeniser with source_language -> tokeniser.src.language, target_language -> tokeniser.trg.language, tokenisation_directory -> tokeniser.src.tokenisation_dir, tokenisation_directory -> tokeniser.trg.tokenisation_dir, moses_installation_directory -> tokeniser.moses.installation model_training := new model_training with max_segment_length -> model_training.max_segment_length, corpus_development_size -> model_training.corpus.development_size, corpus_evaluation_size -> model_training.corpus.evaluation_size, translation_model_directory -> model_training.translation_model.dir, alignment_method -> model_training.method.alignment, reordering_method -> model_training.method.reordering, source_language -> model_training.src.language, moses_installation_directory -> model_training.moses.installation, giza_installation_directory -> model_training.giza.installation, target_language -> model_training.trg.language irstlm := new lang_model with irstlm_installation_directory -> irstlm_installation_dir, smoothing_method -> irstlm_smoothing_method, language_model_directory -> language_model_directory mert := new mert with source_language -> source_language, target_language -> target_language, moses_installation_directory -> moses_installation_dir, mert_directory -> mert_working_directory, mert_max_no_iterations -> mert_max_no_iterations as # Split and transform the input to the tokeniser component # Inputs: src_filename, trg_filename # Outputs: (tokenised_src_filename), (tokenised_trg_filename) (wire src_filename -> src_filename, trg_filename -> _ &&& wire trg_filename -> trg_filename, src_filename -> _) >>> tokeniser >>>
# Merge output from tokeniser # Inputs: (tokenised_src_filename), (tokenised_trg_filename) # Outputs: tokenised_src_filename, tokenised_trg_filename merge top[tokenised_src_filename] -> tokenised_src_filename, bottom[tokenised_trg_filename] -> tokenised_trg_filename >>>
# Train the translation table and target language model # Inputs: tokenised_src_filename, tokenised_trg_filename # Outputs: (moses_ini_filename), ('add_start_end_filename', 'lm_filename', 'compiled_lm_filename') ((wire tokenised_src_filename -> src_filename, tokenised_trg_filename -> trg_filename >>> model_training) &&& (wire tokenised_trg_filename -> input_filename, tokenised_src_filename -> _ >>> irstlm)) >>>
# Merge the output from the TT and LM training component # Inputs: (moses_ini_filename, evaluation_data_filename), # (compiled_lm_filename, add_start_end_filename, lm_filename) # Outputs: moses_ini_filename, evaluation_data_filename, evaluation_data_filename, # trg_language_model_filename, trg_language_model_order, trg_language_model_type merge top[moses_ini_filename] -> moses_ini_filename, top[evaluation_data_filename] -> evaluation_data_filename, bottom[compiled_lm_filename] -> trg_language_model_filename, bottom[add_start_end_filename] -> _, bottom[lm_filename] -> _, 3 -> trg_language_model_order, 9 -> trg_language_model_type >>> mert12checkplf: checkplf.cpp g++ checkplf.cpp -I../../moses/ ../../lib/libmoses.a -o checkplf123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645# Makefile.in generated by automake 1.9.2 from Makefile.am.# @configure_input@
# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,# 2003, 2004 Free Software Foundation, Inc.# This Makefile.in is free software; the Free Software Foundation# gives unlimited permission to copy and/or distribute it,# with or without modifications, as long as this notice is preserved.
# This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY, to the extent permitted by law; without# even the implied warranty of MERCHANTABILITY or FITNESS FOR A# PARTICULAR PURPOSE.
@SET_MAKE@
SOURCES = $(lmserver_SOURCES) $(lmserver_debug_SOURCES)
srcdir = @srcdir@top_srcdir = @top_srcdir@VPATH = @srcdir@pkgdatadir = $(datadir)/@PACKAGE@pkglibdir = $(libdir)/@PACKAGE@pkgincludedir = $(includedir)/@PACKAGE@top_builddir = .am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cdINSTALL = @INSTALL@install_sh_DATA = $(install_sh) -c -m 644install_sh_PROGRAM = $(install_sh) -cinstall_sh_SCRIPT = $(install_sh) -cINSTALL_HEADER = $(INSTALL_DATA)transform = $(program_transform_name)NORMAL_INSTALL = :PRE_INSTALL = :POST_INSTALL = :NORMAL_UNINSTALL = :PRE_UNINSTALL = :POST_UNINSTALL = :build_triplet = @build@host_triplet = @host@target_triplet = @target@bin_PROGRAMS = lmserver$(EXEEXT) lmserver-debug$(EXEEXT)DIST_COMMON = README $(am__configure_deps) $(srcdir)/Makefile.am \ $(srcdir)/Makefile.in $(srcdir)/config.h.in \ $(top_srcdir)/configure AUTHORS COPYING ChangeLog INSTALL NEWS \ compile config.guess config.sub depcomp install-sh missingsubdir = .ACLOCAL_M4 = $(top_srcdir)/aclocal.m4am__aclocal_m4_deps = $(top_srcdir)/configure.acam__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4)am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \ configure.lineno configure.status.linenomkinstalldirs = $(install_sh) -dCONFIG_HEADER = config.hCONFIG_CLEAN_FILES =am__installdirs = "$(DESTDIR)$(bindir)"binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)PROGRAMS = $(bin_PROGRAMS)am_lmserver_OBJECTS = lmserver-lmserver.$(OBJEXT) \ lmserver-thread.$(OBJEXT) lmserver-srilm.$(OBJEXT)lmserver_OBJECTS = $(am_lmserver_OBJECTS)am__objects_1 = lmserver.$(OBJEXT) thread.$(OBJEXT) srilm.$(OBJEXT)am_lmserver_debug_OBJECTS = $(am__objects_1)lmserver_debug_OBJECTS = $(am_lmserver_debug_OBJECTS)DEFAULT_INCLUDES = -I. -I$(srcdir) -I.depcomp = $(SHELL) $(top_srcdir)/depcompam__depfiles_maybe = depfilesCOMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)CCLD = $(CC)LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)CXXLD = $(CXX)CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) \ -o $@SOURCES = $(lmserver_SOURCES) $(lmserver_debug_SOURCES)DIST_SOURCES = $(lmserver_SOURCES) $(lmserver_debug_SOURCES)ETAGS = etagsCTAGS = ctagsDISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)distdir = $(PACKAGE)-$(VERSION)top_distdir = $(distdir)am__remove_distdir = \ { test ! -d $(distdir) \ || { find $(distdir) -type d ! -perm -200 -exec chmod u+w {} ';' \ && rm -fr $(distdir); }; }DIST_ARCHIVES = $(distdir).tar.gzGZIP_ENV = --bestdistuninstallcheck_listfiles = find . -type f -printdistcleancheck_listfiles = find . -type f -printACLOCAL = @ACLOCAL@AMDEP_FALSE = @AMDEP_FALSE@AMDEP_TRUE = @AMDEP_TRUE@AMTAR = @AMTAR@AUTOCONF = @AUTOCONF@AUTOHEADER = @AUTOHEADER@AUTOMAKE = @AUTOMAKE@AWK = @AWK@CC = @CC@CCDEPMODE = @CCDEPMODE@CFLAGS = @CFLAGS@CPP = @CPP@CPPFLAGS = @CPPFLAGS@CXX = @CXX@CXXDEPMODE = @CXXDEPMODE@CXXFLAGS = @CXXFLAGS@CYGPATH_W = @CYGPATH_W@DAEMON_OBJ = @DAEMON_OBJ@DEFS = @DEFS@DEPDIR = @DEPDIR@ECHO_C = @ECHO_C@ECHO_N = @ECHO_N@ECHO_T = @ECHO_T@EGREP = @EGREP@EXEEXT = @EXEEXT@INSTALL_DATA = @INSTALL_DATA@INSTALL_PROGRAM = @INSTALL_PROGRAM@INSTALL_SCRIPT = @INSTALL_SCRIPT@INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@LDFLAGS = @LDFLAGS@LIBOBJS = @LIBOBJS@LIBS = @LIBS@LTLIBOBJS = @LTLIBOBJS@MAKEINFO = @MAKEINFO@OBJEXT = @OBJEXT@PACKAGE = @PACKAGE@PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@PACKAGE_NAME = @PACKAGE_NAME@PACKAGE_STRING = @PACKAGE_STRING@PACKAGE_TARNAME = @PACKAGE_TARNAME@PACKAGE_VERSION = @PACKAGE_VERSION@PATH_SEPARATOR = @PATH_SEPARATOR@SET_MAKE = @SET_MAKE@SHELL = @SHELL@SRI_LM_FALSE = @SRI_LM_FALSE@SRI_LM_TRUE = @SRI_LM_TRUE@STRIP = @STRIP@VERSION = @VERSION@ac_ct_CC = @ac_ct_CC@ac_ct_CXX = @ac_ct_CXX@ac_ct_STRIP = @ac_ct_STRIP@am__fastdepCC_FALSE = @am__fastdepCC_FALSE@am__fastdepCC_TRUE = @am__fastdepCC_TRUE@am__fastdepCXX_FALSE = @am__fastdepCXX_FALSE@am__fastdepCXX_TRUE = @am__fastdepCXX_TRUE@am__include = @am__include@am__leading_dot = @am__leading_dot@am__quote = @am__quote@am__tar = @am__tar@am__untar = @am__untar@bindir = @bindir@build = @build@build_alias = @build_alias@build_cpu = @build_cpu@build_os = @build_os@build_vendor = @build_vendor@datadir = @datadir@exec_prefix = @exec_prefix@host = @host@host_alias = @host_alias@host_cpu = @host_cpu@host_os = @host_os@host_vendor = @host_vendor@includedir = @includedir@infodir = @infodir@install_sh = @install_sh@libdir = @libdir@libexecdir = @libexecdir@localstatedir = @localstatedir@mandir = @mandir@mkdir_p = @mkdir_p@oldincludedir = @oldincludedir@prefix = @prefix@program_transform_name = @program_transform_name@sbindir = @sbindir@sharedstatedir = @sharedstatedir@sysconfdir = @sysconfdir@target = @target@target_alias = @target_alias@target_cpu = @target_cpu@target_os = @target_os@target_vendor = @target_vendor@lmserver_SOURCES = lmserver.c lmserver.h thread.c srilm.cclmserver_debug_SOURCES = $(lmserver_SOURCES)lmserver_CPPFLAGS = -DNDEBUGlmserver_LDADD = @DAEMON_OBJ@ lmserver_debug_LDADD = @DAEMON_OBJ@lmserver_DEPENDENCIES = @DAEMON_OBJ@lmserver_debug_DEPENDENCIES = @DAEMON_OBJ@DIST_DIRS = examplesEXTRA_DIST = examples daemon.call: config.h $(MAKE) $(AM_MAKEFLAGS) all-am
.SUFFIXES:.SUFFIXES: .c .cc .o .objam--refresh: @:$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) @for dep in $?; do \ case '$(am__configure_deps)' in \ *$$dep*) \ echo ' cd $(srcdir) && $(AUTOMAKE) --gnu '; \ cd $(srcdir) && $(AUTOMAKE) --gnu \ && exit 0; \ exit 1;; \ esac; \ done; \ echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu Makefile'; \ cd $(top_srcdir) && \ $(AUTOMAKE) --gnu Makefile.PRECIOUS: MakefileMakefile: $(srcdir)/Makefile.in $(top_builddir)/config.status @case '$?' in \ *config.status*) \ echo ' $(SHELL) ./config.status'; \ $(SHELL) ./config.status;; \ *) \ echo ' cd $(top_builddir) && $(SHELL) ./config.status $@ $(am__depfiles_maybe)'; \ cd $(top_builddir) && $(SHELL) ./config.status $@ $(am__depfiles_maybe);; \ esac;
$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) $(SHELL) ./config.status --recheck
$(top_srcdir)/configure: $(am__configure_deps) cd $(srcdir) && $(AUTOCONF)$(ACLOCAL_M4): $(am__aclocal_m4_deps) cd $(srcdir) && $(ACLOCAL) $(ACLOCAL_AMFLAGS)
config.h: stamp-h1 @if test ! -f $@; then \ rm -f stamp-h1; \ $(MAKE) stamp-h1; \ else :; fi
stamp-h1: $(srcdir)/config.h.in $(top_builddir)/config.status @rm -f stamp-h1 cd $(top_builddir) && $(SHELL) ./config.status config.h$(srcdir)/config.h.in: $(am__configure_deps) cd $(top_srcdir) && $(AUTOHEADER) rm -f stamp-h1 touch $@
distclean-hdr: -rm -f config.h stamp-h1install-binPROGRAMS: $(bin_PROGRAMS) @$(NORMAL_INSTALL) test -z "$(bindir)" || $(mkdir_p) "$(DESTDIR)$(bindir)" @list='$(bin_PROGRAMS)'; for p in $$list; do \ p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \ if test -f $$p \ ; then \ f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \ echo " $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) '$$p' '$(DESTDIR)$(bindir)/$$f'"; \ $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) "$$p" "$(DESTDIR)$(bindir)/$$f" || exit 1; \ else :; fi; \ done
uninstall-binPROGRAMS: @$(NORMAL_UNINSTALL) @list='$(bin_PROGRAMS)'; for p in $$list; do \ f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \ echo " rm -f '$(DESTDIR)$(bindir)/$$f'"; \ rm -f "$(DESTDIR)$(bindir)/$$f"; \ done
clean-binPROGRAMS: -test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS)lmserver$(EXEEXT): $(lmserver_OBJECTS) $(lmserver_DEPENDENCIES) @rm -f lmserver$(EXEEXT) $(CXXLINK) $(lmserver_LDFLAGS) $(lmserver_OBJECTS) $(lmserver_LDADD) $(LIBS)lmserver-debug$(EXEEXT): $(lmserver_debug_OBJECTS) $(lmserver_debug_DEPENDENCIES) @rm -f lmserver-debug$(EXEEXT) $(CXXLINK) $(lmserver_debug_LDFLAGS) $(lmserver_debug_OBJECTS) $(lmserver_debug_LDADD) $(LIBS)
mostlyclean-compile: -rm -f *.$(OBJEXT)
distclean-compile: -rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lmserver-lmserver.Po@am__quote@@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lmserver-srilm.Po@am__quote@@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lmserver-thread.Po@am__quote@@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lmserver.Po@am__quote@@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/srilm.Po@am__quote@@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thread.Po@am__quote@
.c.o:@am__fastdepCC_TRUE@ if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCC_FALSE@ $(COMPILE) -c $<
.c.obj:@am__fastdepCC_TRUE@ if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ `$(CYGPATH_W) '$<'`; \@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCC_FALSE@ $(COMPILE) -c `$(CYGPATH_W) '$<'`
lmserver-lmserver.o: lmserver.c@am__fastdepCC_TRUE@ if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT lmserver-lmserver.o -MD -MP -MF "$(DEPDIR)/lmserver-lmserver.Tpo" -c -o lmserver-lmserver.o `test -f 'lmserver.c' || echo '$(srcdir)/'`lmserver.c; \@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/lmserver-lmserver.Tpo" "$(DEPDIR)/lmserver-lmserver.Po"; else rm -f "$(DEPDIR)/lmserver-lmserver.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='lmserver.c' object='lmserver-lmserver.o' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o lmserver-lmserver.o `test -f 'lmserver.c' || echo '$(srcdir)/'`lmserver.c
lmserver-lmserver.obj: lmserver.c@am__fastdepCC_TRUE@ if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT lmserver-lmserver.obj -MD -MP -MF "$(DEPDIR)/lmserver-lmserver.Tpo" -c -o lmserver-lmserver.obj `if test -f 'lmserver.c'; then $(CYGPATH_W) 'lmserver.c'; else $(CYGPATH_W) '$(srcdir)/lmserver.c'; fi`; \@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/lmserver-lmserver.Tpo" "$(DEPDIR)/lmserver-lmserver.Po"; else rm -f "$(DEPDIR)/lmserver-lmserver.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='lmserver.c' object='lmserver-lmserver.obj' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o lmserver-lmserver.obj `if test -f 'lmserver.c'; then $(CYGPATH_W) 'lmserver.c'; else $(CYGPATH_W) '$(srcdir)/lmserver.c'; fi`
lmserver-thread.o: thread.c@am__fastdepCC_TRUE@ if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT lmserver-thread.o -MD -MP -MF "$(DEPDIR)/lmserver-thread.Tpo" -c -o lmserver-thread.o `test -f 'thread.c' || echo '$(srcdir)/'`thread.c; \@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/lmserver-thread.Tpo" "$(DEPDIR)/lmserver-thread.Po"; else rm -f "$(DEPDIR)/lmserver-thread.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='thread.c' object='lmserver-thread.o' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o lmserver-thread.o `test -f 'thread.c' || echo '$(srcdir)/'`thread.c
lmserver-thread.obj: thread.c@am__fastdepCC_TRUE@ if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT lmserver-thread.obj -MD -MP -MF "$(DEPDIR)/lmserver-thread.Tpo" -c -o lmserver-thread.obj `if test -f 'thread.c'; then $(CYGPATH_W) 'thread.c'; else $(CYGPATH_W) '$(srcdir)/thread.c'; fi`; \@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/lmserver-thread.Tpo" "$(DEPDIR)/lmserver-thread.Po"; else rm -f "$(DEPDIR)/lmserver-thread.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='thread.c' object='lmserver-thread.obj' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o lmserver-thread.obj `if test -f 'thread.c'; then $(CYGPATH_W) 'thread.c'; else $(CYGPATH_W) '$(srcdir)/thread.c'; fi`
.cc.o:@am__fastdepCXX_TRUE@ if $(CXXCOMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ $<
.cc.obj:@am__fastdepCXX_TRUE@ if $(CXXCOMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ `$(CYGPATH_W) '$<'`; \@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
lmserver-srilm.o: srilm.cc@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT lmserver-srilm.o -MD -MP -MF "$(DEPDIR)/lmserver-srilm.Tpo" -c -o lmserver-srilm.o `test -f 'srilm.cc' || echo '$(srcdir)/'`srilm.cc; \@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/lmserver-srilm.Tpo" "$(DEPDIR)/lmserver-srilm.Po"; else rm -f "$(DEPDIR)/lmserver-srilm.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='srilm.cc' object='lmserver-srilm.o' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o lmserver-srilm.o `test -f 'srilm.cc' || echo '$(srcdir)/'`srilm.cc
lmserver-srilm.obj: srilm.cc@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT lmserver-srilm.obj -MD -MP -MF "$(DEPDIR)/lmserver-srilm.Tpo" -c -o lmserver-srilm.obj `if test -f 'srilm.cc'; then $(CYGPATH_W) 'srilm.cc'; else $(CYGPATH_W) '$(srcdir)/srilm.cc'; fi`; \@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/lmserver-srilm.Tpo" "$(DEPDIR)/lmserver-srilm.Po"; else rm -f "$(DEPDIR)/lmserver-srilm.Tpo"; exit 1; fi@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='srilm.cc' object='lmserver-srilm.obj' libtool=no @AMDEPBACKSLASH@@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(lmserver_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o lmserver-srilm.obj `if test -f 'srilm.cc'; then $(CYGPATH_W) 'srilm.cc'; else $(CYGPATH_W) '$(srcdir)/srilm.cc'; fi`uninstall-info-am:
ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ unique=`for i in $$list; do \ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ done | \ $(AWK) ' { files[$$0] = 1; } \ END { for (i in files) print i; }'`; \ mkid -fID $$uniquetags: TAGS
TAGS: $(HEADERS) $(SOURCES) config.h.in $(TAGS_DEPENDENCIES) \ $(TAGS_FILES) $(LISP) tags=; \ here=`pwd`; \ list='$(SOURCES) $(HEADERS) config.h.in $(LISP) $(TAGS_FILES)'; \ unique=`for i in $$list; do \ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ done | \ $(AWK) ' { files[$$0] = 1; } \ END { for (i in files) print i; }'`; \ if test -z "$(ETAGS_ARGS)$$tags$$unique"; then :; else \ test -n "$$unique" || unique=$$empty_fix; \ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ $$tags $$unique; \ fictags: CTAGSCTAGS: $(HEADERS) $(SOURCES) config.h.in $(TAGS_DEPENDENCIES) \ $(TAGS_FILES) $(LISP) tags=; \ here=`pwd`; \ list='$(SOURCES) $(HEADERS) config.h.in $(LISP) $(TAGS_FILES)'; \ unique=`for i in $$list; do \ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ done | \ $(AWK) ' { files[$$0] = 1; } \ END { for (i in files) print i; }'`; \ test -z "$(CTAGS_ARGS)$$tags$$unique" \ || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ $$tags $$unique
GTAGS: here=`$(am__cd) $(top_builddir) && pwd` \ && cd $(top_srcdir) \ && gtags -i $(GTAGS_ARGS) $$here
distclean-tags: -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
distdir: $(DISTFILES) $(am__remove_distdir) mkdir $(distdir) @srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; \ topsrcdirstrip=`echo "$(top_srcdir)" | sed 's|.|.|g'`; \ list='$(DISTFILES)'; for file in $$list; do \ case $$file in \ $(srcdir)/*) file=`echo "$$file" | sed "s|^$$srcdirstrip/||"`;; \ $(top_srcdir)/*) file=`echo "$$file" | sed "s|^$$topsrcdirstrip/|$(top_builddir)/|"`;; \ esac; \ if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \ if test "$$dir" != "$$file" && test "$$dir" != "."; then \ dir="/$$dir"; \ $(mkdir_p) "$(distdir)$$dir"; \ else \ dir=''; \ fi; \ if test -d $$d/$$file; then \ if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ fi; \ cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ else \ test -f $(distdir)/$$file \ || cp -p $$d/$$file $(distdir)/$$file \ || exit 1; \ fi; \ done $(MAKE) $(AM_MAKEFLAGS) \ top_distdir="$(top_distdir)" distdir="$(distdir)" \ dist-hook -find $(distdir) -type d ! -perm -777 -exec chmod a+rwx {} \; -o \ ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \ ! -type d ! -perm -400 -exec chmod a+r {} \; -o \ ! -type d ! -perm -444 -exec $(SHELL) $(install_sh) -c -m a+r {} {} \; \ || chmod -R a+r $(distdir)dist-gzip: distdir tardir=$(distdir) && $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).tar.gz $(am__remove_distdir)
dist-bzip2: distdir tardir=$(distdir) && $(am__tar) | bzip2 -9 -c >$(distdir).tar.bz2 $(am__remove_distdir)
dist-tarZ: distdir tardir=$(distdir) && $(am__tar) | compress -c >$(distdir).tar.Z $(am__remove_distdir)
dist-shar: distdir shar $(distdir) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).shar.gz $(am__remove_distdir)
dist-zip: distdir -rm -f $(distdir).zip zip -rq $(distdir).zip $(distdir) $(am__remove_distdir)
dist dist-all: distdir tardir=$(distdir) && $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).tar.gz $(am__remove_distdir)
# This target untars the dist file and tries a VPATH configuration. Then# it guarantees that the distribution is self-contained by making another# tarfile.distcheck: dist case '$(DIST_ARCHIVES)' in \ *.tar.gz*) \ GZIP=$(GZIP_ENV) gunzip -c $(distdir).tar.gz | $(am__untar) ;;\ *.tar.bz2*) \ bunzip2 -c $(distdir).tar.bz2 | $(am__untar) ;;\ *.tar.Z*) \ uncompress -c $(distdir).tar.Z | $(am__untar) ;;\ *.shar.gz*) \ GZIP=$(GZIP_ENV) gunzip -c $(distdir).shar.gz | unshar ;;\ *.zip*) \ unzip $(distdir).zip ;;\ esac chmod -R a-w $(distdir); chmod a+w $(distdir) mkdir $(distdir)/_build mkdir $(distdir)/_inst chmod a-w $(distdir) dc_install_base=`$(am__cd) $(distdir)/_inst && pwd | sed -e 's,^[^:\\/]:[\\/],/,'` \ && dc_destdir="$${TMPDIR-/tmp}/am-dc-$$$$/" \ && cd $(distdir)/_build \ && ../configure --srcdir=.. --prefix="$$dc_install_base" \ $(DISTCHECK_CONFIGURE_FLAGS) \ && $(MAKE) $(AM_MAKEFLAGS) \ && $(MAKE) $(AM_MAKEFLAGS) dvi \ && $(MAKE) $(AM_MAKEFLAGS) check \ && $(MAKE) $(AM_MAKEFLAGS) install \ && $(MAKE) $(AM_MAKEFLAGS) installcheck \ && $(MAKE) $(AM_MAKEFLAGS) uninstall \ && $(MAKE) $(AM_MAKEFLAGS) distuninstallcheck_dir="$$dc_install_base" \ distuninstallcheck \ && chmod -R a-w "$$dc_install_base" \ && ({ \ (cd ../.. && umask 077 && mkdir "$$dc_destdir") \ && $(MAKE) $(AM_MAKEFLAGS) DESTDIR="$$dc_destdir" install \ && $(MAKE) $(AM_MAKEFLAGS) DESTDIR="$$dc_destdir" uninstall \ && $(MAKE) $(AM_MAKEFLAGS) DESTDIR="$$dc_destdir" \ distuninstallcheck_dir="$$dc_destdir" distuninstallcheck; \ } || { rm -rf "$$dc_destdir"; exit 1; }) \ && rm -rf "$$dc_destdir" \ && $(MAKE) $(AM_MAKEFLAGS) dist \ && rm -rf $(DIST_ARCHIVES) \ && $(MAKE) $(AM_MAKEFLAGS) distcleancheck $(am__remove_distdir) @(echo "$(distdir) archives ready for distribution: "; \ list='$(DIST_ARCHIVES)'; for i in $$list; do echo $$i; done) | \ sed -e '1{h;s/./=/g;p;x;}' -e '$${p;x;}'distuninstallcheck: @cd $(distuninstallcheck_dir) \ && test `$(distuninstallcheck_listfiles) | wc -l` -le 1 \ || { echo "ERROR: files left after uninstall:" ; \ if test -n "$(DESTDIR)"; then \ echo " (check DESTDIR support)"; \ fi ; \ $(distuninstallcheck_listfiles) ; \ exit 1; } >&2distcleancheck: distclean @if test '$(srcdir)' = . ; then \ echo "ERROR: distcleancheck can only run from a VPATH build" ; \ exit 1 ; \ fi @test `$(distcleancheck_listfiles) | wc -l` -eq 0 \ || { echo "ERROR: files left in build directory after distclean:" ; \ $(distcleancheck_listfiles) ; \ exit 1; } >&2check-am: all-amcheck: check-amall-am: Makefile $(PROGRAMS) config.hinstalldirs: for dir in "$(DESTDIR)$(bindir)"; do \ test -z "$$dir" || $(mkdir_p) "$$dir"; \ doneinstall: install-aminstall-exec: install-exec-aminstall-data: install-data-amuninstall: uninstall-am
install-am: all-am @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
installcheck: installcheck-aminstall-strip: $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ `test -z '$(STRIP)' || \ echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` installmostlyclean-generic:
clean-generic:
distclean-generic: -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
maintainer-clean-generic: @echo "This command is intended for maintainers to use" @echo "it deletes files that may require special tools to rebuild."clean: clean-am
clean-am: clean-binPROGRAMS clean-generic mostlyclean-am
distclean: distclean-am -rm -f $(am__CONFIG_DISTCLEAN_FILES) -rm -rf ./$(DEPDIR) -rm -f Makefiledistclean-am: clean-am distclean-compile distclean-generic \ distclean-hdr distclean-tags
dvi: dvi-am
dvi-am:
html: html-am
info: info-am
info-am:
install-data-am:
install-exec-am: install-binPROGRAMS
install-info: install-info-am
install-man:
installcheck-am:
maintainer-clean: maintainer-clean-am -rm -f $(am__CONFIG_DISTCLEAN_FILES) -rm -rf $(top_srcdir)/autom4te.cache -rm -rf ./$(DEPDIR) -rm -f Makefilemaintainer-clean-am: distclean-am maintainer-clean-generic
mostlyclean: mostlyclean-am
mostlyclean-am: mostlyclean-compile mostlyclean-generic
pdf: pdf-am
pdf-am:
ps: ps-am
ps-am:
uninstall-am: uninstall-binPROGRAMS uninstall-info-am
.PHONY: CTAGS GTAGS all all-am am--refresh check check-am clean \ clean-binPROGRAMS clean-generic ctags dist dist-all dist-bzip2 \ dist-gzip dist-hook dist-shar dist-tarZ dist-zip distcheck \ distclean distclean-compile distclean-generic distclean-hdr \ distclean-tags distcleancheck distdir distuninstallcheck dvi \ dvi-am html html-am info info-am install install-am \ install-binPROGRAMS install-data install-data-am install-exec \ install-exec-am install-info install-info-am install-man \ install-strip installcheck installcheck-am installdirs \ maintainer-clean maintainer-clean-generic mostlyclean \ mostlyclean-compile mostlyclean-generic pdf pdf-am ps ps-am \ tags uninstall uninstall-am uninstall-binPROGRAMS \ uninstall-info-am
dist-hook: rm -rf $(distdir)/doc/.svn/ rm -rf $(distdir)/scripts/.svn/ rm -rf $(distdir)/t/.svn/ rm -rf $(distdir)/t/lib/.svn/# Tell versions [3.59,3.63) of GNU make to not export all variables.# Otherwise a system limit (for SysV at least) may be exceeded..NOEXPORT:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768/* $Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $ *//* $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $ *//*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */
#if defined __SUNPRO_C || defined __DECC || defined __HP_cc# pragma ident "@(#)$Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $"# pragma ident "$NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $"#endif
#include <fcntl.h>#include <stdlib.h>#include <unistd.h>
int daemon(int nochdir, int noclose){ int fd;
switch (fork()) { case -1: return (-1); case 0: break; default: _exit(EXIT_SUCCESS); }
if (setsid() == -1) return (-1);
if (nochdir == 0) (void)chdir("/");
if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) { (void)dup2(fd, STDIN_FILENO); (void)dup2(fd, STDOUT_FILENO); (void)dup2(fd, STDERR_FILENO); if (fd > STDERR_FILENO) (void)close(fd); } return (0);}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- *//* * memcached - memory caching daemon * * http://www.danga.com/memcached/ * * Copyright 2003 Danga Interactive, Inc. All rights reserved. * * Use and distribution licensed under the BSD license. See * the LICENSE file for full text. * * Authors: * Anatoly Vorobey <mellon@pobox.com> * Brad Fitzpatrick <brad@danga.com>std * * $Id$ */#include "lmserver.h"#include "srilm.h"#include <sys/stat.h>#include <sys/socket.h>#include <sys/un.h>#include <signal.h>#include <sys/resource.h>#include <sys/uio.h>
/* some POSIX systems need the following definition * to get mlockall flags out of sys/mman.h. */#ifndef _P1003_1B_VISIBLE#define _P1003_1B_VISIBLE#endif/* need this to get IOV_MAX on some platforms. */#ifndef __need_IOV_MAX#define __need_IOV_MAX#endif#include <pwd.h>#include <sys/mman.h>#include <fcntl.h>#include <netinet/tcp.h>#include <arpa/inet.h>#include <errno.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <time.h>#include <assert.h>#include <limits.h>
#ifdef HAVE_MALLOC_H/* OpenBSD has a malloc.h, but warns to use stdlib.h instead */#ifndef __OpenBSD__#include <malloc.h>#endif#endif
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */#ifndef IOV_MAX#if defined(__FreeBSD__) || defined(__APPLE__)# define IOV_MAX 1024#endif#endif
/* * forward declarations */static void drive_machine(conn *c);static int new_socket(struct addrinfo *ai);static int server_socket(const int port, const bool is_udp);static int try_read_command(conn *c);static int try_read_network(conn *c);static int try_read_udp(conn *c);
/* stats */static void stats_reset(void);static void stats_init(void);
/* defaults */static void settings_init(void);
/* event handling, network IO */static void event_handler(const int fd, const short which, void *arg);static void conn_close(conn *c);static void conn_init(void);static void accept_new_conns(const bool do_accept);static bool update_event(conn *c, const int new_flags);static void complete_nread(conn *c);static void process_command(conn *c, char *command);static int transmit(conn *c);static int ensure_iov_space(conn *c);static int add_iov(conn *c, const void *buf, int len);static int add_msghdr(conn *c);
/* time handling */static void set_current_time(void); /* update the global variable holding global 32-bit seconds-since-start time (to avoid 64 bit time_t) */
static void conn_free(conn *c);
/** exported globals **/struct stats stats;struct settings settings;
/** file scope variables **/static item **todelete = NULL;static int delcurr;static int deltotal;static conn *listen_conn = NULL;static struct event_base *main_base;
#define TRANSMIT_COMPLETE 0#define TRANSMIT_INCOMPLETE 1#define TRANSMIT_SOFT_ERROR 2#define TRANSMIT_HARD_ERROR 3
static int *buckets = 0; /* bucket->generation array for a managed instance */
#define REALTIME_MAXDELTA 60*60*24*30/* * given time value that's either unix time or delta from current unix time, return * unix time. Use the fact that delta can't exceed one month (and real time value can't * be that low). */static rel_time_t realtime(const time_t exptime) { /* no. of seconds in 30 days - largest possible delta exptime */
if (exptime == 0) return 0; /* 0 means never expire */
if (exptime > REALTIME_MAXDELTA) { /* if item expiration is at/before the server started, give it an expiration time of 1 second after the server started. (because 0 means don't expire). without this, we'd underflow and wrap around to some large value way in the future, effectively making items expiring in the past really expiring never */ if (exptime <= stats.started) return (rel_time_t)1; return (rel_time_t)(exptime - stats.started); } else { return (rel_time_t)(exptime + current_time); }}
static void stats_init(void) { stats.curr_items = stats.total_items = stats.curr_conns = stats.total_conns = stats.conn_structs = 0; stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = stats.evictions = 0; stats.curr_bytes = stats.bytes_read = stats.bytes_written = 0;
/* make the time we started always be 2 seconds before we really did, so time(0) - time.started is never zero. if so, things like 'settings.oldest_live' which act as booleans as well as values are now false in boolean context... */ stats.started = time(0) - 2;}
static void stats_reset(void) { STATS_LOCK(); stats.total_items = stats.total_conns = 0; stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = stats.evictions = 0; stats.bytes_read = stats.bytes_written = 0; STATS_UNLOCK();}
static void settings_init(void) { settings.srilm = NULL; settings.srilm_order = 3; settings.access=0700; settings.port = 11211; settings.udpport = 0; /* By default this string should be NULL for getaddrinfo() */ settings.inter = NULL; settings.maxbytes = 64 * 1024 * 1024; /* default is 64MB */ settings.maxconns = 1024; /* to limit connections-related memory to about 5MB */ settings.verbose = 0; settings.oldest_live = 0; settings.evict_to_free = 1; /* push old items out of cache when memory runs out */ settings.socketpath = NULL; /* by default, not using a unix socket */ settings.managed = false; settings.factor = 1.25; settings.chunk_size = 48; /* space for a modest key and value */#ifdef USE_THREADS settings.num_threads = 4;#else settings.num_threads = 1;#endif settings.detail_enabled = 0;}
/* returns true if a deleted item's delete-locked-time is over, and it should be removed from the namespace */static bool item_delete_lock_over (item *it) { assert(it->it_flags & ITEM_DELETED); return (current_time >= it->exptime);}
/* * Adds a message header to a connection. * * Returns 0 on success, -1 on out-of-memory. */static int add_msghdr(conn *c){ struct msghdr *msg;
assert(c != NULL);
if (c->msgsize == c->msgused) { msg = realloc(c->msglist, c->msgsize * 2 * sizeof(struct msghdr)); if (! msg) return -1; c->msglist = msg; c->msgsize *= 2; }
msg = c->msglist + c->msgused;
/* this wipes msg_iovlen, msg_control, msg_controllen, and msg_flags, the last 3 of which aren't defined on solaris: */ memset(msg, 0, sizeof(struct msghdr));
msg->msg_iov = &c->iov[c->iovused];
if (c->request_addr_size > 0) { msg->msg_name = &c->request_addr; msg->msg_namelen = c->request_addr_size; }
c->msgbytes = 0; c->msgused++;
if (c->udp) { /* Leave room for the UDP header, which we'll fill in later. */ return add_iov(c, NULL, UDP_HEADER_SIZE); }
return 0;}
/* * Free list management for connections. */
static conn **freeconns;static int freetotal;static int freecurr;
static void conn_init(void) { freetotal = 200; freecurr = 0; if ((freeconns = (conn **)malloc(sizeof(conn *) * freetotal)) == NULL) { fprintf(stderr, "malloc()\n"); } return;}
/* * Returns a connection from the freelist, if any. Should call this using * conn_from_freelist() for thread safety. */conn *do_conn_from_freelist() { conn *c;
if (freecurr > 0) { c = freeconns[--freecurr]; } else { c = NULL; }
return c;}
/* * Adds a connection to the freelist. 0 = success. Should call this using * conn_add_to_freelist() for thread safety. */bool do_conn_add_to_freelist(conn *c) { if (freecurr < freetotal) { freeconns[freecurr++] = c; return false; } else { /* try to enlarge free connections array */ conn **new_freeconns = realloc(freeconns, sizeof(conn *) * freetotal * 2); if (new_freeconns) { freetotal *= 2; freeconns = new_freeconns; freeconns[freecurr++] = c; return false; } } return true;}
conn *conn_new(const int sfd, const int init_state, const int event_flags, const int read_buffer_size, const bool is_udp, struct event_base *base) { conn *c = conn_from_freelist();
if (NULL == c) { if (!(c = (conn *)calloc(1, sizeof(conn)))) { fprintf(stderr, "calloc()\n"); return NULL; }
c->rbuf = c->wbuf = 0; c->ilist = 0; c->suffixlist = 0; c->iov = 0; c->msglist = 0; c->hdrbuf = 0;
c->rsize = read_buffer_size; c->wsize = DATA_BUFFER_SIZE; c->isize = ITEM_LIST_INITIAL; c->suffixsize = SUFFIX_LIST_INITIAL; c->iovsize = IOV_LIST_INITIAL; c->msgsize = MSG_LIST_INITIAL; c->hdrsize = 0;
c->rbuf = (char *)malloc((size_t)c->rsize); c->wbuf = (char *)malloc((size_t)c->wsize); c->ilist = (item **)malloc(sizeof(item *) * c->isize); c->suffixlist = (char **)malloc(sizeof(char *) * c->suffixsize); c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize); c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize);
if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 || c->msglist == 0 || c->suffixlist == 0) { conn_free(c); fprintf(stderr, "malloc()\n"); return NULL; }
STATS_LOCK(); stats.conn_structs++; STATS_UNLOCK(); }
if (settings.verbose > 1) { if (init_state == conn_listening) fprintf(stderr, "<%d server listening\n", sfd); else if (is_udp) fprintf(stderr, "<%d server listening (udp)\n", sfd); else fprintf(stderr, "<%d new client connection\n", sfd); }
c->sfd = sfd; c->udp = is_udp; c->state = init_state; c->rlbytes = 0; c->rbytes = c->wbytes = 0; c->wcurr = c->wbuf; c->rcurr = c->rbuf; c->ritem = 0; c->icurr = c->ilist; c->suffixcurr = c->suffixlist; c->ileft = 0; c->suffixleft = 0; c->iovused = 0; c->msgcurr = 0; c->msgused = 0;
c->write_and_go = conn_read; c->write_and_free = 0; c->item = 0; c->bucket = -1; c->gen = 0;
c->noreply = false;
event_set(&c->event, sfd, event_flags, event_handler, (void *)c); event_base_set(base, &c->event); c->ev_flags = event_flags;
if (event_add(&c->event, 0) == -1) { if (conn_add_to_freelist(c)) { conn_free(c); } perror("event_add"); return NULL; }
STATS_LOCK(); stats.curr_conns++; stats.total_conns++; STATS_UNLOCK();
return c;}
static void conn_cleanup(conn *c) { assert(c != NULL);
if (c->write_and_free) { free(c->write_and_free); c->write_and_free = 0; }}
/* * Frees a connection. */void conn_free(conn *c) { if (c) { if (c->hdrbuf) free(c->hdrbuf); if (c->msglist) free(c->msglist); if (c->rbuf) free(c->rbuf); if (c->wbuf) free(c->wbuf); if (c->ilist) free(c->ilist); if (c->suffixlist) free(c->suffixlist); if (c->iov) free(c->iov); free(c); }}
static void conn_close(conn *c) { assert(c != NULL);
/* delete the event, the socket and the conn */ event_del(&c->event);
if (settings.verbose > 1) fprintf(stderr, "<%d connection closed.\n", c->sfd);
close(c->sfd); accept_new_conns(true); conn_cleanup(c);
/* if the connection has big buffers, just free it */ if (c->rsize > READ_BUFFER_HIGHWAT || conn_add_to_freelist(c)) { conn_free(c); }
STATS_LOCK(); stats.curr_conns--; STATS_UNLOCK();
return;}
/* * Shrinks a connection's buffers if they're too big. This prevents * periodic large "get" requests from permanently chewing lots of server * memory. * * This should only be called in between requests since it can wipe output * buffers! */static void conn_shrink(conn *c) { assert(c != NULL);
if (c->udp) return;
if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { char *newbuf;
if (c->rcurr != c->rbuf) memmove(c->rbuf, c->rcurr, (size_t)c->rbytes);
newbuf = (char *)realloc((void *)c->rbuf, DATA_BUFFER_SIZE);
if (newbuf) { c->rbuf = newbuf; c->rsize = DATA_BUFFER_SIZE; } /* TODO check other branch... */ c->rcurr = c->rbuf; }
if (c->isize > ITEM_LIST_HIGHWAT) { item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0])); if (newbuf) { c->ilist = newbuf; c->isize = ITEM_LIST_INITIAL; } /* TODO check error condition? */ }
if (c->msgsize > MSG_LIST_HIGHWAT) { struct msghdr *newbuf = (struct msghdr *) realloc((void *)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); if (newbuf) { c->msglist = newbuf; c->msgsize = MSG_LIST_INITIAL; } /* TODO check error condition? */ }
if (c->iovsize > IOV_LIST_HIGHWAT) { struct iovec *newbuf = (struct iovec *) realloc((void *)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); if (newbuf) { c->iov = newbuf; c->iovsize = IOV_LIST_INITIAL; } /* TODO check return value */ }}
/* * Sets a connection's current state in the state machine. Any special * processing that needs to happen on certain state transitions can * happen here. */static void conn_set_state(conn *c, int state) { assert(c != NULL);
if (state != c->state) { if (state == conn_read) { conn_shrink(c); //assoc_move_next_bucket(); } c->state = state; }}
/* * Ensures that there is room for another struct iovec in a connection's * iov list. * * Returns 0 on success, -1 on out-of-memory. */static int ensure_iov_space(conn *c) { assert(c != NULL);
if (c->iovused >= c->iovsize) { int i, iovnum; struct iovec *new_iov = (struct iovec *)realloc(c->iov, (c->iovsize * 2) * sizeof(struct iovec)); if (! new_iov) return -1; c->iov = new_iov; c->iovsize *= 2;
/* Point all the msghdr structures at the new list. */ for (i = 0, iovnum = 0; i < c->msgused; i++) { c->msglist[i].msg_iov = &c->iov[iovnum]; iovnum += c->msglist[i].msg_iovlen; } }
return 0;}
/* * Adds data to the list of pending data that will be written out to a * connection. * * Returns 0 on success, -1 on out-of-memory. */
static int add_iov(conn *c, const void *buf, int len) { struct msghdr *m; int leftover; bool limit_to_mtu;
assert(c != NULL);
do { m = &c->msglist[c->msgused - 1];
/* * Limit UDP packets, and the first payloads of TCP replies, to * UDP_MAX_PAYLOAD_SIZE bytes. */ limit_to_mtu = c->udp || (1 == c->msgused);
/* We may need to start a new msghdr if this one is full. */ if (m->msg_iovlen == IOV_MAX || (limit_to_mtu && c->msgbytes >= UDP_MAX_PAYLOAD_SIZE)) { add_msghdr(c); m = &c->msglist[c->msgused - 1]; }
if (ensure_iov_space(c) != 0) return -1;
/* If the fragment is too big to fit in the datagram, split it up */ if (limit_to_mtu && len + c->msgbytes > UDP_MAX_PAYLOAD_SIZE) { leftover = len + c->msgbytes - UDP_MAX_PAYLOAD_SIZE; len -= leftover; } else { leftover = 0; }
m = &c->msglist[c->msgused - 1]; m->msg_iov[m->msg_iovlen].iov_base = (void *)buf; m->msg_iov[m->msg_iovlen].iov_len = len;
c->msgbytes += len; c->iovused++; m->msg_iovlen++;
buf = ((char *)buf) + len; len = leftover; } while (leftover > 0);
return 0;}
/* * Constructs a set of UDP headers and attaches them to the outgoing messages. */static int build_udp_headers(conn *c) { int i; unsigned char *hdr;
assert(c != NULL);
if (c->msgused > c->hdrsize) { void *new_hdrbuf; if (c->hdrbuf) new_hdrbuf = realloc(c->hdrbuf, c->msgused * 2 * UDP_HEADER_SIZE); else new_hdrbuf = malloc(c->msgused * 2 * UDP_HEADER_SIZE); if (! new_hdrbuf) return -1; c->hdrbuf = (unsigned char *)new_hdrbuf; c->hdrsize = c->msgused * 2; }
hdr = c->hdrbuf; for (i = 0; i < c->msgused; i++) { c->msglist[i].msg_iov[0].iov_base = hdr; c->msglist[i].msg_iov[0].iov_len = UDP_HEADER_SIZE; *hdr++ = c->request_id / 256; *hdr++ = c->request_id % 256; *hdr++ = i / 256; *hdr++ = i % 256; *hdr++ = c->msgused / 256; *hdr++ = c->msgused % 256; *hdr++ = 0; *hdr++ = 0; assert((void *) hdr == (void *)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE); }
return 0;}
static void out_string(conn *c, const char *str) { size_t len;
assert(c != NULL);
if (c->noreply) { if (settings.verbose > 1) fprintf(stderr, ">%d NOREPLY %s\n", c->sfd, str); c->noreply = false; conn_set_state(c, conn_read); return; }
if (settings.verbose > 1) fprintf(stderr, ">%d %s\n", c->sfd, str);
len = strlen(str); if ((len + 2) > c->wsize) { /* ought to be always enough. just fail for simplicity */ str = "SERVER_ERROR output line too long"; len = strlen(str); }
memcpy(c->wbuf, str, len); memcpy(c->wbuf + len, "\r\n", 2); c->wbytes = len + 2; c->wcurr = c->wbuf;
conn_set_state(c, conn_write); c->write_and_go = conn_read; return;}
typedef struct token_s { char *value; size_t length;} token_t;
#define COMMAND_TOKEN 0#define SUBCOMMAND_TOKEN 1#define KEY_TOKEN 1#define KEY_MAX_LENGTH 250
#define MAX_TOKENS 8
/* * Tokenize the command string by replacing whitespace with '\0' and update * the token array tokens with pointer to start of each token and length. * Returns total number of tokens. The last valid token is the terminal * token (value points to the first unprocessed character of the string and * length zero). * * Usage example: * * while(tokenize_command(command, ncommand, tokens, max_tokens) > 0) { * for(int ix = 0; tokens[ix].length != 0; ix++) { * ... * } * ncommand = tokens[ix].value - command; * command = tokens[ix].value; * } */static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) { char *s, *e; size_t ntokens = 0;
assert(command != NULL && tokens != NULL && max_tokens > 1);
for (s = e = command; ntokens < max_tokens - 1; ++e) { if (*e == ' ') { if (s != e) { tokens[ntokens].value = s; tokens[ntokens].length = e - s; ntokens++; *e = '\0'; } s = e + 1; } else if (*e == '\0') { if (s != e) { tokens[ntokens].value = s; tokens[ntokens].length = e - s; ntokens++; }
break; /* string end */ } }
/* * If we scanned the whole string, the terminal value pointer is null, * otherwise it is the first unprocessed character. */ tokens[ntokens].value = *e == '\0' ? NULL : e; tokens[ntokens].length = 0; ntokens++;
return ntokens;}
/* set up a connection to write a buffer then free it, used for stats */static void write_and_free(conn *c, char *buf, int bytes) { if (buf) { c->write_and_free = buf; c->wcurr = buf; c->wbytes = bytes; conn_set_state(c, conn_write); c->write_and_go = conn_read; } else { out_string(c, "SERVER_ERROR out of memory writing stats"); }}
static inline void set_noreply_maybe(conn *c, token_t *tokens, size_t ntokens){ int noreply_index = ntokens - 2;
/* NOTE: this function is not the first place where we are going to send the reply. We could send it instead from process_command() if the request line has wrong number of tokens. However parsing malformed line for "noreply" option is not reliable anyway, so it can't be helped. */ if (tokens[noreply_index].value && strcmp(tokens[noreply_index].value, "noreply") == 0) { c->noreply = true; }}
inline static void process_stats_detail(conn *c, const char *command) { assert(c != NULL);
if (strcmp(command, "on") == 0) { settings.detail_enabled = 1; out_string(c, "OK"); } else if (strcmp(command, "off") == 0) { settings.detail_enabled = 0; out_string(c, "OK"); } else { out_string(c, "CLIENT_ERROR usage: stats detail on|off|dump"); }}
static void process_stat(conn *c, token_t *tokens, const size_t ntokens) { rel_time_t now = current_time; char *command; char *subcommand;
assert(c != NULL);
if(ntokens < 2) { out_string(c, "CLIENT_ERROR bad command line"); return; }
command = tokens[COMMAND_TOKEN].value;
if (ntokens == 2 && strcmp(command, "stats") == 0) { char temp[1024]; pid_t pid = getpid(); char *pos = temp;
#ifndef WIN32 struct rusage usage; getrusage(RUSAGE_SELF, &usage);#endif /* !WIN32 */
STATS_LOCK(); pos += sprintf(pos, "STAT pid %u\r\n", pid); pos += sprintf(pos, "STAT uptime %u\r\n", now); pos += sprintf(pos, "STAT time %ld\r\n", now + stats.started); pos += sprintf(pos, "STAT version " VERSION "\r\n"); pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *));#ifndef WIN32 pos += sprintf(pos, "STAT rusage_user %ld.%06ld\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec); pos += sprintf(pos, "STAT rusage_system %ld.%06ld\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);#endif /* !WIN32 */ pos += sprintf(pos, "STAT curr_items %u\r\n", stats.curr_items); pos += sprintf(pos, "STAT total_items %u\r\n", stats.total_items); pos += sprintf(pos, "STAT bytes %llu\r\n", stats.curr_bytes); pos += sprintf(pos, "STAT curr_connections %u\r\n", stats.curr_conns - 1); /* ignore listening conn */ pos += sprintf(pos, "STAT total_connections %u\r\n", stats.total_conns); pos += sprintf(pos, "STAT connection_structures %u\r\n", stats.conn_structs); pos += sprintf(pos, "STAT cmd_get %llu\r\n", stats.get_cmds); pos += sprintf(pos, "STAT cmd_set %llu\r\n", stats.set_cmds); pos += sprintf(pos, "STAT get_hits %llu\r\n", stats.get_hits); pos += sprintf(pos, "STAT get_misses %llu\r\n", stats.get_misses); pos += sprintf(pos, "STAT evictions %llu\r\n", stats.evictions); pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read); pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written); pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (uint64_t) settings.maxbytes); pos += sprintf(pos, "STAT threads %u\r\n", settings.num_threads); pos += sprintf(pos, "END"); STATS_UNLOCK(); out_string(c, temp); return; }
subcommand = tokens[SUBCOMMAND_TOKEN].value;
if (strcmp(subcommand, "reset") == 0) { stats_reset(); out_string(c, "RESET"); return; }
#ifdef HAVE_MALLOC_H#ifdef HAVE_STRUCT_MALLINFO if (strcmp(subcommand, "malloc") == 0) { char temp[512]; struct mallinfo info; char *pos = temp;
info = mallinfo(); pos += sprintf(pos, "STAT arena_size %d\r\n", info.arena); pos += sprintf(pos, "STAT free_chunks %d\r\n", info.ordblks); pos += sprintf(pos, "STAT fastbin_blocks %d\r\n", info.smblks); pos += sprintf(pos, "STAT mmapped_regions %d\r\n", info.hblks); pos += sprintf(pos, "STAT mmapped_space %d\r\n", info.hblkhd); pos += sprintf(pos, "STAT max_total_alloc %d\r\n", info.usmblks); pos += sprintf(pos, "STAT fastbin_space %d\r\n", info.fsmblks); pos += sprintf(pos, "STAT total_alloc %d\r\n", info.uordblks); pos += sprintf(pos, "STAT total_free %d\r\n", info.fordblks); pos += sprintf(pos, "STAT releasable_space %d\r\nEND", info.keepcost); out_string(c, temp); return; }#endif /* HAVE_STRUCT_MALLINFO */#endif /* HAVE_MALLOC_H */ out_string(c, "ERROR");}
static inline void process_srilm_command(conn *c, token_t *tokens, size_t ntokens) { int context[6]; int i = 1; int j = ntokens - 3; while (tokens[i].length) { context[i-1] = srilm_getvoc(tokens[i].value); ++i; } float p = -999.0f; if (context[0] != -1) { context[i-1] = -1; p = srilm_wordprob(context[0], &context[1]); }
memcpy(c->wbuf, &p, sizeof(float)); memcpy(c->wbuf + sizeof(float), "\r\n", 2); c->wbytes = sizeof(float) + 2; c->wcurr = c->wbuf;
conn_set_state(c, conn_write); c->write_and_go = conn_read;}
static void process_command(conn *c, char *command) {
token_t tokens[MAX_TOKENS]; size_t ntokens; int comm;
assert(c != NULL);
if (settings.verbose > 1) fprintf(stderr, "<%d %s\n", c->sfd, command);
/* * for commands set/add/replace, we build an item and read the data * directly into it, then continue in nread_complete(). */
c->msgcurr = 0; c->msgused = 0; c->iovused = 0; if (add_msghdr(c) != 0) { out_string(c, "SERVER_ERROR out of memory preparing response"); return; }
ntokens = tokenize_command(command, tokens, MAX_TOKENS); if (ntokens >1 && strcmp(tokens[COMMAND_TOKEN].value, "prob") == 0) { process_srilm_command(c, tokens, ntokens); } else if (ntokens >= 2 && (strcmp(tokens[COMMAND_TOKEN].value, "stats") == 0)) {
process_stat(c, tokens, ntokens);
} else if (ntokens == 2 && (strcmp(tokens[COMMAND_TOKEN].value, "version") == 0)) {
out_string(c, "VERSION " VERSION);
} else if (ntokens == 2 && (strcmp(tokens[COMMAND_TOKEN].value, "quit") == 0)) {
conn_set_state(c, conn_closing);
} else { out_string(c, "ERROR"); } return;}
/* * if we have a complete line in the buffer, process it. */static int try_read_command(conn *c) { char *el, *cont;
assert(c != NULL); assert(c->rcurr <= (c->rbuf + c->rsize));
if (c->rbytes == 0) return 0; el = memchr(c->rcurr, '\n', c->rbytes); if (!el) return 0; cont = el + 1; if ((el - c->rcurr) > 1 && *(el - 1) == '\r') { el--; } *el = '\0';
assert(cont <= (c->rcurr + c->rbytes));
process_command(c, c->rcurr);
c->rbytes -= (cont - c->rcurr); c->rcurr = cont;
assert(c->rcurr <= (c->rbuf + c->rsize));
return 1;}
/* * read a UDP request. * return 0 if there's nothing to read. */static int try_read_udp(conn *c) { int res;
assert(c != NULL);
c->request_addr_size = sizeof(c->request_addr); res = recvfrom(c->sfd, c->rbuf, c->rsize, 0, &c->request_addr, &c->request_addr_size); if (res > 8) { unsigned char *buf = (unsigned char *)c->rbuf; STATS_LOCK(); stats.bytes_read += res; STATS_UNLOCK();
/* Beginning of UDP packet is the request ID; save it. */ c->request_id = buf[0] * 256 + buf[1];
/* If this is a multi-packet request, drop it. */ if (buf[4] != 0 || buf[5] != 1) { out_string(c, "SERVER_ERROR multi-packet request not supported"); return 0; }
/* Don't care about any of the rest of the header. */ res -= 8; memmove(c->rbuf, c->rbuf + 8, res);
c->rbytes += res; c->rcurr = c->rbuf; return 1; } return 0;}
/* * read from network as much as we can, handle buffer overflow and connection * close. * before reading, move the remaining incomplete fragment of a command * (if any) to the beginning of the buffer. * return 0 if there's nothing to read on the first read. */static int try_read_network(conn *c) { int gotdata = 0; int res;
assert(c != NULL);
if (c->rcurr != c->rbuf) { if (c->rbytes != 0) /* otherwise there's nothing to copy */ memmove(c->rbuf, c->rcurr, c->rbytes); c->rcurr = c->rbuf; }
while (1) { if (c->rbytes >= c->rsize) { char *new_rbuf = realloc(c->rbuf, c->rsize * 2); if (!new_rbuf) { if (settings.verbose > 0) fprintf(stderr, "Couldn't realloc input buffer\n"); c->rbytes = 0; /* ignore what we read */ out_string(c, "SERVER_ERROR out of memory reading request"); c->write_and_go = conn_closing; return 1; } c->rcurr = c->rbuf = new_rbuf; c->rsize *= 2; }
/* unix socket mode doesn't need this, so zeroed out. but why * is this done for every command? presumably for UDP * mode. */ if (!settings.socketpath) { c->request_addr_size = sizeof(c->request_addr); } else { c->request_addr_size = 0; }
int avail = c->rsize - c->rbytes; res = read(c->sfd, c->rbuf + c->rbytes, avail); if (res > 0) { STATS_LOCK(); stats.bytes_read += res; STATS_UNLOCK(); gotdata = 1; c->rbytes += res; if (res == avail) { continue; } else { break; } } if (res == 0) { /* connection closed */ conn_set_state(c, conn_closing); return 1; } if (res == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) break; /* Should close on unhandled errors. */ conn_set_state(c, conn_closing); return 1; } } return gotdata;}
static bool update_event(conn *c, const int new_flags) { assert(c != NULL);
struct event_base *base = c->event.ev_base; if (c->ev_flags == new_flags) return true; if (event_del(&c->event) == -1) return false; event_set(&c->event, c->sfd, new_flags, event_handler, (void *)c); event_base_set(base, &c->event); c->ev_flags = new_flags; if (event_add(&c->event, 0) == -1) return false; return true;}
/* * Sets whether we are listening for new connections or not. */void accept_new_conns(const bool do_accept) { conn *next;
if (! is_listen_thread()) return;
for (next = listen_conn; next; next = next->next) { if (do_accept) { update_event(next, EV_READ | EV_PERSIST); if (listen(next->sfd, 1024) != 0) { perror("listen"); } } else { update_event(next, 0); if (listen(next->sfd, 0) != 0) { perror("listen"); } } }}
/* * Transmit the next chunk of data from our list of msgbuf structures. * * Returns: * TRANSMIT_COMPLETE All done writing. * TRANSMIT_INCOMPLETE More data remaining to write. * TRANSMIT_SOFT_ERROR Can't write any more right now. * TRANSMIT_HARD_ERROR Can't write (c->state is set to conn_closing) */static int transmit(conn *c) { assert(c != NULL);
if (c->msgcurr < c->msgused && c->msglist[c->msgcurr].msg_iovlen == 0) { /* Finished writing the current msg; advance to the next. */ c->msgcurr++; } if (c->msgcurr < c->msgused) { ssize_t res; struct msghdr *m = &c->msglist[c->msgcurr];
res = sendmsg(c->sfd, m, 0); if (res > 0) { STATS_LOCK(); stats.bytes_written += res; STATS_UNLOCK();
/* We've written some of the data. Remove the completed iovec entries from the list of pending writes. */ while (m->msg_iovlen > 0 && res >= m->msg_iov->iov_len) { res -= m->msg_iov->iov_len; m->msg_iovlen--; m->msg_iov++; }
/* Might have written just part of the last iovec entry; adjust it so the next write will do the rest. */ if (res > 0) { m->msg_iov->iov_base += res; m->msg_iov->iov_len -= res; } return TRANSMIT_INCOMPLETE; } if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { if (!update_event(c, EV_WRITE | EV_PERSIST)) { if (settings.verbose > 0) fprintf(stderr, "Couldn't update event\n"); conn_set_state(c, conn_closing); return TRANSMIT_HARD_ERROR; } return TRANSMIT_SOFT_ERROR; } /* if res==0 or res==-1 and error is not EAGAIN or EWOULDBLOCK, we have a real error, on which we close the connection */ if (settings.verbose > 0) perror("Failed to write, and not due to blocking");
if (c->udp) conn_set_state(c, conn_read); else conn_set_state(c, conn_closing); return TRANSMIT_HARD_ERROR; } else { return TRANSMIT_COMPLETE; }}
static void drive_machine(conn *c) { bool stop = false; int sfd, flags = 1; socklen_t addrlen; struct sockaddr_storage addr; int res;
assert(c != NULL);
while (!stop) {
switch(c->state) { case conn_listening: addrlen = sizeof(addr); if ((sfd = accept(c->sfd, (struct sockaddr *)&addr, &addrlen)) == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { /* these are transient, so don't log anything */ stop = true; } else if (errno == EMFILE) { if (settings.verbose > 0) fprintf(stderr, "Too many open connections\n"); accept_new_conns(false); stop = true; } else { perror("accept()"); stop = true; } break; } if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) { perror("setting O_NONBLOCK"); close(sfd); break; } dispatch_conn_new(sfd, conn_read, EV_READ | EV_PERSIST, DATA_BUFFER_SIZE, false); break;
case conn_read: if (try_read_command(c) != 0) { continue; } if ((c->udp ? try_read_udp(c) : try_read_network(c)) != 0) { continue; } /* we have no command line and no data to read from network */ if (!update_event(c, EV_READ | EV_PERSIST)) { if (settings.verbose > 0) fprintf(stderr, "Couldn't update event\n"); conn_set_state(c, conn_closing); break; } stop = true; break;
case conn_nread: assert(!"nread should not be possible"); break;
case conn_swallow: /* we are reading sbytes and throwing them away */ if (c->sbytes == 0) { conn_set_state(c, conn_read); break; }
/* first check if we have leftovers in the conn_read buffer */ if (c->rbytes > 0) { int tocopy = c->rbytes > c->sbytes ? c->sbytes : c->rbytes; c->sbytes -= tocopy; c->rcurr += tocopy; c->rbytes -= tocopy; break; }
/* now try reading from the socket */ res = read(c->sfd, c->rbuf, c->rsize > c->sbytes ? c->sbytes : c->rsize); if (res > 0) { STATS_LOCK(); stats.bytes_read += res; STATS_UNLOCK(); c->sbytes -= res; break; } if (res == 0) { /* end of stream */ conn_set_state(c, conn_closing); break; } if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { if (!update_event(c, EV_READ | EV_PERSIST)) { if (settings.verbose > 0) fprintf(stderr, "Couldn't update event\n"); conn_set_state(c, conn_closing); break; } stop = true; break; } /* otherwise we have a real error, on which we close the connection */ if (settings.verbose > 0) fprintf(stderr, "Failed to read, and not due to blocking\n"); conn_set_state(c, conn_closing); break;
case conn_write: /* * We want to write out a simple response. If we haven't already, * assemble it into a msgbuf list (this will be a single-entry * list for TCP or a two-entry list for UDP). */ if (c->iovused == 0 || (c->udp && c->iovused == 1)) { if (add_iov(c, c->wcurr, c->wbytes) != 0 || (c->udp && build_udp_headers(c) != 0)) { if (settings.verbose > 0) fprintf(stderr, "Couldn't build response\n"); conn_set_state(c, conn_closing); break; } }
/* fall through... */
case conn_mwrite: switch (transmit(c)) { case TRANSMIT_COMPLETE: if (c->state == conn_write) { if (c->write_and_free) { free(c->write_and_free); c->write_and_free = 0; } conn_set_state(c, c->write_and_go); } else { if (settings.verbose > 0) fprintf(stderr, "Unexpected state %d\n", c->state); conn_set_state(c, conn_closing); } break;
case TRANSMIT_INCOMPLETE: case TRANSMIT_HARD_ERROR: break; /* Continue in state machine. */
case TRANSMIT_SOFT_ERROR: stop = true; break; } break;
case conn_closing: if (c->udp) conn_cleanup(c); else conn_close(c); stop = true; break; } }
return;}
void event_handler(const int fd, const short which, void *arg) { conn *c;
c = (conn *)arg; assert(c != NULL);
c->which = which;
/* sanity */ if (fd != c->sfd) { if (settings.verbose > 0) fprintf(stderr, "Catastrophic: event fd doesn't match conn fd!\n"); conn_close(c); return; }
drive_machine(c);
/* wait for next event */ return;}
static int new_socket(struct addrinfo *ai) { int sfd; int flags;
if ((sfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) { perror("socket()"); return -1; }
if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) { perror("setting O_NONBLOCK"); close(sfd); return -1; } return sfd;}
/* * Sets a socket's send buffer size to the maximum allowed by the system. */static void maximize_sndbuf(const int sfd) { socklen_t intsize = sizeof(int); int last_good = 0; int min, max, avg; int old_size;
/* Start with the default size. */ if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) != 0) { if (settings.verbose > 0) perror("getsockopt(SO_SNDBUF)"); return; }
/* Binary-search for the real maximum. */ min = old_size; max = MAX_SENDBUF_SIZE;
while (min <= max) { avg = ((unsigned int)(min + max)) / 2; if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (void *)&avg, intsize) == 0) { last_good = avg; min = avg + 1; } else { max = avg - 1; } }
if (settings.verbose > 1) fprintf(stderr, "<%d send buffer was %d, now %d\n", sfd, old_size, last_good);}
static int server_socket(const int port, const bool is_udp) { int sfd; struct linger ling = {0, 0}; struct addrinfo *ai; struct addrinfo *next; struct addrinfo hints; char port_buf[NI_MAXSERV]; int error; int success = 0;
int flags =1;
/* * the memset call clears nonstandard fields in some impementations * that otherwise mess things up. */ memset(&hints, 0, sizeof (hints)); hints.ai_flags = AI_PASSIVE|AI_ADDRCONFIG; if (is_udp) { hints.ai_protocol = IPPROTO_UDP; hints.ai_socktype = SOCK_DGRAM; hints.ai_family = AF_INET; /* This left here because of issues with OSX 10.5 */ } else { hints.ai_family = AF_UNSPEC; hints.ai_protocol = IPPROTO_TCP; hints.ai_socktype = SOCK_STREAM; }
snprintf(port_buf, NI_MAXSERV, "%d", port); error= getaddrinfo(settings.inter, port_buf, &hints, &ai); if (error != 0) { if (error != EAI_SYSTEM) fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error)); else perror("getaddrinfo()");
return 1; }
for (next= ai; next; next= next->ai_next) { conn *listen_conn_add; if ((sfd = new_socket(next)) == -1) { freeaddrinfo(ai); return 1; }
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags)); if (is_udp) { maximize_sndbuf(sfd); } else { setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags)); setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling)); setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags)); }
if (bind(sfd, next->ai_addr, next->ai_addrlen) == -1) { if (errno != EADDRINUSE) { perror("bind()"); close(sfd); freeaddrinfo(ai); return 1; } close(sfd); continue; } else { success++; if (!is_udp && listen(sfd, 1024) == -1) { perror("listen()"); close(sfd); freeaddrinfo(ai); return 1; } }
if (is_udp) { int c;
for (c = 0; c < settings.num_threads; c++) { /* this is guaranteed to hit all threads because we round-robin */ dispatch_conn_new(sfd, conn_read, EV_READ | EV_PERSIST, UDP_READ_BUFFER_SIZE, 1); } } else { if (!(listen_conn_add = conn_new(sfd, conn_listening, EV_READ | EV_PERSIST, 1, false, main_base))) { fprintf(stderr, "failed to create listening connection\n"); exit(EXIT_FAILURE); }
listen_conn_add->next = listen_conn; listen_conn = listen_conn_add; } }
freeaddrinfo(ai);
/* Return zero iff we detected no errors in starting up connections */ return success == 0;}
static int new_socket_unix(void) { int sfd; int flags;
if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket()"); return -1; }
if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) { perror("setting O_NONBLOCK"); close(sfd); return -1; } return sfd;}
static int server_socket_unix(const char *path, int access_mask) { int sfd; struct linger ling = {0, 0}; struct sockaddr_un addr; struct stat tstat; int flags =1; int old_umask;
if (!path) { return 1; }
if ((sfd = new_socket_unix()) == -1) { return 1; }
/* * Clean up a previous socket file if we left it around */ if (lstat(path, &tstat) == 0) { if (S_ISSOCK(tstat.st_mode)) unlink(path); }
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags)); setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags)); setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
/* * the memset call clears nonstandard fields in some impementations * that otherwise mess things up. */ memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX; strcpy(addr.sun_path, path); old_umask=umask( ~(access_mask&0777)); if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { perror("bind()"); close(sfd); umask(old_umask); return 1; } umask(old_umask); if (listen(sfd, 1024) == -1) { perror("listen()"); close(sfd); return 1; } if (!(listen_conn = conn_new(sfd, conn_listening, EV_READ | EV_PERSIST, 1, false, main_base))) { fprintf(stderr, "failed to create listening connection\n"); exit(EXIT_FAILURE); }
return 0;}
/* * We keep the current time of day in a global variable that's updated by a * timer event. This saves us a bunch of time() system calls (we really only * need to get the time once a second, whereas there can be tens of thousands * of requests a second) and allows us to use server-start-relative timestamps * rather than absolute UNIX timestamps, a space savings on systems where * sizeof(time_t) > sizeof(unsigned int). */volatile rel_time_t current_time;static struct event clockevent;
/* time-sensitive callers can call it by hand with this, outside the normal ever-1-second timer */static void set_current_time(void) { struct timeval timer;
gettimeofday(&timer, NULL); current_time = (rel_time_t) (timer.tv_sec - stats.started);}
static void clock_handler(const int fd, const short which, void *arg) { struct timeval t = {.tv_sec = 1, .tv_usec = 0}; static bool initialized = false;
if (initialized) { /* only delete the event if it's actually there. */ evtimer_del(&clockevent); } else { initialized = true; }
evtimer_set(&clockevent, clock_handler, 0); event_base_set(main_base, &clockevent); evtimer_add(&clockevent, &t);
set_current_time();}
static void usage(void) { printf(PACKAGE " " VERSION "\n"); printf("-p <num> TCP port number to listen on (default: 11211)\n" "-U <num> UDP port number to listen on (default: 0, off)\n" "-s <file> unix socket path to listen on (disables network support)\n" "-a <mask> access mask for unix socket, in octal (default 0700)\n" "-l <ip_addr> interface to listen on, default is INDRR_ANY\n" "-d run as a daemon\n" "-r maximize core file limit\n" "-u <username> assume identity of <username> (only when run as root)\n" "-m <num> max memory to use for items in megabytes, default is 64 MB\n" "-M return error on memory exhausted (rather than removing items)\n" "-c <num> max simultaneous connections, default is 1024\n" "-k lock down all paged memory. Note that there is a\n" " limit on how much memory you may lock. Trying to\n" " allocate more than that would fail, so be sure you\n" " set the limit correctly for the user you started\n" " the daemon with (not for -u <username> user;\n" " under sh this is done with 'ulimit -S -l NUM_KB').\n" "-v verbose (print errors/warnings while in event loop)\n" "-vv very verbose (also print client commands/reponses)\n" "-h print this help and exit\n" "-i print memcached and libevent license\n" "-b run a managed instanced (mnemonic: buckets)\n" "-P <file> save PID in <file>, only used with -d option\n" "-f <factor> chunk size growth factor, default 1.25\n" "-n <bytes> minimum space allocated for key+value+flags, default 48\n"
#if defined(HAVE_GETPAGESIZES) && defined(HAVE_MEMCNTL) "-L Try to use large memory pages (if available). Increasing\n" " the memory page size could reduce the number of TLB misses\n" " and improve the performance. In order to get large pages\n" " from the OS, memcached will allocate the total item-cache\n" " in one large chunk.\n"#endif );
#ifdef USE_THREADS printf("-t <num> number of threads to use, default 4\n");#endif return;}
static void usage_license(void) { printf(PACKAGE " " VERSION "\n\n"); printf( "Copyright (c) 2003, Danga Interactive, Inc. <http://www.danga.com/>\n" "All rights reserved.\n" "\n" "Redistribution and use in source and binary forms, with or without\n" "modification, are permitted provided that the following conditions are\n" "met:\n" "\n" " * Redistributions of source code must retain the above copyright\n" "notice, this list of conditions and the following disclaimer.\n" "\n" " * Redistributions in binary form must reproduce the above\n" "copyright notice, this list of conditions and the following disclaimer\n" "in the documentation and/or other materials provided with the\n" "distribution.\n" "\n" " * Neither the name of the Danga Interactive nor the names of its\n" "contributors may be used to endorse or promote products derived from\n" "this software without specific prior written permission.\n" "\n" "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n" "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n" "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n" "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n" "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n" "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n" "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n" "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n" "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n" "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n" "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" "\n" "\n" "This product includes software developed by Niels Provos.\n" "\n" "[ libevent ]\n" "\n" "Copyright 2000-2003 Niels Provos <provos@citi.umich.edu>\n" "All rights reserved.\n" "\n" "Redistribution and use in source and binary forms, with or without\n" "modification, are permitted provided that the following conditions\n" "are met:\n" "1. Redistributions of source code must retain the above copyright\n" " notice, this list of conditions and the following disclaimer.\n" "2. Redistributions in binary form must reproduce the above copyright\n" " notice, this list of conditions and the following disclaimer in the\n" " documentation and/or other materials provided with the distribution.\n" "3. All advertising materials mentioning features or use of this software\n" " must display the following acknowledgement:\n" " This product includes software developed by Niels Provos.\n" "4. The name of the author may not be used to endorse or promote products\n" " derived from this software without specific prior written permission.\n" "\n" "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n" "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n" "OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n" "IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n" "INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n" "NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n" "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n" "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n" "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n" "THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" );
return;}
static void save_pid(const pid_t pid, const char *pid_file) { FILE *fp; if (pid_file == NULL) return;
if ((fp = fopen(pid_file, "w")) == NULL) { fprintf(stderr, "Could not open the pid file %s for writing\n", pid_file); return; }
fprintf(fp,"%ld\n", (long)pid); if (fclose(fp) == -1) { fprintf(stderr, "Could not close the pid file %s.\n", pid_file); return; }}
static void remove_pidfile(const char *pid_file) { if (pid_file == NULL) return;
if (unlink(pid_file) != 0) { fprintf(stderr, "Could not remove the pid file %s.\n", pid_file); }
}
static void sig_handler(const int sig) { printf("SIGINT handled.\n"); exit(EXIT_SUCCESS);}
#if defined(HAVE_GETPAGESIZES) && defined(HAVE_MEMCNTL)/* * On systems that supports multiple page sizes we may reduce the * number of TLB-misses by using the biggest available page size */int enable_large_pages(void) { int ret = -1; size_t sizes[32]; int avail = getpagesizes(sizes, 32); if (avail != -1) { size_t max = sizes[0]; struct memcntl_mha arg = {0}; int ii;
for (ii = 1; ii < avail; ++ii) { if (max < sizes[ii]) { max = sizes[ii]; } }
arg.mha_flags = 0; arg.mha_pagesize = max; arg.mha_cmd = MHA_MAPSIZE_BSSBRK;
if (memcntl(0, 0, MC_HAT_ADVISE, (caddr_t)&arg, 0, 0) == -1) { fprintf(stderr, "Failed to set large pages: %s\n", strerror(errno)); fprintf(stderr, "Will use default page size\n"); } else { ret = 0; } } else { fprintf(stderr, "Failed to get supported pagesizes: %s\n", strerror(errno)); fprintf(stderr, "Will use default page size\n"); }
return ret;}#endif
int main (int argc, char **argv) { int c; int x; bool lock_memory = false; bool daemonize = false; bool preallocate = false; int maxcore = 0; char *username = NULL; char *pid_file = NULL; struct passwd *pw; struct sigaction sa; struct rlimit rlim; /* listening socket */ static int *l_socket = NULL;
/* udp socket */ static int *u_socket = NULL; static int u_socket_count = 0;
/* handle SIGINT */ signal(SIGINT, sig_handler);
/* init settings */ settings_init();
/* set stderr non-buffering (for running under, say, daemontools) */ setbuf(stderr, NULL);
/* process arguments */ while ((c = getopt(argc, argv, "x:o:a:bp:s:U:m:Mc:khirvdl:u:P:f:s:n:t:L")) != -1) { switch (c) { case 'x': settings.srilm = optarg; break; case 'o': settings.srilm_order = atoi(optarg); break; case 'a': /* access for unix domain socket, as octal mask (like chmod)*/ settings.access= strtol(optarg,NULL,8); break;
case 'U': settings.udpport = atoi(optarg); break; case 'b': settings.managed = true; break; case 'p': settings.port = atoi(optarg); break; case 's': settings.socketpath = optarg; break; case 'm': settings.maxbytes = ((size_t)atoi(optarg)) * 1024 * 1024; break; case 'M': settings.evict_to_free = 0; break; case 'c': settings.maxconns = atoi(optarg); break; case 'h': usage(); exit(EXIT_SUCCESS); case 'i': usage_license(); exit(EXIT_SUCCESS); case 'k': lock_memory = true; break; case 'v': settings.verbose++; break; case 'l': settings.inter= strdup(optarg); break; case 'd': daemonize = true; break; case 'r': maxcore = 1; break; case 'u': username = optarg; break; case 'P': pid_file = optarg; break; case 'f': settings.factor = atof(optarg); if (settings.factor <= 1.0) { fprintf(stderr, "Factor must be greater than 1\n"); return 1; } break; case 'n': settings.chunk_size = atoi(optarg); if (settings.chunk_size == 0) { fprintf(stderr, "Chunk size must be greater than 0\n"); return 1; } break; case 't': settings.num_threads = atoi(optarg); if (settings.num_threads == 0) { fprintf(stderr, "Number of threads must be greater than 0\n"); return 1; } break;#if defined(HAVE_GETPAGESIZES) && defined(HAVE_MEMCNTL) case 'L' : if (enable_large_pages() == 0) { preallocate = true; } break;#endif default: fprintf(stderr, "Illegal argument \"%c\"\n", c); return 1; } }
if (maxcore != 0) { struct rlimit rlim_new; /* * First try raising to infinity; if that fails, try bringing * the soft limit to the hard. */ if (getrlimit(RLIMIT_CORE, &rlim) == 0) { rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY; if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) { /* failed. try raising just to the old max */ rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max; (void)setrlimit(RLIMIT_CORE, &rlim_new); } } /* * getrlimit again to see what we ended up with. Only fail if * the soft limit ends up 0, because then no core files will be * created at all. */
if ((getrlimit(RLIMIT_CORE, &rlim) != 0) || rlim.rlim_cur == 0) { fprintf(stderr, "failed to ensure corefile creation\n"); exit(EXIT_FAILURE); } }
/* * If needed, increase rlimits to allow as many connections * as needed. */
if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) { fprintf(stderr, "failed to getrlimit number of files\n"); exit(EXIT_FAILURE); } else { int maxfiles = settings.maxconns; if (rlim.rlim_cur < maxfiles) rlim.rlim_cur = maxfiles + 3; if (rlim.rlim_max < rlim.rlim_cur) rlim.rlim_max = rlim.rlim_cur; if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) { fprintf(stderr, "failed to set rlimit for open files. Try running as root or requesting smaller maxconns value.\n"); exit(EXIT_FAILURE); } }
/* daemonize if requested */ /* if we want to ensure our ability to dump core, don't chdir to / */ if (daemonize) { int res; res = daemon(maxcore, settings.verbose); if (res == -1) { fprintf(stderr, "failed to daemon() in order to daemonize\n"); return 1; } }
/* lock paged memory if needed */ if (lock_memory) {#ifdef HAVE_MLOCKALL int res = mlockall(MCL_CURRENT | MCL_FUTURE); if (res != 0) { fprintf(stderr, "warning: -k invalid, mlockall() failed: %s\n", strerror(errno)); }#else fprintf(stderr, "warning: -k invalid, mlockall() not supported on this platform. proceeding without.\n");#endif }
/* lose root privileges if we have them */ if (getuid() == 0 || geteuid() == 0) { if (username == 0 || *username == '\0') { fprintf(stderr, "can't run as root without the -u switch\n"); return 1; } if ((pw = getpwnam(username)) == 0) { fprintf(stderr, "can't find the user %s to switch to\n", username); return 1; } if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { fprintf(stderr, "failed to assume identity of user %s\n", username); return 1; } }
/* initialize main thread libevent instance */ main_base = event_init();
/* initialize other stuff */ stats_init(); conn_init(); if (!settings.srilm) { fprintf(stderr, "please specify a LM file with -x\n"); exit(EXIT_FAILURE); } srilm_init(settings.srilm, settings.srilm_order);
/* managed instance? alloc and zero a bucket array */ if (settings.managed) { buckets = malloc(sizeof(int) * MAX_BUCKETS); if (buckets == 0) { fprintf(stderr, "failed to allocate the bucket array"); exit(EXIT_FAILURE); } memset(buckets, 0, sizeof(int) * MAX_BUCKETS); }
/* * ignore SIGPIPE signals; we can use errno==EPIPE if we * need that information */ sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if (sigemptyset(&sa.sa_mask) == -1 || sigaction(SIGPIPE, &sa, 0) == -1) { perror("failed to ignore SIGPIPE; sigaction"); exit(EXIT_FAILURE); } /* start up worker threads if MT mode */ thread_init(settings.num_threads, main_base); /* save the PID in if we're a daemon, do this after thread_init due to a file descriptor handling bug somewhere in libevent */ if (daemonize) save_pid(getpid(), pid_file); /* initialise clock event */ clock_handler(0, 0, 0);
/* create unix mode sockets after dropping privileges */ if (settings.socketpath != NULL) { if (server_socket_unix(settings.socketpath,settings.access)) { fprintf(stderr, "failed to listen\n"); exit(EXIT_FAILURE); } }
/* create the listening socket, bind it, and init */ if (settings.socketpath == NULL) { int udp_port;
if (server_socket(settings.port, 0)) { fprintf(stderr, "failed to listen\n"); exit(EXIT_FAILURE); } /* * initialization order: first create the listening sockets * (may need root on low ports), then drop root if needed, * then daemonise if needed, then init libevent (in some cases * descriptors created by libevent wouldn't survive forking). */ udp_port = settings.udpport ? settings.udpport : settings.port;
/* create the UDP listening socket and bind it */ if (server_socket(udp_port, 1)) { fprintf(stderr, "failed to listen on UDP port %d\n", settings.udpport); exit(EXIT_FAILURE); } }
/* enter the event loop */ event_base_loop(main_base, 0); /* remove the PID file if we're a daemon */ if (daemonize) remove_pidfile(pid_file); /* Clean up strdup() call for bind() address */ if (settings.inter) free(settings.inter); if (l_socket) free(l_socket); if (u_socket) free(u_socket);
return 0;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367#! /bin/sh# Common stub for a few missing GNU programs while installing.
scriptversion=2006-05-10.23
# Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2005, 2006# Free Software Foundation, Inc.# Originally by Fran,cois Pinard <pinard@iro.umontreal.ca>, 1996.
# This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2, or (at your option)# any later version.
# This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA# 02110-1301, USA.
# As a special exception to the GNU General Public License, if you# distribute this file as part of a program that contains a# configuration script generated by Autoconf, you may include it under# the same distribution terms that you use for the rest of that program.
if test $# -eq 0; then echo 1>&2 "Try \`$0 --help' for more information" exit 1fi
run=:sed_output='s/.* --output[ =]\([^ ]*\).*/\1/p'sed_minuso='s/.* -o \([^ ]*\).*/\1/p'
# In the cases where this matters, `missing' is being run in the# srcdir already.if test -f configure.ac; then configure_ac=configure.acelse configure_ac=configure.infi
msg="missing on your system"
case $1 in--run) # Try to run requested program, and just exit if it succeeds. run= shift "$@" && exit 0 # Exit code 63 means version mismatch. This often happens # when the user try to use an ancient version of a tool on # a file that requires a minimum version. In this case we # we should proceed has if the program had been absent, or # if --run hadn't been passed. if test $? = 63; then run=: msg="probably too old" fi ;;
-h|--h|--he|--hel|--help) echo "\$0 [OPTION]... PROGRAM [ARGUMENT]...
Handle \`PROGRAM [ARGUMENT]...' for when PROGRAM is missing, or return anerror status if there is no known handling for PROGRAM.
Options: -h, --help display this help and exit -v, --version output version information and exit --run try to run the given command, and emulate it if it fails
Supported PROGRAM values: aclocal touch file \`aclocal.m4' autoconf touch file \`configure' autoheader touch file \`config.h.in' autom4te touch the output file, or create a stub one automake touch all \`Makefile.in' files bison create \`y.tab.[ch]', if possible, from existing .[ch] flex create \`lex.yy.c', if possible, from existing .c help2man touch the output file lex create \`lex.yy.c', if possible, from existing .c makeinfo touch the output file tar try tar, gnutar, gtar, then tar without non-portable flags yacc create \`y.tab.[ch]', if possible, from existing .[ch]
Send bug reports to <bug-automake@gnu.org>." exit $? ;;
-v|--v|--ve|--ver|--vers|--versi|--versio|--version) echo "missing $scriptversion (GNU Automake)" exit $? ;;
-*) echo 1>&2 "$0: Unknown \`$1' option" echo 1>&2 "Try \`$0 --help' for more information" exit 1 ;;
esac
# Now exit if we have it, but it failed. Also exit now if we# don't have it and --version was passed (most likely to detect# the program).case $1 in lex|yacc) # Not GNU programs, they don't have --version. ;;
tar) if test -n "$run"; then echo 1>&2 "ERROR: \`tar' requires --run" exit 1 elif test "x$2" = "x--version" || test "x$2" = "x--help"; then exit 1 fi ;;
*) if test -z "$run" && ($1 --version) > /dev/null 2>&1; then # We have it, but it failed. exit 1 elif test "x$2" = "x--version" || test "x$2" = "x--help"; then # Could not run --version or --help. This is probably someone # running `$TOOL --version' or `$TOOL --help' to check whether # $TOOL exists and not knowing $TOOL uses missing. exit 1 fi ;;esac
# If it does not exist, or fails to run (possibly an outdated version),# try to emulate it.case $1 in aclocal*) echo 1>&2 "\WARNING: \`$1' is $msg. You should only need it if you modified \`acinclude.m4' or \`${configure_ac}'. You might want to install the \`Automake' and \`Perl' packages. Grab them from any GNU archive site." touch aclocal.m4 ;;
autoconf) echo 1>&2 "\WARNING: \`$1' is $msg. You should only need it if you modified \`${configure_ac}'. You might want to install the \`Autoconf' and \`GNU m4' packages. Grab them from any GNU archive site." touch configure ;;
autoheader) echo 1>&2 "\WARNING: \`$1' is $msg. You should only need it if you modified \`acconfig.h' or \`${configure_ac}'. You might want to install the \`Autoconf' and \`GNU m4' packages. Grab them from any GNU archive site." files=`sed -n 's/^[ ]*A[CM]_CONFIG_HEADER(\([^)]*\)).*/\1/p' ${configure_ac}` test -z "$files" && files="config.h" touch_files= for f in $files; do case $f in *:*) touch_files="$touch_files "`echo "$f" | sed -e 's/^[^:]*://' -e 's/:.*//'`;; *) touch_files="$touch_files $f.in";; esac done touch $touch_files ;;
automake*) echo 1>&2 "\WARNING: \`$1' is $msg. You should only need it if you modified \`Makefile.am', \`acinclude.m4' or \`${configure_ac}'. You might want to install the \`Automake' and \`Perl' packages. Grab them from any GNU archive site." find . -type f -name Makefile.am -print | sed 's/\.am$/.in/' | while read f; do touch "$f"; done ;;
autom4te) echo 1>&2 "\WARNING: \`$1' is needed, but is $msg. You might have modified some files without having the proper tools for further handling them. You can get \`$1' as part of \`Autoconf' from any GNU archive site."
file=`echo "$*" | sed -n "$sed_output"` test -z "$file" && file=`echo "$*" | sed -n "$sed_minuso"` if test -f "$file"; then touch $file else test -z "$file" || exec >$file echo "#! /bin/sh" echo "# Created by GNU Automake missing as a replacement of" echo "# $ $@" echo "exit 0" chmod +x $file exit 1 fi ;;
bison|yacc) echo 1>&2 "\WARNING: \`$1' $msg. You should only need it if you modified a \`.y' file. You may need the \`Bison' package in order for those modifications to take effect. You can get \`Bison' from any GNU archive site." rm -f y.tab.c y.tab.h if test $# -ne 1; then eval LASTARG="\${$#}" case $LASTARG in *.y) SRCFILE=`echo "$LASTARG" | sed 's/y$/c/'` if test -f "$SRCFILE"; then cp "$SRCFILE" y.tab.c fi SRCFILE=`echo "$LASTARG" | sed 's/y$/h/'` if test -f "$SRCFILE"; then cp "$SRCFILE" y.tab.h fi ;; esac fi if test ! -f y.tab.h; then echo >y.tab.h fi if test ! -f y.tab.c; then echo 'main() { return 0; }' >y.tab.c fi ;;
lex|flex) echo 1>&2 "\WARNING: \`$1' is $msg. You should only need it if you modified a \`.l' file. You may need the \`Flex' package in order for those modifications to take effect. You can get \`Flex' from any GNU archive site." rm -f lex.yy.c if test $# -ne 1; then eval LASTARG="\${$#}" case $LASTARG in *.l) SRCFILE=`echo "$LASTARG" | sed 's/l$/c/'` if test -f "$SRCFILE"; then cp "$SRCFILE" lex.yy.c fi ;; esac fi if test ! -f lex.yy.c; then echo 'main() { return 0; }' >lex.yy.c fi ;;
help2man) echo 1>&2 "\WARNING: \`$1' is $msg. You should only need it if you modified a dependency of a manual page. You may need the \`Help2man' package in order for those modifications to take effect. You can get \`Help2man' from any GNU archive site."
file=`echo "$*" | sed -n "$sed_output"` test -z "$file" && file=`echo "$*" | sed -n "$sed_minuso"` if test -f "$file"; then touch $file else test -z "$file" || exec >$file echo ".ab help2man is required to generate this page" exit 1 fi ;;
makeinfo) echo 1>&2 "\WARNING: \`$1' is $msg. You should only need it if you modified a \`.texi' or \`.texinfo' file, or any other file indirectly affecting the aspect of the manual. The spurious call might also be the consequence of using a buggy \`make' (AIX, DU, IRIX). You might want to install the \`Texinfo' package or the \`GNU make' package. Grab either from any GNU archive site." # The file to touch is that specified with -o ... file=`echo "$*" | sed -n "$sed_output"` test -z "$file" && file=`echo "$*" | sed -n "$sed_minuso"` if test -z "$file"; then # ... or it is the one specified with @setfilename ... infile=`echo "$*" | sed 's/.* \([^ ]*\) *$/\1/'` file=`sed -n ' /^@setfilename/{ s/.* \([^ ]*\) *$/\1/ p q }' $infile` # ... or it is derived from the source name (dir/f.texi becomes f.info) test -z "$file" && file=`echo "$infile" | sed 's,.*/,,;s,.[^.]*$,,'`.info fi # If the file does not exist, the user really needs makeinfo; # let's fail without touching anything. test -f $file || exit 1 touch $file ;;
tar) shift
# We have already tried tar in the generic part. # Look for gnutar/gtar before invocation to avoid ugly error # messages. if (gnutar --version > /dev/null 2>&1); then gnutar "$@" && exit 0 fi if (gtar --version > /dev/null 2>&1); then gtar "$@" && exit 0 fi firstarg="$1" if shift; then case $firstarg in *o*) firstarg=`echo "$firstarg" | sed s/o//` tar "$firstarg" "$@" && exit 0 ;; esac case $firstarg in *h*) firstarg=`echo "$firstarg" | sed s/h//` tar "$firstarg" "$@" && exit 0 ;; esac fi
echo 1>&2 "\WARNING: I can't seem to be able to run \`tar' with the given arguments. You may want to install GNU tar or Free paxutils, or check the command line arguments." exit 1 ;;
*) echo 1>&2 "\WARNING: \`$1' is needed, and is $msg. You might have modified some files without having the proper tools for further handling them. Check the \`README' file, it often tells you about the needed prerequisites for installing this package. You may also peek at any GNU archive site, in case some other package would contain this missing \`$1' program." exit 1 ;;esac
exit 0
# Local variables:# eval: (add-hook 'write-file-hooks 'time-stamp)# time-stamp-start: "scriptversion="# time-stamp-format: "%:y-%02m-%02d.%02H"# time-stamp-end: "$"# End:12345678#ifndef lmserver_srilm_h#define lmserver_srilm_h
void srilm_init(const char* fname, int order);int srilm_getvoc(const char* word);float srilm_wordprob(int, int*);
#endif12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788tmcombine - a tool for Moses translation model combination
Author: Rico Sennrich <sennrich [AT] cl.uzh.ch>
ABOUT-----
This program handles the combination of Moses phrase tables, either throughlinear interpolation of the phrase translation probabilities/lexical weights,or through a recomputation based on the (weighted) combined counts.
It also supports an automatic search for weights that minimize the cross-entropybetween the model and a tuning set of word/phrase alignments.
REQUIREMENTS------------
The script requires Python >= 2.6.SciPy is recommended. If it is missing, an ad-hoc hill-climbing optimizer will be used (which may be slower, but is actually recommended for PyPy and/or a high number of models).On Debian-based systems, you can install SciPy from the repository: sudo apt-get install python-scipy
USAGE-----
for usage information, run ./tmcombine.py -h
Two basic command line examples:
linearly interpolate two translation models with fixed weights: ./tmcombine.py combine_given_weights test/model1 test/model2 -w "0.1,0.9;0.1,1;0.2,0.8;0.5,0.5" -o test/phrase-table_test2
do a count-based combination of two translation models with weights that minimize perplexity on a set of reference phrase pairs. ./tmcombine.py combine_given_tuning_set test/model1 test/model2 -o test/phrase-table_test5 -m counts -r test/extract
Typically, you have to specify one action out of the following:
- `combine_given_weights`: write a new phrase table with defined weights
- `combine_given_tuning_set`: write a new phrase table, using the weights that minimize cross-entropy on a tuning set
- `compare_cross_entropies`: print cross-entropies for each model/feature, using the intersection of phrase pairs.
- `compute_cross_entropy`: return cross-entropy for a tuning set, a set of models and a set of weights.
- `return_best_cross_entropy`: return the set of weights and cross-entropy that is optimal for a tuning set and a set of models.
You can check the docstrings of `Combine_TMs()` for more information and find some example commands in the function `test()`.Some configuration options (i.e. normalization of linear interpolation) are not accessible from the command line.You can gain a bit more flexibility by writing/modifying python code that initializes `Combine_TMs()` with your desired arguments, or by just fiddling with the default values in the script.
Regression tests (check if the output files (`test/phrase-table_testN`) differ from the files in the repositorys): ./tmcombine.py test
FURTHER NOTES-------------
- Different combination algorithms require different statistics. To be on the safe side, use the option and `-write-lexical-counts` when training models.
- The script assumes that phrase tables are sorted (to allow incremental, more memory-friendly processing). Sort the tables with `LC_ALL=C`. Phrase tables produced by Moses are sorted correctly.
- Some configurations require additional statistics that are loaded in memory (lexical tables; complete list of target phrases). If memory consumption is a problem, use the option --lowmem (slightly slower and writes temporary files to disk), or consider pruning your phrase table before combining (e.g. using Johnson et al. 2007).
- The script can read/write gzipped files, but the Python implementation is slow. You're better off unzipping the files on the command line and working with the unzipped files. The script will automatically search for the unzipped file first, and for the gzipped file if the former doesn't exist.
- The cross-entropy estimation assumes that phrase tables contain true probability distributions (i.e. a probability mass of 1 for each conditional probability distribution). If this is not true, the results may be skewed.
- Unknown phrase pairs are not considered for the cross-entropy estimation. A comparison of models with different vocabularies may be misleading.
- Don't directly compare cross-entropies obtained from a combination with different modes. Depending on how some corner cases are treated, linear interpolation does not distribute the full probability mass and thus shows higher (i.e. worse) cross-entropies.
REFERENCES----------
The algorithms are described in
Sennrich, Rico (2012). Perplexity Minimization for Translation Model Domain Adaptation in Statistical Machine Translation. In: Proceedings of EACL 2012.
The evaluated algorithms are:
- linear interpolation (naive): default - linear interpolation (modified): use options `--normalized` and `--recompute_lexweights` - weighted counts: use option `-m counts`123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382# Author: Steven J. Bethard <steven.bethard@gmail.com>.
"""Command-line parsing library
This module is an optparse-inspired command-line parsing library that:
- handles both optional and positional arguments - produces highly informative usage messages - supports parsers that dispatch to sub-parsers
The following is a simple usage example that sums integers from thecommand-line and writes the result to a file::
parser = argparse.ArgumentParser( description='sum the integers at the command line') parser.add_argument( 'integers', metavar='int', nargs='+', type=int, help='an integer to be summed') parser.add_argument( '--log', default=sys.stdout, type=argparse.FileType('w'), help='the file where the sum should be written') args = parser.parse_args() args.log.write('%s' % sum(args.integers)) args.log.close()
The module contains the following public classes:
- ArgumentParser -- The main entry point for command-line parsing. As the example above shows, the add_argument() method is used to populate the parser with actions for optional and positional arguments. Then the parse_args() method is invoked to convert the args at the command-line into an object with attributes.
- ArgumentError -- The exception raised by ArgumentParser objects when there are errors with the parser's actions. Errors raised while parsing the command-line are caught by ArgumentParser and emitted as command-line messages.
- FileType -- A factory for defining types of files to be created. As the example above shows, instances of FileType are typically passed as the type= argument of add_argument() calls.
- Action -- The base class for parser actions. Typically actions are selected by passing strings like 'store_true' or 'append_const' to the action= argument of add_argument(). However, for greater customization of ArgumentParser actions, subclasses of Action may be defined and passed as the action= argument.
- HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter, ArgumentDefaultsHelpFormatter -- Formatter classes which may be passed as the formatter_class= argument to the ArgumentParser constructor. HelpFormatter is the default, RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser not to change the formatting for help text, and ArgumentDefaultsHelpFormatter adds information about argument defaults to the help.
All other classes in this module are considered implementation details.(Also note that HelpFormatter and RawDescriptionHelpFormatter are onlyconsidered public as object names -- the API of the formatter objects isstill considered an implementation detail.)"""
__version__ = '1.1'__all__ = [ 'ArgumentParser', 'ArgumentError', 'ArgumentTypeError', 'FileType', 'HelpFormatter', 'ArgumentDefaultsHelpFormatter', 'RawDescriptionHelpFormatter', 'RawTextHelpFormatter', 'MetavarTypeHelpFormatter', 'Namespace', 'Action', 'ONE_OR_MORE', 'OPTIONAL', 'PARSER', 'REMAINDER', 'SUPPRESS', 'ZERO_OR_MORE',]
import collections as _collectionsimport copy as _copyimport os as _osimport re as _reimport sys as _sysimport textwrap as _textwrap
from gettext import gettext as _, ngettext
SUPPRESS = '==SUPPRESS=='
OPTIONAL = '?'ZERO_OR_MORE = '*'ONE_OR_MORE = '+'PARSER = 'A...'REMAINDER = '...'_UNRECOGNIZED_ARGS_ATTR = '_unrecognized_args'
# =============================# Utility functions and classes# =============================
class _AttributeHolder(object): """Abstract base class that provides __repr__.
The __repr__ method returns a string in the format:: ClassName(attr=name, attr=name, ...) The attributes are determined either by a class-level attribute, '_kwarg_names', or by inspecting the instance __dict__. """
def __repr__(self): type_name = type(self).__name__ arg_strings = [] for arg in self._get_args(): arg_strings.append(repr(arg)) for name, value in self._get_kwargs(): arg_strings.append('%s=%r' % (name, value)) return '%s(%s)' % (type_name, ', '.join(arg_strings))
def _get_kwargs(self): return sorted(self.__dict__.items())
def _get_args(self): return []
def _ensure_value(namespace, name, value): if getattr(namespace, name, None) is None: setattr(namespace, name, value) return getattr(namespace, name)
# ===============# Formatting Help# ===============
class HelpFormatter(object): """Formatter for generating usage messages and argument help strings.
Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. """
def __init__(self, prog, indent_increment=2, max_help_position=24, width=None):
# default setting for width if width is None: try: width = int(_os.environ['COLUMNS']) except (KeyError, ValueError): width = 80 width -= 2
self._prog = prog self._indent_increment = indent_increment self._max_help_position = max_help_position self._width = width
self._current_indent = 0 self._level = 0 self._action_max_length = 0
self._root_section = self._Section(self, None) self._current_section = self._root_section
self._whitespace_matcher = _re.compile(r'\s+') self._long_break_matcher = _re.compile(r'\n\n\n+')
# =============================== # Section and indentation methods # =============================== def _indent(self): self._current_indent += self._indent_increment self._level += 1
def _dedent(self): self._current_indent -= self._indent_increment assert self._current_indent >= 0, 'Indent decreased below 0.' self._level -= 1
class _Section(object):
def __init__(self, formatter, parent, heading=None): self.formatter = formatter self.parent = parent self.heading = heading self.items = []
def format_help(self): # format the indented section if self.parent is not None: self.formatter._indent() join = self.formatter._join_parts for func, args in self.items: func(*args) item_help = join([func(*args) for func, args in self.items]) if self.parent is not None: self.formatter._dedent()
# return nothing if the section was empty if not item_help: return ''
# add the heading if the section was non-empty if self.heading is not SUPPRESS and self.heading is not None: current_indent = self.formatter._current_indent heading = '%*s%s:\n' % (current_indent, '', self.heading) else: heading = ''
# join the section-initial newline, the heading and the help return join(['\n', heading, item_help, '\n'])
def _add_item(self, func, args): self._current_section.items.append((func, args))
# ======================== # Message building methods # ======================== def start_section(self, heading): self._indent() section = self._Section(self, self._current_section, heading) self._add_item(section.format_help, []) self._current_section = section
def end_section(self): self._current_section = self._current_section.parent self._dedent()
def add_text(self, text): if text is not SUPPRESS and text is not None: self._add_item(self._format_text, [text])
def add_usage(self, usage, actions, groups, prefix=None): if usage is not SUPPRESS: args = usage, actions, groups, prefix self._add_item(self._format_usage, args)
def add_argument(self, action): if action.help is not SUPPRESS:
# find all invocations get_invocation = self._format_action_invocation invocations = [get_invocation(action)] for subaction in self._iter_indented_subactions(action): invocations.append(get_invocation(subaction))
# update the maximum item length invocation_length = max([len(s) for s in invocations]) action_length = invocation_length + self._current_indent self._action_max_length = max(self._action_max_length, action_length)
# add the item to the list self._add_item(self._format_action, [action])
def add_arguments(self, actions): for action in actions: self.add_argument(action)
# ======================= # Help-formatting methods # ======================= def format_help(self): help = self._root_section.format_help() if help: help = self._long_break_matcher.sub('\n\n', help) help = help.strip('\n') + '\n' return help
def _join_parts(self, part_strings): return ''.join([part for part in part_strings if part and part is not SUPPRESS])
def _format_usage(self, usage, actions, groups, prefix): if prefix is None: prefix = _('usage: ')
# if usage is specified, use that if usage is not None: usage = usage % dict(prog=self._prog)
# if no optionals or positionals are available, usage is just prog elif usage is None and not actions: usage = '%(prog)s' % dict(prog=self._prog)
# if optionals and positionals are available, calculate usage elif usage is None: prog = '%(prog)s' % dict(prog=self._prog)
# split optionals from positionals optionals = [] positionals = [] for action in actions: if action.option_strings: optionals.append(action) else: positionals.append(action)
# build full usage string format = self._format_actions_usage action_usage = format(optionals + positionals, groups) usage = ' '.join([s for s in [prog, action_usage] if s])
# wrap the usage parts if it's too long text_width = self._width - self._current_indent if len(prefix) + len(usage) > text_width:
# break usage into wrappable parts part_regexp = r'\(.*?\)+|\[.*?\]+|\S+' opt_usage = format(optionals, groups) pos_usage = format(positionals, groups) opt_parts = _re.findall(part_regexp, opt_usage) pos_parts = _re.findall(part_regexp, pos_usage) assert ' '.join(opt_parts) == opt_usage assert ' '.join(pos_parts) == pos_usage
# helper for wrapping lines def get_lines(parts, indent, prefix=None): lines = [] line = [] if prefix is not None: line_len = len(prefix) - 1 else: line_len = len(indent) - 1 for part in parts: if line_len + 1 + len(part) > text_width: lines.append(indent + ' '.join(line)) line = [] line_len = len(indent) - 1 line.append(part) line_len += len(part) + 1 if line: lines.append(indent + ' '.join(line)) if prefix is not None: lines[0] = lines[0][len(indent):] return lines
# if prog is short, follow it with optionals or positionals if len(prefix) + len(prog) <= 0.75 * text_width: indent = ' ' * (len(prefix) + len(prog) + 1) if opt_parts: lines = get_lines([prog] + opt_parts, indent, prefix) lines.extend(get_lines(pos_parts, indent)) elif pos_parts: lines = get_lines([prog] + pos_parts, indent, prefix) else: lines = [prog]
# if prog is long, put it on its own line else: indent = ' ' * len(prefix) parts = opt_parts + pos_parts lines = get_lines(parts, indent) if len(lines) > 1: lines = [] lines.extend(get_lines(opt_parts, indent)) lines.extend(get_lines(pos_parts, indent)) lines = [prog] + lines
# join lines into usage usage = '\n'.join(lines)
# prefix with 'usage:' return '%s%s\n\n' % (prefix, usage)
def _format_actions_usage(self, actions, groups): # find group indices and identify actions in groups group_actions = set() inserts = {} for group in groups: try: start = actions.index(group._group_actions[0]) except ValueError: continue else: end = start + len(group._group_actions) if actions[start:end] == group._group_actions: for action in group._group_actions: group_actions.add(action) if not group.required: if start in inserts: inserts[start] += ' [' else: inserts[start] = '[' inserts[end] = ']' else: if start in inserts: inserts[start] += ' (' else: inserts[start] = '(' inserts[end] = ')' for i in range(start + 1, end): inserts[i] = '|'
# collect all actions format strings parts = [] for i, action in enumerate(actions):
# suppressed arguments are marked with None # remove | separators for suppressed arguments if action.help is SUPPRESS: parts.append(None) if inserts.get(i) == '|': inserts.pop(i) elif inserts.get(i + 1) == '|': inserts.pop(i + 1)
# produce all arg strings elif not action.option_strings: default = self._get_default_metavar_for_positional(action) part = self._format_args(action, default)
# if it's in a group, strip the outer [] if action in group_actions: if part[0] == '[' and part[-1] == ']': part = part[1:-1]
# add the action string to the list parts.append(part)
# produce the first way to invoke the option in brackets else: option_string = action.option_strings[0]
# if the Optional doesn't take a value, format is: # -s or --long if action.nargs == 0: part = '%s' % option_string
# if the Optional takes a value, format is: # -s ARGS or --long ARGS else: default = self._get_default_metavar_for_optional(action) args_string = self._format_args(action, default) part = '%s %s' % (option_string, args_string)
# make it look optional if it's not required or in a group if not action.required and action not in group_actions: part = '[%s]' % part
# add the action string to the list parts.append(part)
# insert things at the necessary indices for i in sorted(inserts, reverse=True): parts[i:i] = [inserts[i]]
# join all the action items with spaces text = ' '.join([item for item in parts if item is not None])
# clean up separators for mutually exclusive groups open = r'[\[(]' close = r'[\])]' text = _re.sub(r'(%s) ' % open, r'\1', text) text = _re.sub(r' (%s)' % close, r'\1', text) text = _re.sub(r'%s *%s' % (open, close), r'', text) text = _re.sub(r'\(([^|]*)\)', r'\1', text) text = text.strip()
# return the text return text
def _format_text(self, text): if '%(prog)' in text: text = text % dict(prog=self._prog) text_width = self._width - self._current_indent indent = ' ' * self._current_indent return self._fill_text(text, text_width, indent) + '\n\n'
def _format_action(self, action): # determine the required width and the entry label help_position = min(self._action_max_length + 2, self._max_help_position) help_width = self._width - help_position action_width = help_position - self._current_indent - 2 action_header = self._format_action_invocation(action)
# ho nelp; start on same line and add a final newline if not action.help: tup = self._current_indent, '', action_header action_header = '%*s%s\n' % tup
# short action name; start on the same line and pad two spaces elif len(action_header) <= action_width: tup = self._current_indent, '', action_width, action_header action_header = '%*s%-*s ' % tup indent_first = 0
# long action name; start on the next line else: tup = self._current_indent, '', action_header action_header = '%*s%s\n' % tup indent_first = help_position
# collect the pieces of the action help parts = [action_header]
# if there was help for the action, add lines of help text if action.help: help_text = self._expand_help(action) help_lines = self._split_lines(help_text, help_width) parts.append('%*s%s\n' % (indent_first, '', help_lines[0])) for line in help_lines[1:]: parts.append('%*s%s\n' % (help_position, '', line))
# or add a newline if the description doesn't end with one elif not action_header.endswith('\n'): parts.append('\n')
# if there are any sub-actions, add their help as well for subaction in self._iter_indented_subactions(action): parts.append(self._format_action(subaction))
# return a single string return self._join_parts(parts)
def _format_action_invocation(self, action): if not action.option_strings: default = self._get_default_metavar_for_positional(action) metavar, = self._metavar_formatter(action, default)(1) return metavar
else: parts = []
# if the Optional doesn't take a value, format is: # -s, --long if action.nargs == 0: parts.extend(action.option_strings)
# if the Optional takes a value, format is: # -s ARGS, --long ARGS else: default = self._get_default_metavar_for_optional(action) args_string = self._format_args(action, default) for option_string in action.option_strings: parts.append('%s %s' % (option_string, args_string))
return ', '.join(parts)
def _metavar_formatter(self, action, default_metavar): if action.metavar is not None: result = action.metavar elif action.choices is not None: choice_strs = [str(choice) for choice in action.choices] result = '{%s}' % ','.join(choice_strs) else: result = default_metavar
def format(tuple_size): if isinstance(result, tuple): return result else: return (result, ) * tuple_size return format
def _format_args(self, action, default_metavar): get_metavar = self._metavar_formatter(action, default_metavar) if action.nargs is None: result = '%s' % get_metavar(1) elif action.nargs == OPTIONAL: result = '[%s]' % get_metavar(1) elif action.nargs == ZERO_OR_MORE: result = '[%s [%s ...]]' % get_metavar(2) elif action.nargs == ONE_OR_MORE: result = '%s [%s ...]' % get_metavar(2) elif action.nargs == REMAINDER: result = '...' elif action.nargs == PARSER: result = '%s ...' % get_metavar(1) else: formats = ['%s' for _ in range(action.nargs)] result = ' '.join(formats) % get_metavar(action.nargs) return result
def _expand_help(self, action): params = dict(vars(action), prog=self._prog) for name in list(params): if params[name] is SUPPRESS: del params[name] for name in list(params): if hasattr(params[name], '__name__'): params[name] = params[name].__name__ if params.get('choices') is not None: choices_str = ', '.join([str(c) for c in params['choices']]) params['choices'] = choices_str return self._get_help_string(action) % params
def _iter_indented_subactions(self, action): try: get_subactions = action._get_subactions except AttributeError: pass else: self._indent() for subaction in get_subactions(): yield subaction self._dedent()
def _split_lines(self, text, width): text = self._whitespace_matcher.sub(' ', text).strip() return _textwrap.wrap(text, width)
def _fill_text(self, text, width, indent): text = self._whitespace_matcher.sub(' ', text).strip() return _textwrap.fill(text, width, initial_indent=indent, subsequent_indent=indent)
def _get_help_string(self, action): return action.help
def _get_default_metavar_for_optional(self, action): return action.dest.upper()
def _get_default_metavar_for_positional(self, action): return action.dest
class RawDescriptionHelpFormatter(HelpFormatter): """Help message formatter which retains any formatting in descriptions.
Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. """
def _fill_text(self, text, width, indent): return ''.join(indent + line for line in text.splitlines(keepends=True))
class RawTextHelpFormatter(RawDescriptionHelpFormatter): """Help message formatter which retains formatting of all help text.
Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. """
def _split_lines(self, text, width): return text.splitlines()
class ArgumentDefaultsHelpFormatter(HelpFormatter): """Help message formatter which adds default values to argument help.
Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. """
def _get_help_string(self, action): help = action.help if '%(default)' not in action.help: if action.default is not SUPPRESS: defaulting_nargs = [OPTIONAL, ZERO_OR_MORE] if action.option_strings or action.nargs in defaulting_nargs: help += ' (default: %(default)s)' return help
class MetavarTypeHelpFormatter(HelpFormatter): """Help message formatter which uses the argument 'type' as the default metavar value (instead of the argument 'dest')
Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. """
def _get_default_metavar_for_optional(self, action): return action.type.__name__
def _get_default_metavar_for_positional(self, action): return action.type.__name__
# =====================# Options and Arguments# =====================
def _get_action_name(argument): if argument is None: return None elif argument.option_strings: return '/'.join(argument.option_strings) elif argument.metavar not in (None, SUPPRESS): return argument.metavar elif argument.dest not in (None, SUPPRESS): return argument.dest else: return None
class ArgumentError(Exception): """An error from creating or using an argument (optional or positional).
The string value of this exception is the message, augmented with information about the argument that caused it. """
def __init__(self, argument, message): self.argument_name = _get_action_name(argument) self.message = message
def __str__(self): if self.argument_name is None: format = '%(message)s' else: format = 'argument %(argument_name)s: %(message)s' return format % dict(message=self.message, argument_name=self.argument_name)
class ArgumentTypeError(Exception): """An error from trying to convert a command line string to a type.""" pass
# ==============# Action classes# ==============
class Action(_AttributeHolder): """Information about how to convert command line strings to Python objects.
Action objects are used by an ArgumentParser to represent the information needed to parse a single argument from one or more strings from the command line. The keyword arguments to the Action constructor are also all attributes of Action instances.
Keyword Arguments:
- option_strings -- A list of command-line option strings which should be associated with this action.
- dest -- The name of the attribute to hold the created object(s)
- nargs -- The number of command-line arguments that should be consumed. By default, one argument will be consumed and a single value will be produced. Other values include: - N (an integer) consumes N arguments (and produces a list) - '?' consumes zero or one arguments - '*' consumes zero or more arguments (and produces a list) - '+' consumes one or more arguments (and produces a list) Note that the difference between the default and nargs=1 is that with the default, a single value will be produced, while with nargs=1, a list containing a single value will be produced.
- const -- The value to be produced if the option is specified and the option uses an action that takes no values.
- default -- The value to be produced if the option is not specified.
- type -- The type which the command-line arguments should be converted to, should be one of 'string', 'int', 'float', 'complex' or a callable object that accepts a single string argument. If None, 'string' is assumed.
- choices -- A container of values that should be allowed. If not None, after a command-line argument has been converted to the appropriate type, an exception will be raised if it is not a member of this collection.
- required -- True if the action must always be specified at the command line. This is only meaningful for optional command-line arguments.
- help -- The help string describing the argument.
- metavar -- The name to be used for the option's argument with the help string. If None, the 'dest' value will be used as the name. """
def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None): self.option_strings = option_strings self.dest = dest self.nargs = nargs self.const = const self.default = default self.type = type self.choices = choices self.required = required self.help = help self.metavar = metavar
def _get_kwargs(self): names = [ 'option_strings', 'dest', 'nargs', 'const', 'default', 'type', 'choices', 'help', 'metavar', ] return [(name, getattr(self, name)) for name in names]
def __call__(self, parser, namespace, values, option_string=None): raise NotImplementedError(_('.__call__() not defined'))
class _StoreAction(Action):
def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None): if nargs == 0: raise ValueError('nargs for store actions must be > 0; if you ' 'have nothing to store, actions such as store ' 'true or store const may be more appropriate') if const is not None and nargs != OPTIONAL: raise ValueError('nargs must be %r to supply const' % OPTIONAL) super(_StoreAction, self).__init__( option_strings=option_strings, dest=dest, nargs=nargs, const=const, default=default, type=type, choices=choices, required=required, help=help, metavar=metavar)
def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, values)
class _StoreConstAction(Action):
def __init__(self, option_strings, dest, const, default=None, required=False, help=None, metavar=None): super(_StoreConstAction, self).__init__( option_strings=option_strings, dest=dest, nargs=0, const=const, default=default, required=required, help=help)
def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, self.const)
class _StoreTrueAction(_StoreConstAction):
def __init__(self, option_strings, dest, default=False, required=False, help=None): super(_StoreTrueAction, self).__init__( option_strings=option_strings, dest=dest, const=True, default=default, required=required, help=help)
class _StoreFalseAction(_StoreConstAction):
def __init__(self, option_strings, dest, default=True, required=False, help=None): super(_StoreFalseAction, self).__init__( option_strings=option_strings, dest=dest, const=False, default=default, required=required, help=help)
class _AppendAction(Action):
def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None): if nargs == 0: raise ValueError('nargs for append actions must be > 0; if arg ' 'strings are not supplying the value to append, ' 'the append const action may be more appropriate') if const is not None and nargs != OPTIONAL: raise ValueError('nargs must be %r to supply const' % OPTIONAL) super(_AppendAction, self).__init__( option_strings=option_strings, dest=dest, nargs=nargs, const=const, default=default, type=type, choices=choices, required=required, help=help, metavar=metavar)
def __call__(self, parser, namespace, values, option_string=None): items = _copy.copy(_ensure_value(namespace, self.dest, [])) items.append(values) setattr(namespace, self.dest, items)
class _AppendConstAction(Action):
def __init__(self, option_strings, dest, const, default=None, required=False, help=None, metavar=None): super(_AppendConstAction, self).__init__( option_strings=option_strings, dest=dest, nargs=0, const=const, default=default, required=required, help=help, metavar=metavar)
def __call__(self, parser, namespace, values, option_string=None): items = _copy.copy(_ensure_value(namespace, self.dest, [])) items.append(self.const) setattr(namespace, self.dest, items)
class _CountAction(Action):
def __init__(self, option_strings, dest, default=None, required=False, help=None): super(_CountAction, self).__init__( option_strings=option_strings, dest=dest, nargs=0, default=default, required=required, help=help)
def __call__(self, parser, namespace, values, option_string=None): new_count = _ensure_value(namespace, self.dest, 0) + 1 setattr(namespace, self.dest, new_count)
class _HelpAction(Action):
def __init__(self, option_strings, dest=SUPPRESS, default=SUPPRESS, help=None): super(_HelpAction, self).__init__( option_strings=option_strings, dest=dest, default=default, nargs=0, help=help)
def __call__(self, parser, namespace, values, option_string=None): parser.print_help() parser.exit()
class _VersionAction(Action):
def __init__(self, option_strings, version=None, dest=SUPPRESS, default=SUPPRESS, help="show program's version number and exit"): super(_VersionAction, self).__init__( option_strings=option_strings, dest=dest, default=default, nargs=0, help=help) self.version = version
def __call__(self, parser, namespace, values, option_string=None): version = self.version if version is None: version = parser.version formatter = parser._get_formatter() formatter.add_text(version) parser.exit(message=formatter.format_help())
class _SubParsersAction(Action):
class _ChoicesPseudoAction(Action):
def __init__(self, name, aliases, help): metavar = dest = name if aliases: metavar += ' (%s)' % ', '.join(aliases) sup = super(_SubParsersAction._ChoicesPseudoAction, self) sup.__init__(option_strings=[], dest=dest, help=help, metavar=metavar)
def __init__(self, option_strings, prog, parser_class, dest=SUPPRESS, help=None, metavar=None):
self._prog_prefix = prog self._parser_class = parser_class self._name_parser_map = _collections.OrderedDict() self._choices_actions = []
super(_SubParsersAction, self).__init__( option_strings=option_strings, dest=dest, nargs=PARSER, choices=self._name_parser_map, help=help, metavar=metavar)
def add_parser(self, name, **kwargs): # set prog from the existing prefix if kwargs.get('prog') is None: kwargs['prog'] = '%s %s' % (self._prog_prefix, name)
aliases = kwargs.pop('aliases', ())
# create a pseudo-action to hold the choice help if 'help' in kwargs: help = kwargs.pop('help') choice_action = self._ChoicesPseudoAction(name, aliases, help) self._choices_actions.append(choice_action)
# create the parser and add it to the map parser = self._parser_class(**kwargs) self._name_parser_map[name] = parser
# make parser available under aliases also for alias in aliases: self._name_parser_map[alias] = parser
return parser
def _get_subactions(self): return self._choices_actions
def __call__(self, parser, namespace, values, option_string=None): parser_name = values[0] arg_strings = values[1:]
# set the parser name if requested if self.dest is not SUPPRESS: setattr(namespace, self.dest, parser_name)
# select the parser try: parser = self._name_parser_map[parser_name] except KeyError: args = {'parser_name': parser_name, 'choices': ', '.join(self._name_parser_map)} msg = _('unknown parser %(parser_name)r (choices: %(choices)s)') % args raise ArgumentError(self, msg)
# parse all the remaining options into the namespace # store any unrecognized options on the object, so that the top # level parser can decide what to do with them namespace, arg_strings = parser.parse_known_args(arg_strings, namespace) if arg_strings: vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, []) getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
# ==============# Type classes# ==============
class FileType(object): """Factory for creating file object types
Instances of FileType are typically passed as type= arguments to the ArgumentParser add_argument() method.
Keyword Arguments: - mode -- A string indicating how the file is to be opened. Accepts the same values as the builtin open() function. - bufsize -- The file's desired buffer size. Accepts the same values as the builtin open() function. """
def __init__(self, mode='r', bufsize=-1): self._mode = mode self._bufsize = bufsize
def __call__(self, string): # the special argument "-" means sys.std{in,out} if string == '-': if 'r' in self._mode: return _sys.stdin elif 'w' in self._mode: return _sys.stdout else: msg = _('argument "-" with mode %r') % self._mode raise ValueError(msg)
# all other arguments are used as file names try: return open(string, self._mode, self._bufsize) except IOError as e: message = _("can't open '%s': %s") raise ArgumentTypeError(message % (string, e))
def __repr__(self): args = self._mode, self._bufsize args_str = ', '.join(repr(arg) for arg in args if arg != -1) return '%s(%s)' % (type(self).__name__, args_str)
# ===========================# Optional and Positional Parsing# ===========================
class Namespace(_AttributeHolder): """Simple object for storing attributes.
Implements equality by attribute names and values, and provides a simple string representation. """
def __init__(self, **kwargs): for name in kwargs: setattr(self, name, kwargs[name])
def __eq__(self, other): return vars(self) == vars(other)
def __ne__(self, other): return not (self == other)
def __contains__(self, key): return key in self.__dict__
class _ActionsContainer(object):
def __init__(self, description, prefix_chars, argument_default, conflict_handler): super(_ActionsContainer, self).__init__()
self.description = description self.argument_default = argument_default self.prefix_chars = prefix_chars self.conflict_handler = conflict_handler
# set up registries self._registries = {}
# register actions self.register('action', None, _StoreAction) self.register('action', 'store', _StoreAction) self.register('action', 'store_const', _StoreConstAction) self.register('action', 'store_true', _StoreTrueAction) self.register('action', 'store_false', _StoreFalseAction) self.register('action', 'append', _AppendAction) self.register('action', 'append_const', _AppendConstAction) self.register('action', 'count', _CountAction) self.register('action', 'help', _HelpAction) self.register('action', 'version', _VersionAction) self.register('action', 'parsers', _SubParsersAction)
# raise an exception if the conflict handler is invalid self._get_handler()
# action storage self._actions = [] self._option_string_actions = {}
# groups self._action_groups = [] self._mutually_exclusive_groups = []
# defaults storage self._defaults = {}
# determines whether an "option" looks like a negative number self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$')
# whether or not there are any optionals that look like negative # numbers -- uses a list so it can be shared and edited self._has_negative_number_optionals = []
# ==================== # Registration methods # ==================== def register(self, registry_name, value, object): registry = self._registries.setdefault(registry_name, {}) registry[value] = object
def _registry_get(self, registry_name, value, default=None): return self._registries[registry_name].get(value, default)
# ================================== # Namespace default accessor methods # ================================== def set_defaults(self, **kwargs): self._defaults.update(kwargs)
# if these defaults match any existing arguments, replace # the previous default on the object with the new one for action in self._actions: if action.dest in kwargs: action.default = kwargs[action.dest]
def get_default(self, dest): for action in self._actions: if action.dest == dest and action.default is not None: return action.default return self._defaults.get(dest, None)
# ======================= # Adding argument actions # ======================= def add_argument(self, *args, **kwargs): """ add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...) """
# if no positional args are supplied or only one is supplied and # it doesn't look like an option string, parse a positional # argument chars = self.prefix_chars if not args or len(args) == 1 and args[0][0] not in chars: if args and 'dest' in kwargs: raise ValueError('dest supplied twice for positional argument') kwargs = self._get_positional_kwargs(*args, **kwargs)
# otherwise, we're adding an optional argument else: kwargs = self._get_optional_kwargs(*args, **kwargs)
# if no default was supplied, use the parser-level default if 'default' not in kwargs: dest = kwargs['dest'] if dest in self._defaults: kwargs['default'] = self._defaults[dest] elif self.argument_default is not None: kwargs['default'] = self.argument_default
# create the action object, and add it to the parser action_class = self._pop_action_class(kwargs) if not callable(action_class): raise ValueError('unknown action "%s"' % (action_class,)) action = action_class(**kwargs)
# raise an error if the action type is not callable type_func = self._registry_get('type', action.type, action.type) if not callable(type_func): raise ValueError('%r is not callable' % (type_func,))
# raise an error if the metavar does not match the type if hasattr(self, "_get_formatter"): try: self._get_formatter()._format_args(action, None) except TypeError: raise ValueError("length of metavar tuple does not match nargs")
return self._add_action(action)
def add_argument_group(self, *args, **kwargs): group = _ArgumentGroup(self, *args, **kwargs) self._action_groups.append(group) return group
def add_mutually_exclusive_group(self, **kwargs): group = _MutuallyExclusiveGroup(self, **kwargs) self._mutually_exclusive_groups.append(group) return group
def _add_action(self, action): # resolve any conflicts self._check_conflict(action)
# add to actions list self._actions.append(action) action.container = self
# index the action by any option strings it has for option_string in action.option_strings: self._option_string_actions[option_string] = action
# set the flag if any option strings look like negative numbers for option_string in action.option_strings: if self._negative_number_matcher.match(option_string): if not self._has_negative_number_optionals: self._has_negative_number_optionals.append(True)
# return the created action return action
def _remove_action(self, action): self._actions.remove(action)
def _add_container_actions(self, container): # collect groups by titles title_group_map = {} for group in self._action_groups: if group.title in title_group_map: msg = _('cannot merge actions - two groups are named %r') raise ValueError(msg % (group.title)) title_group_map[group.title] = group
# map each action to its group group_map = {} for group in container._action_groups:
# if a group with the title exists, use that, otherwise # create a new group matching the container's group if group.title not in title_group_map: title_group_map[group.title] = self.add_argument_group( title=group.title, description=group.description, conflict_handler=group.conflict_handler)
# map the actions to their new group for action in group._group_actions: group_map[action] = title_group_map[group.title]
# add container's mutually exclusive groups # NOTE: if add_mutually_exclusive_group ever gains title= and # description= then this code will need to be expanded as above for group in container._mutually_exclusive_groups: mutex_group = self.add_mutually_exclusive_group( required=group.required)
# map the actions to their new mutex group for action in group._group_actions: group_map[action] = mutex_group
# add all actions to this container or their group for action in container._actions: group_map.get(action, self)._add_action(action)
def _get_positional_kwargs(self, dest, **kwargs): # make sure required is not specified if 'required' in kwargs: msg = _("'required' is an invalid argument for positionals") raise TypeError(msg)
# mark positional arguments as required if at least one is # always required if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]: kwargs['required'] = True if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs: kwargs['required'] = True
# return the keyword arguments with no option strings return dict(kwargs, dest=dest, option_strings=[])
def _get_optional_kwargs(self, *args, **kwargs): # determine short and long option strings option_strings = [] long_option_strings = [] for option_string in args: # error on strings that don't start with an appropriate prefix if not option_string[0] in self.prefix_chars: args = {'option': option_string, 'prefix_chars': self.prefix_chars} msg = _('invalid option string %(option)r: ' 'must start with a character %(prefix_chars)r') raise ValueError(msg % args)
# strings starting with two prefix characters are long options option_strings.append(option_string) if option_string[0] in self.prefix_chars: if len(option_string) > 1: if option_string[1] in self.prefix_chars: long_option_strings.append(option_string)
# infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x' dest = kwargs.pop('dest', None) if dest is None: if long_option_strings: dest_option_string = long_option_strings[0] else: dest_option_string = option_strings[0] dest = dest_option_string.lstrip(self.prefix_chars) if not dest: msg = _('dest= is required for options like %r') raise ValueError(msg % option_string) dest = dest.replace('-', '_')
# return the updated keyword arguments return dict(kwargs, dest=dest, option_strings=option_strings)
def _pop_action_class(self, kwargs, default=None): action = kwargs.pop('action', default) return self._registry_get('action', action, action)
def _get_handler(self): # determine function from conflict handler string handler_func_name = '_handle_conflict_%s' % self.conflict_handler try: return getattr(self, handler_func_name) except AttributeError: msg = _('invalid conflict_resolution value: %r') raise ValueError(msg % self.conflict_handler)
def _check_conflict(self, action):
# find all options that conflict with this option confl_optionals = [] for option_string in action.option_strings: if option_string in self._option_string_actions: confl_optional = self._option_string_actions[option_string] confl_optionals.append((option_string, confl_optional))
# resolve any conflicts if confl_optionals: conflict_handler = self._get_handler() conflict_handler(action, confl_optionals)
def _handle_conflict_error(self, action, conflicting_actions): message = ngettext('conflicting option string: %s', 'conflicting option strings: %s', len(conflicting_actions)) conflict_string = ', '.join([option_string for option_string, action in conflicting_actions]) raise ArgumentError(action, message % conflict_string)
def _handle_conflict_resolve(self, action, conflicting_actions):
# remove all conflicting options for option_string, action in conflicting_actions:
# remove the conflicting option action.option_strings.remove(option_string) self._option_string_actions.pop(option_string, None)
# if the option now has no option string, remove it from the # container holding it if not action.option_strings: action.container._remove_action(action)
class _ArgumentGroup(_ActionsContainer):
def __init__(self, container, title=None, description=None, **kwargs): # add any missing keyword arguments by checking the container update = kwargs.setdefault update('conflict_handler', container.conflict_handler) update('prefix_chars', container.prefix_chars) update('argument_default', container.argument_default) super_init = super(_ArgumentGroup, self).__init__ super_init(description=description, **kwargs)
# group attributes self.title = title self._group_actions = []
# share most attributes with the container self._registries = container._registries self._actions = container._actions self._option_string_actions = container._option_string_actions self._defaults = container._defaults self._has_negative_number_optionals = \ container._has_negative_number_optionals self._mutually_exclusive_groups = container._mutually_exclusive_groups
def _add_action(self, action): action = super(_ArgumentGroup, self)._add_action(action) self._group_actions.append(action) return action
def _remove_action(self, action): super(_ArgumentGroup, self)._remove_action(action) self._group_actions.remove(action)
class _MutuallyExclusiveGroup(_ArgumentGroup):
def __init__(self, container, required=False): super(_MutuallyExclusiveGroup, self).__init__(container) self.required = required self._container = container
def _add_action(self, action): if action.required: msg = _('mutually exclusive arguments must be optional') raise ValueError(msg) action = self._container._add_action(action) self._group_actions.append(action) return action
def _remove_action(self, action): self._container._remove_action(action) self._group_actions.remove(action)
class ArgumentParser(_AttributeHolder, _ActionsContainer): """Object for parsing command line strings into Python objects.
Keyword Arguments: - prog -- The name of the program (default: sys.argv[0]) - usage -- A usage message (default: auto-generated from arguments) - description -- A description of what the program does - epilog -- Text following the argument descriptions - parents -- Parsers whose arguments should be copied into this one - formatter_class -- HelpFormatter class for printing help messages - prefix_chars -- Characters that prefix optional arguments - fromfile_prefix_chars -- Characters that prefix files containing additional arguments - argument_default -- The default value for all arguments - conflict_handler -- String indicating how to handle conflicts - add_help -- Add a -h/-help option """
def __init__(self, prog=None, usage=None, description=None, epilog=None, version=None, parents=[], formatter_class=HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True):
if version is not None: import warnings warnings.warn( """The "version" argument to ArgumentParser is deprecated. """ """Please use """ """"add_argument(..., action='version', version="N", ...)" """ """instead""", DeprecationWarning)
superinit = super(ArgumentParser, self).__init__ superinit(description=description, prefix_chars=prefix_chars, argument_default=argument_default, conflict_handler=conflict_handler)
# default setting for prog if prog is None: prog = _os.path.basename(_sys.argv[0])
self.prog = prog self.usage = usage self.epilog = epilog self.version = version self.formatter_class = formatter_class self.fromfile_prefix_chars = fromfile_prefix_chars self.add_help = add_help
add_group = self.add_argument_group self._positionals = add_group(_('positional arguments')) self._optionals = add_group(_('optional arguments')) self._subparsers = None
# register types def identity(string): return string self.register('type', None, identity)
# add help and version arguments if necessary # (using explicit default to override global argument_default) default_prefix = '-' if '-' in prefix_chars else prefix_chars[0] if self.add_help: self.add_argument( default_prefix+'h', default_prefix*2+'help', action='help', default=SUPPRESS, help=_('show this help message and exit')) if self.version: self.add_argument( default_prefix+'v', default_prefix*2+'version', action='version', default=SUPPRESS, version=self.version, help=_("show program's version number and exit"))
# add parent arguments and defaults for parent in parents: self._add_container_actions(parent) try: defaults = parent._defaults except AttributeError: pass else: self._defaults.update(defaults)
# ======================= # Pretty __repr__ methods # ======================= def _get_kwargs(self): names = [ 'prog', 'usage', 'description', 'version', 'formatter_class', 'conflict_handler', 'add_help', ] return [(name, getattr(self, name)) for name in names]
# ================================== # Optional/Positional adding methods # ================================== def add_subparsers(self, **kwargs): if self._subparsers is not None: self.error(_('cannot have multiple subparser arguments'))
# add the parser class to the arguments if it's not present kwargs.setdefault('parser_class', type(self))
if 'title' in kwargs or 'description' in kwargs: title = _(kwargs.pop('title', 'subcommands')) description = _(kwargs.pop('description', None)) self._subparsers = self.add_argument_group(title, description) else: self._subparsers = self._positionals
# prog defaults to the usage message of this parser, skipping # optional arguments and with no "usage:" prefix if kwargs.get('prog') is None: formatter = self._get_formatter() positionals = self._get_positional_actions() groups = self._mutually_exclusive_groups formatter.add_usage(self.usage, positionals, groups, '') kwargs['prog'] = formatter.format_help().strip()
# create the parsers action and add it to the positionals list parsers_class = self._pop_action_class(kwargs, 'parsers') action = parsers_class(option_strings=[], **kwargs) self._subparsers._add_action(action)
# return the created parsers action return action
def _add_action(self, action): if action.option_strings: self._optionals._add_action(action) else: self._positionals._add_action(action) return action
def _get_optional_actions(self): return [action for action in self._actions if action.option_strings]
def _get_positional_actions(self): return [action for action in self._actions if not action.option_strings]
# ===================================== # Command line argument parsing methods # ===================================== def parse_args(self, args=None, namespace=None): args, argv = self.parse_known_args(args, namespace) if argv: msg = _('unrecognized arguments: %s') self.error(msg % ' '.join(argv)) return args
def parse_known_args(self, args=None, namespace=None): # args default to the system args if args is None: args = _sys.argv[1:]
# default Namespace built from parser defaults if namespace is None: namespace = Namespace()
# add any action defaults that aren't present for action in self._actions: if action.dest is not SUPPRESS: if not hasattr(namespace, action.dest): if action.default is not SUPPRESS: default = action.default if isinstance(action.default, str): default = self._get_value(action, default) setattr(namespace, action.dest, default)
# add any parser defaults that aren't present for dest in self._defaults: if not hasattr(namespace, dest): setattr(namespace, dest, self._defaults[dest])
# parse the arguments and exit if there are any errors try: namespace, args = self._parse_known_args(args, namespace) if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR): args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR)) delattr(namespace, _UNRECOGNIZED_ARGS_ATTR) return namespace, args except ArgumentError: err = _sys.exc_info()[1] self.error(str(err))
def _parse_known_args(self, arg_strings, namespace): # replace arg strings that are file references if self.fromfile_prefix_chars is not None: arg_strings = self._read_args_from_files(arg_strings)
# map all mutually exclusive arguments to the other arguments # they can't occur with action_conflicts = {} for mutex_group in self._mutually_exclusive_groups: group_actions = mutex_group._group_actions for i, mutex_action in enumerate(mutex_group._group_actions): conflicts = action_conflicts.setdefault(mutex_action, []) conflicts.extend(group_actions[:i]) conflicts.extend(group_actions[i + 1:])
# find all option indices, and determine the arg_string_pattern # which has an 'O' if there is an option at an index, # an 'A' if there is an argument, or a '-' if there is a '--' option_string_indices = {} arg_string_pattern_parts = [] arg_strings_iter = iter(arg_strings) for i, arg_string in enumerate(arg_strings_iter):
# all args after -- are non-options if arg_string == '--': arg_string_pattern_parts.append('-') for arg_string in arg_strings_iter: arg_string_pattern_parts.append('A')
# otherwise, add the arg to the arg strings # and note the index if it was an option else: option_tuple = self._parse_optional(arg_string) if option_tuple is None: pattern = 'A' else: option_string_indices[i] = option_tuple pattern = 'O' arg_string_pattern_parts.append(pattern)
# join the pieces together to form the pattern arg_strings_pattern = ''.join(arg_string_pattern_parts)
# converts arg strings to the appropriate and then takes the action seen_actions = set() seen_non_default_actions = set()
def take_action(action, argument_strings, option_string=None): seen_actions.add(action) argument_values = self._get_values(action, argument_strings)
# error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" if argument_values is not action.default: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): if conflict_action in seen_non_default_actions: msg = _('not allowed with argument %s') action_name = _get_action_name(conflict_action) raise ArgumentError(action, msg % action_name)
# take the action if we didn't receive a SUPPRESS value # (e.g. from a default) if argument_values is not SUPPRESS: action(self, namespace, argument_values, option_string)
# function to convert arg_strings into an optional action def consume_optional(start_index):
# get the optional identified at this index option_tuple = option_string_indices[start_index] action, option_string, explicit_arg = option_tuple
# identify additional optionals in the same arg string # (e.g. -xyz is the same as -x -y -z if no args are required) match_argument = self._match_argument action_tuples = [] while True:
# if we found no optional action, skip it if action is None: extras.append(arg_strings[start_index]) return start_index + 1
# if there is an explicit argument, try to match the # optional's string arguments to only this if explicit_arg is not None: arg_count = match_argument(action, 'A')
# if the action is a single-dash option and takes no # arguments, try to parse more single-dash options out # of the tail of the option string chars = self.prefix_chars if arg_count == 0 and option_string[1] not in chars: action_tuples.append((action, [], option_string)) char = option_string[0] option_string = char + explicit_arg[0] new_explicit_arg = explicit_arg[1:] or None optionals_map = self._option_string_actions if option_string in optionals_map: action = optionals_map[option_string] explicit_arg = new_explicit_arg else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg)
# if the action expect exactly one argument, we've # successfully matched the option; exit the loop elif arg_count == 1: stop = start_index + 1 args = [explicit_arg] action_tuples.append((action, args, option_string)) break
# error if a double-dash option did not use the # explicit argument else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg)
# if there is no explicit argument, try to match the # optional's string arguments with the following strings # if successful, exit the loop else: start = start_index + 1 selected_patterns = arg_strings_pattern[start:] arg_count = match_argument(action, selected_patterns) stop = start + arg_count args = arg_strings[start:stop] action_tuples.append((action, args, option_string)) break
# add the Optional to the list and return the index at which # the Optional's string args stopped assert action_tuples for action, args, option_string in action_tuples: take_action(action, args, option_string) return stop
# the list of Positionals left to be parsed; this is modified # by consume_positionals() positionals = self._get_positional_actions()
# function to convert arg_strings into positional actions def consume_positionals(start_index): # match as many Positionals as possible match_partial = self._match_arguments_partial selected_pattern = arg_strings_pattern[start_index:] arg_counts = match_partial(positionals, selected_pattern)
# slice off the appropriate arg strings for each Positional # and add the Positional and its args to the list for action, arg_count in zip(positionals, arg_counts): args = arg_strings[start_index: start_index + arg_count] start_index += arg_count take_action(action, args)
# slice off the Positionals that we just parsed and return the # index at which the Positionals' string args stopped positionals[:] = positionals[len(arg_counts):] return start_index
# consume Positionals and Optionals alternately, until we have # passed the last option string extras = [] start_index = 0 if option_string_indices: max_option_string_index = max(option_string_indices) else: max_option_string_index = -1 while start_index <= max_option_string_index:
# consume any Positionals preceding the next option next_option_string_index = min([ index for index in option_string_indices if index >= start_index]) if start_index != next_option_string_index: positionals_end_index = consume_positionals(start_index)
# only try to parse the next optional if we didn't consume # the option string during the positionals parsing if positionals_end_index > start_index: start_index = positionals_end_index continue else: start_index = positionals_end_index
# if we consumed all the positionals we could and we're not # at the index of an option string, there were extra arguments if start_index not in option_string_indices: strings = arg_strings[start_index:next_option_string_index] extras.extend(strings) start_index = next_option_string_index
# consume the next optional and any arguments for it start_index = consume_optional(start_index)
# consume any positionals following the last Optional stop_index = consume_positionals(start_index)
# if we didn't consume all the argument strings, there were extras extras.extend(arg_strings[stop_index:])
# make sure all required actions were present required_actions = [_get_action_name(action) for action in self._actions if action.required and action not in seen_actions] if required_actions: self.error(_('the following arguments are required: %s') % ', '.join(required_actions))
# make sure all required groups had one option present for group in self._mutually_exclusive_groups: if group.required: for action in group._group_actions: if action in seen_non_default_actions: break
# if no actions were used, report the error else: names = [_get_action_name(action) for action in group._group_actions if action.help is not SUPPRESS] msg = _('one of the arguments %s is required') self.error(msg % ' '.join(names))
# return the updated namespace and the extra arguments return namespace, extras
def _read_args_from_files(self, arg_strings): # expand arguments referencing files new_arg_strings = [] for arg_string in arg_strings:
# for regular arguments, just add them back into the list if arg_string[0] not in self.fromfile_prefix_chars: new_arg_strings.append(arg_string)
# replace arguments referencing files with the file content else: try: args_file = open(arg_string[1:]) try: arg_strings = [] for arg_line in args_file.read().splitlines(): for arg in self.convert_arg_line_to_args(arg_line): arg_strings.append(arg) arg_strings = self._read_args_from_files(arg_strings) new_arg_strings.extend(arg_strings) finally: args_file.close() except IOError: err = _sys.exc_info()[1] self.error(str(err))
# return the modified argument list return new_arg_strings
def convert_arg_line_to_args(self, arg_line): return [arg_line]
def _match_argument(self, action, arg_strings_pattern): # match the pattern for this action to the arg strings nargs_pattern = self._get_nargs_pattern(action) match = _re.match(nargs_pattern, arg_strings_pattern)
# raise an exception if we weren't able to find a match if match is None: nargs_errors = { None: _('expected one argument'), OPTIONAL: _('expected at most one argument'), ONE_OR_MORE: _('expected at least one argument'), } default = ngettext('expected %s argument', 'expected %s arguments', action.nargs) % action.nargs msg = nargs_errors.get(action.nargs, default) raise ArgumentError(action, msg)
# return the number of arguments matched return len(match.group(1))
def _match_arguments_partial(self, actions, arg_strings_pattern): # progressively shorten the actions list by slicing off the # final actions until we find a match result = [] for i in range(len(actions), 0, -1): actions_slice = actions[:i] pattern = ''.join([self._get_nargs_pattern(action) for action in actions_slice]) match = _re.match(pattern, arg_strings_pattern) if match is not None: result.extend([len(string) for string in match.groups()]) break
# return the list of arg string counts return result
def _parse_optional(self, arg_string): # if it's an empty string, it was meant to be a positional if not arg_string: return None
# if it doesn't start with a prefix, it was meant to be positional if not arg_string[0] in self.prefix_chars: return None
# if the option string is present in the parser, return the action if arg_string in self._option_string_actions: action = self._option_string_actions[arg_string] return action, arg_string, None
# if it's just a single character, it was meant to be positional if len(arg_string) == 1: return None
# if the option string before the "=" is present, return the action if '=' in arg_string: option_string, explicit_arg = arg_string.split('=', 1) if option_string in self._option_string_actions: action = self._option_string_actions[option_string] return action, option_string, explicit_arg
# search through all possible prefixes of the option string # and all actions in the parser for possible interpretations option_tuples = self._get_option_tuples(arg_string)
# if multiple actions match, the option string was ambiguous if len(option_tuples) > 1: options = ', '.join([option_string for action, option_string, explicit_arg in option_tuples]) args = {'option': arg_string, 'matches': options} msg = _('ambiguous option: %(option)s could match %(matches)s') self.error(msg % args)
# if exactly one action matched, this segmentation is good, # so return the parsed action elif len(option_tuples) == 1: option_tuple, = option_tuples return option_tuple
# if it was not found as an option, but it looks like a negative # number, it was meant to be positional # unless there are negative-number-like options if self._negative_number_matcher.match(arg_string): if not self._has_negative_number_optionals: return None
# if it contains a space, it was meant to be a positional if ' ' in arg_string: return None
# it was meant to be an optional but there is no such option # in this parser (though it might be a valid option in a subparser) return None, arg_string, None
def _get_option_tuples(self, option_string): result = []
# option strings starting with two prefix characters are only # split at the '=' chars = self.prefix_chars if option_string[0] in chars and option_string[1] in chars: if '=' in option_string: option_prefix, explicit_arg = option_string.split('=', 1) else: option_prefix = option_string explicit_arg = None for option_string in self._option_string_actions: if option_string.startswith(option_prefix): action = self._option_string_actions[option_string] tup = action, option_string, explicit_arg result.append(tup)
# single character options can be concatenated with their arguments # but multiple character options always have to have their argument # separate elif option_string[0] in chars and option_string[1] not in chars: option_prefix = option_string explicit_arg = None short_option_prefix = option_string[:2] short_explicit_arg = option_string[2:]
for option_string in self._option_string_actions: if option_string == short_option_prefix: action = self._option_string_actions[option_string] tup = action, option_string, short_explicit_arg result.append(tup) elif option_string.startswith(option_prefix): action = self._option_string_actions[option_string] tup = action, option_string, explicit_arg result.append(tup)
# shouldn't ever get here else: self.error(_('unexpected option string: %s') % option_string)
# return the collected option tuples return result
def _get_nargs_pattern(self, action): # in all examples below, we have to allow for '--' args # which are represented as '-' in the pattern nargs = action.nargs
# the default (None) is assumed to be a single argument if nargs is None: nargs_pattern = '(-*A-*)'
# allow zero or one arguments elif nargs == OPTIONAL: nargs_pattern = '(-*A?-*)'
# allow zero or more arguments elif nargs == ZERO_OR_MORE: nargs_pattern = '(-*[A-]*)'
# allow one or more arguments elif nargs == ONE_OR_MORE: nargs_pattern = '(-*A[A-]*)'
# allow any number of options or arguments elif nargs == REMAINDER: nargs_pattern = '([-AO]*)'
# allow one argument followed by any number of options or arguments elif nargs == PARSER: nargs_pattern = '(-*A[-AO]*)'
# all others should be integers else: nargs_pattern = '(-*%s-*)' % '-*'.join('A' * nargs)
# if this is an optional action, -- is not allowed if action.option_strings: nargs_pattern = nargs_pattern.replace('-*', '') nargs_pattern = nargs_pattern.replace('-', '')
# return the pattern return nargs_pattern
# ======================== # Value conversion methods # ======================== def _get_values(self, action, arg_strings): # for everything but PARSER args, strip out '--' if action.nargs not in [PARSER, REMAINDER]: arg_strings = [s for s in arg_strings if s != '--']
# optional argument produces a default when not present if not arg_strings and action.nargs == OPTIONAL: if action.option_strings: value = action.const else: value = action.default if isinstance(value, str): value = self._get_value(action, value) self._check_value(action, value)
# when nargs='*' on a positional, if there were no command-line # args, use the default if it is anything other than None elif (not arg_strings and action.nargs == ZERO_OR_MORE and not action.option_strings): if action.default is not None: value = action.default else: value = arg_strings self._check_value(action, value)
# single argument or optional argument produces a single value elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]: arg_string, = arg_strings value = self._get_value(action, arg_string) self._check_value(action, value)
# REMAINDER arguments convert all values, checking none elif action.nargs == REMAINDER: value = [self._get_value(action, v) for v in arg_strings]
# PARSER arguments convert all values, but check only the first elif action.nargs == PARSER: value = [self._get_value(action, v) for v in arg_strings] self._check_value(action, value[0])
# all other types of nargs produce a list else: value = [self._get_value(action, v) for v in arg_strings] for v in value: self._check_value(action, v)
# return the converted value return value
def _get_value(self, action, arg_string): type_func = self._registry_get('type', action.type, action.type) if not callable(type_func): msg = _('%r is not callable') raise ArgumentError(action, msg % type_func)
# convert the value to the appropriate type try: result = type_func(arg_string)
# ArgumentTypeErrors indicate errors except ArgumentTypeError: name = getattr(action.type, '__name__', repr(action.type)) msg = str(_sys.exc_info()[1]) raise ArgumentError(action, msg)
# TypeErrors or ValueErrors also indicate errors except (TypeError, ValueError): name = getattr(action.type, '__name__', repr(action.type)) args = {'type': name, 'value': arg_string} msg = _('invalid %(type)s value: %(value)r') raise ArgumentError(action, msg % args)
# return the converted value return result
def _check_value(self, action, value): # converted value must be one of the choices (if specified) if action.choices is not None and value not in action.choices: args = {'value': value, 'choices': ', '.join(map(repr, action.choices))} msg = _('invalid choice: %(value)r (choose from %(choices)s)') raise ArgumentError(action, msg % args)
# ======================= # Help-formatting methods # ======================= def format_usage(self): formatter = self._get_formatter() formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) return formatter.format_help()
def format_help(self): formatter = self._get_formatter()
# usage formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups)
# description formatter.add_text(self.description)
# positionals, optionals and user-defined groups for action_group in self._action_groups: formatter.start_section(action_group.title) formatter.add_text(action_group.description) formatter.add_arguments(action_group._group_actions) formatter.end_section()
# epilog formatter.add_text(self.epilog)
# determine help from format above return formatter.format_help()
def format_version(self): import warnings warnings.warn( 'The format_version method is deprecated -- the "version" ' 'argument to ArgumentParser is no longer supported.', DeprecationWarning) formatter = self._get_formatter() formatter.add_text(self.version) return formatter.format_help()
def _get_formatter(self): return self.formatter_class(prog=self.prog)
# ===================== # Help-printing methods # ===================== def print_usage(self, file=None): if file is None: file = _sys.stdout self._print_message(self.format_usage(), file)
def print_help(self, file=None): if file is None: file = _sys.stdout self._print_message(self.format_help(), file)
def print_version(self, file=None): import warnings warnings.warn( 'The print_version method is deprecated -- the "version" ' 'argument to ArgumentParser is no longer supported.', DeprecationWarning) self._print_message(self.format_version(), file)
def _print_message(self, message, file=None): if message: if file is None: file = _sys.stderr file.write(message)
# =============== # Exiting methods # =============== def exit(self, status=0, message=None): if message: self._print_message(message, _sys.stderr) _sys.exit(status)
def error(self, message): """error(message: string)
Prints a usage message incorporating the message to stderr and exits.
If you override this in a subclass, it should not return -- it should either exit or raise an exception. """ self.print_usage(_sys.stderr) args = {'prog': self.prog, 'message': message} self.exit(2, _('%(prog)s: error: %(message)s\n') % args)12345678ad af 500 1000bd bf 5 10der le 20285 102586der NULL 12926 704917gipfel sommet 3485 7322pass col 419 2911pass passeport 7 28sitzung séance 14 5912345678af ad 500 1000bf bd 5 10col pass 419 615le der 20285 113635passeport pass 7 615retrouvé NULL 34 1016136séance sitzung 14 33sommet gipfel 3485 570012345678ad af 0.5bd bf 0.5der le 0.1977365der NULL 0.0183369gipfel sommet 0.4759629pass col 0.1439368pass passeport 0.2500000sitzung séance 0.237288112345678af ad 0.5bf bd 0.5col pass 0.6813008le der 0.1785101passeport pass 0.0113821retrouvé NULL 0.0000335séance sitzung 0.4242424sommet gipfel 0.611403512345678ad ||| af ||| 0.3 0.3 0.3 0.3 0.5 0.5 0.5 0.5 2.718 ||| 0-0 ||| 1000 1000 ||| sparse_feature 1bd ||| bf ||| 0.3 0.3 0.3 0.3 0.5 0.5 0.5 0.5 2.718 ||| 0-0 ||| 10 10 |||der gipfel ||| sommet ||| 0.3 0.3 0.3 0.3 0.00327135 0.00872768 0.0366795 0.611403 2.718 ||| 1-0 ||| 5808 518der pass ||| le col ||| 0.3 0.3 0.3 0.3 0.0173565 0.0284616 0.288889 0.121619 2.718 ||| 0-0 1-1 ||| 749 45pass ||| col ||| 0.3 0.3 0.3 0.3 0.1952 0.143937 0.628866 0.681301 2.718 ||| 0-0 ||| 1875 582pass ||| passeport retrouvé ||| 0.3 0.3 0.3 0.3 0.5 0.25 0.00171821 3.813e-07 2.718 ||| 0-0 ||| 2 582pass ||| passeport ||| 0.3 0.3 0.3 0.3 0.266667 0.25 0.00687285 0.0113821 2.718 ||| 0-0 ||| 15 582sitzung ||| séance ||| 0.3 0.3 0.3 0.3 0.272727 0.237288 0.352941 0.424242 2.718 ||| 0-0 ||| 22 1712345678ad af 500 1000bd bf 5 10der le 20285 102586der NULL 12926 704917gipfel sommet 3485 7322pass col 419 2911pass passeport 7 28sitzung séance 14 5912345678af ad 500 1000bf bd 5 10col pass 419 615le der 20285 113635passeport pass 7 615retrouvé NULL 34 1016136séance sitzung 14 33sommet gipfel 3485 570012345678ad af 0.5bd bf 0.5der le 0.1977365der NULL 0.0183369gipfel sommet 0.4759629pass col 0.1439368pass passeport 0.2500000sitzung séance 0.237288112345678af ad 0.5bf bd 0.5col pass 0.6813008le der 0.1785101passeport pass 0.0113821retrouvé NULL 0.0000335séance sitzung 0.4242424sommet gipfel 0.611403512345678ad [X][X] [X] ||| af [X][X] [X] ||| 0.5 0.5 0.5 0.5 2.718 ||| 0-0 1-1 ||| 1000 1000bd [X] ||| bf [X] ||| 0.5 0.5 0.5 0.5 2.718 ||| 0-0 ||| 10 10der gipfel [X] ||| sommet [X] ||| 0.00327135 0.00872768 0.0366795 0.611403 2.718 ||| 1-0 ||| 5808 518der [X][X] pass [X] ||| le [X][X] col [X] ||| 0.0173565 0.0284616 0.288889 0.121619 2.718 ||| 0-0 1-1 2-2 ||| 749 45pass [X] ||| col [X] ||| 0.1952 0.143937 0.628866 0.681301 2.718 ||| 0-0 ||| 1875 582pass [X] ||| passeport retrouvé [X] ||| 0.5 0.25 0.00171821 3.813e-07 2.718 ||| 0-0 ||| 2 582pass [X] ||| passeport [X] ||| 0.266667 0.25 0.00687285 0.0113821 2.718 ||| 0-0 ||| 15 582[X][X] sitzung [X] ||| [X][X] séance [X] ||| 0.272727 0.237288 0.352941 0.424242 2.718 ||| 0-0 1-1 ||| 22 1712345678ad af 100 1000bd bf 1 10der le 150181 944391der NULL 54483 3595140gipfel sommet 3421 9342pass col 2 70pass passeport 73 379sitzung séance 3441 575312345678af ad 100 1000bf bd 1 10col pass 2 108le der 150181 1356104passeport pass 73 108retrouvé NULL 43 6276240séance sitzung 3441 6142sommet gipfel 3421 490812345678ad af 0.1bd bf 0.1der le 0.1590242der NULL 0.0151546gipfel sommet 0.366195pass col 0.0285714pass passeport 0.1926121sitzung séance 0.598122712345678af ad 0.1bf bd 0.1col pass 0.0185185le der 0.1107445passeport pass 0.6759259retrouvé NULL 0.0000069séance sitzung 0.5602410sommet gipfel 0.697025312345ad [X][X] [X] ||| af [X][X] [X] ||| 0.1 0.1 0.1 0.1 2.718 ||| 0-0 1-1 ||| 1000 1000bd [X] ||| bf [X] ||| 0.1 0.1 0.1 0.1 2.718 ||| 0-0 ||| 10 10der [X][X] pass [X] ||| le [X][X] passeport [X] ||| 0.16 0.03063 0.4 0.0748551 2.718 ||| 0-0 1-1 2-2 ||| 25 10pass [X] ||| passeport [X] ||| 0.28022 0.192612 0.607143 0.675926 2.718 ||| 0-0 ||| 182 84[X][X] sitzung [X] ||| [X][X] séance [X] ||| 0.784521 0.598123 0.516654 0.560241 2.718 ||| 0-0 1-1 ||| 4251 645512345678ad ||| af ||| 0.3 0.3 0.3 0.3 ||| 0-0 ||| 1000 1000bd ||| bf ||| 0.3 0.3 0.3 0.3 ||| 0-0 ||| 10 10der gipfel ||| sommet ||| 0.00163568 0.00436384 0.0183397 0.305702 ||| 1-0 ||| 5808 518der pass ||| le col ||| 0.00867825 0.0142308 0.144445 0.0608095 ||| 0-0 1-1 ||| 749 45pass ||| col ||| 0.0976 0.0719685 0.314433 0.340651 ||| 0-0 ||| 1875 582pass ||| passeport retrouvé ||| 0.25 0.125 0.000859105 1.9065e-07 ||| 0-0 ||| 2 582pass ||| passeport ||| 0.273444 0.221306 0.307008 0.343654 ||| 0-0 ||| 15 582sitzung ||| séance ||| 0.528624 0.417705 0.434797 0.492241 ||| 0-0 ||| 22 17123456789ad ||| af ||| 0.3 0.3 0.3 0.3 0.115771 0.35574 0.472359 0.469238 ||| 0-0 ||| 25362.6029089 1074.23173673 ||| sparse_feature 1bd ||| bf ||| 0.3 0.3 0.3 0.3 0.115771 0.35574 0.472359 0.469238 ||| 0-0 ||| 253.626029089 10.7423173673 ||| der gipfel ||| sommet ||| 0.3 0.3 0.3 0.3 0.00327135 0.00686984 0.0366795 0.617135 ||| 1-0 ||| 5808.0 518.0der pass ||| le col ||| 0.3 0.3 0.3 0.3 0.0173565 0.023534 0.284201 0.0972183 ||| 0-0 1-1 ||| 749.0 45.7423173673der pass ||| le passeport ||| 6e-10 6e-10 6e-10 6e-10 0.16 0.0329324 0.0064913 0.00303408 ||| 0-0 1-1 ||| 609.065072723 45.7423173673pass ||| col ||| 0.3 0.3 0.3 0.3 0.1952 0.142393 0.6222 0.671744 ||| 0-0 ||| 1875.0 588.235465885pass ||| passeport retrouvé ||| 0.3 0.3 0.3 0.3 0.5 0.199258 0.0017 5.11945e-07 ||| 0-0 ||| 2.0 588.235465885pass ||| passeport ||| 0.3 0.3 0.3 0.3 0.280174 0.199258 0.0132359 0.0209644 ||| 0-0 ||| 4448.99372942 588.235465885sitzung ||| séance ||| 0.3 0.3 0.3 0.3 0.784412 0.59168 0.511045 0.552002 ||| 0-0 ||| 103587.424966 496.165860589mosesdecoder/contrib/arrow-pipelines/READMEAdded+58
mosesdecoder/contrib/arrow-pipelines/documentation/training-pipeline/moses-pypeline.diaAdded
mosesdecoder/contrib/arrow-pipelines/pcl/MakefileAdded+23
mosesdecoder/contrib/arrow-pipelines/pcl/components/MakefileAdded+24
mosesdecoder/contrib/arrow-pipelines/pcl/components/src_trg_tokeniser.cfgAdded+10
mosesdecoder/contrib/arrow-pipelines/pcl/components/src_trg_tokeniser.pclAdded+40
mosesdecoder/contrib/arrow-pipelines/pcl/components/translation_model_training.cfgAdded+15
mosesdecoder/contrib/arrow-pipelines/pcl/components/translation_model_training.pclAdded+70
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/MakefileAdded+14
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/__init__.pyAdded
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/cleanup/__init__.pyAdded
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/cleanup/cleanup.pyAdded+129
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/__init__.pyAdded
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/data_split.cfgAdded+7
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/data_split.pyAdded+144
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/test_data/data.deAdded+50
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/data_split/test_data/data.enAdded+50
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/irstlm_build/__init__.pyAdded
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/irstlm_build/irstlm_build.pyAdded+117
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/mert/__init__.pyAdded
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/mert/mert.pyAdded+98
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/model_training/__init__.pyAdded
mosesdecoder/contrib/arrow-pipelines/pcl/components/wrappers/model_training/model_training.pyAdded+103
mosesdecoder/contrib/arrow-pipelines/pcl/training_pipeline.cfgAdded+21
mosesdecoder/contrib/arrow-pipelines/pcl/training_pipeline.pclAdded+117
mosesdecoder/contrib/checkplf/MakefileAdded+2
mosesdecoder/contrib/lmserver/Makefile.inAdded+645
mosesdecoder/contrib/lmserver/daemon.cAdded+68
mosesdecoder/contrib/lmserver/lmserver.cAdded+2140
mosesdecoder/contrib/lmserver/missingAdded+367
mosesdecoder/contrib/lmserver/srilm.hAdded+8
mosesdecoder/contrib/tmcombine/README.mdAdded+88
mosesdecoder/contrib/tmcombine/argparse.pyAdded+2382
mosesdecoder/contrib/tmcombine/test/model3/model/lex.counts.e2fAdded+8
mosesdecoder/contrib/tmcombine/test/model3/model/lex.counts.f2eAdded+8
mosesdecoder/contrib/tmcombine/test/model3/model/lex.e2fAdded+8
mosesdecoder/contrib/tmcombine/test/model3/model/lex.f2eAdded+8
mosesdecoder/contrib/tmcombine/test/model3/model/phrase-tableAdded+8
mosesdecoder/contrib/tmcombine/test/model5/model/lex.counts.e2fAdded+8
mosesdecoder/contrib/tmcombine/test/model5/model/lex.counts.f2eAdded+8
mosesdecoder/contrib/tmcombine/test/model5/model/lex.e2fAdded+8
mosesdecoder/contrib/tmcombine/test/model5/model/lex.f2eAdded+8
mosesdecoder/contrib/tmcombine/test/model5/model/phrase-tableAdded+8
mosesdecoder/contrib/tmcombine/test/model6/model/lex.counts.e2fAdded+8
mosesdecoder/contrib/tmcombine/test/model6/model/lex.counts.f2eAdded+8
mosesdecoder/contrib/tmcombine/test/model6/model/lex.e2fAdded+8
mosesdecoder/contrib/tmcombine/test/model6/model/lex.f2eAdded+8
mosesdecoder/contrib/tmcombine/test/model6/model/phrase-tableAdded+5
mosesdecoder/contrib/tmcombine/test/phrase-table_test1Added+8
mosesdecoder/contrib/tmcombine/test/phrase-table_test10Added+9