Skip to main content

Creating binary patch files containing changes in the directory

Project description

Creating binary patch files containing changes in the directory

Simple usage :

from stateman import GetState, GetDiff, CreatePatch, ApplyPatch
import os
import shutil
patch_file="new.patch"

def write_file(filename, text):
    with open(filename,'w') as f:
        f.write(text)


# Clear workspace
if os.path.exists(".tmp"):
    shutil.rmtree(".tmp")
if os.path.exists(".tmp2"):
    shutil.rmtree(".tmp2")


# Init workspace .tmp with one subfolder
root_dir = os.path.abspath(".tmp")
target_dir = os.path.abspath(".tmp2")
os.makedirs(".tmp")
subfolder = os.path.join(root_dir,".git")
os.makedirs(subfolder)

# Create files in workspace
write_file(os.path.join(subfolder,"testfile1.txt"), "test1")
write_file(os.path.join(subfolder,"testfile2.txt"), "test2")
write_file(os.path.join(root_dir,"testfile3.txt"), "test3")
write_file(os.path.join(root_dir,"testfile4.txt"), "test4")

# Get state of workspace
state1 = GetState(root_dir, exclude = r'.git'+os.path.sep)
print(state1)

# Make a copy of workspace
shutil.copytree(root_dir, target_dir)

# Make a changes
os.remove(os.path.join(subfolder,"testfile2.txt"))
os.remove(os.path.join(root_dir,"testfile4.txt"))
write_file(os.path.join(subfolder,"testfile2.txt"), "changed")

# Get state of workspace after changes
state2 = GetState(root_dir, exclude = r'.git'+os.path.sep)
print(state2)

# Get diff between spaces
diff = GetDiff(state1, state2)
print (diff)

# Create patch file
CreatePatch(root_dir, patch_file, diff)  

# Apply patch on copy of workspace, maked before changes
ApplyPatch(target_dir, patch_file, exclude = r'.git'+os.path.sep)

# Copy of workspace after apply patch identical as workspace 

Source Code:

Travis CI Deploys:

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

stateman-1.2.1.tar.gz (3.4 kB view hashes)

Uploaded Source

Built Distribution

stateman-1.2.1-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

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