def test_delete_stanza(self): service = Service([ Stanza('script://test1', {}), Stanza('package://test2', {}), Stanza('script://test3', {}), ]) config.delete_stanza(service, 'script', 'test3') config.delete_stanza(service, 'script', 'test1') for _ in config.iter_stanzas(service, 'script'): self.fail('No stanza should be found') cnt = 0 for stanza, name in config.iter_stanzas(service, 'package'): self.assertEqual('package://test2', stanza.name) cnt += 1 self.assertEqual(1, cnt)
def all_package_names(service): yielded_names = set() for _, package_name in config.iter_stanzas(service, scheme): yielded_names.add(package_name) for dependent_package_name in dependent_package_names(package_name): yielded_names.add(dependent_package_name) return yielded_names
def get(service, script_name): for stanza, name in config.iter_stanzas(service, scheme): if name == script_name: return { 'content': base64.decodestring(stanza.__getattr__('content')), 'is_removable': stanza.access['removable'] == '1', } return None
def test_iter_stanzas(self): service = Service([ Stanza('script://test1', {}), Stanza('package://test2', {}), Stanza('script://test3', {}), ]) cnt = 0 for stanza, name in config.iter_stanzas(service, 'script'): self.assertTrue(name.startswith('test')) self.assertTrue(stanza.name.startswith('script://')) cnt += 1 self.assertEqual(2, cnt)
def test_create_stanza(self): service = Service([ Stanza('script://test1', {}), Stanza('package://test2', {}), Stanza('script://test3', {}), ]) config.create_stanza(service, 'test', 'test4', { 'a': 'b' }) cnt = 0 for stanza, name in config.iter_stanzas(service, 'test'): self.assertEqual(cnt, 0) cnt += 1 self.assertEqual(getattr(stanza, 'a'), 'b')
def r_stats(service): stats_id = str(uuid.uuid1()) number_of_packages = 0 for _, package_name in config.iter_stanzas(service, packages.scheme): log({ 'stats_id': stats_id, 'stats_package_name': package_name, }) number_of_packages += 1 log({ 'stats_id': stats_id, 'stats_number_of_packages': number_of_packages, })
def create_files(service): lock_file_path = path.get_file('scripts.lock') with lockfile.file_lock(lock_file_path): custom_scripts_path = get_custom_scripts_path() filenames = set() # check configuration for requred scripts for stanza, name in config.iter_stanzas(service, scheme): filename = name + os.path.extsep + extension full_path = os.path.join(custom_scripts_path, filename) # in case there is no uploaded attribute, add it with the current timestamp if not hasattr(stanza, uploaded_stanza_key): stanza = config.create_stanza(service, scheme, name, { 'content': stanza.__getattr__('content'), uploaded_stanza_key: int(time.time()) }) if os.path.exists(full_path): # compare file creation date with stanza creation date modify_time = int(os.path.getmtime(full_path)) uploaded_time = int(getattr(stanza, uploaded_stanza_key)) if uploaded_time > modify_time: os.remove(full_path) create_file = True else: create_file = False else: create_file = True if create_file: script_content = base64.decodestring(stanza.__getattr__('content')) with open(full_path, 'wb') as f: f.write(script_content) filenames.add(filename) # remove files that are no longer in the list of configures scripts for filename in os.listdir(custom_scripts_path): if filename.endswith(os.path.extsep + extension): if not filename in filenames: script_path = os.path.join(custom_scripts_path, filename) os.remove(script_path)
def update_library(service): with lock_packages_and_library(): # download required package archives for stanza, package_name in config.iter_stanzas(service, scheme): install_package(service, package_name) # collect files that should be present archive_filenames = set() package_metadata_filenames = set() package_names = all_package_names(service) for package_name in package_names: archive_filenames.add(get_local_package_filename(package_name)) package_metadata_filenames.add( get_metadata_package_state_filename(package_name)) # remove packages that are no longer in the list of required packages packages_path = get_packages_path() for filename in os.listdir(packages_path): if filename not in archive_filenames: script_path = os.path.join(packages_path, filename) os.remove(script_path) # uninstall packages that are no longer in the list of required packages library_path = get_library_path() for package_name in os.listdir(library_path): if package_name not in package_names: if not package_name.startswith('.'): package_path = os.path.join(library_path, package_name) shutil.rmtree(package_path) # delete packages metadata that are no longer in the list of required packages packages_metadata_path = get_packages_metadata_path() for filename in os.listdir(packages_metadata_path): if filename not in package_metadata_filenames: if not filename.startswith('.'): package_metadata_path = os.path.join( packages_metadata_path, filename) os.remove(package_metadata_path)
def update_library(service): with lock_packages_and_library(): # download required package archives for stanza, package_name in config.iter_stanzas(service, scheme): install_package(service, package_name) # collect files that should be present archive_filenames = set() package_metadata_filenames = set() package_names = all_package_names(service) for package_name in package_names: archive_filenames.add(get_local_package_filename(package_name)) package_metadata_filenames.add(get_metadata_package_state_filename(package_name)) # remove packages that are no longer in the list of required packages packages_path = get_packages_path() for filename in os.listdir(packages_path): if not filename in archive_filenames: script_path = os.path.join(packages_path, filename) os.remove(script_path) # uninstall packages that are no longer in the list of required packages library_path = get_library_path() for package_name in os.listdir(library_path): if not package_name in package_names: if not package_name.startswith('.'): package_path = os.path.join(library_path, package_name) shutil.rmtree(package_path) # delete packages metadata that are no longer in the list of required packages packages_metadata_path = get_packages_metadata_path() for filename in os.listdir(packages_metadata_path): if not filename in package_metadata_filenames: if not filename.startswith('.'): package_metadata_path = os.path.join(packages_metadata_path, filename) os.remove(package_metadata_path)
def iter_stanzas(service): return config.iter_stanzas(service, scheme)