Skip to main content

Library for synchronization two folders

Project description

Synchronization folders package

Hello everyone! This is the repository of my package on Python "sync-folders".

Table of contents

Motivation

Someday I made the script, that synchronizes two folders by date of theirs files. After time I've wanted to create package on PyPi. Also I've added some new features for work with files, in addition to syncing.

Build status

Here you can see build status of continuous integration/continuous deployment:

Python package Lint Code Base Upload Python Package PyPI - Downloads

Code style

I'm using Codacy for automate my code quality.

Codacy Badge

Dependencies

You can see all dependencies from requirements.txt here.

Features

With my package you can sync two folders, manage logs files, delete empty folders and old files, read and create zip-archives.

Installation

First install Python.

If you don't have pip, install it.

Then type in terminal:

pip install sync-folders --upgrade

Importing

# Import module
from sync_folders import main, purgelog, cleaner

Fast usage

Example of usage this module

Synchronization

main.sync('./test_a', './test_b')
# Expected creation of `logs.txt`

Files output

main.files('./')
""" Expected output

.gitattributes   Last Modified: 18 Jun 2020, 08 52
.gitignore       Last Modified: 10 Jun 2020, 06 39
CONTRIBUTING.md  Last Modified: 23 Jun 2020, 12 26
LICENSE  Last Modified: 09 Jun 2020, 18 10
README.md        Last Modified: 23 Jun 2020, 18 20
requirements.txt         Last Modified: 10 Jun 2020, 15 00
setup.cfg        Last Modified: 10 Jun 2020, 07 08
setup.py         Last Modified: 21 Jun 2020, 14 03
_config.yml      Last Modified: 10 Jun 2020, 09 28
"""

Dirs output

main.dirs('./tests')
""" Expected output

.venv
test_a
test_b
"""

Reading the file data

print(main.read_file('./index.py'))
""" Expected output

from sync_folders import main, purgelog, cleaner

print(main.read_file('./index.py'))
"""

Write in the file

Not an appending

main.write_file('./test.txt', 'your text')

List of the dirs

main.list_dir('./')
""" Expected result

['.git', '.github', '.venv', 'sync_folders', 'tests', 'util']
"""

List of the files

main.get_files('./')
""" Expected result

[
  {'name': 'python-package.yml', 'date': 1592564708.6109703, 'date_str': '19 Jun 2020, 11 05'}, 
  {'name': 'python-publish.yml', 'date': 1591772746.2324488, 'date_str': '10 Jun 2020, 07 05'}
]
"""

Read zip-archive

main.read_zip('./test.zip')
""" Expected output

test_a/
            Compress size: 0.0 in KB
            Filesize: 0.0 in KB
test_a/test.docx
            Compress size: 8.490234375 in KB
            Filesize: 11.1572265625 in KB
test_b/
            Compress size: 0.0 in KB
            Filesize: 0.0 in KB
"""

List of the elements in archive

main.files_in_zip('test.zip')
""" Expected result

['test_a/', 'test_a/test.docx', 'test_b/']
"""

Extract element from archive

main.extract('test.zip', 'test_a/test.docx')
# Expected creation of `test_a/test.docx`

Create archive

main.create_zip(['_config.yml', 'LICENSE', 'setup.py'], './test.zip')
# Expected creation of `test.zip`

Cleaner

cleaner.cleaner(['./test_a', './test_a/test_c', './test_b'], 5)
# Expected creation of `logs.txt`
""" Expected output

START TIME: Tue Jun 23 22:01:00 2020
Total deleted size: 0.0 MB
Total deleted files: 0
Total deleted empty folders: 3
FINISH TIME: Tue Jun 23 22:01:00 2020
"""

Purgelog

purgelog.purgelog('./logs.txt', 5, 2)
""" Expected output
Copied: logs.txt to 1_logs.txt
"""
# Expected creation of `1_logs.txt`

API

main.sync( path_a, path_b )

Name Type Argument Default Description
path_a string <required> None the path to the directory
path_b string <required> None the path to the directory

main.files( path )

Name Type Argument Default Description
path string <required> None the path to the directory

main.dirs( path )

Name Type Argument Default Description
path string <required> None the path to the directory

main.read_file( path )

Name Type Argument Default Description
path string <required> None the path to the file

main.write_file( path, data )

Name Type Argument Default Description
path string <required> None the path to the file
data string <required> None the content

main.list_dir( path )

Name Type Argument Default Description
path string <required> None the path to the directory

main.get_files( path )

Name Type Argument Default Description
path string <required> None the path to the directory

main.read_zip( path )

Name Type Argument Default Description
path string <required> None the path to the archive

main.files_in_zip( path )

Name Type Argument Default Description
path string <required> None the path to the archive

main.extract( path_to_zip, path_to_file )

Name Type Argument Default Description
path_to_zip string <required> None the path to the archive
path_to_file string <not required> None the path to the need file for extracting

main.create_zip( files, path)

Name Type Argument Default Description
files list <required> None the list of pathes to the files for archiving
path string <required> None the path to the new archive

cleaner.cleaner( folders, limit )

Name Type Argument Default Description
folders list <required> None the array of the folders
limit int <required> None the limit of the days for comparing

purgelog.purgelog( log-file, limit, number )

Name Type Argument Default Description
log-file string <required> None the path to the log-file
limit int <required> None the limit of the maximum memory
number int <required> None the number of the maximum available number of the logs file

Code Example

Here you can see the function, that syncs two folders

# Main function for synchronize two folders
def sync(path_a=None, path_b=None):
    if not path_a or not path_b:
        raise NameError('Required path to both dirs')
    logs = ''
    files_in_a = get_files(path_a)
    files_in_b = get_files(path_b)
    same_files = []
    for file_a in files_in_a:
        for file_b in files_in_b:
            if file_b['name'] == file_a['name']:
                # compare dates
                if file_b['date'] < file_a['date']:
                    # change
                    shutil.copy2(path_a + '/' + file_a['name'], path_b)
                    logs += f"Change {file_a['name']} in {path_b}" + '\n'
            same_files.append(file_b['name'])
    for file_a in files_in_a:
        if not file_a['name'] in same_files:
            # move to b
            shutil.copy2(path_a + '/' + file_a['name'], path_b)
            logs += f"Create {file_a['name']} in {path_b}" + '\n'

    write_file('./logs.txt', logs)

Tests

Unit-testing with pytest:

:smile:I give you the link to GitHub Actions, where you can see all my tests.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Also look at the CONTRIBUTING.md.

Credits

Links to videos and articles which helped me to build this project:

License

GitHub

MIT © mezgoodle

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sync-folders-1.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sync_folders-1.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file sync-folders-1.1.0.tar.gz.

File metadata

  • Download URL: sync-folders-1.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for sync-folders-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b16066be74b3677b0fd2d76b6de395d8817e797e3eaf4addca75a64489437635
MD5 d3bbcf69b573846996ab17c2dcbab099
BLAKE2b-256 4be4be6f610468c0b5842b499734a95827d6335cead9a666cba994c6ca9e5faf

See more details on using hashes here.

File details

Details for the file sync_folders-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: sync_folders-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for sync_folders-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae26da08137bfdb77a34779224e3f4ebbc4971f3a46c8e7e4e51cf336a9b5611
MD5 a0927286ee4a1877524de5304c53fa1e
BLAKE2b-256 1f72107693721b48021c77a28adf0cf7d43f5d19020efbb73230d27095d2c114

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page