예제 #1
0
 def test_target_label(self):
     '''Test labels of sos_targets'''
     a = sos_targets('a')
     self.assertEqual(a.labels, [''])
     b = sos_targets(['a', 'b'])
     self.assertEqual(b.labels, ['', ''])
     c = sos_targets(['a1', 'b1'], _source='here')
     self.assertEqual(c.labels, ['here', 'here'])
     c.extend(b)
     self.assertEqual(c.labels, ['here', 'here', '', ''])
     #
     self.assertEqual(c.select('').labels, ['', ''])
     self.assertEqual(c.select('here').labels, ['here', 'here'])
     self.assertEqual(c['here'], [file_target('a1'), file_target('b1')])
     self.assertTrue(isinstance(c['here'], sos_targets))
     #
     # function item
     self.assertTrue(isinstance(c.select(1), sos_targets))
     self.assertEqual(c.select(1).labels, ['here'])
     self.assertEqual(c.select(1), ['b1'])
     #
     # test slice of groups
     res = sos_targets(a=['a.txt', 'b.txt'],
                       b=['c.txt', 'd.txt'],
                       group_by=1)
     self.assertEqual(len(res.groups), 4)
     self.assertEqual(res.labels, ['a', 'a', 'b', 'b'])
     res_a = res['a']
     self.assertEqual(len(res_a), 2)
     self.assertEqual(res_a.labels, ['a', 'a'])
     self.assertEqual(len(res_a.groups), 4)
     self.assertEqual(len(res_a.groups[0]), 1)
     self.assertEqual(len(res_a.groups[1]), 1)
     self.assertEqual(len(res_a.groups[2]), 0)
     self.assertEqual(len(res_a.groups[3]), 0)
예제 #2
0
 def test_target_label(self):
     """Test labels of sos_targets"""
     a = sos_targets("a")
     self.assertEqual(a.labels, [""])
     b = sos_targets(["a", "b"])
     self.assertEqual(b.labels, ["", ""])
     c = sos_targets(["a1", "b1"], _source="here")
     self.assertEqual(c.labels, ["here", "here"])
     c.extend(b)
     self.assertEqual(c.labels, ["here", "here", "", ""])
     #
     self.assertEqual(c.select("").labels, ["", ""])
     self.assertEqual(c.select("here").labels, ["here", "here"])
     self.assertEqual(c["here"], [file_target("a1"), file_target("b1")])
     self.assertTrue(isinstance(c["here"], sos_targets))
     #
     # function item
     self.assertTrue(isinstance(c.select(1), sos_targets))
     self.assertEqual(c.select(1).labels, ["here"])
     self.assertEqual(c.select(1), ["b1"])
     #
     # test slice of groups
     res = sos_targets(a=["a.txt", "b.txt"],
                       b=["c.txt", "d.txt"],
                       group_by=1)
     self.assertEqual(len(res.groups), 4)
     self.assertEqual(res.labels, ["a", "a", "b", "b"])
     res_a = res["a"]
     self.assertEqual(len(res_a), 2)
     self.assertEqual(res_a.labels, ["a", "a"])
     self.assertEqual(len(res_a.groups), 4)
     self.assertEqual(len(res_a.groups[0]), 1)
     self.assertEqual(len(res_a.groups[1]), 1)
     self.assertEqual(len(res_a.groups[2]), 0)
     self.assertEqual(len(res_a.groups[3]), 0)
예제 #3
0
    def testInput(self):
        '''Test input directive'''
        self.touch(['a.txt', 'b.txt', 'a.pdf', 'a0', 'a1'])
        script = SoS_Script('''
[0]
files = ['a.txt', 'b.txt']

input: 'a.pdf', files

''')
        wf = script.workflow()
        Base_Executor(wf).run(mode='dryrun')
        #
        # test input types
        script = SoS_Script('''
[0:shared={'i':'_input', 'o':'_output'}]
files = (f"a{i}" for i in range(2))
input: {'a.txt', 'b.txt'}, files
output: (f"a{x}" for x in _input)

''')
        wf = script.workflow()
        Base_Executor(wf).run(mode='dryrun')
        self.assertEqual(sorted(env.sos_dict['i']),
                         sos_targets(['a.txt', 'a0', 'a1', 'b.txt']))
        self.assertEqual(sorted(env.sos_dict['o']),
                         sos_targets(['aa.txt', 'aa0', 'aa1', 'ab.txt']))
