Esempio n. 1
0
def main(args=None):
    """
    Used by the pywatch script to handle command-line
    args.
    """

    if not args:
        args = sys.argv[1:]

    usage = 'usage: %prog [options] "command" file1 file2 ...'
    parser = OptionParser(usage=usage)
    parser.add_option("-v", "--verbose",
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Output timestamp when commands are run.")
    parser.add_option("--version",
                      action="store_true",
                      dest="version",
                      default=False,
                      help="Output verion number and exit.")
    options, args = parser.parse_args(args)

    if options.version:
        print "pywatch %s" % VERSION
        sys.exit(0)

    if len(args) < 2:
        print parser.error("You must provide a shell command and at least one file.")

    cmds = [args[0], ]
    files = args[1:]
    w = Watcher(cmds=cmds, files=files, verbose=options.verbose)
    w.run_monitor()
    sys.exit(0)
Esempio n. 2
0
def main(args=None):
    """
    Used by the pywatch script to handle command-line
    args.
    """

    if not args:
        args = sys.argv[1:]

    usage = 'usage: %prog [options] "command" file1 file2 ...'
    parser = OptionParser(usage=usage)
    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Output timestamp when commands are run.")
    parser.add_option("--version",
                      action="store_true",
                      dest="version",
                      default=False,
                      help="Output verion number and exit.")
    parser.add_option("--no-clear",
                      action="store_true",
                      default=True,
                      help="Don't clear the terminal when files change.")
    options, args = parser.parse_args(args)

    if options.version:
        print "pywatch %s" % VERSION
        sys.exit(0)

    if len(args) < 2:
        print parser.error(
            "You must provide a shell command and at least one file.")

    cmds = [
        args[0],
    ]
    files = args[1:]

    w = Watcher(cmds=cmds,
                files=files,
                verbose=options.verbose,
                clear=options.no_clear)
    w.run_monitor()
    sys.exit(0)
Esempio n. 3
0
    def test_add_files(self):
        """When files are added, either at init or via add_files
        watcher.files length should reflect that. Non-files or duplicates
        shouldn't be reflected."""
        
        self.assertEqual(0, len(self.watcher.files))
        self.watcher.add_files(self.fixture("a.txt"), self.fixture("b.txt"))
        self.assertEqual(2, len(self.watcher.files))
       
        self.watcher.add_files(self.fixture("c.txt"))
        self.assertEqual(3, len(self.watcher.files))

        w = Watcher(files=[self.fixture("a.txt"), self.fixture("b.txt"), self.fixture("c.txt")])
        self.assertEqual(3, len(w.files))
Esempio n. 4
0
    def test_add_cmds(self):
        """When files are added, either at init or via add_cmds
        watcher.cmds length should reflect that. Non-files or duplicates
        shouldn't be reflected."""
       
        w = Watcher()
        self.assertEqual(0, len(w.cmds))
        w.add_cmds("python %s" % self.fixture('sample.py'), self.fixture('sample.py'))
        self.assertEqual(2, len(w.cmds))
       

        w = Watcher(cmds=["python %s" % self.fixture('sample.py')])
        self.assertEqual(1, len(w.cmds))
Esempio n. 5
0
class WatcherTest(unittest.TestCase):
    def touch(self, filename):
        os.utime(filename, None)

    def setUp(self):
        base_path = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))
        self.fixtures_path = os.path.join(base_path, 'fixtures')
        self.watcher = Watcher()

    def tearDown(self):
        self.watcher.stop_monitor()
        del self.watcher

    def fixture(self, *filenames):
        return os.path.join(self.fixtures_path, *filenames)

    def test_directories(self):
        self.watcher.add_files(self.fixture("subdir"))
        self.assert_(len(self.watcher.files) > 1, "Expected more than 1 file, %s watched" % len(self.watcher.files))

        self.touch(self.fixture("subdir", "bar.txt")) 
        self.watcher.monitor_once()
        self.assertEqual(1, self.watcher.num_runs)

        self.touch(self.fixture("subdir", "subsubdir", "baz.txt"))
        self.watcher.monitor_once()
        self.assertEqual(2, self.watcher.num_runs)

    def test_add_files(self):
        """When files are added, either at init or via add_files
        watcher.files length should reflect that. Non-files or duplicates
        shouldn't be reflected."""
        
        self.assertEqual(0, len(self.watcher.files))
        self.watcher.add_files(self.fixture("a.txt"), self.fixture("b.txt"))
        self.assertEqual(2, len(self.watcher.files))
       
        self.watcher.add_files(self.fixture("c.txt"))
        self.assertEqual(3, len(self.watcher.files))

        w = Watcher(files=[self.fixture("a.txt"), self.fixture("b.txt"), self.fixture("c.txt")])
        self.assertEqual(3, len(w.files))
   
    def test_add_cmds(self):
        """When files are added, either at init or via add_cmds
        watcher.cmds length should reflect that. Non-files or duplicates
        shouldn't be reflected."""
       
        w = Watcher()
        self.assertEqual(0, len(w.cmds))
        w.add_cmds("python %s" % self.fixture('sample.py'), self.fixture('sample.py'))
        self.assertEqual(2, len(w.cmds))
       

        w = Watcher(cmds=["python %s" % self.fixture('sample.py')])
        self.assertEqual(1, len(w.cmds))


    def test_file_monitoring(self):
        """Files that are touched should trigger an execution. watcher.num_runs
        should reflect the number of times this Watcher instance has executed
        its command list."""

        self.assertEqual(0, self.watcher.num_runs)

        self.watcher.add_files(self.fixture("a.txt"), self.fixture("b.txt"))
        self.assertEqual(0, self.watcher.num_runs)

        self.touch(self.fixture("a.txt")) 
        self.watcher.monitor_once()
        self.assertEqual(1, self.watcher.num_runs)

        self.touch(self.fixture("b.txt"))
        self.watcher.monitor_once()
        self.assertEqual(2, self.watcher.num_runs)

        self.watcher.monitor_once()
        self.assertEqual(2, self.watcher.num_runs)

    def test_continous_file_monitoring(self):
        """Watcher.monitor() should run continously executing the command
        list whenever files change."""

        self.watcher.add_files(self.fixture("a.txt"), self.fixture("b.txt"), self.fixture("c.txt"))
        self.watcher.monitor()

        self.touch(self.fixture("a.txt"))
        time.sleep(2)
        self.assertEqual(1, self.watcher.num_runs)
        
        self.watcher.stop_monitor()

    def test_clear_terminal_by_default(self):
        with patch('pywatch.watcher.os.system') as mocked_system:
            self.watcher.execute()
            mocked_system.assert_called_with('clear')

    def test_dont_clear_terminal_when_set(self):
        with patch('pywatch.watcher.os.system') as mocked_system:
            self.watcher.clear = False
            self.watcher.cmds = ['']
            self.watcher.execute()
            mocked_system.assert_called_once_with('')
Esempio n. 6
0
 def setUp(self):
     base_path = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))
     self.fixtures_path = os.path.join(base_path, 'fixtures')
     self.watcher = Watcher()