Esempio n. 1
0
    def acquire(self):
        """Attempt to acquire a mutex. Returns True if mutex is acquired."""
        self.reset()
        try:
            # handle stale pids (older than boot time, pid not a process)
            if self.is_stale():
                logger.debug(f'{self} - deleting stale pid file')
                delete_file(self.file_name, ignore_errors=True)

            # open() with exclusive access ('x') is an atomic condition
            # Note: We keep this file handle open until we close our mutex.
            pid = os.getpid()
            self.file_handle = open(self.file_name, 'x')

            # by convention: pid files consist of the pid and a newline character
            self.file_handle.write(f'{pid}\n')
            self.file_handle.flush()

            # if we made it this far then mutex successfully acquired
            self.pid = pid
            logger.debug(f'{self}:acquire')
            return True

        except OSError:
            # failed to acquire mutex
            self.reset()
            logger.debug(f'{self}:acquire')
            return False
Esempio n. 2
0
 def release(self):
     """Release a mutex."""
     logger.debug(f'{self}:release')
     if self.file_handle:
         self.file_handle.close()
         self.reset()
         delete_file(self.file_name, ignore_errors=True)
Esempio n. 3
0
	def remove(self, name):
		logger.debug(self._context(f'Removing vault'))
		if not is_file(self._file_name(name)):
			logger.warning(self._context(f'Vault does not exist'))
		else:
			delete_file(self._file_name(name), ignore_errors=True)
			if is_file(self._file_name(name)):
				logger.warning('Unable to remove vault')
Esempio n. 4
0
def test():

    # configuration driven support
    config = ConfigSectionKey("../conf", "../local")
    config = config
    config.load("bootstrap.ini", "bootstrap")
    config.load("init.ini")
    config.load("connect.ini")

    bs_test = BlobStore()
    resource = config("resource:bs_test_local")
    bs_test.create(resource)
    bs_test.connect(resource)
    bs_test.remove(resource)
    bs_test.create(resource)

    # # good things
    save_text("testfile-1.txt", "test file")
    delete_file("testfile-2.txt", ignore_errors=True)

    # expected Connection exception
    try:
        bs_test.put("testfile-1.txt", "downloads/testfile-1.txt")
    except ConnectionError as e:
        logger.info(f"Non-connected resource raised ConnectionError as expected: {e}")

    bs_test.connect(resource)
    assert bs_test.put("testfile-1.txt", "downloads/testfile-1.txt")
    assert bs_test.put("testfile-1.txt", "downloads/testfile-2.txt")
    assert bs_test.put("testfile-1.txt", "downloads/testfile-3.txt")
    assert bs_test.get("testfile-2.txt", "downloads/testfile-2.txt")

    downloads_folder_only = ["downloads"]
    downloads_folder_files = [
        "downloads/testfile-1.txt",
        "downloads/testfile-2.txt",
        "downloads/testfile-3.txt",
    ]
    # assert bs_test.list() == downloads_folder_only
    # assert bs_test.list('*') == downloads_folder_only
    # assert bs_test.list('/') == downloads_folder_only
    # assert bs_test.list('/downloads') == downloads_folder_files
    # assert bs_test.list('downloads') == downloads_folder_files
    # assert bs_test.list('downloads/') == downloads_folder_files

    bs_test.list("downloads")
    bs_test.list("downloads/")
    bs_test.list("downloads/*")
    bs_test.delete("downloads/testfile-1.txt")
    bs_test.list("downloads/*")

    # bad things
    assert not bs_test.list("bad-path*")
    assert not bs_test.put("bad-file-1.txt", "downloads/bad-file.txt")
    assert not bs_test.get("bad-file-2.txt", "downloads/bad-file.txt")
    assert not bs_test.delete("downloads/bad-file.txt")
    bs_test.clear()
Esempio n. 5
0
    def archive_capture_file(self, notification):

        # get name of file (key) that triggered this call
        source_object_key = notification.object_key

        # extract out the file name
        source_file_name = just_file_name(source_object_key)

        # if source_file_name is empty, ignore notification
        if not source_file_name:
            logger.debug(
                f'Ignoring notification without object key (file name): {notification}'
            )
            return

        # if source_file_name is capture_state.zip, ignore it
        # Note: This keeps the latest capture_state.zip file in each capture folder for recovery purposes.
        if source_file_name == 'capture_state.zip':
            logger.debug(f'Ignoring capture_state.zip file notification')
            return

        # TODO: Add activity_log (vs job_log/stat_log) references.

        # make sure work folder exists and is empty
        work_folder = 'sessions/archive_work'
        clear_folder(work_folder)

        # get file, copy to archive then delete from capture
        source_objectstore_name = notification.objectstore_name
        source_file_name = f'{work_folder}/' + source_file_name

        # get the posted file
        logger.info(
            f'Getting {source_file_name} from {source_objectstore_name}::{source_object_key}'
        )
        source_objectstore = Objectstore(source_objectstore_name, self.cloud)
        source_objectstore.get(source_file_name, source_object_key)

        # move (copy) the posted file to the archive object_store
        logger.info(
            f'Moving {source_file_name} to archive_object_store::{source_object_key}'
        )
        archive_objectstore_name = self.cloud.archive_objectstore
        archive_objectstore = Objectstore(archive_objectstore_name, self.cloud)
        archive_objectstore.put(source_file_name, source_object_key)

        # then delete file from source object_store	and local work folder
        logger.info(
            f'Deleting {source_object_key} from {source_objectstore_name}')
        source_objectstore.delete(source_object_key)
        delete_file(source_file_name)

        # TODO: update stat_log
        # TODO: update stage queue

        return