예제 #4
0
 def testTargetGroupBy(self):
     '''Test new option group_by to sos_targets'''
     res = sos_targets('e.txt', 'f.ext', a=['a.txt', 'b.txt'], b=['c.txt', 'd.txt'], group_by=1)
     self.assertEqual(len(res.groups), 6)
     self.assertEqual(res.labels, ['', '', 'a', 'a', 'b', 'b'])
     #
     res = sos_targets(res, group_by=2)
     self.assertEqual(len(res.groups), 3)
예제 #5
0
 def test_target_group_by(self):
     """Test new option group_by to sos_targets"""
     res = sos_targets("e.txt",
                       "f.ext",
                       a=["a.txt", "b.txt"],
                       b=["c.txt", "d.txt"],
                       group_by=1)
     self.assertEqual(len(res.groups), 6)
     self.assertEqual(res.labels, ["", "", "a", "a", "b", "b"])
     #
     res = sos_targets(res, group_by=2)
     self.assertEqual(len(res.groups), 3)
예제 #6
0
 def testTargetPairedWith(self):
     '''Test paired_with targets with vars'''
     res = sos_targets('e.txt', 'f.ext', a=['a.txt', 'b.txt'], b=['c.txt', 'd.txt'], group_by=1).paired_with('_name', ['e', 'f', 'a', 'b', 'c', 'd'])
     for i,n in enumerate(['e', 'f', 'a', 'b', 'c', 'd']):
         self.assertEqual(res[i]._name, n)
     #
     res = copy.deepcopy(res)
     for i,n in enumerate(['e', 'f', 'a', 'b', 'c', 'd']):
         self.assertEqual(res[i]._name, n)
     #
     # test assert for length difference
     self.assertRaises(Exception, sos_targets('e.txt', 'f.ext').paired_with,
             'name', ['e', 'f', 'a', 'b', 'c', 'd'])
예제 #7
0
 def testTargetGroupWith(self):
     '''Test group_with targets with vars'''
     res = sos_targets('e.txt', 'f.ext', a=['a.txt', 'b.txt'], b=['c.txt', 'd.txt'], group_by=2).group_with('name', ['a1', 'a2', 'a3'])
     for i,n in enumerate(['a1', 'a2', 'a3']):
         self.assertEqual(res.groups[i].name, n)
     #
     res = copy.deepcopy(res)
     for i,n in enumerate(['a1', 'a2', 'a3']):
         self.assertEqual(res.groups[i].name, n)
     #
     # test assert for length difference
     self.assertRaises(Exception, sos_targets('e.txt', 'f.ext', group_by=1).group_with,
             'name', ['e', 'f', 'g'])
예제 #8
0
    def testAnalyzeOutputFrom(self):
        '''Test extracting of from=value option from input'''
        script = SoS_Script('''
[A_1]
input:  output_from('B')

[A_2]
input: something_unknown, sos_groups(output_from(['C1', 'C2']), by=2), group_by=1
''')
        wf = script.workflow('A')
        for section in wf.sections:
            res = analyze_section(section)
            if section.names[0][1] == 1:
                self.assertEqual(res['step_depends'], sos_targets(sos_step('B')))
            if section.names[0][1] == 2:
                self.assertTrue(res['step_depends'] == sos_targets(sos_step('C1'), sos_step('C2')))
예제 #9
0
 def test_expand_wildcard(self):
     """test wildcard expansion of sos_targets"""
     a = sos_targets("*.py")
     self.assertGreater(len(a), 1)
     #
     a = paths("*.py")
     self.assertGreater(len(a), 1)
