python 3.x – Simple folder/file lock handler

I have two python scripts (or one bash script + one python script) which are working independently from another, one script puts data into a folder, the other one reads from it.

Since I don’t want to accidentally read from the folder at the same time it’s updated, I created this simple folder lock using a dummy file.

In the bash script a similar thing is done, by checking whether that lock.file exists, and if not create it, put the current PID into it, and after it’s done delete it.

import os
import sys
import time
import logging

LOGGER = logging.getLogger()

class SimpleFolderLock():
    def __init__(self, folder_path, time_out=5):
        self._folder_to_lock = folder_path
        self._lock_file_path = os.path.join(self._folder_to_lock, "folder.lock")
        self._lock_time_out = time_out
        
    def __enter__(self):
        start_time = time.time()
        while os.path.exists(self._lock_file_path):
            LOGGER.debug(f"Waiting for {self._lock_file_path} to clear")
            time.sleep(5)
            if (time.time() - start_time) > self._lock_time_out:
                LOGGER.error(
                    f"Time-out for waiting for {self._lock_file_path} to clear")
                raise TimeoutError(f"Can't lock {self._folder_to_lock} after {self._lock_time_out} seconds")
    
        LOGGER.debug(f"Locking with {self._lock_file_path}")
        with open(self._lock_file_path, "w") as fn:
            fn.write(str(os.getpid()))
            
        return self._lock_file_path
    
    def __exit__(self, type, value, traceback):
        try:
            LOGGER.debug(f"Clearing {self._lock_file_path}")
            os.remove(self._lock_file_path)
        except FileNotFoundError:
            LOGGER.error(f"File {self._lock_file_path} already got deleted")

And for a simple test:

# Just some dummy logging configuration
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
test_folder = "/tmp/test_dir/"
os.makedirs(test_folder, exist_ok=True)
with SimpleFolderLock(test_folder) as lock:
    lock_path = lock
    LOGGER.info(f"Locked with {lock_path}")
    assert os.path.exists(lock_path)
    fiddle_around_in_folder(test_folder)

    
assert not os.path.exists(lock_path)

repair – How do I disassemble FujiFilm X-T1 camera to disable the ISO dial lock button protection?

How to disassemble / take apart Fujifilm X-T1 and how to disable ISO Dial lock switch.

It is relatively easy to take apart FujiFilm X-T1. You only need one type of screwdriver, although the screws themselves are different. So as usual, make sure to have several small containers and a piece of paper to write down/draw where a particular screw came from. It took me less than half an hour. And I had no idea what I was doing. Took longer to document the process ūüôā

The way the camera opens is, the top part comes loose and the back slides out, separating from the front. WARNING: DO NOT remove the top first. Just unscrew it and notice that it is loose and leave it be. The reason is that it has several short ribbon (film) cables, and if you lift it up first, you’re likely to disconnect or mis-align them, or worse!

So first remove all the screws that I will mention below in diagrams, then slide the back side out, and the then the top will easily lean back. The back side is preventing it from leaning back. Here in this photo you can see that I tried it at first without removing the backside:
enter image description here

As you’re removing the back side make sure to support the top so that it doesn’t fall, pulling on the cables. Back side also has ribbon cables but it’s much more easy to handle, they’re only attached towards the bottom (see image below).

So if you do everything right, no need to unplug any of the cables. If you’ve ever dealt with ribbon cables you know why I’m stressing this part so much. They and the ports they go into are very fragile and difficult to handle. Tweezers can scratch them and even cut the connection. (Here they’re not that big of a deal as in Fuji lenses I opened previously, but still why bother if you don’t have to right?)

So, lets do it step by step. 

1) Bottom: there are 8 screws – unscrew all

2) Front: only 1 (small) screw on the right side, peal off the rubber covering.enter image description here

Left/Right is when facing the camera.

3) Left: 2 above, 2 below (all behind the rubber)

4) Right: 2 above (behind the rubber) and one behind the USB door

5) Back side: Lift up the viewfinder rubber guard to reveal 2 screws, and flip up the LCD to reveal two screws there: enter image description here

The lower part behind the LCD has 4 more screws. Those ones only hold the LCD in place. Don’t remove them.

6) Once these screws are removed the camera can come apart. Again, don’t pull on the top part. Slide the back side out as in this diagram:enter image description here

enter image description here

It comes apart this way, so the middle sides belong to the front part. That’s why in the previous pic I’m pushing the top edge out with my index fingers and bottom with my thumbs.enter image description here

7) And it opens up like thisenter image description here

8) Now with the back cover down, the top part can be leaned backwards easily:enter image description here

9) And you have access to the ISO dial mechanism. Just unscrew that middle screw and follow the diagram to remove the evil silver ring. It actually snaps on/out. So if you ever wanted to put it back, it’s a possibility.enter image description here

