def is_changed(table, zip_file, r_name): # First just try checking the modification date of the file. hash_value = compute_hash(zip_file) last_hash_entry = retrieve_hash_by_name(table, r_name) if last_hash_entry is not None: print("last hash = {}".format(last_hash_entry['value'])) else: notify_admins("A new election has been detected: {}".format(r_name)) print("new hash = {}".format(hash_value)) zf = PyZipFile(zip_file) last_mod = None last_mod = datetime(*zf.getinfo("summary.csv").date_time) if last_hash_entry is None: # No previous hash found return True, last_hash_entry, last_mod # Check database of hashes. if hash_value == last_hash_entry['value']: return False, last_hash_entry, last_mod try: last_mod = datetime(*zf.getinfo("summary.csv").date_time) prev_mod = last_hash_entry['last_modified'] if prev_mod is not None and prev_mod != '': previous_modification_date = datetime.strptime( prev_mod, "%Y-%m-%d %H:%M") if last_mod <= previous_modification_date: return False, last_hash_entry, last_mod except: print( "Unable to compare the last hash entry's file modification date with the current file's last modification date." ) return True, last_hash_entry, last_mod
class ArchiveTestCase(TestCase): def setUp(self): tmp = tempfile.mkstemp(prefix=__name__) self.zipfile_path = tmp[1] self.zipfile = PyZipFile(self.zipfile_path, 'w') self.pj_root = os.path.abspath( os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) def tearDown(self): os.remove(self.zipfile_path) def test_create_zipfile(self): archive = Archive('test.zip') ok_(hasattr(archive.create_zipfile(), 'read')) with PyZipFile(archive._zippath, 'r', compression=ZIP_DEFLATED) as zipfile: ok_('lambda_function.pyc' in zipfile.namelist()) ok_('.lamvery_secret.json' in zipfile.namelist()) def test_create_zipfile_with_single_file(self): archive = Archive('test.zip', function_filename='lambda_function.py', single_file=True) archive.create_zipfile() with PyZipFile(archive._zippath, 'r', compression=ZIP_DEFLATED) as zipfile: ok_('lambda_function.py' in zipfile.namelist()) ok_(not ('.lamvery_secret.json' in zipfile.namelist())) def test_archive_dir(self): archive = Archive('test.zip') archive._archive_dir(self.zipfile, self.pj_root) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) def test_archive_file(self): archive = Archive('test.zip') archive._archive_file( self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) archive._archive_file( self.zipfile, os.path.join(self.pj_root, 'README.md')) ok_(isinstance(self.zipfile.getinfo('README.md'), zipfile.ZipInfo)) @raises(KeyError) def test_archive_single_file_key_error(self): self._single_file = True archive = Archive('test.zip', single_file=True) archive._archive_file( self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) def test_archive_single_file(self): self._single_file = True archive = Archive('test.zip', single_file=True) archive._archive_file( self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.py'), zipfile.ZipInfo)) def test_is_exclude(self): archive = Archive('test.zip', exclude=['^\.lamvery\.yml$']) eq_(archive.is_exclude('foo.txt'), False) eq_(archive.is_exclude('.lamvery.yml'), True) def test_is_exclude_file(self): archive = Archive('test.zip') eq_(archive.is_exclude_file('test.zip'), True) eq_(archive.is_exclude_file('foo.txt'), False) archive.is_exclude = Mock(return_value=True) eq_(archive.is_exclude_file('foo.txt'), True) def test_is_exclude_dir(self): archive = Archive('test.zip') eq_(archive.is_exclude_dir('.git'), True) eq_(archive.is_exclude_dir('foo'), False) archive.is_exclude = Mock(return_value=True) eq_(archive.is_exclude_file('foo'), True) def test_is_source_file(self): archive = Archive('test.zip') eq_(archive.is_source_file('foo.py'), True) eq_(archive.is_source_file('foo.pyc'), True) eq_(archive.is_source_file('foo.php'), False) def test_get_paths(self): archive = Archive('test.zip') paths = archive._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) archive = Archive('test.zip', no_libs=True) paths = archive._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) ok_(os.path.join(self.pj_root, 'lambda_function.py') in paths) ok_(os.path.join(self.pj_root, 'lambda_function.pyc') in paths) ok_(os.path.join(self.pj_root, '.lamvery.yml') in paths) archive = Archive('test.zip', function_filename='test.py', single_file=True) paths = archive._get_paths() ok_(os.path.join(self.pj_root, 'test.py') in paths) ok_(not os.path.join(self.pj_root, 'lambda_function.pyc') in paths) ok_(not os.path.join(self.pj_root, '.lamvery.yml') in paths) del os.environ['VIRTUAL_ENV'] archive = Archive('test.zip') paths = archive._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) def test_get_size(self): archive = Archive('test.zip') archive.create_zipfile() ok_(isinstance(archive.get_size(), int))
class ArchiveTestCase(TestCase): def setUp(self): tmp = tempfile.mkstemp(prefix=__name__) self.zipfile_path = tmp[1] self.zipfile = PyZipFile(self.zipfile_path, 'w') self.pj_root = os.path.abspath( os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) def tearDown(self): os.remove(self.zipfile_path) def test_create_zipfile(self): archive = Archive('test.zip') ok_(hasattr(archive.create_zipfile(), 'read')) with PyZipFile(archive._zippath, 'r', compression=ZIP_DEFLATED) as zipfile: ok_('lambda_function.pyc' in zipfile.namelist()) ok_('.lamvery_secret.json' in zipfile.namelist()) def test_create_zipfile_with_single_file(self): archive = Archive('test.zip', function_filename='lambda_function.py', single_file=True) archive.create_zipfile() with PyZipFile(archive._zippath, 'r', compression=ZIP_DEFLATED) as zipfile: ok_('lambda_function.py' in zipfile.namelist()) ok_(not ('.lamvery_secret.json' in zipfile.namelist())) def test_archive_dir(self): archive = Archive('test.zip') archive._archive_dir(self.zipfile, self.pj_root) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) def test_archive_file(self): archive = Archive('test.zip') archive._archive_file(self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) archive._archive_file(self.zipfile, os.path.join(self.pj_root, 'README.md')) ok_(isinstance(self.zipfile.getinfo('README.md'), zipfile.ZipInfo)) @raises(KeyError) def test_archive_single_file_key_error(self): self._single_file = True archive = Archive('test.zip', single_file=True) archive._archive_file(self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) def test_archive_single_file(self): self._single_file = True archive = Archive('test.zip', single_file=True) archive._archive_file(self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.py'), zipfile.ZipInfo)) def test_is_exclude(self): archive = Archive('test.zip', exclude=['^\.lamvery\.yml$']) eq_(archive.is_exclude('foo.txt'), False) eq_(archive.is_exclude('.lamvery.yml'), True) def test_is_exclude_file(self): archive = Archive('test.zip') eq_(archive.is_exclude_file('test.zip'), True) eq_(archive.is_exclude_file('foo.txt'), False) archive.is_exclude = Mock(return_value=True) eq_(archive.is_exclude_file('foo.txt'), True) def test_is_exclude_dir(self): archive = Archive('test.zip') eq_(archive.is_exclude_dir('.git'), True) eq_(archive.is_exclude_dir('foo'), False) archive.is_exclude = Mock(return_value=True) eq_(archive.is_exclude_file('foo'), True) def test_is_source_file(self): archive = Archive('test.zip') eq_(archive.is_source_file('foo.py'), True) eq_(archive.is_source_file('foo.pyc'), True) eq_(archive.is_source_file('foo.php'), False) def test_get_paths(self): archive = Archive('test.zip') paths = archive._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) archive = Archive('test.zip', no_libs=True) paths = archive._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) ok_(os.path.join(self.pj_root, 'lambda_function.py') in paths) ok_(os.path.join(self.pj_root, 'lambda_function.pyc') in paths) ok_(os.path.join(self.pj_root, '.lamvery.yml') in paths) archive = Archive('test.zip', function_filename='test.py', single_file=True) paths = archive._get_paths() ok_(os.path.join(self.pj_root, 'test.py') in paths) ok_(not os.path.join(self.pj_root, 'lambda_function.pyc') in paths) ok_(not os.path.join(self.pj_root, '.lamvery.yml') in paths) del os.environ['VIRTUAL_ENV'] archive = Archive('test.zip') paths = archive._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) def test_get_size(self): archive = Archive('test.zip') archive.create_zipfile() ok_(isinstance(archive.get_size(), int))
class BuilderTestCase(TestCase): def setUp(self): tmp = tempfile.mkstemp(prefix=__name__) self.zipfile_path = tmp[1] self.zipfile = PyZipFile(self.zipfile_path, 'w') self.pj_root = os.path.abspath( os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) def tearDown(self): os.remove(self.zipfile_path) try: os.remove('test.json') except: pass def test_build(self): builder = Builder('test.zip') builder._runtime = RUNTIME_NODE_JS ok_(hasattr(builder.build(), 'read')) with PyZipFile(builder._zippath, 'r', compression=ZIP_DEFLATED) as zipfile: ok_('lambda_function.pyc' in zipfile.namelist()) ok_('.lamvery_secret.json' in zipfile.namelist()) def test_build_with_single_file(self): builder = Builder('test.zip', function_filename='lambda_function.py', single_file=True) builder.build() with PyZipFile(builder._zippath, 'r', compression=ZIP_DEFLATED) as zipfile: ok_('lambda_function.py' in zipfile.namelist()) ok_(not ('.lamvery_secret.json' in zipfile.namelist())) def test_generate_json(self): builder = Builder('test.zip') builder._generate_json( JSON_FILE_NAME, {'foo': 2, 'bar': 3}) data = json.load(open(JSON_FILE_NAME, 'r')) eq_(data.get('foo'), 2) def test_archive_dir(self): builder = Builder('test.zip') builder._archive_dir(self.zipfile, self.pj_root) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) def test_archive_file(self): builder = Builder('test.zip') builder._archive_file( self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) builder._archive_file( self.zipfile, os.path.join(self.pj_root, 'README.md')) ok_(isinstance(self.zipfile.getinfo('README.md'), zipfile.ZipInfo)) def test_archive_dist(self): builder = Builder('test.zip') builder._archive_dist(self.zipfile, 'lamvery.js') ok_(isinstance(self.zipfile.getinfo('lamvery.js'), zipfile.ZipInfo)) @raises(KeyError) def test_archive_single_file_key_error(self): self._single_file = True builder = Builder('test.zip', single_file=True) builder._archive_file( self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo)) def test_archive_single_file(self): self._single_file = True builder = Builder('test.zip', single_file=True) builder._archive_file( self.zipfile, os.path.join(self.pj_root, 'setup.py')) ok_(isinstance(self.zipfile.getinfo('setup.py'), zipfile.ZipInfo)) def test_is_exclude(self): builder = Builder('test.zip', exclude=['^\.lamvery\.yml$']) eq_(builder.is_exclude('foo.txt'), False) eq_(builder.is_exclude('.lamvery.yml'), True) def test_is_exclude_file(self): builder = Builder('test.zip') eq_(builder.is_exclude_file('test.zip'), True) eq_(builder.is_exclude_file('foo.txt'), False) builder.is_exclude = Mock(return_value=True) eq_(builder.is_exclude_file('foo.txt'), True) def test_is_exclude_dir(self): builder = Builder('test.zip') eq_(builder.is_exclude_dir('.git'), True) eq_(builder.is_exclude_dir('foo'), False) builder.is_exclude = Mock(return_value=True) eq_(builder.is_exclude_file('foo'), True) def test_is_source_file(self): builder = Builder('test.zip') eq_(builder.is_source_file('foo.py'), True) eq_(builder.is_source_file('foo.pyc'), True) eq_(builder.is_source_file('foo.php'), False) def test_get_paths(self): builder = Builder('test.zip') paths = builder._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) builder = Builder('test.zip', no_libs=True) paths = builder._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) ok_(os.path.join(self.pj_root, 'lambda_function.py') in paths) ok_(os.path.join(self.pj_root, 'lambda_function.pyc') in paths) ok_(os.path.join(self.pj_root, '.lamvery.yml') in paths) builder = Builder('test.zip', function_filename='test.py', single_file=True) paths = builder._get_paths() ok_(os.path.join(self.pj_root, 'test.py') in paths) ok_(not os.path.join(self.pj_root, 'lambda_function.pyc') in paths) ok_(not os.path.join(self.pj_root, '.lamvery.yml') in paths) builder = Builder('test.zip', clean_build=True) paths = builder._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') not in paths) del os.environ['VIRTUAL_ENV'] builder = Builder('test.zip') paths = builder._get_paths() ok_(os.path.join(self.pj_root, 'lamvery') in paths) def test_get_size(self): builder = Builder('test.zip') builder.build() ok_(isinstance(builder.get_size(), int))