예제 #10
0
 def test_expand_wildcard(self):
     '''test wildcard expansion of sos_targets'''
     a = sos_targets('*.py')
     self.assertGreater(len(a), 1)
     #
     a = paths('*.py')
     self.assertGreater(len(a), 1)
예제 #11
0
 def test_target_format(self):
     """Test string interpolation of targets"""
     for target, fmt, res in [
         ("a.txt", "", "a.txt"),
         (sos_targets("a.txt"), "", "a.txt"),
         (sos_targets(["a.txt"]), "", "a.txt"),
         (sos_targets([r"c:\path\a.txt"]), "p", "/c/path/a.txt"),
         (sos_targets([r"c:path\a.txt"]), "p", "/c/path/a.txt"),
         (sos_targets("/a/b/a.txt"), "b", "a.txt"),
         (sos_targets("a b.txt"), "q", ("'a b.txt'", '"a b.txt"')),
         (sos_targets("a b.txt"), "x", ".txt"),
     ]:
         if isinstance(res, str):
             self.assertEqual(
                 interpolate("{{target:{}}}".format(fmt), globals(),
                             locals()),
                 res,
                 "Interpolation of {}:{} should be {}".format(
                     target, fmt, res),
             )
         else:
             self.assertTrue(
                 interpolate("{{target:{}}}".format(fmt), globals(),
                             locals()) in res,
                 "Interpolation of {}:{} should be one of {}".format(
                     target, fmt, res),
             )
예제 #12
0
파일: test_target.py 프로젝트: pythseq/SoS
 def testTargetFormat(self):
     '''Test string interpolation of targets'''
     for target, fmt, res in [
             ('a.txt', '', 'a.txt'),
             (sos_targets('a.txt'), '', 'a.txt'),
             (sos_targets(['a.txt']), '', 'a.txt'),
             (sos_targets('/a/b/a.txt'), 'b', 'a.txt'),
             (sos_targets('a b.txt'), 'q', ("'a b.txt'", '"a b.txt"')),
             (sos_targets('a b.txt'), 'x', ".txt"),
             ]:
         if isinstance(res, str):
             self.assertEqual(
                 interpolate('{{target:{}}}'.format(fmt), globals(), locals()), res,
                     "Interpolation of {}:{} should be {}".format(target, fmt, res))
         else:
             self.assertTrue(
                 interpolate('{{target:{}}}'.format(fmt), globals(), locals()) in res,
                     "Interpolation of {}:{} should be one of {}".format(target, fmt, res))
예제 #13
0
파일: test_utils.py 프로젝트: pgcudahy/sos
def test_analyze_output_from():
    '''Test extracting of from=value option from input'''
    script = SoS_Script('''
[A_1]
input:  output_from('B')

[A_2]
input: something_unknown, sos_groups(output_from(['C1', 'C2']), by=2), group_by=1
''')
    wf = script.workflow('A')
    Base_Executor(wf)
    for section in wf.sections:
        res = analyze_section(section, analysis_type='forward')
        if section.names[0][1] == 1:
            assert res['step_depends'] == sos_targets(sos_step('B'))
        if section.names[0][1] == 2:
            assert res['step_depends'] == sos_targets(sos_step('C1'),
                                                      sos_step('C2'))
