Commit 5fc367ad4674717f37eb01157192ca0fdd758614

Authored by Göran Krampe
1 parent 08abcf1d

Renamed to blimp, support for conf file in git root.

Showing 2 changed files with 69 additions and 48 deletions
.gitignore 0 → 100644
  1 +blimp
  2 +nimcache
... ...
blimp.nim
1 1 import md5, os, osproc, parseopt2, strutils, parsecfg, streams
2 2  
3   -# This is a little utility program for handling large files
  3 +# blimp is a little utility program for handling large files
4 4 # in git repositories. Its inspired by git-fat and s3annex
5   -# but doesn't rely on S3 for storage.
  5 +# but doesn't rely on S3 for storage, is a single binary without
  6 +# need for Python, and has less features than git-fat. So far.
6 7 #
7   -# Use "fat d mybigfile" to deflate it before commit.
8   -# Use "fat i mybigfile" to inflate it back to original size.
  8 +# Manual use:
  9 +#
  10 +# Use "blimp d mybigfile" to deflate it before commit.
  11 +# Use "blimp i mybigfile" to inflate it back to original size.
9 12 #
10 13 # When deflated the file only has an md5sum string inside it.
11 14 #
12 15 # The file is copied over into:
13   -# <homedir>/fatstore/<originalfilename>-<md5sum>
  16 +# <homedir>/blimpStore/<originalfilename>-<md5sum>
  17 +#
  18 +# Configuration is in:
  19 +# <gitroot>/.blimp.conf
  20 +# ~/blimpstore/.blimp.conf
14 21  
15   -var fatstore, remoteFatstore: string
  22 +var blimpStore, remoteBlimpStore: string
16 23  
17 24  
18   -# Load fatstore.conf file, overkill for now but...
  25 +# Load blimp.conf file, overkill for now but...
19 26 proc parseConfFile(filename: string) =
20 27 echo filename
21 28 var f = newFileStream(filename, fmRead)
... ... @@ -31,7 +38,7 @@ proc parseConfFile(filename: string) =
31 38 continue # Ignore
32 39 of cfgKeyValuePair:
33 40 if e.key == "remote":
34   - remoteFatstore = e.value
  41 + remoteBlimpStore = e.value
35 42 else:
36 43 quit("Unknown configuration: " & e.key)
37 44 of cfgOption:
... ... @@ -41,75 +48,87 @@ proc parseConfFile(filename: string) =
41 48 close(p)
42 49  
43 50  
44   -# Upload a file to the remote master fatstore
45   -proc uploadFile(fatfilename: string) =
46   - if remoteFatstore.isNil:
47   - echo("Remote fatstore not set in configuration file, not uploading content:\n\t" & fatfilename)
  51 +# Upload a file to the remote master blimpStore
  52 +proc uploadFile(blimpFilename: string) =
  53 + if remoteBlimpStore.isNil:
  54 + echo("Remote blimpstore not set in configuration file, not uploading content:\n\t" & blimpFilename)
48 55 return
49   - let errorCode = execCmd("rsync -a " & fatstore / fatfilename & " " & remoteFatstore)
50   - if errorCode != 0: quit("Something went wrong uploading content to " & remoteFatstore, 2)
  56 + let errorCode = execCmd("rsync -a " & blimpStore / blimpFilename & " " & remoteBlimpStore)
  57 + if errorCode != 0: quit("Something went wrong uploading content to " & remoteBlimpStore, 2)