Esempio n. 6
0
    def _save_position_db_file(data):
        if common.file_exist(trader_db_position_filename):
            common.delete_file(trader_db_position_filename)

        try:
            common.dict_to_file(data, trader_db_position_filename)
        except Exception as err:
            return err.message

        return None
Esempio n. 7
0
 def delete(self, blob_name):
     """Delete blob."""
     target_file_name = self._blob_file(blob_name)
     if not is_file(target_file_name):
         warning_message = f"Blob name does not exist"
         logger.warning(self._context(warning_message, blob_name))
         is_success = False
     else:
         logger.debug(self._context(f"Deleting blob", blob_name))
         delete_file(target_file_name)
         is_success = True
     return is_success
Esempio n. 8
0
	def is_stopped(self):
		"""Sample is_stopped() implementation checking command file for command to execute."""

		stop_status = False
		command_file_name = f'{script_name()}.command'
		command = load_lines(command_file_name, line_count=1)
		command = command.strip().lower()
		if command:
			logger.info(f'Command: {command}')
			delete_file(command_file_name)
			if command in ('die', 'exit', 'kill', 'quit', 'stop'):
				stop_status = True
			if command in ('diagnostics', 'dump', 'info'):
				self.dump()

		return stop_status
Esempio n. 9
0
def backup(ctx, args):
    if args.skip_backup:
        return
    save_dir = ctx.get_path("{save_dir}")
    backup_file = ctx.get_path("{backup_file}")
    if args.dry_run:
        print("BACKUP: " + repr(save_dir))
        return
    try:
        backup_files(target_filename=backup_file, dirname=save_dir)
    except OSError as err:
        print("Error writing backup file %r:" % backup_file,
              err,
              file=sys.stderr)
        common.delete_file(backup_file)
        raise KeyboardInterrupt()
Esempio n. 10
0
def test_delete_file():
    setup_test_files()

    # Set the readonly.txt file to read/write so it can be deleted
    readonly_file_name = f'{test_folder_path}/readonly.txt'
    if is_file(readonly_file_name):
        os.chmod(readonly_file_name, S_IWUSR | S_IREAD)

    # delete both the readonly.txt and the readwrite.txt files
    delete_file(f'{test_folder_path}/readonly.txt')
    delete_file(f'{test_folder_path}/readwrite.txt')

    # assert both files have been deleted
    assert is_file(f'{test_folder_path}/readwrite.txt') is False
    assert is_file(f'{test_folder_path}/readonly.txt') is False

    teardown_test_files()
Esempio n. 11
0
 def run_apisan(self, c_fname, as_fname, unless=[]):
     try:
         self.run("APISAN",
                  c_fname,
                  as_fname,
                  self.apisan + " compile %s " +
                  os.environ.get('CFLAGS', ''),
                  c_fname,
                  unless=unless)
         if Run.C not in self.args.keep:
             delete_file(c_fname)
     except StopExecution:
         # Only log when program has not been user-terminated
         if not self.cancelled.is_set(
         ) and self.args.log_ignored is not None:
             print(c_fname, file=self.args.log_ignored)
             self.args.log_ignored.flush()  # Ensure the filename is written
         raise
Esempio n. 12
0
def test_encrypt_decrypt_text_file():
    """Test text file encryption/decryption."""

    # setup
    key = 'abc'
    original = 'This is text file data.'
    original_file_name = 'test_encryption_file.txt'
    encrypted_file_name = original_file_name + '_encrypted'
    decrypted_file_name = original_file_name + '_decrypted'
    save_text(original, original_file_name)

    # test
    encrypt_text_file(key, original_file_name, encrypted_file_name)
    encrypted = load_text(encrypted_file_name)
    decrypt_text_file(key, encrypted_file_name, decrypted_file_name)
    decrypted = load_text(decrypted_file_name)
    _output_encrypt_decrypt_results(original, encrypted, decrypted)

    # cleanup
    delete_file(original_file_name)
    delete_file(encrypted_file_name)
    delete_file(decrypted_file_name)
Esempio n. 13
0
	def clear(self):
		if self.endpoint:
			delete_file(self.endpoint, ignore_errors=True)
Esempio n. 14
0
 def run_salento(c_fname=c_fname, as_fname=as_fname):
     env.run_apisan(c_fname, as_fname)
     delete_file(c_fname)
Esempio n. 15
0
 def run_san2sal(self, as_fname, sal_fname):
     self.run("SAN2SAL", as_fname, sal_fname, self.as2sal + "-i %s -o %s",
              as_fname, sal_fname)
     if not Run.APISAN in self.args.keep:
         delete_file(as_fname)