예제 #14
0
    def _testSignature(self, text, steps):
        '''Test recognizing the format of SoS script'''
        script = SoS_Script(text)
        for f in ['temp/a.txt', 'temp/b.txt']:
            if file_target(f).exists():
                file_target(f).unlink()
        #
        # only the first step
        wf = script.workflow(':0')
        env.config['sig_mode'] = 'force'
        res = Base_Executor(wf, config={'default_queue': 'localhost'}).run()
        self.assertTrue(os.path.isfile('temp/a.txt'))
        self.assertTrue(os.path.isfile('temp/b.txt'))
        self.assertTrue(res['__completed__']['__step_completed__'], steps)
        with open('temp/a.txt') as ta:
            self.assertTrue(ta.read(), 'a.txt')
        with open('temp/b.txt') as tb:
            self.assertTrue(tb.read(), 'b.txt')
        env.config['sig_mode'] = 'assert'
        res = Base_Executor(wf, config={'default_queue': 'localhost'}).run()
        self.assertEqual(res['__completed__']['__step_completed__'], 0)
        # all of them
        wf = script.workflow()
        env.config['sig_mode'] = 'default'
        # generate files (default step 0 and 1)
        Base_Executor(wf, config={'default_queue': 'localhost'}).run()
        # now, rerun in build mode
        env.config['sig_mode'] = 'build'
        res = Base_Executor(wf, config={'default_queue': 'localhost'}).run()
        self.assertEqual(res['__completed__']['__step_completed__'], 0)
        #
        self.assertTrue(os.path.isfile('temp/c.txt'))
        self.assertTrue(os.path.isfile('temp/d.txt'))
        with open('temp/c.txt') as tc:
            self.assertTrue(tc.read(), 'a.txt')
        with open('temp/d.txt') as td:
            self.assertTrue(td.read(), 'b.txt')
        self.assertEqual(env.sos_dict['oa'],
                         sos_targets('temp/c.txt', 'temp/d.txt'))
        #
        # now in assert mode, the signature should be there
        env.config['sig_mode'] = 'assert'
        res = Base_Executor(wf, config={'default_queue': 'localhost'}).run()
        self.assertEqual(res['__completed__']['__step_completed__'], 0)

        #
        env.config['sig_mode'] = 'default'
        res = Base_Executor(wf, config={'default_queue': 'localhost'}).run()
        self.assertEqual(res['__completed__']['__step_completed__'], 0)

        #
        # change script a little bit
        script = SoS_Script('# comment\n' + text)
        wf = script.workflow()
        env.config['sig_mode'] = 'assert'
        res = Base_Executor(wf, config={'default_queue': 'localhost'}).run()
        self.assertEqual(res['__completed__']['__step_completed__'], 0)
예제 #15
0
    def testAnalyzeFromOption(self):
        '''Test extracting of from=value option from input'''
        script = SoS_Script('''
[A_1]
input:  from_steps='B'

[A_2]
input: something_unknown, from_steps=['B', 'C2'], group_by=1
''')
        wf = script.workflow('A')
        for section in wf.sections:
            res = analyze_section(section)
            if section.names[0][1] == '1':
                self.assertEqual(res['step_depends'],
                                 sos_targets(sos_step('B')))
            if section.names[0][1] == '2':
                self.assertTrue(res['step_depends'] == sos_targets(
                    sos_step('B'), sos_step('C2')))
예제 #16
0
 def test_remove_targets(self):
     '''Test sos_target.remove_targets()'''
     a = sos_targets(sos_step('1'), 'a.txt')._group(by=1)
     a.remove_targets(type=sos_step)
     self.assertEqual(len(a), 1)
     self.assertEqual(len(a.groups), 2)
     self.assertEqual(len(a._groups[0]._indexes), 0)
     self.assertEqual(len(a._groups[0]._labels), 0)
     self.assertEqual(a._groups[1]._indexes, [0])
     self.assertEqual(len(a._groups[1]._labels), 1)
예제 #17
0
 def test_remove_targets(self):
     """Test sos_target.remove_targets()"""
     a = sos_targets(sos_step("1"), "a.txt")._group(by=1)
     a.remove_targets(type=sos_step)
     self.assertEqual(len(a), 1)
     self.assertEqual(len(a.groups), 2)
     self.assertEqual(len(a._groups[0]._indexes), 0)
     self.assertEqual(len(a._groups[0]._labels), 0)
     self.assertEqual(a._groups[1]._indexes, [0])
     self.assertEqual(len(a._groups[1]._labels), 1)
예제 #18
0
    def testOutputGroupBy(self):
        '''Test group_by parameter of step output'''
        # group_by = 'all'
        self.touch(['a{}.txt'.format(x) for x in range(4)])
        #
        script = SoS_Script('''
[0: shared='executed']

executed = []
input: ['a{}.txt'.format(x) for x in range(4)], group_by=2
output: ['a{}.txt.bak'.format(x) for x in range(4)], group_by=2

executed.append(_output)

''')
        wf = script.workflow()
        Base_Executor(wf).run(mode='dryrun')
        self.assertEqual(env.sos_dict['executed'], [sos_targets(
            'a0.txt.bak', 'a1.txt.bak'), sos_targets('a2.txt.bak', 'a3.txt.bak')])
