def double_dump_test(): """ Perform a "double dump test" on every demo database. TODO: convert this to a Lino management command. """ raise Exception("Not yet converted after 20150129") if len(env.demo_databases) == 0: return a = Path(env.temp_dir, 'a') b = Path(env.temp_dir, 'b') rmtree_after_confirm(a) rmtree_after_confirm(b) #~ if not confirm("This will possibly break the demo databases. Are you sure?"): #~ return #~ a.mkdir() with lcd(env.temp_dir): for db in env.demo_databases: if a.exists(): a.rmtree() if b.exists(): b.rmtree() local("django-admin.py dump2py --settings=%s --traceback a" % db) local( "django-admin.py run --settings=%s --traceback a/restore.py" % db) local("django-admin.py dump2py --settings=%s --traceback b" % db) local("diff a b")
class tempdir(object): def __enter__(self): self.tempdir = Path(tempfile.mkdtemp()) return self.tempdir def __exit__(self, type_, value, traceback): self.tempdir.rmtree()
def set_up_and_import_file(self): temp_dir = Path(tempfile.mkdtemp()) temp_file = Path(temp_dir, 'test.csv') with open(temp_file, 'a') as f: header = '' header_fields = [ 'route', 'account_number', 'house_number', 'pre_direction', 'street', 'street_type', 'post_direction', 'unit', 'city_state_zip' ] for x, field in enumerate(header_fields): if x + 1 == len(header_fields): header = header + field + '\n' else: header = header + field + ',' f.write(header) f.write('123,123,123,S,TEST,RD,S,#5B,"TEST, FL 32174"') mommy.make( 'route_update.Directional', direction='South', abbreviation='S' ) mommy.make('route_update.StreetType', name='Road', abbreviation='RD') mommy.make('route_update.State', name='Florida', abbreviation='FL') Location.create_active_locations_from_file(temp_file) temp_dir.rmtree()
class Test_Inupypi(unittest.TestCase): def setUp(self): self.app = inupypi.app.test_client() self.workspace = Path(tempfile.mkdtemp()) self.packages = ['p1', 'p2', 'p3', 'p4'] self.files = ['f1', 'f2', 'f3', 'f4'] self.app.application.config['INUPYPI_REPO'] = self.workspace def tearDown(self): self.workspace.rmtree() def test_app_with_missing_package_dir(self): self.app.application.config['INUPYPI_REPO'] = Path(self.workspace, 'a') assert self.app.get('/').status_code == 500 assert '500:' in self.app.get('/').data def test_app_without_packages(self): assert 'inetutils PyPI Server' in self.app.get('/').data assert 'Available Packages' not in self.app.get('/').data def test_app_with_package_folders(self): env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) assert 'inetutils PyPI Server' in self.app.get('/').data assert 'Available EggBaskets' in self.app.get('/').data def test_app_package(self): env_create_packages(self.workspace, self.packages) for p in self.packages: for f in self.files: page = self.app.get('/' + p + '/').data assert '404 - Page not found' not in page assert f not in page env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: for f in self.files: assert f in self.app.get('/test/' + p + '/').data def test_app_package_file(self): env_create_packages(self.workspace, self.packages) for p in self.packages: for f in self.files: response = self.app.get('/test/' + p + '/get/' + f) assert response.status_code == 404 env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: for f in self.files: f = '%s.tar.gz' % f response = self.app.get('/test/' + p + '/get/' + f) assert response.status_code == 200 assert response.content_type == 'application/x-tar' assert response.headers.get('Content-Disposition') == \ 'attachment; filename=%s' % f
class Test_Inupypi(unittest.TestCase): def setUp(self): self.app = inupypi.app.test_client() self.workspace = Path(tempfile.mkdtemp()) self.packages = ['p1', 'p2', 'p3', 'p4'] self.files = ['f1', 'f2', 'f3', 'f4'] self.app.application.config['INUPYPI_REPO'] = self.workspace def tearDown(self): self.workspace.rmtree() def test_app_with_missing_package_dir(self): self.app.application.config['INUPYPI_REPO'] = Path(self.workspace, 'a') assert self.app.get('/').status_code == 500 assert '500:' in self.app.get('/').data def test_app_without_packages(self): assert 'inetutils PyPI Server' in self.app.get('/').data assert 'Available Packages' not in self.app.get('/').data def test_app_with_package_folders(self): env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) assert 'inetutils PyPI Server' in self.app.get('/').data assert 'Available EggBaskets' in self.app.get('/').data def test_app_package(self): env_create_packages(self.workspace, self.packages) for p in self.packages: for f in self.files: page = self.app.get('/'+p+'/').data assert '404 - Page not found' not in page assert f not in page env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: for f in self.files: assert f in self.app.get('/test/'+p+'/').data def test_app_package_file(self): env_create_packages(self.workspace, self.packages) for p in self.packages: for f in self.files: response = self.app.get('/test/'+p+'/get/'+f) assert response.status_code == 404 env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: for f in self.files: f = '%s.tar.gz' % f response = self.app.get('/test/'+p+'/get/'+f) assert response.status_code == 200 assert response.content_type == 'application/x-tar' assert response.headers.get('Content-Disposition') == \ 'attachment; filename=%s' % f
def delete(self, name): user = User(self.path, self.git, name) dest = Path(self.path, 'keydir/%s' % name) if not dest.exists(): raise ValueError('Repository %s not existing.' % name) dest.rmtree() self.git.commit([str(dest)], 'Deleted user %s.' % name) return user
def test_load_states_space_end_of_line(self): temp_dir = Path(tempfile.mkdtemp()) temp_file = Path(temp_dir, 'test.txt') temp_file.write_file('Florida FL \n') State.load_states(temp_file.absolute()) temp_dir.rmtree() self.assertEqual('Florida', State.objects.first().name) self.assertEqual('FL', State.objects.first().abbreviation)
def test_load_directionals_no_space_end_of_line(self): temp_dir = Path(tempfile.mkdtemp()) temp_file = Path(temp_dir, 'test.txt') temp_file.write_file('Northeast NE\n') Directional.load_directionals(temp_file.absolute()) temp_dir.rmtree() self.assertEqual('Northeast', Directional.objects.first().direction) self.assertEqual('NE', Directional.objects.first().abbreviation)
def test_load_street_types_no_space_end_of_line(self): temp_dir = Path(tempfile.mkdtemp()) temp_file = Path(temp_dir, 'test.txt') temp_file.write_file('VILLAGE VILL VLG\n') StreetType.load_street_types(temp_file.absolute()) temp_dir.rmtree() self.assertEqual('VILLAGE VILL', StreetType.objects.first().name) self.assertEqual('VLG', StreetType.objects.first().abbreviation)
def delete(self, name): user = User.get_by_name(name, self.path, self.git) if not user: return dest = Path(self.path, 'keydir/%s' % name) for repo in user.repos: Repository.get(repo, self.path, self.git).users.remove(user.name) for group in user.groups: GroupManager(self.path).user_delete(group, user.name) if dest.exists(): dest.rmtree() self.git.commit([str(dest)], 'Deleted user %s.' % name)
def main(): if len(sys.argv) != 2 or sys.argv[1] not in ['KNMP', 'Stanford', 'both']: print("Usage: python %s <dataset>" % sys.argv[0]) print("\tDataset should be either 'KNMP' or 'Stanford' or 'both'") sys.exit(1) # create a clean temporary directory if os.path.exists("tmp"): shutil.rmtree("tmp") os.makedirs("tmp") work_dir = Path("tmp") work_dir.rmtree() work_dir.mkdir() if sys.argv[1] in ['KNMP', 'both']: create('KNMP', work_dir) if sys.argv[1] in ['Stanford', 'both']: create('Stanford', work_dir)
def env_create_package_files(workspace, packages, files): for p in packages: if files: for f in files: f = Path(workspace, 'test', p, f) f.mkdir(parents=True) version = '.'.join([x for x in f.name if x.isdigit()]) i = env_create_package_pkginfo(p, version) Path(f, '%s.egg-info' % f.name).mkdir() Path(f, 'PKG-INFO').write_file(i) Path(f, '%s.egg-info' % f.name, 'PKG-INFO').write_file(i) os.chdir(f.parent) tar = tarfile.open('%s.tar.gz' % f, 'w:gz') tar.add('%s' % f.name) tar.close() f.rmtree() else: Path(workspace, 'test', p).mkdir(parents=True)
def setup_test_sdist(): if len(env.demo_databases) == 0: return ve_path = Path(env.temp_dir, 'test_sdist') #~ if ve_path.exists(): ve_path.rmtree() #~ rmtree_after_confirm(ve_path) ve_path.mkdir() script = ve_path.child('tmp.sh') context = dict(name=env.SETUP_INFO['name'], sdist_dir=env.sdist_dir, ve_path=ve_path) #~ file(script,'w').write(TEST_SDIST_TEMPLATE % context) txt = TEST_SDIST_TEMPLATE % context for db in env.demo_databases: txt += "django-admin.py test --settings=%s --traceback\n" % db script.write_file(txt) script.chmod(0o777) with lcd(ve_path): local(script)
class Base(object): """ set and make directory Parameters ---------- home : str set a directory as home """ def __init__(self, home='.'): """ set home directory Parameters ---------- home : str set a directory as home Returns ------- """ self._home = Path(home).absolute() def __str__(self): return self.home def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self.home) # def __abs(self, string): # return os.path.abspath(string) @property def home(self): return self._home.__str__() @home.setter def home(self, path): self._home = Path(path).absolute() def make_home(self, force=False): """ make home directory Parameters ---------- force : bool if True, if home exists and is a dir that containing contents, then delete contents in it, if exists and not a dir, remove it and make dir Returns ------- """ self.__mkdir(force) def __mkdir(self, force=False): if self._home.exists(): if not self._home.isdir(): if not force: raise Exception('%s exists but is not a dir' % self.home) self._home.remove() self._home.mkdir() if force: self._home.rmtree() self._home.mkdir() else: self._home.mkdir(parents=True) def __rmdir(self, force=False): if self._home.exists(): if not self._home.isdir(): if not force: raise Exception('%s exists but is not a dir' % self.home) self._home.remove() if force: self._home.rmtree() else: self._home.rmdir() def rm_home(self, force=False): """ remove home directory Parameters ---------- force : bool if True, if home exists and is a dir that containing contents, then delete it and it's contents, if exists and not a dir, remove then Returns ------- """ self.__rmdir(force)
class ProjectInstaller(Installer): flavor = 'django_custom' git_repo = 'https://github.com/Libermentix/project_skeletton_directory.git' def __init__(self, project_dir, project_name, db_sudo=False, db_sudo_user=None, *args, **kwargs): super(ProjectInstaller, self).__init__( project_dir, project_name, *args, **kwargs ) self.var_dict = dict( project_dir=add_trailing_slash(project_dir), project_name=add_trailing_slash(project_name) ) self._tmp_dir = None if self.install_path.exists(): self.install_path.rmtree() self.db_installer = DatabaseInstaller( project_dir=project_dir, project_name=project_name, sudo=db_sudo, sudo_user=db_sudo_user ) self.django_installer = DjangoInstaller( project_dir=project_dir, project_name=project_name ) def run(self): self.run_prepare_configuration() self.run_create_configuration() self.run_post_create_configuration() def run_prepare_configuration(self): self.get_git_repo() self.install_skeletton() self.install_requirements() def run_post_create_configuration(self): """ run the the post_run_command_stack """ self.db_installer() self.django_installer() self.move_to_venv(which_one='postactivate') self.move_to_venv(which_one='activate') self.finish_queued_commands() #run the post create configuration command for the children for item in self.db_installer.post_run_command_stack: # should be a callable or None if item: item() for item in self.django_installer.post_run_command_stack: # should be a callable or None if item: logger.info('%s: Executing a django_installer_script ...' % item) item() @property def requirements_file(self): return Path( self.install_path, 'requirements', 'base.txt' ).absolute() @property def repo_dir(self): #get last directory = self.git_repo.split('/')[-1:][0] #remove .git directory = directory.split('.')[0] return Path(self._tmp_dir, directory) def create_tmp_dir(self): # TODO: # Account for existing project paths, here it should ask to remove # or abort. self._tmp_dir = Path(self.install_path, 'tmp') self._tmp_dir.mkdir() self._tmp_dir.chdir() def delete_tmp_dir(self): self.project_dir.chdir() self._tmp_dir.rmtree() def get_git_repo(self): self.create_tmp_dir() logger.info('Cloning repository ...') if self.repo_dir.exists(): logger.info('Repo dir exists removing it...') self.repo_dir.rmtree() git.Git().clone(self.git_repo) logger.info('..done') def install_skeletton(self): logger.info('Installing %s' % self.flavor) source = Path(self.repo_dir, self.flavor) #move all items in the directory into the install_path for item in source.listdir(): item.move(self.install_path) self.delete_tmp_dir() logger.info('...done') def install_virtualenv(self): """ Calls a script that creates the virtual environment and installs its dependencies, currently only sports python2.7 support. """ exec_path = Path(Path(__file__).parent, 'bash', 'installer.sh') command = '%s %s %s %s' % (exec_path, self.install_path, self.project_name, self.requirements_file) logger.info('Installing virtualenv... (calling %s)' % command) self.run_command(command) def install_requirements(self): if not self.is_envwrapper: self.install_virtualenv() else: # we can assume that we are in the virtualenv now, and mkproject # was called command = 'pip install -r %s' % self.requirements_file self.run_command(command)
def temp_dir(): tmp = Path(tempfile.mkdtemp()) try: yield tmp finally: tmp.rmtree()
class Test_Packages(unittest.TestCase): def setUp(self): self.app = app.test_client() self.workspace = Path(tempfile.mkdtemp()) self.packages = ['p1', 'p2', 'p3', 'p4'] self.files = ['f1', 'f2', 'f3', 'f4'] self.files.sort(reverse=True) self.app.application.config['INUPYPI_REPO'] = self.workspace def tearDown(self): self.workspace.rmtree() def test_get_packages_without_packages_folder(self): self.app.application.config['INUPYPI_REPO'] = Path(self.workspace, 'a') try: get_packages('test') return False except: return True def test_get_packages(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) created_packages = [Path(self.workspace, 'test', package) for package in self.packages] packages = [p.filepath for p in get_packages('test')] assert created_packages == packages def test_get_packages_from_folders_only(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) packages_and_files = sorted([Path(package) for package in self.packages + self.files]) created_packages = [Path(self.workspace, 'test', package) for package in self.packages] packages = [p.filepath for p in get_packages('test')] assert get_packages('test') != packages_and_files assert created_packages == packages def test_get_package_files(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) for p in self.packages: assert get_package_files('test', p) == [] env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: files = sorted([Path(self.workspace, 'test', p, '%s.tar.gz' % f) for f in self.files]) package_files = sorted([pkg.filepath for pkg in get_package_files('test', p)]) assert package_files == files def test_get_current_package(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: created_packages = Path(self.workspace, 'test', p, '%s.tar.gz' % sorted(self.files, reverse=True)[0]) assert get_current_package('test', p) == created_packages def test_get_file(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: for f in self.files: assert get_file('test', p, f) == False for p in self.packages: for f in self.files: f = '%s.tar.gz' % f assert get_file('test', p, f) == Path(self.workspace, 'test', p, f)
class Test_Packages(unittest.TestCase): def setUp(self): self.app = app.test_client() self.workspace = Path(tempfile.mkdtemp()) self.packages = ['p1', 'p2', 'p3', 'p4'] self.files = ['f1', 'f2', 'f3', 'f4'] self.files.sort(reverse=True) self.app.application.config['INUPYPI_REPO'] = self.workspace def tearDown(self): self.workspace.rmtree() def test_get_packages_without_packages_folder(self): self.app.application.config['INUPYPI_REPO'] = Path(self.workspace, 'a') try: get_packages('test') return False except: return True def test_get_packages(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) created_packages = [ Path(self.workspace, 'test', package) for package in self.packages ] packages = [p.filepath for p in get_packages('test')] assert created_packages == packages def test_get_packages_from_folders_only(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) packages_and_files = sorted( [Path(package) for package in self.packages + self.files]) created_packages = [ Path(self.workspace, 'test', package) for package in self.packages ] packages = [p.filepath for p in get_packages('test')] assert get_packages('test') != packages_and_files assert created_packages == packages def test_get_package_files(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) for p in self.packages: assert get_package_files('test', p) == [] env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: files = sorted([ Path(self.workspace, 'test', p, '%s.tar.gz' % f) for f in self.files ]) package_files = sorted( [pkg.filepath for pkg in get_package_files('test', p)]) assert package_files == files def test_get_current_package(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: created_packages = Path( self.workspace, 'test', p, '%s.tar.gz' % sorted(self.files, reverse=True)[0]) assert get_current_package('test', p) == created_packages def test_get_file(self): self.app.application.config['INUPYPI_REPO'] = self.workspace env_create_packages(self.workspace, self.packages) env_create_package_files(self.workspace, self.packages, self.files) for p in self.packages: for f in self.files: assert get_file('test', p, f) == False for p in self.packages: for f in self.files: f = '%s.tar.gz' % f assert get_file('test', p, f) == Path(self.workspace, 'test', p, f)
def _clear_cache_for_report(report): report_dir = Path(report).parent cache_dir = Path(report_dir, '.cache') if cache_dir.isdir(): print 'removing cache dir:\n\t{}'.format(cache_dir) cache_dir.rmtree()
def _clear_figs_for_report(report): report_dir = Path(report).parent figs_dir = Path(report_dir, 'figs') if figs_dir.isdir(): print 'removing figs dir:\n\t{}'.format(figs_dir) figs_dir.rmtree()
class DictToLocationTest(TestCase): temp_dir = None temp_file = None def setUp(self): self.temp_dir = Path(tempfile.mkdtemp()) self.temp_file = Path(self.temp_dir, 'test.csv') with open(self.temp_file, 'a') as f: header = '' header_fields = [ 'route', 'account_number', 'house_number', 'pre_direction', 'street', 'street_type', 'post_direction', 'unit', 'city_state_zip' ] for x, field in enumerate(header_fields): if x + 1 == len(header_fields): header = header + field + '\n' else: header = header + field + ',' f.write(header) mommy.make( 'route_update.Directional', direction='South', abbreviation='S' ) mommy.make('route_update.StreetType', name='Road', abbreviation='RD') mommy.make('route_update.State', name='Florida', abbreviation='FL') super(DictToLocationTest, self).setUp() def tearDown(self): self.temp_dir.rmtree() super(DictToLocationTest, self).tearDown() def add_record_to_file(self): with open(self.temp_file, 'a') as f: f.write('123,123,123,S,TEST,RD,S,#5B,"TEST, FL 32174"') def get_dict_to_location(self): with open(self.temp_file, 'r') as f: csv_reader = csv.DictReader(f, delimiter=',', quotechar='"') for row in csv_reader: loc = DictToLocation(row) return loc def test_set_route(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.route, Route) self.assertEqual('123', str(Route.objects.first().number)) def test_set_house_number(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.house_number, HouseNumber) self.assertEqual('123', HouseNumber.objects.first().number) def test_set_pre_direction_not_none(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.pre_direction, Directional) self.assertEqual('S', Directional.objects.first().abbreviation) self.assertEqual(1, Directional.objects.all().count()) def test_set_pre_direction_none(self): with open(self.temp_file, 'a') as f: f.write('123,123,123,,TEST,RD,S,#5B,"TEST, FL 32174"') loc = self.get_dict_to_location() self.assertNotIsInstance(loc.pre_direction, Directional) self.assertIsNone(loc.pre_direction) def test_set_street(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.street, Street) self.assertEqual('TEST', Street.objects.first().name) def test_set_street_type_not_none(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.street_type, StreetType) self.assertEqual('RD', StreetType.objects.first().abbreviation) self.assertEqual(1, StreetType.objects.all().count()) def test_set_street_type_none(self): with open(self.temp_file, 'a') as f: f.write('123,123,123,S,TEST,,S,#5B,"TEST, FL 32174"') loc = self.get_dict_to_location() self.assertNotIsInstance(loc.street_type, StreetType) self.assertIsNone(loc.street_type) def test_set_post_direction_not_none(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.post_direction, Directional) self.assertEqual('S', Directional.objects.first().abbreviation) self.assertEqual(1, Directional.objects.all().count()) def test_set_post_direction_none(self): with open(self.temp_file, 'a') as f: f.write('123,123,123,S,TEST,RD,,#5B,"TEST, FL 32174"') loc = self.get_dict_to_location() self.assertNotIsInstance(loc.post_direction, Directional) self.assertIsNone(loc.post_direction) def test_set_unit_not_none(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.unit, Unit) self.assertEqual('5B', Unit.objects.first().name) def test_set_unit_none(self): with open(self.temp_file, 'a') as f: f.write('123,123,123,S,TEST,RD,S,,"TEST, FL 32174"') loc = self.get_dict_to_location() self.assertNotIsInstance(loc.unit, Unit) self.assertIsNone(loc.unit) def test_set_zip_code(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.zip_code, ZipCode) self.assertEqual('32174', ZipCode.objects.first().zip) def test_set_location_no_none_values(self): self.add_record_to_file() loc = self.get_dict_to_location() self.assertIsInstance(loc.location, Location) self.assertEqual('123', str(Location.objects.first().account_number)) def test_set_location_none_values(self): with open(self.temp_file, 'a') as f: f.write('123,123,123,,TEST,,,,"TEST, FL 32174"') loc = self.get_dict_to_location() self.assertIsInstance(loc.location, Location) self.assertEqual('123', str(Location.objects.first().account_number))
def test_rmtree_broken_symlink(self): symlink = Path(self.d, "symlink") symlink.write_link("broken") assert symlink.lexists() symlink.rmtree() assert not symlink.lexists()