10) And here’s how it looks like inside. enter image description here

Before doing this, I was wondering if there’s a spring inside etc, and that afterwards there would be a hole I would need to seal… but none of that!
Once you take out the ring and close up the camera, the button is still in place and presses down like before, except it has ZERO effect now! The ISO dial still clicks as you rotate it, same as before. It’s like the magic fairy came and took only this silly ISO dial lock “feature” away without any side effects, with an exception of course, that you actually have to do it all yourself ūüėČ

mysql – Can two transactions lock rows in the same table in parallel?

I am trying to grasp the concept of MySQL locks and came across this section in the docs: https://dev.mysql.com/doc/refman/8.0/en/innodb-locks-set.html:

A locking read, an UPDATE, or a DELETE generally set record locks on every index record that is scanned in the processing of the SQL statement. It does not matter whether there are WHERE conditions in the statement that would exclude the row. InnoDB does not remember the exact WHERE condition, but only knows which index ranges were scanned.

That leads me to the conclusion that two transactions can never lock rows in the same table in parallel. Let me explain it on the following example:

Assume I have the following table:

CREATE TABLE t1 (id INT, PRIMARY KEY (id)) ENGINE = InnoDB;

Further, lets assume I have 7 rows inserted. This is the balanced tree of the primary key:

enter image description here

Now assume we lock row with id 3:

SELECT * FROM t1 WHERE id=3 FOR UPDATE

From the above paragrph

A locking read (..) set record locks on every index record that is scanned in the processing of the SQL statement.

I would guess that there is an exclusive lock on row 3,5,10. Is that right?

If that would be the case, then

SELECT * FROM t1 WHERE id=100 FOR UPDATE

would try to set an exclusive lock on row 10, because it scanned in the process to find 100. But that means, it has to wait for the other transaction to be finished.

This implies, any transaction that locks a row in table t1 will always first lock row with id 10. Thus, its not possible that two transactions lock two different rows in parallel.

My questions are:

  • Is that actually true?
  • How can I actually see the balanced index tree, so I can do some local testing?

oracle – ODI remove auto lock when object / datastore is opened

I was being stupid and enabled automatically lock object during edit / opened when I was asked the first time.

Now I want to remove the automatically lock from GUI but I couldn’t seem to find it.
I googled with such topics

https://www.appservgrid.com/documentation111/docs/fmw11g1114documentation/integrate.1111/e12643/appendix_b.htm

https://odielt.wordpress.com/tag/odi-studio-settings/

So I searched userpref.xml inside the hard drive, I do find one userpref.xml.

I opened the file and inside it’s just

<?xml version="1.0" encoding="UTF-8" ?>
<UserPreferences xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.oracle.com/odi/userprefs UserPref.xsd"
                 xmlns="http://www.oracle.com/odi/userprefs">
  <Preference>
    <AcessibilitySetting>
      <Screenreader>false</Screenreader>
      <HighContrast>false</HighContrast>
      <LargeFonts>false</LargeFonts>
    </AcessibilitySetting>
    <UserName>None</UserName>
    <Repository>
      <Name>None</Name>
      <GUID>None</GUID>
    </Repository>
  </Preference>
</UserPreferences>

What can I do to make it not locking up anymore each time I open an object? If it can be done through GUI then it’s perfect if not, what should I do inside the XML file?

Thanks in advance for any help and advices.

keyboard – Switching layouts with Caps Lock doesn’t work if Caps Lock was pressed right after another key

I’m on macOS Big Sur 11.2.1 and I’m experiencing a weird behavior of Caps Lock as the layout switcher.

Caps Lock can be used for switching layouts by turning the Use Caps Lock key to switch layout setting in Settings > Keyboard.

It’s working fine, except when Caps Lock is pressed right after another key, e.g. Backspace.

Is the issue reproducible for someone else? Is it the macOS issue or the issue of my setup? If it’s the macOS issue, could it be fixed somehow?

Thanks!

iphone – Does restoring a local backup using iTunes enable activation lock?

Let’s say I have an Apple ID that’s been disabled/locked for security reasons.

  • This account is linked to my iPhone and in order to remove it or to simply disable find my iPhone, I have to answer the security questions on the account which I don’t remember anymore. So basically the account is glued to the device with FMI enabled.
  • So then I get a new phone and restore a local iTunes backup that I made of the old phone with the account issue on to the new phone.

FMI shows as enabled on the new phone’s settings but I am not sure it’s actually enabled. IMEI checker sites such as imeipro show that it’s disabled but I want to get your opinion on the matter. I want to format the new phone and worry it too will become activation locked.

Does restoring a local backup using iTunes enable activation lock?