예제 #19
0
 def testTargetSource(self):
     '''Test source of sos_targets'''
     a = sos_targets('a')
     self.assertEqual(a.sources, [''])
     b = sos_targets(['a', 'b'])
     self.assertEqual(b.sources, ['', ''])
     c = sos_targets(['a1', 'b1'], source='here')
     self.assertEqual(c.sources, ['here', 'here'])
     c.extend(b)
     self.assertEqual(c.sources, ['here', 'here', '', ''])
     #
     self.assertEqual(c.slice('').sources, ['', ''])
     self.assertEqual(c.slice('here').sources, ['here', 'here'])
     self.assertEqual(c['here'], [file_target('a1'), file_target('b1')])
     self.assertTrue(isinstance(c['here'], sos_targets))
     #
     # function item
     self.assertTrue(isinstance(c.slice(1), sos_targets))
     self.assertEqual(c.slice(1).sources, ['here'])
     self.assertEqual(c.slice(1), ['b1'])
예제 #20
0
 def test_target_group_with(self):
     """Test group_with targets with vars"""
     res = sos_targets("e.txt",
                       "f.ext",
                       a=["a.txt", "b.txt"],
                       b=["c.txt", "d.txt"],
                       group_by=2).group_with("name", ["a1", "a2", "a3"])
     for i, n in enumerate(["a1", "a2", "a3"]):
         self.assertEqual(res.groups[i].name, n)
     #
     res = copy.deepcopy(res)
     for i, n in enumerate(["a1", "a2", "a3"]):
         self.assertEqual(res.groups[i].name, n)
     #
     # test assert for length difference
     self.assertRaises(
         Exception,
         sos_targets("e.txt", "f.ext", group_by=1).group_with,
         "name",
         ["e", "f", "g"],
     )
예제 #21
0
 def test_target_paired_with(self):
     """Test paired_with targets with vars"""
     res = sos_targets("e.txt",
                       "f.ext",
                       a=["a.txt", "b.txt"],
                       b=["c.txt", "d.txt"],
                       group_by=1).paired_with(
                           "_name", ["e", "f", "a", "b", "c", "d"])
     for i, n in enumerate(["e", "f", "a", "b", "c", "d"]):
         self.assertEqual(res[i]._name, n)
     #
     res = copy.deepcopy(res)
     for i, n in enumerate(["e", "f", "a", "b", "c", "d"]):
         self.assertEqual(res[i]._name, n)
     #
     # test assert for length difference
     self.assertRaises(
         Exception,
         sos_targets("e.txt", "f.ext").paired_with,
         "name",
         ["e", "f", "a", "b", "c", "d"],
     )
예제 #22
0
    def _testSignature(self, text):
        '''Test recognizing the format of SoS script'''
        env.config['wait_for_task'] = True
        script = SoS_Script(text)
        for f in ['temp/a.txt', 'temp/b.txt']:
            file_target(f).remove('both')
        #
        # only the first step
        wf = script.workflow('default:0')
        env.config['sig_mode'] = 'force'
        Base_Executor(wf).run()
        self.assertTrue(os.path.isfile('temp/a.txt'))
        self.assertTrue(os.path.isfile('temp/b.txt'))
        with open('temp/a.txt') as ta:
            self.assertTrue(ta.read(), 'a.txt')
        with open('temp/b.txt') as tb:
            self.assertTrue(tb.read(), 'b.txt')
        env.config['sig_mode'] = 'assert'
        Base_Executor(wf).run()
        # all of them
        wf = script.workflow()
        env.config['sig_mode'] = 'default'
        # generate files (default step 0 and 1)
        Base_Executor(wf).run()
        # now, rerun in build mode
        env.config['sig_mode'] = 'build'
        Base_Executor(wf).run()
        #
        self.assertTrue(os.path.isfile('temp/c.txt'))
        self.assertTrue(os.path.isfile('temp/d.txt'))
        with open('temp/c.txt') as tc:
            self.assertTrue(tc.read(), 'a.txt')
        with open('temp/d.txt') as td:
            self.assertTrue(td.read(), 'b.txt')
        self.assertEqual(env.sos_dict['oa'],
                         sos_targets('temp/c.txt', 'temp/d.txt'))
        #
        # now in assert mode, the signature should be there
        env.config['sig_mode'] = 'assert'
        Base_Executor(wf).run()

        #
        env.config['sig_mode'] = 'default'
        Base_Executor(wf).run()

        #
        # change script a little bit
        script = SoS_Script('# comment\n' + text)
        wf = script.workflow()
        env.config['sig_mode'] = 'assert'
        Base_Executor(wf).run()
