summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--code/directory.py22
-rw-r--r--code/fsobject.py6
-rw-r--r--ranger5
-rw-r--r--test/dirsize_benchmark.py26
-rw-r--r--test/tc_directory.py29
5 files changed, 74 insertions, 14 deletions
diff --git a/code/directory.py b/code/directory.py
index da7c0948..2554ca72 100644
--- a/code/directory.py
+++ b/code/directory.py
@@ -12,6 +12,7 @@ class Directory(fsobject.FSObject):
 		self.files = None
 		self.filter = None
 		self.pointed_index = None
+		self.pointed_file = None
 	
 	def load_content(self):
 		self.stop_if_frozen()
@@ -22,8 +23,7 @@ class Directory(fsobject.FSObject):
 			basenames = os.listdir(self.path)
 			mapped = map(lambda name: os.path.join(self.path, name), basenames)
 			self.filenames = list(mapped)
-			self.infostring = ' %d' % len(self.filenames)
-			debug.log('infostring set!')
+			self.infostring = ' %d' % len(self.filenames) # update the infostring
 			self.files = []
 			for name in self.filenames:
 				if os.path.isdir(name):
@@ -35,7 +35,23 @@ class Directory(fsobject.FSObject):
 	
 	def load_content_once(self):
 		self.stop_if_frozen()
-		if not self.content_loaded: self.load_content()
+		if not self.content_loaded:
+			self.load_content()
+			return True
+		return False
+
+	def load_content_if_outdated(self):
+		self.stop_if_frozen()
+		if self.load_content_once(): return True
+
+		import os
+		real_mtime = os.stat(self.path).st_mtime
+		cached_mtime = self.stat.st_mtime
+
+		if real_mtime != cached_mtime:
+			self.load_content()
+			return True
+		return False
 
 	def __len__(self):
 		if not self.accessible: raise fsobject.NotLoadedYet()
diff --git a/code/fsobject.py b/code/fsobject.py
index 148976d2..6e93140f 100644
--- a/code/fsobject.py
+++ b/code/fsobject.py
@@ -36,7 +36,7 @@ class FSObject(object):
 
 			if os.path.isdir(self.path):
 				self.type = fstype.Directory
-				self.infostring = '--'
+				self.infostring = ' %d' % len(os.listdir(self.path))
 			elif os.path.isfile(self.path):
 				self.type = fstype.File
 				self.infostring = ' %d' % self.stat.st_size
@@ -60,17 +60,15 @@ class FSObject(object):
 
 	def load_if_outdated(self):
 		self.stop_if_frozen()
-		import os
-
 		if self.load_once(): return True
 
+		import os
 		real_mtime = os.stat(self.path).st_mtime
 		cached_mtime = self.stat.st_mtime
 
 		if real_mtime != cached_mtime:
 			self.load()
 			return True
-
 		return False
 
 	def clone(self):
diff --git a/ranger b/ranger
index 4973e42f..b6050ca8 100644
--- a/ranger
+++ b/ranger
@@ -12,10 +12,11 @@ from code import debug, fm, ui, options, environment
 # TODO: load config
 
 def main():
-	import locale
+	import locale, os
+	os.stat_float_times(True)
 	locale.setlocale(locale.LC_ALL, 'en_US.utf8')
 
-	path = '..'
+	path = '/srv/music/compilations/'
 	opt = options.get()
 	env = environment.Environment()
 
diff --git a/test/dirsize_benchmark.py b/test/dirsize_benchmark.py
new file mode 100644
index 00000000..38f0bfd7
--- /dev/null
+++ b/test/dirsize_benchmark.py
@@ -0,0 +1,26 @@
+import os, time
+class Dirsize():
+	def a(path):
+		return len(os.listdir(path))
+
+	def b(path):
+		for _, dirs, files in os.walk(path):
+			return len(files) + len(dirs)
+
+	def c(path):
+		first = next(os.walk(path))
+		return len(first[1]) + len(first[2])
+
+paths = {
+		'/usr/lib': None,
+		'/usr/bin': None,
+		'/home/hut': None
+}
+
+for key in paths.keys():
+	paths[key] = Dirsize.a(key) # assume Dirsize.a() returns a correct result
+	for algo in ['a', 'b', 'c']:
+		t = time.time()
+		for i in range(4):
+			assert Dirsize.__dict__[algo](key) == paths[key]
+		print("algorithm %s: %20s: %f" % (algo, key, time.time() - t))
diff --git a/test/tc_directory.py b/test/tc_directory.py
index f97f9913..ebbd6b5e 100644
--- a/test/tc_directory.py
+++ b/test/tc_directory.py
@@ -1,6 +1,7 @@
 import unittest
-import sys, os
+import sys, os, time
 sys.path.append('../code')
+os.stat_float_times(True)
 import directory, fsobject, file, debug
 
 TESTDIR = os.path.realpath(os.path.join(os.path.dirname(sys.argv[0]), 'testdir'))
@@ -78,15 +79,33 @@ class Test1(unittest.TestCase):
 		self.assertRaises(fsobject.FrozenException, clone.load_content)
 
 	def test_load_if_outdated(self):
-		if os.path.exists(TESTFILE): os.unlink(TESTFILE)
+		# modify the directory. If the time between the last modification
+		# was within the filesystems resolution of mtime, we should have a re-load.
+
+		def modify_dir():
+			open(TESTFILE, 'w').close()
+			os.unlink(TESTFILE)
+
+		def mtime():
+			return os.stat(TESTDIR).st_mtime
+
 		dir = directory.Directory(TESTDIR)
 		dir.load()
 
-		open(TESTFILE, 'w').close()
+		# If the modification happens to be in the same second as the
+		# last modification, it will result in mtime having the same
+		# integer value. So we wait until the resolution is exceeded
+		# and mtime differs.
+		old_mtime = mtime()
+		for i in range(50):
+			modify_dir()
+			if old_mtime != mtime(): break
+			time.sleep(0.1)
+		else:
+			# fail after 5 seconds of trying
+			self.fail("Cannot perform test: mtime of TESTDIR is not being updated.")
 
 		self.assertTrue(dir.load_if_outdated())
 
-		os.unlink(TESTFILE)
-
 unittest.main()
 
a> 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674