51 58  
52   -# Download a file to the remote master fatstore
53   -proc downloadFile(fatfilename: string) =
54   - if remoteFatstore.isNil:
55   - quit("Remote fatstore not set in configuration file, can not download content:\n\t" & fatfilename)
56   - let errorCode = execCmd("rsync -a " & remoteFatstore / fatfilename & " " & fatstore / "")
57   - if errorCode != 0: quit("Something went wrong downloading " & fatfilename & " from " & remoteFatstore, 3)
58   -
59   -
60   -# Copy content to fatstore, no upload yet.
61   -proc copyToFatstore(filename, fatfilename: string) =
62   - if not existsFile(fatstore / fatfilename):
63   - copyFile(filename, fatstore / fatfilename)
64   - uploadFile(fatfilename)
65   -
66   -# Copy content from fatstore, and downloading first if needed
67   -proc copyFromFatstore(fatfilename, filename: string) =
68   - if not existsFile(fatstore / fatfilename):
69   - downloadFile(fatfilename)
70   - copyFile(fatstore / fatfilename, filename)
  59 +# Download a file to the remote master blimpStore
  60 +proc downloadFile(blimpFilename: string) =
  61 + if remoteBlimpStore.isNil:
  62 + quit("Remote blimpstore not set in configuration file, can not download content:\n\t" & blimpFilename)
  63 + let errorCode = execCmd("rsync -a " & remoteBlimpStore / blimpFilename & " " & blimpStore / "")
  64 + if errorCode != 0: quit("Something went wrong downloading " & blimpFilename & " from " & remoteBlimpStore, 3)
  65 +
  66 +
  67 +# Copy content to blimpStore, no upload yet.
  68 +proc copyToBlimpStore(filename, blimpFilename: string) =
  69 + if not existsFile(blimpStore / blimpFilename):
  70 + copyFile(filename, blimpStore / blimpFilename)
  71 + uploadFile(blimpFilename)
  72 +
  73 +# Copy content from blimpStore, and downloading first if needed
  74 +proc copyFromblimpStore(blimpFilename, filename: string) =
  75 + if not existsFile(blimpStore / blimpFilename):
  76 + downloadFile(blimpFilename)
  77 + copyFile(blimpStore / blimpFilename, filename)
71 78  
72 79  
73   -# Copy original file to fatstore and replace with hash stub in git.
  80 +# Copy original file to blimpStore and replace with hash stub in git.
74 81 proc deflate(filename: string) =
75 82 let content = readFile(filename)
76 83 if content[0..4] == "hash:":
77 84 quit("File is already deflated, ignored.", 5)
78 85 let hash = getMD5(content)
79   - let fatfilename = filename & "-" & hash
80   - copyToFatstore(filename, fatfilename)
81   - writeFile(filename, "hash:" & fatfilename)
  86 + let blimpFilename = filename & "-" & hash
  87 + copyToBlimpStore(filename, blimpFilename)
  88 + writeFile(filename, "hash:" & blimpFilename)
82 89  
83   -# Parse out hash from hash stub and copy back original content from fatstore.
  90 +# Parse out hash from hash stub and copy back original content from blimpStore.
84 91 proc inflate(filename: string) =
85 92 var hashfile: File
86 93 if not open(hashfile, filename):
87 94 quit("Could not open file: " & filename, 4)
88 95 let hashline = split(string(readLine(hashfile)), {':'})
89 96 if hashline[0] == "hash":
90   - let fatfilename = hashline[1]
  97 + let blimpfilename = hashline[1]
91 98 #removeFile(filename)
92   - copyFromFatstore(fatfilename, filename)
  99 + copyFromblimpStore(blimpfilename, filename)
93 100 else:
94   - quit("File is not a fat file.", 5)
95   -
  101 + quit("File is not a blimp file.", 5)
  102 +
  103 +# Find git root dir or fall back on current dir
  104 +proc gitRoot(): string =
  105 + try:
  106 + let tup = execCmdEx("git rev-parse --show-toplevel")
  107 + if tup[1] == 0:
  108 + result = tup[0]
  109 + else:
  110 + result = getCurrentDir()
  111 + except:
  112 + result = getCurrentDir()
96 113  
97 114 ################################ main #####################################
98 115  
99   -# Hardwired to "fatstore" directory in home dir.
100   -fatstore = getHomeDir() / "fatstore"
  116 +# Hardwired to "blimpstore" directory in home dir.
  117 +blimpStore = getHomeDir() / "blimpstore"
101 118  
102 119 # Make sure we have the dir, or create it.
103 120 try:
104   - if not existsDir(fatstore): createDir(fatstore)
  121 + if not existsDir(blimpStore): createDir(blimpStore)
105 122 except:
106   - quit("Could not create " & fatstore & " directory.", 1)
  123 + quit("Could not create " & blimpStore & " directory.", 1)
107 124  
108 125  
109   -# Parse configuration if it exists
110   -parseConfFile(fatstore / "fatstore.conf")
  126 +# Parse configuration files if they exist
  127 +parseConfFile(gitRoot() / ".blimp.conf")
  128 +parseConfFile(blimpStore / ".blimp.conf")
111 129  
112 130 # Only a command and a path as argument
  131 +# TODO: Change to lapp
113 132 let argv = commandLineParams()
114 133 let command = argv[0]
115 134 let filename = argv[1]
... ...