예제 #23
0
 def test_sos_targets_signature(self):
     """Test save and validate signatures of sos_targets"""
     with open("a.txt", "w") as a:
         a.write("text1")
     with open("b.txt", "w") as b:
         b.write("text2")
     t = sos_targets("a.txt", "b.txt")
     sig = t.target_signature()
     self.assertTrue(t.validate(sig))
     # variables does not affect signature
     t[0].set("a", 2)
     self.assertEqual(sig, t.target_signature())
     #
     t.set("cc", "another string")
     self.assertTrue(t.validate(sig))
예제 #24
0
 def test_zap(self):
     """Test zap"""
     with open("testzap.txt", "w") as sf:
         sf.write("some text")
     path("testzap.txt").zap()
     self.assertTrue(os.path.isfile("testzap.txt.zapped"))
     self.assertFalse(os.path.isfile("testzap.txt"))
     # re-zap is ok
     file_target("testzap.txt").zap()
     self.assertTrue(os.path.isfile("testzap.txt.zapped"))
     self.assertFalse(os.path.isfile("testzap.txt"))
     # non-existent file
     os.remove("testzap.txt.zapped")
     self.assertRaises(FileNotFoundError, path("testzap.txt").zap)
     #
     with open("testzap.txt", "w") as sf:
         sf.write("some text")
     with open("testzap1.txt", "w") as sf:
         sf.write("some text")
     paths("testzap.txt", "testzap1.txt").zap()
     self.assertTrue(os.path.isfile("testzap.txt.zapped"))
     self.assertFalse(os.path.isfile("testzap.txt"))
     self.assertTrue(os.path.isfile("testzap1.txt.zapped"))
     self.assertFalse(os.path.isfile("testzap1.txt"))
     #
     os.remove("testzap.txt.zapped")
     os.remove("testzap1.txt.zapped")
     with open("testzap.txt", "w") as sf:
         sf.write("some text")
     with open("testzap1.txt", "w") as sf:
         sf.write("some text")
     sos_targets(["testzap.txt", "testzap1.txt"]).zap()
     self.assertTrue(os.path.isfile("testzap.txt.zapped"))
     self.assertFalse(os.path.isfile("testzap.txt"))
     self.assertTrue(os.path.isfile("testzap1.txt.zapped"))
     self.assertFalse(os.path.isfile("testzap1.txt"))
예제 #25
0
 def test_sos_targets_signature(self):
     '''Test save and validate signatures of sos_targets'''
     with open('a.txt', 'w') as a:
         a.write('text1')
     with open('b.txt', 'w') as b:
         b.write('text2')
     t = sos_targets('a.txt', 'b.txt')
     sig = t.target_signature()
     self.assertTrue(t.validate(sig))
     # variables does not affect signature
     t[0].set('a', 2)
     self.assertEqual(sig, t.target_signature())
     #
     t.set('cc', 'another string')
     self.assertTrue(t.validate(sig))
