Welcome, guest | Sign In | My Account | Store | Cart

A basic form of version control that when used in combination with dropbox and notepad++ (optional) provides reliable backup with tags of files/folder.

Files mode can be used for individual files, in practice i used it with python files primarily. Folder mode allows for backup of folders, especially useful for a class split over various files with __init__.py in root of a folder.

Python, 113 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Basic Version Control
# Author: commentator8

# Takes the given file and tag as argument, 
# and saves a copy to run dir/VC with incremental number and tag
# Input: c:/dir1/dir2/file.py "tag"
# Saved Output: c:/dir1/dir2/VC/file_0001_tag.py
# Can add extra argument as follows -v to increment first number of series.

# files example (notepad++ F5): python c:\users\user\dropbox\txt\versionControl\versioner.py "$(FULL_CURRENT_PATH)" "Threading Working" -v
# folder example: python c:\users\user\dropbox\txt\versionControl\versioner.py "C:\Users\user\Dropbox\txt\Downloader" "Initial backup"

import os, sys
import time
import shutil
import zipfile

def list_dir(path, zip):
    files_to_zip = []
    for root, dirs, files in os.walk(path):
        if root.split('\\')[-1] in ['index']:
            continue
            
        for file in files:
            if file.split('.')[-1] != 'zip' or root.split('\\')[-1] != 'VC':
                files_to_zip.append(os.path.join(root, file))
            
    return files_to_zip

version_up = False
if '-v' in sys.argv:
    version_up = True
    sys.argv.remove('-v')

if len(sys.argv) != 3:  
    print 'Wrong number of arguments passed in. Please try again.'
    print sys.argv
    time.sleep(4)
    exit()    

file = sys.argv[1].replace('\'"', '')
file_tag = sys.argv[2].replace('\'"', '')

file_name = os.path.split(file)[-1].split('.')[0]
file_ext = os.path.split(file)[-1].split('.')[-1]

if file_name == '' or os.path.isdir(file):
    folder_name = os.path.split(file.strip('\\'))[-1]
    if file[-1] not in ['\\', '/']:
        file += '\\'
    folder = file

if '/' not in file and '\\' not in file:
    print 'Check if this is a file passed in'
    time.sleep(4)
    exit()

vc_path = os.path.split(file)[0]  + '/VC/'

highest_ver = 0
for dirname, dirnames, filenames in os.walk(vc_path):
    for f in filenames:
        file_name_vc = f.partition('_')[0]
        number = (f.partition('_')[-1]).partition('_')[0]
        tag = ((f.partition('_')[-1]).partition('_')[-1]).partition('_')[0]
        
        # allow for multiple backed up files in single dir
        if file_name:
            if file_name_vc != file_name:
                continue
        elif file_name_vc != folder_name:
            continue
        if number > highest_ver:
            highest_ver = int(number)
            

    
series_number = (str(highest_ver + 1).zfill(4))  
            
if version_up:
    series_number = str(int(series_number[0]) + 1) + '001'

if os.path.isdir(file):
    print 'Copy folder:\n"%s"\n\nto destination:\n"%s"\n\nwith version:\n"%s"\n\nand tag:\n"%s"\n' % (folder_name, vc_path, series_number, file_tag)
else:    
    print 'Copy file:\n"%s"\n\nto destination:\n"%s"\n\nwith version:\n"%s"\n\nand tag:\n"%s"\n' % (file_name, vc_path, series_number, file_tag)

answer = raw_input('\nDo you want to continue?\n')

if answer.lower() in ['y', 'yes']:
    pass
else:
    exit()

if not os.path.exists(vc_path):
    os.makedirs(vc_path)
    
if os.path.isdir(file):
    new_file = vc_path + folder_name + '_' + series_number + "_" + file_tag + '.zip'
    zip = zipfile.ZipFile(new_file, 'w')
    files_to_zip = list_dir(folder, zip)

    for i in files_to_zip:
        zip.write(i, arcname = i.replace(folder.rpartition(folder_name)[0], ''))  
             
    zip.close()
else:
    new_file = vc_path + file_name + '_' + series_number + "_" + file_tag + '.' + file_ext     
    shutil.copyfile(file, new_file)

print     
print 'All Done'      
time.sleep(2)

2 comments

Paul Campbell 10 years, 8 months ago  # | flag

Have you looked at some of the other Source Code Control Systems?

CVS is pretty old but easy to use

Subversion was a big improvement. I used it for a number of years. It may also fit your needs.

For some time Git ( and Mercurial + others) have captured most current development. These products are are used extensively in the FOSS and in the commercial marketplace. If you work with a team, particulary where members are remote, it helps to insure that shared code is safely stored and available for frequent updates and downloads.

Take a look at sites like Github.com or Beanstalk.com they are free and very well documented.

There are free repositories, such as GitHub, BeanStalk and numerous others. These

commentator8 (author) 10 years, 8 months ago  # | flag

Hi Paul,

Thanks, but yes, i am aware of all of the above, and use them infrequently. Anything of project size, or work related inevitably involves mercurial or SVN etc, but for personal projects coded up at home in notepad++... i found the above script useful. Not for collaborative purposes, but rather purely for version control.

To that end, i think it might come in use (generally in combination with dropbox, admittedly) for the occasional casual coder.