Commits

Russell Ballestrini  committed f7de624

filevault 0.1.0, first commit

  • Participants

Comments (0)

Files changed (5)

+# use glob syntax.
+syntax: glob
+
+*.pyc
+*.swp
+*.orig
+*.egg*
+*.gz
+build/*
+filevault 
+============
+
+filevault is a class for managing a tree of files on the filesystem.
+
+A Vault will:
+
+* Create a directory tree of custom depth
+* Spreads files out which keeps CLI snappy when traversing the tree
+* Scale to hundreds of thousands of files
+* Obfuscate directory paths and filenames 
+
+how to install
+===================
+
+Setuptools::
+
+ easy_install filevault
+
+Pip::
+
+ pip install filevault
+
+how to use
+=================
+
+How to create a default Vault object::
+  
+ from filevault import Vault
+
+ v = Vault()
+
+You may (and should) customize the Vault instance.  Here are the arguments:
+
+vaultpath
+ Where should the tree be created? Defaults to 'vault' in pwd.
+
+depth
+ How deep should the tree span? Defaults to 3 directories deep.
+
+salt
+ Add a custom salt for a unique and more secure tree, defaults to 'changeme'
+
+Another custom example::
+ 
+ from filevault import Vault
+
+ v = Vault( vaultpath="/tmp/for-test", depth=2, salt="sugar" )
+
+Now that we have a Vault object named v, we may review its two methods:
+
+create_filename( seed, ext='' )
+ Create a valid vault filename seeded with input. Optional extension.
+
+create_random_filename( ext='' )
+ Create a valid vault filename seeded with random input. Optional extension. 
+
+Here is a full example::
+
+ # import Vault class
+ from filevault import Vault
+
+ # create vault object named v, with custom path, depth, and salt
+ v = Vault( vaultpath="/tmp/for-test", depth=2, salt="sugar" )
+
+ # print a valid vault filename with extension
+ print v.create_filename( "my-first-file", ".png" )
+
+ # result:
+ # 3/9/3993817d4f9b3867c6db29b23c9d2ff9bb8a87d89426002adbb6ed34289d9e32.png
+
+ print v.create_filename( "my-first-file", ".png" )
+
+ # Same result:
+ # 3/9/3993817d4f9b3867c6db29b23c9d2ff9bb8a87d89426002adbb6ed34289d9e32.png
+
+ # print a random valid vault filename without extension
+ v.create_random_filename()
+
+ # result:
+ # 6/1/6169d6ee0ac0bc63ab667fb94d9cc747df0c03596ac43e24a51b3517d74bdc42 
+
+
+how can I thank you?
+========================
+
+Check out my `webpage to screenshot service <http://linkpeek.com>`_ and give me some feedback, tips, or advice.  Every little bit of help counts.
+
+

File filevault.py

+from os import path
+from os import makedirs
+from uuid import uuid4
+from hashlib import sha256
+from itertools import permutations
+
+HEX="0123456789abcdef"
+
+class Vault( object ):
+    def __init__( self, vaultpath='vault', depth=3, salt='changeme' ):
+        self.vaultpath = vaultpath
+        self.depth = depth
+        self.salt = salt
+        self.initVault()
+
+    def initVault( self ):
+        """Build the vault directories if they don't exist"""
+        if path.exists( self.vaultpath ):
+            return True
+
+        perms = permutations( HEX * self.depth, self.depth )
+        for perm in perms:
+            try: makedirs( path.join( self.vaultpath, *perm ) )
+            except OSError: pass
+
+    def _generate_filename( self, h, ext='' ):
+        """Accept a hash, return a valid file path"""
+        dirs = h[ 0 : self.depth ]
+        if ext and not ext.startswith( '.' ): ext = '.' + ext
+        return path.join( *dirs ) + '/' + h + ext
+
+    def create_filename( self, seed, ext='' ):
+        """Accept a seed, return a valid file path"""
+        h = sha256( seed + self.salt ).hexdigest()
+        return self._generate_filename( h, ext )
+
+    def create_random_filename( self, ext='' ):
+        """Return a random but valid file path"""
+        h = sha256( str( uuid4() ) ).hexdigest()
+        return self._generate_filename( h, ext )
+
+
+# installation: easy_install filevault
+
+from setuptools import setup
+
+setup( 
+    name = 'filevault',
+    version = '0.1.0',
+    description = 'filevault: manage a tree of directories and files',
+    keywords = 'file vault filevault tree directories obfuscate',
+    long_description = open('README.rst').read(),
+
+    author = 'Russell Ballestrini',
+    author_email = 'russell@ballestrini.net',
+    url = 'https://bitbucket.org/russellballestrini/filevault/src',
+
+    platforms = ['All'],
+    license = 'Public Domain',
+
+    py_modules = ['filevault'],
+    include_package_data = True,
+)
+
+# setup keyword args: http://peak.telecommunity.com/DevCenter/setuptools
+
+# built and uploaded to pypi with this:
+# python setup.py sdist bdist_egg register upload
+

File test_filevault.py

+from filevault import Vault
+
+# HASH from default salt
+A_HASH = "fa1fcedb71e2aa8054bce52050500a1339df532f20b27e927020b0d3249c4c29"
+A_HASH_PATH = "f/a/1/"
+
+# HASH from custom salt
+B_HASH = "629c5f83210b2635dd14e73b59950a3c41113599044254b4dc8648784d221041"
+SALT = "if there's a booger in your sugar pass the salt"   
+
+VAULT_PATH = "/tmp/test-vault-will-be-deleted/"
+
+def test_a_with_default_salt():
+    v = Vault( vaultpath = VAULT_PATH )
+    assert A_HASH in v.create_filename('a')
+
+def test_a_and_ext_with_default_salt():
+    v = Vault( vaultpath = VAULT_PATH )
+    a_filename = v.create_filename('a','png')
+    assert A_HASH in a_filename and '.png' in a_filename
+
+def test_default_depth():
+    v = Vault( vaultpath = VAULT_PATH )
+    a_filename = v.create_filename('a')
+    assert A_HASH in a_filename
+    assert len( a_filename.split('/') ) == 4
+
+def test_custom_depth_one():
+    v = Vault( vaultpath = VAULT_PATH, depth = 1 )
+    a_filename = v.create_filename('a')
+    assert A_HASH in a_filename
+    assert len( a_filename.split('/') ) == 2
+
+def test_custom_depth_four():
+    v = Vault( vaultpath = VAULT_PATH, depth = 4 )
+    a_filename = v.create_filename('a')
+    assert A_HASH in a_filename
+    assert len( a_filename.split('/') ) == 5
+
+def test_valid_depth_path():
+    v = Vault( vaultpath = VAULT_PATH ) 
+    a_filename = v.create_filename('a')
+    assert A_HASH_PATH in a_filename 
+
+def test_custom_salt():
+    v = Vault( vaultpath = VAULT_PATH, salt = SALT )
+    assert B_HASH in v.create_filename('b')
+
+def test_create_random_filename():
+    v = Vault( vaultpath = VAULT_PATH ) 
+    random_filename = v.create_random_filename()
+    assert len( random_filename.split('/') ) == 4
+    assert len( random_filename ) == 70
+
+def test_ext_equals_dot_ext():
+    v = Vault( vaultpath = VAULT_PATH )
+    assert v.create_filename('a','png') == v.create_filename('a','.png') 
+
+def test_tardown_filevault():
+    from shutil import rmtree
+    rmtree( VAULT_PATH )