def html_output(self): ext = '.html' today = datetime.date.today().isoformat() sha = self.test_file + ".html.sha" # cannot recover if generating html fails options = (['--zip'] + self.options + ['-f', 'html', self.test_file, self.test_out + ext + '.zip']) try: self.gdoc_to(*options, _err=self.test_err + ".html.log") # XXX it hangs without -n, didn't have time to figure out why out_dir = os.path.dirname(self.test_out) sh.unzip('-n', '-d', out_dir, self.test_out + ext + '.zip') sh.sed('-i', '-e', 's/%s/TODAYS_DATE/g' % today, self.test_out + ext) test_result = slurp('%s.html' % self.test_out) except ErrorReturnCode as e: self.say(red("gdoc-to failed: {}. See {}.html.log"), e, self.test_err) self.say(red("Ran in {}"), os.getcwd()) self.failed = True sh.rm('-f', sha) return try: html5check(self.test_out + ext, _out=self.test_out + ".html.errors") except ErrorReturnCode: self.say(red("Test output did not validate as XHTML5!")) self.say(red("\tSee {}.html.errors"), self.test_out) self.failed = True if test_result != slurp(self.test_file + ext): # the file changed, but the change might be okay spit(self._canonical_body(self.test_out + ext), self.test_out + ".body") spit(self._canonical_body(self.test_file + ext), self.test_out + ".canon.body") if (slurp(self.test_out + '.body') == slurp(self.test_out + '.canon.body')): self.say(yellow("File changed. Updating canonical file.")) sh.cp(self.test_out + ext, self.test_file + ext) else: self.say(red("HTML body changed!")) self.say(red("\tSee {}.*"), fail_path(self.test_name)) sh.cp(self.test_out + ext, fail_path(self.test_name + ext)) sh.diff('-u', self.test_file + ext, self.test_out + ext, _out=fail_path(self.test_name + ".html.diff"), _ok_code=[0, 1]) sh.cp(self.test_out + ".body", fail_path(self.test_name + ".body")) sh.cp(self.test_out + ".canon.body", fail_path(self.test_name + ".body.expected")) sh.diff('-u', self.test_out + ".canon.body", self.test_out + ".body", _out=fail_path(self.test_name + '.body.diff'), _ok_code=[0, 1]) self.failed = True
def different(file1, file2): ''' Returns whether the files are different. ''' # diff succeeds if there is a difference, and fails if no difference try: sh.diff(file1, file2, brief=True) different = False except sh.ErrorReturnCode: different = True return different
def cmp_files(self, f1, f2): from ngs_mapper.compat import check_output try: assert exists(f1), "{0} doesn't exist".format(f1) assert exists(f2), "{0} doesn't exist".format(f2) import sh #print check_output('diff {0} {1}'.format(f1, f2), shell=True) print sh.diff(f1,f2) return True except subprocess.CalledProcessError as e: print e print e.output print "Here is the generated file for comparison" print open(f2).read() return False
def compare(base_dir, hash_a, hash_b): """Take the results of two performance runs and compare them.""" file_a = os.path.join(base_dir, hash_a + ".json") file_b = os.path.join(base_dir, hash_b + ".json") description = "{0} vs {1}".format(hash_a, hash_b) report_benchmark_results(file_a, file_b, description) # From the two json files extract the profiles and diff them generate_profile_file(file_a, hash_a, base_dir) generate_profile_file(file_b, hash_b, base_dir) sh.diff("-u", os.path.join(base_dir, hash_a + "_profile.txt"), os.path.join(base_dir, hash_b + "_profile.txt"), _out=os.path.join(IMPALA_HOME, "performance_result_profile_diff.txt"), _ok_code=[0, 1])
def same_metadata(self, is_fail_expected): ext = '.meta.yml' try: self.gdoc_to(*(self.options + ['-f', 'meta', self.test_file]), _out=self.test_out + ext, _err=self.test_err + '.meta.log') except sh.ErrorReturnCode: if not is_fail_expected: self.say(red('Checking metadata failed!')) self.say(red('\tSee {}.meta.log'), self.test_err) self.failed = True if os.path.exists(self.test_file + ext): diff = sh.diff(self.test_out + ext, self.test_file + ext, _out=self.test_out + '.meta.yml.diff', _ok_code=[0, 1]) if diff.exit_code != 0: self.say( red("Test meta file changed!") + " [%s]" % self.test_out) self.say(red("\tSee {}.meta.yml* and {}.meta.log"), fail_path(self.test_name), self.test_err) sh.cp(self.test_out + ext, fail_path(self.test_name + ext)) sh.cp(self.test_out + '.meta.yml.diff', fail_path(self.test_name + '.meta.yml.diff')) self.failed = True else: self.say(red("No meta to test against!") + " [%s]" % self.test_out) sh.cp(self.test_out + ext, fail_path(self.test_name + ext)) self.failed = True
def grade(uniqname, link): print("Grading {}".format(uniqname)) with pushd("373-f15-linked-list"): wget(link, "-O", "{}.c".format(uniqname)) rm("-f", "list.c", "list.o", "list") ln("-s", "{}.c".format(uniqname), "list.c") make("run") try: diff("list.out", "golden.out") perfect_grade(uniqname) except sh.ErrorReturnCode_1: try: diff("list.out", "naive.out") no_change(uniqname) except sh.ErrorReturnCode_1: handgrade(uniqname)
def xxd_diff(old, new): ''' just for fun ''' # xxd -p for pure hexdump # -p must pass in before file name shell = check_shell() # support : bash, zsh # not support : dash cmd = 'diff <(xxd -p {}) <(xxd -p {})'.format(old, new) if shell['bash']: return sh.bash('-c', cmd, _ok_code=[0, 1]) elif shell['zsh']: return sh.zsh('-c', cmd, _ok_code=[0, 1]) else: tmp_old = '/var/tmp/old_hex' tmp_new = '/var/tmp/new_hex' sh.xxd('-p', old, _out=tmp_old) sh.xxd('-p', new, _out=tmp_new) patch = sh.diff(old, new, _iter=True, _ok_code=[0, 1]) sh.rm('-f', tmp_old) sh.rm('-f', tmp_new) return patch
def same_metadata(self, is_fail_expected): ext = '.meta.yml' try: self.gdoc_to(*(self.options + ['-f', 'meta', self.test_file]), _out=self.test_out + ext, _err=self.test_err + '.meta.log') except sh.ErrorReturnCode: if not is_fail_expected: self.say(red('Checking metadata failed!')) self.say(red('\tSee {}.meta.log'), self.test_err) self.failed = True if os.path.exists(self.test_file + ext): diff = sh.diff(self.test_out + ext, self.test_file + ext, _out=self.test_out + '.meta.yml.diff', _ok_code=[0, 1]) if diff.exit_code != 0: self.say(red("Test meta file changed!") + " [%s]" % self.test_out) self.say(red("\tSee {}.meta.yml* and {}.meta.log"), fail_path(self.test_name), self.test_err) sh.cp(self.test_out + ext, fail_path(self.test_name + ext)) sh.cp(self.test_out + '.meta.yml.diff', fail_path(self.test_name + '.meta.yml.diff')) self.failed = True else: self.say(red("No meta to test against!") + " [%s]" % self.test_out) sh.cp(self.test_out + ext, fail_path(self.test_name + ext)) self.failed = True
def test02(self): config = self.load_config(self._p('conf/2_test.yml')) try: run(config) except SystemExit as e: pass diff_res = sh.diff(self._p("test_output/I88_force_close.csv"), self._p("expect_output/2_I88_force_close.csv")) self.assertEqual(diff_res, "")
def test01(self): config = self.load_config(self._p('conf/1_test.yml')) try: run(config) except SystemExit as e: pass self.fetch_fields(self._p("test_output/trades.csv"), self._p("test_output/trades_filter.csv")) diff_res = sh.diff(self._p("test_output/trades_filter.csv"), self._p("expect_output/1_trades_filter.csv")) self.assertEqual(diff_res, "")
def test01(self): config = self.load_config(self._p('conf/1_test.yml')) try: run(config) except SystemExit as e: pass diff_res = sh.diff(self._p("test_output/I88_stop_profit_loss.csv"), self._p("expect_output/1_I88_stop_profit_loss.csv")) self.assertEqual(diff_res, "")
def base_diff(d1, d2, file): c1 = read_file(d1, file) c2 = read_file(d2, file) if c1 == c2: return EQUAL, None result = sh.diff("-u", os.path.join(d1, file), os.path.join(d2, file), _ok_code=[0, 1]) return DIFFER, str(result)
def diff(self, dictionary, target): try: p = sh.diff( dictionary.name, target.name, text=True, ed=True, minimal=True, _ok_code=[0, 1]) return p.stdout except sh.ErrorReturnCode_2 as e: if 'No newline at end of file' in e.stderr: return e.stdout else: raise e
def update_remote_file(path, func, show_diff=False): localpath = get_remote_file(path) localbackuppath = "%s.backup" % localpath sh.cp(localpath, localbackuppath) func(localpath) if show_diff: p = sh.diff('-Nau', localpath, localbackuppath, _ok_code=[0, 1]) print(p.stdout) push_remote_file(localpath, path)
def new_meta_output(self): diff = sh.diff(self.test_out + ".oldmeta.yml", self.test_file + ".meta.yml", _out=self.test_out + ".oldmeta.yml.diff", _ok_code=[0, 1]) if diff.exit_code != 0: self.say(red("Flag --new-meta changed output!")) self.say(red("\tSee {}.oldmeta.yml*"), fail_path(self.test_name)) sh.cp(self.test_out + ".oldmeta.yml.diff", fail_path(self.test_name + ".oldmeta.yml.diff")) sh.cp(self.test_out + ".oldmeta.yml", fail_path(self.test_name + ".oldmeta.yml")) self.failed = True
def replace_attribute(module_name, attribute_name, new_value, dry_run=True): init_file = '%s/__init__.py' % module_name _, tmp_file = tempfile.mkstemp() with open(init_file) as input_file: with open(tmp_file, 'w') as output_file: for line in input_file: if line.startswith(attribute_name): line = "%s = '%s'\n" % (attribute_name, new_value) output_file.write(line) if not dry_run: path(tmp_file).move(init_file) else: log.debug(sh.diff(tmp_file, init_file, _ok_code=1))
def test_expand(self): test_repo_bkup = _p.join(self.tmpdir.name, 'hello-git-repo.bkup') sh.cp(self.test_repo, test_repo_bkup, r=True) git_expand(self.test_repo, self.tmpdir.name, 'branch-A', 'branch-B', 'branch-C') with open(_p.join(self.tmpdir.name, 'branch-A', 'A'), 'r') as f: assert f.read() == 'A' with open(_p.join(self.tmpdir.name, 'branch-B', 'B'), 'r') as f: assert f.read() == 'B' with open(_p.join(self.tmpdir.name, 'branch-C', 'C'), 'r') as f: assert f.read() == 'C' assert not str(sh.diff(self.test_repo, test_repo_bkup, r=True))
def html_input(self): # FIXME(alexander): this should really compare # internal format rather than tex output, # but the trouble is whitespace; the diff -w # trick does not work reliably for the internal format, # because it can affect linebreaks in pretty printing # and newlines and tabs are escaped. self.gdoc_to(*(self.options + ['-f', 'tex', self.test_file, self.test_out + '-orig.tex']), _err=self.test_err + ".html-in-orig.log") self.gdoc_to(*(self.options + ['-f', 'tex', self.test_file + '.html', self.test_out + '-html.tex']), _err=self.test_err + ".html-in-tex.log") res = sh.diff(self.test_out + '-html.tex', self.test_out + '-orig.tex', _out=fail_path(self.test_name + '.html-input.diff'), _ok_code=[0, 1]) if res.exit_code: self.failed = True self.say(red("HTML import gives different results!")) self.say(red("\tSee {}.*"), fail_path(self.test_name))
def html_input(self): # FIXME(alexander): this should really compare # internal format rather than tex output, # but the trouble is whitespace; the diff -w # trick does not work reliably for the internal format, # because it can affect linebreaks in pretty printing # and newlines and tabs are escaped. self.gdoc_to( *(self.options + ['-f', 'tex', self.test_file, self.test_out + '-orig.tex']), _err=self.test_err + ".html-in-orig.log") self.gdoc_to(*(self.options + [ '-f', 'tex', self.test_file + '.html', self.test_out + '-html.tex' ]), _err=self.test_err + ".html-in-tex.log") res = sh.diff(self.test_out + '-html.tex', self.test_out + '-orig.tex', _out=fail_path(self.test_name + '.html-input.diff'), _ok_code=[0, 1]) if res.exit_code: self.failed = True self.say(red("HTML import gives different results!")) self.say(red("\tSee {}.*"), fail_path(self.test_name))
def same_meta_as_orig(self): newmeta = self.test_out + ".newmeta.yml" try: self.gdoc_to(*(self.options + ['-f', 'meta', self.test_new]), _out=newmeta, _err=self.test_err + ".newmeta.log") except ErrorReturnCode: self.failed = True diff = sh.diff(newmeta, self.test_out + ".oldmeta.yml", _out=self.test_out + ".newmeta.yml.diff", _ok_code=[0, 1]) if diff.exit_code != 0: failed_test_path = fail_path(self.test_name) self.say(red("Reading back new file gives different meta!")) self.say("\tSee {}.(new|old)meta.yml* and %s", failed_test_path, self.test_new) sh.mkdir('-p', failed_test_path) sh.cp(newmeta + '.diff', failed_test_path + ".newmeta.yml.diff") sh.cp(newmeta, failed_test_path + ".newmeta.yml") sh.cp(self.test_out + ".oldmeta.yml", failed_test_path + ".oldmeta.yml") self.failed = True
from sh import cp, rm, diff, ErrorReturnCode import sh import os SURSA_VERIFICATA = 'sesiune-N3.cpp' cp('../' + SURSA_VERIFICATA, '.') os.system('g++ ' + SURSA_VERIFICATA + ' -O3') filename = 'grader_test' for i in range(1, 11): print 'Testul ', i cp(filename + str(i) + '.in', 'sesiune.in') os.system('time ./a.out') print diff('sesiune.out', filename + str(i) + '.ok', _ok_code=[0, 1]) for extension in ['in', 'out']: rm('sesiune.' + extension) rm(SURSA_VERIFICATA) rm('a.out')
def html_output(self): ext = '.html' today = datetime.date.today().isoformat() sha = self.test_file + ".html.sha" # cannot recover if generating html fails options = ( ['--zip'] + self.options + ['-f', 'html', self.test_file, self.test_out + ext + '.zip']) try: self.gdoc_to(*options, _err=self.test_err + ".html.log") # XXX it hangs without -n, didn't have time to figure out why out_dir = os.path.dirname(self.test_out) sh.unzip('-n', '-d', out_dir, self.test_out + ext + '.zip') sh.sed('-i', '-e', 's/%s/TODAYS_DATE/g' % today, self.test_out + ext) test_result = slurp('%s.html' % self.test_out) except ErrorReturnCode as e: self.say(red("gdoc-to failed: {}. See {}.html.log"), e, self.test_err) self.say(red("Ran in {}"), os.getcwd()) self.failed = True sh.rm('-f', sha) return try: html5check(self.test_out + ext, _out=self.test_out + ".html.errors") except ErrorReturnCode: self.say(red("Test output did not validate as XHTML5!")) self.say(red("\tSee {}.html.errors"), self.test_out) self.failed = True if test_result != slurp(self.test_file + ext): # the file changed, but the change might be okay spit(self._canonical_body(self.test_out + ext), self.test_out + ".body") spit(self._canonical_body(self.test_file + ext), self.test_out + ".canon.body") if (slurp(self.test_out + '.body') == slurp(self.test_out + '.canon.body')): self.say(yellow("File changed. Updating canonical file.")) sh.cp(self.test_out + ext, self.test_file + ext) else: self.say(red("HTML body changed!")) self.say(red("\tSee {}.*"), fail_path(self.test_name)) sh.cp(self.test_out + ext, fail_path(self.test_name + ext)) sh.diff('-u', self.test_file + ext, self.test_out + ext, _out=fail_path(self.test_name + ".html.diff"), _ok_code=[0, 1]) sh.cp(self.test_out + ".body", fail_path(self.test_name + ".body")) sh.cp(self.test_out + ".canon.body", fail_path(self.test_name + ".body.expected")) sh.diff('-u', self.test_out + ".canon.body", self.test_out + ".body", _out=fail_path(self.test_name + '.body.diff'), _ok_code=[0, 1]) self.failed = True
def print_file_diff(self, fh1, fh2): '''show the diff of two files when a test fails for easy debugging''' sh.diff(fh1, fh2)
from sh import cp, rm, diff, ErrorReturnCode import sh import os SURSA_VERIFICATA = 'conjectura_and.cpp' cp('../' + SURSA_VERIFICATA, '.') os.system('g++ ' + SURSA_VERIFICATA) filename = 'grader_test' for i in range(1, 11): print 'Testul ', i cp(filename + str(i) + '.in', 'conjectura.in') os.system('./a.out') print diff('conjectura.out', filename + str(i) + '.ok', _ok_code=[0, 1]) for extension in ['in', 'out']: rm('conjectura.' + extension) rm(SURSA_VERIFICATA) rm('a.out')
def diff(old, new): # diff will return 1 return sh.diff(old, new, _iter=True, _ok_code=[0, 1])
from sh import cp, rm, diff, ErrorReturnCode import sh import os SURSA_VERIFICATA = 'lipsa_n_patrat.cpp' cp('../' + SURSA_VERIFICATA, '.') os.system('g++ ' + SURSA_VERIFICATA) filename = 'grader_test' for i in range(1, 11): print 'Testul ', i cp(filename + str(i) + '.in', 'lipsa.in') os.system('./a.out') print diff('lipsa.out', filename + str(i) + '.ok', _ok_code=[0, 1]) for extension in ['in', 'out']: rm('lipsa.' + extension) rm(SURSA_VERIFICATA) rm('a.out')
) sh.Command(run)( binary, _in = open('./test/{program}/in/{test}'.format( program=program, test=test, )), _out = tempfile, ) out_correct = './test/{program}/out/{test}'.format( program=program, test=test, ) same = True try: ret = sh.diff(tempfile, out_correct, _ok_code=range(0,2)) if ret.exit_code == 1: print('Tvoj program dal zly vysledok v ulohe {} na priklade {}:\n{}'.format(program, test, ret)) same = False prog_bad[program].append(test) except sh.ErrorReturnCode: print('UGH: toto by sa nemalo stat.') exit(1) prog_stat[program][same] += 1 for program in programs: print('Program {}: {}/{}, chybne vysledky na vstupoch: {}'.format( program, prog_stat[program][True], prog_stat[program][True] + prog_stat[program][False], prog_bad[program], ))
from sh import cp, rm, diff, ErrorReturnCode import sh import os SURSA_VERIFICATA = 'teleport-palcu-back.cpp' cp('../' + SURSA_VERIFICATA, '.') os.system('g++ ' + SURSA_VERIFICATA) filename = 'grader_test' for i in range(1, 11): print 'Testul ', i cp(filename + str(i) + '.in', 'teleport.in') os.system('./a.out') print diff('teleport.out', filename + str(i) + '.ok', _ok_code=[0, 1]) for extension in ['in', 'out']: rm('teleport.' + extension) rm(SURSA_VERIFICATA) rm('a.out')
def managed(name, source=None, hash=None, hash_name=None, saltenv='base', engine='jinja', skip_verify=False, context=None, defaults=None, commit=True, **vars): ret = {'changes': {}, 'comment': '', 'name': name, 'result': True} sfn, _, __ = __salt__['file.get_managed'](name=name, template=engine, source=source, source_hash=hash, source_hash_name=hash_name, user=None, group=None, mode=None, attrs=None, saltenv=saltenv, context=context, defaults=defaults, skip_verify=skip_verify, **vars) su('admin', _in="discard") # Discard pending changes with open(sfn, 'rb') as commands: command_block = """ start transaction configure terminal {} configure terminal show candidate-configuration | save /tmp/gestion_candidate show running-configuration | save /tmp/gestion_running """.format(commands.read()) errors = StringIO() su('admin', _in=command_block, _out='/dev/null', _err=errors) errors = errors.getvalue() errors = errors.replace( "WARNING: Cluster manager is using default credentials", "").strip() if errors: ret['result'] = False ret['comment'] = errors return ret with open("/tmp/gestion_candidate_tail", 'wb') as f: tail("/tmp/gestion_candidate", n="+6", _out=f) with open("/tmp/gestion_running_tail", 'wb') as f: tail("/tmp/gestion_running", n="+6", _out=f) changes = diff("/tmp/gestion_running_tail", "/tmp/gestion_candidate_tail", "--label", "Current configuration", "--label", "New configuration", u=True, _ok_code=[0, 1]) if changes: ret['changes']['diff'] = str(changes) if __opts__['test']: if ret['result']: ret['result'] = None su('admin', _in="discard") elif not commit: su('admin', _in="discard") ret['comment'] = 'Changes have not been commited' else: su('admin', _in="commit") su('admin', _in="copy running-configuration startup-configuration") salt.utils.files.remove(sfn) return ret
def diff(self): sh.diff(self.out_path, self.np_out_path)
print "no script dir found" scripts = [] else: scripts = [f for f in listdir(script_dir) if f.endswith(".json")] for script in scripts: try: script_metadata = json.load(open(join(script_dir, script))) old_file = join(dname, script + ".old") new_file = join(dname, script + ".new") new_out = open(new_file,'w') command = sh.Command(join(script_dir,script_metadata["script"])) print("running " + join(script_dir,script_metadata["script"])) command(_out=new_out) if not isfile(old_file): sh.cp(new_file,old_file) diff = sh.diff(old_file,new_file,_ok_code=[0,1]) if len(diff) > 0: message = str(open(new_file).read()) payload = { "channel":script_metadata["channel"], "username":script_metadata["user"], "text": script_metadata["title"], "icon_emoji":script_metadata["emoji"], "attachments" : [ { "color":"good", "fields":[ { "title":"new value", "value":message, "short":False
from sh import cp, rm, diff, ErrorReturnCode import sh import os SURSA_VERIFICATA = 'conuri-robert.cpp' cp('../' + SURSA_VERIFICATA, '.') os.system('g++ ' + SURSA_VERIFICATA) filename = 'grader_test' for i in range(1, 11): print 'Testul ', i cp(filename + str(i) + '.in', 'conuri.in') os.system('./a.out') print diff('conuri.out', filename + str(i) + '.ok', _ok_code=[0, 1]) for extension in ['in', 'out']: rm('conuri.' + extension) rm(SURSA_VERIFICATA) rm('a.out')
print "source: use checkout in folder", olddir if not os.path.isdir(newdirPath): print "clone target repo" git.clone(gitRepo, newdir) else: print "target: use checkout in folder", newdir # checkout correct version print "checkout ", oldver git.checkout(oldver, _cwd=olddirPath) print "checkout ", newver git.checkout(newver, _cwd=newdirPath) # diff changedList = lsdiff( diff("-Nura", "--exclude=.git*", '.', olddirPath, _ok_code=[0, 1, 2, 3], _cwd=newdirPath) ) print "Changed Files:" print changedList # grunt build in new folder npm.install(_cwd=newdirPath) grunt.min(_cwd=newdirPath) changedModuleIds = [] for f in changedList: filePath = f.strip(' \t\n\r') fileParts = filePath.split('/') modulName = fileParts[1] destDirs = fileParts[2:]