예제 #26
0
 def test_zap(self):
     '''Test zap'''
     with open('testzap.txt', 'w') as sf:
         sf.write('some text')
     path('testzap.txt').zap()
     self.assertTrue(os.path.isfile('testzap.txt.zapped'))
     self.assertFalse(os.path.isfile('testzap.txt'))
     # re-zap is ok
     file_target('testzap.txt').zap()
     self.assertTrue(os.path.isfile('testzap.txt.zapped'))
     self.assertFalse(os.path.isfile('testzap.txt'))
     # non-existent file
     os.remove('testzap.txt.zapped')
     self.assertRaises(FileNotFoundError, path('testzap.txt').zap)
     #
     with open('testzap.txt', 'w') as sf:
         sf.write('some text')
     with open('testzap1.txt', 'w') as sf:
         sf.write('some text')
     paths('testzap.txt', 'testzap1.txt').zap()
     self.assertTrue(os.path.isfile('testzap.txt.zapped'))
     self.assertFalse(os.path.isfile('testzap.txt'))
     self.assertTrue(os.path.isfile('testzap1.txt.zapped'))
     self.assertFalse(os.path.isfile('testzap1.txt'))
     #
     os.remove('testzap.txt.zapped')
     os.remove('testzap1.txt.zapped')
     with open('testzap.txt', 'w') as sf:
         sf.write('some text')
     with open('testzap1.txt', 'w') as sf:
         sf.write('some text')
     sos_targets(['testzap.txt', 'testzap1.txt']).zap()
     self.assertTrue(os.path.isfile('testzap.txt.zapped'))
     self.assertFalse(os.path.isfile('testzap.txt'))
     self.assertTrue(os.path.isfile('testzap1.txt.zapped'))
     self.assertFalse(os.path.isfile('testzap1.txt'))
예제 #27
0
 def init_input_output_vars(self):
     # we keep these variables (which can be result of stepping through previous statements)
     # if no input and/or output statement is defined
     for key in ('step_input', '_depends', 'step_output', 'step_depends',
                 '_depends'):
         if key not in env.sos_dict:
             env.sos_dict.set(key, sos_targets([]))
     if '_output' not in env.sos_dict:
         env.sos_dict.set('_output', sos_targets(_undetermined=True))
     if any(x[0] == ':' and x[1] == 'input' for x in self.step.statements):
         env.sos_dict.set('step_input', sos_targets([]))
         env.sos_dict.set('_input', sos_targets([]))
     if any(x[0] == ':' and x[1] == 'output' for x in self.step.statements):
         env.sos_dict.set('step_output', sos_targets([]))
         env.sos_dict.set('_output', sos_targets([]))
     env.sos_dict.pop('__default_output__', None)
