The code in the main.py
is a Python script designed to synchronize the contents of a source directory with a destination directory. The script offers command-line arguments for flexibility, allowing users to specify paths for source and destination directories, define a log file location, and set a synchronization period.
destination folder
.os
: For interacting with the operating system.filecmp
: To compare files in source
and destination
directories.shutil
: For copying files.argparse
: For handling command-line arguments.logging
: To implement logging of synchronization activities (copy/remove/update).signal
: For handling process signals and graceful termination.hashlig
: For generating hashes from each files in the source directory
.time
: For delaying regular synchronization.stat
: For changing mode in files.unittest
, paytest
: For different types of testing.tempfile
: For createting temporary files and directories for testing needs.When file explorers count the memory size of hidden files and folders, they typically follow a set of steps that involve file system interactions and specific rules about what constitutes a "hidden" file. Hereβs a detailed explanation of how this process generally works:
File explorers interact with the file system to gather information about files and directories. The file system keeps track of various attributes of each file, including:
Hidden files are usually identified based on certain attributes. The criteria for what constitutes a hidden file can vary between operating systems:
.hiddenfile
is a hidden file, while hiddenfile
is not.When counting the size of files in a directory, file explorers typically do the following:
For each file or folder that the explorer encounters, it will perform the following steps:
After traversing the entire directory structure and calculating the sizes, the explorer will typically display the total size of the selected directory and its contents, which can include both visible and hidden files, depending on user settings. If the user has opted to show hidden files, those sizes will be included in the total. If not, they will be omitted.
File explorers often do not include hidden files and folders in their calculations of memory size for a few key reasons related to user experience, system performance, and the nature of hidden files themselves. Based on this we can make a conclusion that we can't see visually a whole memory size of the some folders in the file explorers (Windows, Linux, MacOS) even when we copy them. The original and copy looks the same, but in case copying via programming code operations we can run into a strange difference in memory size of original and a copy of the same folder.
First ensure you have installed Python and pip on your system.
1. Upgrade your pip
python -m pip install --upgrade pip
2. Clone repo
git clone https://github.com/Nazar-Pichak/One_Way_Folder_Sync.git
3. Navigate into your git repo
cd >> cloned repo
4. Create virtual environment
python -m venv env
5. Activate virtual environment
.venv\Scripts\activate
source .venv/bin/activate
6. Install dependencies into virtual environment
python -m pip install -r requirements.txt
7. Run the code
src
directory:
cd >> src\
python main.py
\xxx\src> python main.py "C:\Users\your_name\Desktop\source_dir" "C:\Users\your_name\Desktop\dest_dir" "C:\Users\your_name\Desktop\logfile.log" 10
**8. Results**
<picture>
<source srcset="https://fonts.gstatic.com/s/e/notoemoji/latest/1f680/512.webp" type="image/webp">
<img src="https://fonts.gstatic.com/s/e/notoemoji/latest/1f680/512.gif" alt="π" width="38" height="38">
</picture>
<picture>
<source srcset="https://fonts.gstatic.com/s/e/notoemoji/latest/1f680/512.webp" type="image/webp">
<img src="https://fonts.gstatic.com/s/e/notoemoji/latest/1f680/512.gif" alt="π" width="38" height="38">
</picture>
<picture>
<source srcset="https://fonts.gstatic.com/s/e/notoemoji/latest/1f680/512.webp" type="image/webp">
<img src="https://fonts.gstatic.com/s/e/notoemoji/latest/1f680/512.gif" alt="π" width="38" height="38">
</picture>
So now, when our **rocket** is running, it will create an identical copy of the `source directory` into previous desired place. As this is one way synchronization, we have to assume that `source directory` is a **main** directory.
To see general functionality, make any changes to the `source directory` by creating, deleting, updating files and folders while script is running with infinit loop and follow changes into `destination directory`. We have to see that both directories are the same. However when you try to change content of the `destination directory`, it will be overwritten corresponding to the content of the `source directory`.
## Contribution guide <picture> <source srcset="https://fonts.gstatic.com/s/e/notoemoji/latest/270f_fe0f/512.webp" type="image/webp"> <img src="https://fonts.gstatic.com/s/e/notoemoji/latest/270f_fe0f/512.gif" alt="β" width="32" height="32"> </picture>
Contributions and improvements are wellcome as well. Please feel free to report any [issues](https://github.com/Nazar-Pichak/One_Way_Folder_Sync/issues) and bugs as this project is completely new. Improvments are still being made.
You can also take this script as a basis for implementing **two way folder synchronization** for example, and create a new branch with more complex functionality.
**1. Make your king Fork πππ**
- This will create a copy of the repository in your GitHub account
![alt text](images/img_2.png)
**2. Clone Forked repository localy πππ**
git clone https://github.com/your_name/One_Way_Folder_Sync.git
**3. Set Up the Upstream Remote**
- Navigate into the project:
cd >> cloned repo
- Add the original repository as an upstream remote to keep your fork in sync:
git remote add upstream https://github.com/Nazar-Pichak/One_Way_Folder_Sync.git
This allows you to keep your forked repository up-to-date with the latest changes from the main repository.
- Verify remotes with:
git remote -v
**4. Setting up the local environment**
python -m venv env
**5. Activate virtual environment**
- For Windows:
.venv\Scripts\activate
- For Unix/macOS:
source .venv/bin/activate
**6. Install dependencies into virtual environment**
python -m pip install -r requirements.txt
**7. Create a new branch**
- Before making any changes, create a new branch to keep your work organized and isolated.
- Run command:
git checkout -b your_branch_name
**8. Add your Python improvements to the code or documentation**
![wired-lineal-1322-python-code-language-hover-pinch (1)](https://github.com/user-attachments/assets/a14a3609-f02b-4aac-bbe7-d081c505672c)
![wired-lineal-1322-python-code-language-hover-pinch (1)](https://github.com/user-attachments/assets/a14a3609-f02b-4aac-bbe7-d081c505672c)
![wired-lineal-1322-python-code-language-hover-pinch (1)](https://github.com/user-attachments/assets/a14a3609-f02b-4aac-bbe7-d081c505672c)
![wired-lineal-1322-python-code-language-hover-pinch (1)](https://github.com/user-attachments/assets/a14a3609-f02b-4aac-bbe7-d081c505672c)
**9. Run tests and ensure all works fine without any breakdowns**
- Navigate into `test` directory:
cd test\
- Run test itself by invoking `pytest` framework:
pytest -v
10. If all tests are passed β β β β then create commits and push changes
git add .
git commit -m "Description of changes made"
git push origin your_branch_name
11. Open your king Pull Request πππ
12. Congratulations, you have done it.
© 2024 by Nazar Pichak.