예제 #28
0
def Rmarkdown(script=None,
              input=None,
              output=None,
              args='{input:r}, output_file={output:ar}',
              **kwargs):
    '''Convert input file to output using Rmarkdown

    The input can be specified in three ways:

    1. instant script, which is assumed to be in md format

    Rmarkdown:   output='report.html'
      script

    2. one or more input files. The format is determined by extension of input file

    Rmarkdown(input, output='report.html')

    3. input file specified by command line option `-r` .
    Rmarkdown(output='report.html')

    If no output is specified, it is assumed to be in html format
    and is written to standard output.

    You can specify more options using the args parameter of the action. The default value
    of args is `${input!r} --output ${output!ar}'
    '''
    if not R_library('rmarkdown').target_exists():
        raise RuntimeError('Library rmarkdown does not exist')

    input = sos_targets(collect_input(script, input))

    output = sos_targets(output)
    if len(output) == 0:
        write_to_stdout = True
        output = sos_targets(
            tempfile.NamedTemporaryFile(mode='w+t',
                                        suffix='.html',
                                        delete=False).name)
    else:
        write_to_stdout = False
    #
    ret = 1
    try:
        #   render(input, output_format = NULL, output_file = NULL, output_dir = NULL,
        #        output_options = NULL, intermediates_dir = NULL,
        #        runtime = c("auto", "static", "shiny"),
        #        clean = TRUE, params = NULL, knit_meta = NULL, envir = parent.frame(),
        #        run_Rmarkdown = TRUE, quiet = FALSE, encoding = getOption("encoding"))
        cmd = interpolate(f'Rscript -e "rmarkdown::render({args})"', {
            'input': input,
            'output': output
        })
        env.logger.trace(f'Running command "{cmd}"')
        if env.config['run_mode'] == 'interactive':
            # need to catch output and send to python output, which will in trun be hijacked by SoS notebook
            p = subprocess.Popen(cmd,
                                 shell=True,
                                 stderr=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            #pid = p.pid
            out, err = p.communicate()
            sys.stdout.write(out.decode())
            sys.stderr.write(err.decode())
            ret = p.returncode
        else:
            p = subprocess.Popen(cmd, shell=True)
            #pid = p.pid
            ret = p.wait()
    except Exception as e:
        env.logger.error(e)
    if ret != 0:
        temp_file = os.path.join('.sos', f'{"Rmarkdown"}_{os.getpid()}.md')
        shutil.copyfile(str(input), temp_file)
        cmd = interpolate(f'Rscript -e "rmarkdown::render({args})"', {
            'input': input,
            'output': sos_targets(temp_file)
        })
        raise RuntimeError(
            f'Failed to execute script. Please use command \n"{cmd}"\nunder {os.getcwd()} to test it.'
        )
    if write_to_stdout:
        with open(str(output[0])) as out:
            sys.stdout.write(out.read())
    else:
        env.logger.info(f'Report saved to {output}')
예제 #29
0
 def test_merging_of_sos_targets(self):
     '''Test merging of multiple sos targets'''
     # merge 0 to 0
     res = sos_targets('a.txt', 'b.txt',
                       sos_targets('c.txt', 'd.txt', group_by=1))
     self.assertEqual(len(res), 4)
     self.assertEqual(len(res.groups), 2)
     self.assertEqual(res.groups[0], ['a.txt', 'b.txt', 'c.txt'])
     self.assertEqual(res.groups[1], ['a.txt', 'b.txt', 'd.txt'])
     # merge N to N
     N1 = sos_targets('c.txt', 'd.txt', group_by=1)
     N2 = sos_targets('a1.txt', 'a2.txt', 'a3.txt', 'a4.txt', group_by=2)
     res = sos_targets(N1, N2)
     self.assertEqual(len(res), 6)
     self.assertEqual(len(res.groups), 2)
     self.assertEqual(res.groups[0], ['c.txt', 'a1.txt', 'a2.txt'])
     self.assertEqual(res.groups[1], ['d.txt', 'a3.txt', 'a4.txt'])
     # test N to M
     N1 = sos_targets('c.txt', 'd.txt', group_by=1)
     N2 = sos_targets('a1.txt', 'a2.txt', 'a3.txt', 'a4.txt', group_by=1)
     self.assertRaises(Exception, sos_targets, N1, N2)
     # merge 1 to N
     N1 = sos_targets('c.txt', 'd.txt', group_by='all')
     N2 = sos_targets('a1.txt', 'a2.txt', 'a3.txt', 'a4.txt', group_by=2)
     res = sos_targets(N1, N2)
     self.assertEqual(len(res), 6)
     self.assertEqual(len(res.groups), 2)
     self.assertEqual(res.groups[0], ['c.txt', 'd.txt', 'a1.txt', 'a2.txt'])
     self.assertEqual(res.groups[1], ['c.txt', 'd.txt', 'a3.txt', 'a4.txt'])
     # merge N to 1
     res = sos_targets(N2, N1)
     self.assertEqual(len(res), 6)
     self.assertEqual(len(res.groups), 2)
     self.assertEqual(res.groups[0], ['a1.txt', 'a2.txt', 'c.txt', 'd.txt'])
     self.assertEqual(res.groups[1], ['a3.txt', 'a4.txt', 'c.txt', 'd.txt'])
예제 #30
0
 def test_iter_targets(self):
     '''Test iterator interface of targets'''
     t = sos_targets('1', '2', ['3', '4'])
     self.assertEqual(len(t), 4)
     for idx, i in enumerate(t):
         self.assertEqual(str(i), str(idx + 1))