Blame view

blimp.nim 19.7 KB
3e2f4c14   Göran Krampe   Added upload comm...
1
  import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp, subexes, tables, sets
74272cdb   Göran Krampe   First commit
2
  
5fc367ad   Göran Krampe   Renamed to blimp,...
3
  # blimp is a little utility program for handling large files
74272cdb   Göran Krampe   First commit
4
  # in git repositories. Its inspired by git-fat and s3annex
6884d534   Göran Krampe   Fixed unixy path ...
5
  # but doesn't rely on S3 for storage - it uses scp but
ba6047f5   Göran Krampe   Added areas, move...
6
  # performs the file operations using externally configured commands.
aaf9bcef   Göran Krampe   Upped to version ...
7
  # It is a single binary without any dependencies.
74272cdb   Göran Krampe   First commit
8
  #
ba6047f5   Göran Krampe   Added areas, move...
9
10
11
12
  # It can also keep track of multiple remote areas and do up/downloads
  # independent from git. This is useful in build scripts, up and
  # downloading artifacts.
  #
3e2f4c14   Göran Krampe   Added upload comm...
13
  # Just run blimp --help for detailed help.
dab0c5b0   Göran Krampe   Added --version, ...
14
  
33f6041b   Andreas Rumpf   make blimp compil...
15
  const
dab0c5b0   Göran Krampe   Added --version, ...
16
    versionMajor* = 0
49df26ea   Göran Krampe   Updated for new Nim
17
    versionMinor* = 5
386557e6   Göran Krampe   Added support for...
18
    versionPatch* = 0
dab0c5b0   Göran Krampe   Added --version, ...
19
    versionAsString* = $versionMajor & "." & $versionMinor & "." & $versionPatch
74272cdb   Göran Krampe   First commit
20
  
ba6047f5   Göran Krampe   Added areas, move...
21
22
23
24
25
26
27
28
  type
    RemoteArea = ref object of RootObj
      name*: string
      url*: string
      upload*: string
      download*: string
      delete*: string
  
b9ad52ff   Göran Krampe   Added default con...
29
  var
ba6047f5   Göran Krampe   Added areas, move...
30
    blimpStore, blimpVersion: string = nil
78569137   Göran Krampe   Added directory e...
31
    homeDir, currentDir, gitRootDir: string
ec7d9613   Göran Krampe   Added option --fi...
32
    verbose, stdio, onAllDeflated, onAllFiltered: bool
ba6047f5   Göran Krampe   Added areas, move...
33
34
35
    stdinContent, area: string = nil
    areas = newTable[string, RemoteArea]()
    remoteArea: RemoteArea
74272cdb   Göran Krampe   First commit
36
  
6884d534   Göran Krampe   Fixed unixy path ...
37
  let defaultConfig = """
ba6047f5   Göran Krampe   Added areas, move...
38
39
  [blimp]
  # Minimal version, otherwise stop
7d1929db   Göran Krampe   Added version in ...
40
  version = """ & versionAsString & """
6884d534   Göran Krampe   Fixed unixy path ...
41
42
43
44
  #
  # Set your local blimpstore directory. You can use %home%,
  # %cwd% and %gitroot% in paths, works cross platform.
  #
78569137   Göran Krampe   Added directory e...
45
46
47
  # Example:
  #   # Place it inside the git clone
  #   blimpstore = "%gitroot%/.git/blimpstore"
ba6047f5   Göran Krampe   Added areas, move...
48
  #
78569137   Göran Krampe   Added directory e...
49
50
51
52
53
  #   # Place it in current working directory (not very useful)
  #   blimpstore = "%cwd%/blimpstore"
  #
  # Default:
  #   # Place it in the users home directory
6884d534   Göran Krampe   Fixed unixy path ...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  blimpstore = "%home%/blimpstore"
  
  
  [remote]
  # The "remote" area represents the shared remote blimpstore.
  # This area is the default area used unless --area is used.
  url = "blimp@build.3dicc.com:/var/opt/blimpstore"
  
  # On Windows we rely on msysgit that includes scp.
  # On XP, make sure to put your ssh keys
  # in c:\Program Files\git\.ssh (slightly odd place)
  # On Win7 it uses c:\Users\gokr\.ssh - you can check with
  # "ssh -v somehost" to see where it looks for the keys.
  #
  # When expanding the command templates below:
  #   $1 is the blimp filename
  #   $2 is the area url above
  #   $3 is the local blimpstore directory
eb67d6ae   Göran Krampe   Added compression...
72
73
  upload = "scp -pqC '$3/$1' '$2/'"
  download = "scp -pqC '$2/$1' '$3/'"
6884d534   Göran Krampe   Fixed unixy path ...
74
75
76
77
78
79
  
  
  [release]
  # This area can be used with upload/download commands and --area option.
  # -r makes it possible to give directories as arguments for recursive handling.
  url = "blimp@build.3dicc.com:/var/opt/release"
eb67d6ae   Göran Krampe   Added compression...
80
81
  upload = "scp -rC '$3/$1' '$2/'"
  download = "scp -rC '$2/$1' '$3/'"
6884d534   Göran Krampe   Fixed unixy path ...
82
83
  """
  
386557e6   Göran Krampe   Added support for...
84
85
  
  proc cmd(cmd: string): string =
78569137   Göran Krampe   Added directory e...
86
    try:
ba6047f5   Göran Krampe   Added areas, move...
87
88
89
90
      # Otherwise pipes will not work for git commands etc
      when defined(windows):
        let tup = execCmdEx("cmd /c \"" & cmd & "\"")
      else:
33f6041b   Andreas Rumpf   make blimp compil...
91
        let tup = execCmdEx(cmd)
ba6047f5   Göran Krampe   Added areas, move...
92
      #echo "cmd: " & $cmd & "err:" & $tup[1]
78569137   Göran Krampe   Added directory e...
93
94
95
96
97
98
99
      if tup[1] == 0:
        result = strip(tup[0])
      else:
        result = nil
    except:
      result = nil
  
543fb248   Göran Krampe   Added deinit to r...
100
  # Various git commands
386557e6   Göran Krampe   Added support for...
101
102
  proc gitRoot(): string =
    cmd("git rev-parse --show-toplevel")
386557e6   Göran Krampe   Added support for...
103
104
  proc gitConfigSet(key, val: string) =
    discard cmd("git config " & $key & " " & $val)
543fb248   Göran Krampe   Added deinit to r...
105
106
  proc gitConfigUnset(key: string) =
    discard cmd("git config --unset " & $key)
386557e6   Göran Krampe   Added support for...
107
108
109
  proc gitConfigGet(key: string): string =
    cmd("git config --get " & $key)
  
386557e6   Göran Krampe   Added support for...
110
111
112
  # Ensure the filter is set
  proc ensureBlimpFilter() =
    if gitConfigGet("filter.blimp.clean") != "":
543fb248   Göran Krampe   Added deinit to r...
113
114
      gitConfigSet("filter.blimp.clean", "\"blimp -s d %f\"")
      gitConfigSet("filter.blimp.smudge", "\"blimp -s i %f\"")
386557e6   Göran Krampe   Added support for...
115
  
543fb248   Göran Krampe   Added deinit to r...
116
117
118
119
120
  # Ensure the filter is set
  proc removeBlimpFilter() =
    if gitConfigGet("filter.blimp.clean") != "":
      gitConfigUnset("filter.blimp.clean")
      gitConfigUnset("filter.blimp.smudge")
386557e6   Göran Krampe   Added support for...
121
  
78569137   Göran Krampe   Added directory e...
122
123
124
125
126
  # Simple expansion of %home%, %cwd% and %gitroot%
  proc expandDirs(templ: string): string =
    result = templ.replace("%home%", homeDir)
    result = result.replace("%cwd%", currentDir)
    if result.contains("%gitroot%"):
33f6041b   Andreas Rumpf   make blimp compil...
127
      if gitRootDir.isNil: quit("Not in a git clone, can not expand %gitroot% in '" & templ & "'")
78569137   Göran Krampe   Added directory e...
128
129
      result = result.replace("%gitroot%", gitRootDir)
  
ce54e838   Göran Krampe   Fix broken merge,...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  # Copy of readAllBuffer from sysio.nim - we need to call it
  # instead of readAll(stdin) which doesn't work on Windows. readAll
  # first tries to find file size, and that trips up Windows.
  proc readAllX(file: File): string =
    result = ""
    var buffer = newString(4096)
    while true:
      var read = readBuffer(file, addr(buffer[0]), 4096)
      if read == 4096:
        result.add(buffer)
      else:
        buffer.setLen(read)
        result.add(buffer)
        break
  
dab0c5b0   Göran Krampe   Added --version, ...
145
  # Load a blimp.conf file
74272cdb   Göran Krampe   First commit
146
  proc parseConfFile(filename: string) =
74272cdb   Göran Krampe   First commit
147
148
    var f = newFileStream(filename, fmRead)
    if f != nil:
dab0c5b0   Göran Krampe   Added --version, ...
149
      if verbose: echo "Reading config: " & filename
33f6041b   Andreas Rumpf   make blimp compil...
150
      var p: CfgParser
74272cdb   Göran Krampe   First commit
151
      open(p, f, filename)
ba6047f5   Göran Krampe   Added areas, move...
152
153
      var section: string
      var area: RemoteArea
74272cdb   Göran Krampe   First commit
154
155
156
      while true:
        var e = next(p)
        case e.kind
33f6041b   Andreas Rumpf   make blimp compil...
157
        of cfgEof:
74272cdb   Göran Krampe   First commit
158
159
          break
        of cfgSectionStart:
ba6047f5   Göran Krampe   Added areas, move...
160
          section = e.section
74272cdb   Göran Krampe   First commit
161
        of cfgKeyValuePair:
ba6047f5   Göran Krampe   Added areas, move...
162
163
164
165
          case section
          of "blimp":
            case e.key
            of "blimpstore":
33f6041b   Andreas Rumpf   make blimp compil...
166
              if blimpStore.isNil: blimpStore = expandDirs(e.value)
ba6047f5   Göran Krampe   Added areas, move...
167
168
169
170
            of "version":
              if blimpVersion.isNil: blimpVersion = e.value
            else:
              quit("Unknown blimp configuration: " & e.key)
74272cdb   Göran Krampe   First commit
171
          else:
ba6047f5   Göran Krampe   Added areas, move...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
            # Then we presume its an area
            if area.isNil or area.name != section:
              area = RemoteArea(name: section)
              areas[area.name] = area
            case e.key
            of "url":
              if area.url.isNil: area.url = expandDirs(e.value)
            of "upload":
              if area.upload.isNil: area.upload = expandDirs(e.value)
            of "download":
              if area.download.isNil: area.download = expandDirs(e.value)
            of "delete":
              if area.delete.isNil: area.delete = expandDirs(e.value)
            else:
              quit("Unknown area configuration: " & e.key)
74272cdb   Göran Krampe   First commit
187
188
189
190
191
192
        of cfgOption:
          quit("Unknown configuration: " & e.key)
        of cfgError:
          quit("Parsing " & filename & ": " & e.msg)
      close(p)
  
db95b901   Göran Krampe   Various fixes, mo...
193
  # Trivial helper to enable verbose
ba6047f5   Göran Krampe   Added areas, move...
194
  proc run(cmd: string): int =
db95b901   Göran Krampe   Various fixes, mo...
195
196
    if verbose: echo(cmd)
    execCmd(cmd)
74272cdb   Göran Krampe   First commit
197
  
6884d534   Göran Krampe   Fixed unixy path ...
198
199
200
201
202
203
204
205
206
207
208
  # Perhaps not perfect but tries to convert c:\foo\bar to /c/foo/bar
  # but only if we are on windows, otherwise we do nothing.
  proc toUnixyPath(path: string): string =
    when defined(windows):
      if path.len > 1 and path[1] == ':':
        let parts = path.split({':', '\\'})
        result = "/" & parts.join("/")
      else:
        result = path
    else:
      result = path
ba6047f5   Göran Krampe   Added areas, move...
209
210
211
212
213
  
  # Upload a file to the remote area
  proc uploadFile(filename, fromDir: string) =
    if remoteArea.isNil:
      echo("Remote area not set in configuration file, skipping uploading content:\n\t" & filename)
74272cdb   Göran Krampe   First commit
214
      return
6884d534   Göran Krampe   Fixed unixy path ...
215
    let errorCode = run(format(remoteArea.upload, filename, remoteArea.url, toUnixyPath(fromDir)))
b9ad52ff   Göran Krampe   Added default con...
216
    if errorCode != 0:
ba6047f5   Göran Krampe   Added areas, move...
217
      quit("Something went wrong uploading " & filename & " to " & remoteArea.url, 2)
33f6041b   Andreas Rumpf   make blimp compil...
218
  
ba6047f5   Göran Krampe   Added areas, move...
219
220
221
222
  # Download a file to the remote area
  proc downloadFile(filename, toDir: string) =
    if remoteArea.isNil:
      quit("Remote area not set in configuration file, can not download content:\n\t" & filename)
6884d534   Göran Krampe   Fixed unixy path ...
223
    let errorCode = run(format(remoteArea.download, filename, remoteArea.url, toUnixyPath(toDir)))
b9ad52ff   Göran Krampe   Added default con...
224
    if errorCode != 0:
ba6047f5   Göran Krampe   Added areas, move...
225
      quit("Something went wrong downloading " & filename & " from " & remoteArea.url, 3)
5fc367ad   Göran Krampe   Renamed to blimp,...
226
  
e914743a   Göran Krampe   Added remove comm...
227
  # Delete a file from the remote master blimpStore
ba6047f5   Göran Krampe   Added areas, move...
228
229
  proc remoteDeleteFile(filename: string) =
    if remoteArea.isNil:
e914743a   Göran Krampe   Added remove comm...
230
      return
6884d534   Göran Krampe   Fixed unixy path ...
231
    let errorCode = run(format(remoteArea.delete, filename, remoteArea.url, toUnixyPath(blimpStore)))
e914743a   Göran Krampe   Added remove comm...
232
    if errorCode != 0:
ba6047f5   Göran Krampe   Added areas, move...
233
      quit("Something went wrong deleting " & filename & " from " & remoteArea.url, 3)
5fc367ad   Göran Krampe   Renamed to blimp,...
234
  
3e2f4c14   Göran Krampe   Added upload comm...
235
  # Copy content to blimpStore and upload if it was a new file or upload == true.
5fc367ad   Göran Krampe   Renamed to blimp,...
236
237
  proc copyToBlimpStore(filename, blimpFilename: string) =
    if not existsFile(blimpStore / blimpFilename):
aaf9bcef   Göran Krampe   Upped to version ...
238
239
240
241
242
243
244
      if stdio:
        try:
          writeFile(blimpStore / blimpFilename, stdinContent)
        except:
          quit("Failed writing file: " & blimpStore / blimpFilename & " from stdin", 1)
      else:
        copyFile(filename, blimpStore / blimpFilename)
ba6047f5   Göran Krampe   Added areas, move...
245
      uploadFile(blimpFilename, blimpStore)
33f6041b   Andreas Rumpf   make blimp compil...
246
  
5fc367ad   Göran Krampe   Renamed to blimp,...
247
248
  
  # Copy content from blimpStore, and downloading first if needed
e914743a   Göran Krampe   Added remove comm...
249
  proc copyFromBlimpStore(blimpFilename, filename: string) =
5fc367ad   Göran Krampe   Renamed to blimp,...
250
    if not existsFile(blimpStore / blimpFilename):
ba6047f5   Göran Krampe   Added areas, move...
251
      downloadFile(blimpFilename, blimpStore)
aaf9bcef   Göran Krampe   Upped to version ...
252
253
254
255
256
257
258
259
    if stdio:
      try:
        var content = readFile(blimpStore / blimpFilename)
        write(stdout, content)
      except:
        quit("Failed reading file: " & blimpStore / blimpFilename & " to stdout", 1)
    else:
      copyFile(blimpStore / blimpFilename, filename)
74272cdb   Göran Krampe   First commit
260
  
e914743a   Göran Krampe   Added remove comm...
261
262
263
264
265
266
  # Delete from blimpStore and remote.
  proc deleteFromBlimpStore(blimpFilename, filename: string) =
    if existsFile(blimpStore / blimpFilename):
      removeFile(blimpStore / blimpFilename)
    remoteDeleteFile(blimpFilename)
  
aaf9bcef   Göran Krampe   Upped to version ...
267
  proc blimpFileNameFromString(line: string): string =
386557e6   Göran Krampe   Added support for...
268
269
    let hashline = split(strip(line), ':')
    if hashline[0] == "blimphash":
e914743a   Göran Krampe   Added remove comm...
270
271
272
273
      result = hashline[1]
    else:
      result = nil
  
3e2f4c14   Göran Krampe   Added upload comm...
274
  # Pick out blimpFilename (filename & "-" & hash) or nil
aaf9bcef   Göran Krampe   Upped to version ...
275
276
277
278
279
280
281
  proc blimpFileName(filename: string): string =
    if stdio:
      blimpFileNameFromString(stdinContent)
    else:
      var hashfile: File
      if not open(hashfile, filename):
        quit("Failed opening file: " & filename, 4)
33f6041b   Andreas Rumpf   make blimp compil...
282
      blimpFileNameFromString(string(readLine(hashfile)))
aaf9bcef   Göran Krampe   Upped to version ...
283
  
e914743a   Göran Krampe   Added remove comm...
284
285
  # Get hash and compute blimpFilename
  proc computeBlimpFilename(filename: string): string =
dab0c5b0   Göran Krampe   Added --version, ...
286
287
288
289
290
291
    var content: string
    try:
      content = readFile(filename)
    except:
      quit("Failed opening file: " & filename, 1)
    let hash = getMD5(content)
14d16aa7   Göran Krampe   Now the git filte...
292
    result = extractFilename(filename) & "-" & hash
33f6041b   Andreas Rumpf   make blimp compil...
293
  
5fc367ad   Göran Krampe   Renamed to blimp,...
294
  # Copy original file to blimpStore and replace with hash stub in git.
74272cdb   Göran Krampe   First commit
295
  proc deflate(filename: string) =
dab0c5b0   Göran Krampe   Added --version, ...
296
297
298
299
300
301
302
    if verbose: echo "Deflating " & filename
    var blimpFilename = blimpFilename(filename)
    if not blimpFilename.isNil:
      echo("\t" & filename & " is already deflated, skipping.")
    else:
      blimpFilename = computeBlimpFilename(filename)
      copyToBlimpStore(filename, blimpFilename)
aaf9bcef   Göran Krampe   Upped to version ...
303
      if stdio:
386557e6   Göran Krampe   Added support for...
304
        write(stdout, "blimphash:" & blimpFilename)
aaf9bcef   Göran Krampe   Upped to version ...
305
      else:
386557e6   Göran Krampe   Added support for...
306
        writeFile(filename, "blimphash:" & blimpFilename)
dab0c5b0   Göran Krampe   Added --version, ...
307
      if verbose: echo("\t" & filename & " deflated.")
e914743a   Göran Krampe   Added remove comm...
308
  
386557e6   Göran Krampe   Added support for...
309
  # Iterator over all deflated files in the git clone
49df26ea   Göran Krampe   Updated for new Nim
310
  iterator allDeflated(): string =
7e507b62   Göran Krampe   Fixes discovered ...
311
    let filenames = cmd("git ls-files " & gitRootDir).split({'\l', '\c'})
386557e6   Göran Krampe   Added support for...
312
313
314
    for fn in filenames:
      if not blimpFilename(fn).isNil:
        yield fn
33f6041b   Andreas Rumpf   make blimp compil...
315
  
ec7d9613   Göran Krampe   Added option --fi...
316
  # Iterator over all files matching the blimp filter in the git clone
49df26ea   Göran Krampe   Updated for new Nim
317
  iterator allFiltered(): string =
7e507b62   Göran Krampe   Fixes discovered ...
318
    let lines = cmd("git ls-files | git check-attr --stdin filter").split({'\l', '\c'})
ec7d9613   Göran Krampe   Added option --fi...
319
320
321
322
    for line in lines:
      let status = line.split(':')
      if strip(status[2]) == "blimp":
        yield status[0]
386557e6   Göran Krampe   Added support for...
323
  
5fc367ad   Göran Krampe   Renamed to blimp,...
324
  # Parse out hash from hash stub and copy back original content from blimpStore.
74272cdb   Göran Krampe   First commit
325
  proc inflate(filename: string) =
dab0c5b0   Göran Krampe   Added --version, ...
326
    if verbose: echo "Inflating " & filename
e914743a   Göran Krampe   Added remove comm...
327
328
    let blimpFilename = blimpFilename(filename)
    if blimpFilename.isNil:
dab0c5b0   Göran Krampe   Added --version, ...
329
      echo("\t" & filename & " is not deflated, skipping.")
e914743a   Göran Krampe   Added remove comm...
330
331
    else:
      copyFromBlimpStore(blimpfilename, filename)
dab0c5b0   Göran Krampe   Added --version, ...
332
      if verbose: echo("\t" & filename & " inflated.")
e914743a   Göran Krampe   Added remove comm...
333
334
335
336
337
338
339
  
  # Inflates file first (if deflated) and then removes current content for it,
  # both locally and in remote.
  proc remove(filename: string) =
    var blimpFilename = blimpFilename(filename)
    if not blimpFilename.isNil:
      copyFromBlimpStore(blimpfilename, filename)
74272cdb   Göran Krampe   First commit
340
    else:
e914743a   Göran Krampe   Added remove comm...
341
342
343
      blimpFilename = computeBlimpFilename(filename)
    deleteFromBlimpStore(blimpfilename, filename)
    echo("\t" & filename & " content removed from blimpstore locally and remotely.")
5fc367ad   Göran Krampe   Renamed to blimp,...
344
  
dab0c5b0   Göran Krampe   Added --version, ...
345
  
ba6047f5   Göran Krampe   Added areas, move...
346
347
  # Make sure a file already in blimpstore is uploaded to remote area.
  proc push(filename: string) =
3e2f4c14   Göran Krampe   Added upload comm...
348
349
350
351
352
    if verbose: echo "Uploading " & filename
    var blimpFilename = blimpFilename(filename)
    if blimpFilename.isNil:
      blimpFilename = computeBlimpFilename(filename)
    if existsFile(blimpStore / blimpFilename):
ba6047f5   Göran Krampe   Added areas, move...
353
      uploadFile(blimpFilename, blimpStore)
3e2f4c14   Göran Krampe   Added upload comm...
354
355
356
357
      if verbose: echo("\t" & filename & " uploaded.")
    else:
      if verbose: echo("\t" & filename & " is not in blimpstore, skipping.")
  
ba6047f5   Göran Krampe   Added areas, move...
358
359
360
361
362
  # Upload a file to a remote area
  proc upload(filename: string) =
    if verbose: echo "Uploading " & filename
    uploadFile(filename, currentDir)
    if verbose: echo("\t" & filename & " uploaded.")
33f6041b   Andreas Rumpf   make blimp compil...
363
  
ba6047f5   Göran Krampe   Added areas, move...
364
365
366
367
368
  # Download a file from a remote area.
  proc download(filename: string) =
    if verbose: echo "Downloading " & filename
    downloadFile(filename, currentDir)
    if verbose: echo("\t" & filename & " downloaded.")
3e2f4c14   Göran Krampe   Added upload comm...
369
  
dab0c5b0   Göran Krampe   Added --version, ...
370
371
372
373
374
375
376
377
378
379
380
381
382
  proc setupBlimpStore() =
    try:
      if not existsDir(blimpStore):
        createDir(blimpStore)
    except:
      quit("Could not create " & blimpStore & " directory.", 1)
  
    try:
      if not existsFile(blimpStore / ".blimp.conf"):
        writeFile(blimpStore / ".blimp.conf", defaultConfig)
    except:
      quit("Could not create .blimp.conf config file in " & blimpStore & " directory.", 1)
  
78569137   Göran Krampe   Added directory e...
383
384
385
  proc `$`(x: string): string =
    if x.isNil: "nil" else: x
  
dab0c5b0   Göran Krampe   Added --version, ...
386
387
388
  proc dumpConfig() =
    echo "\nDump of configuration:"
    echo "\tblimpStore: " & blimpStore
ba6047f5   Göran Krampe   Added areas, move...
389
390
391
    echo "\tremote-url: " & remoteArea.url
    echo "\tremote-upload: " & remoteArea.upload
    echo "\tremote-download: " & remoteArea.download
aaf9bcef   Göran Krampe   Upped to version ...
392
    echo "\tblimpVersion: " & $blimpVersion
dab0c5b0   Göran Krampe   Added --version, ...
393
    echo "\n"
74272cdb   Göran Krampe   First commit
394
  
386557e6   Göran Krampe   Added support for...
395
  let synopsis = """
db95b901   Göran Krampe   Various fixes, mo...
396
    blimp [options] <command> <filenames...>
ba6047f5   Göran Krampe   Added areas, move...
397
398
399
      -h,--help                  Show this
      --version                  Show version of blimp
      -v,--verbose               Verbosity, only works without -s
386557e6   Göran Krampe   Added support for...
400
      ----------
543fb248   Göran Krampe   Added deinit to r...
401
402
      <command>  (string)        (d)eflate, (i)nflate, init, deinit,
                                 remove, push, upload, download
ba6047f5   Göran Krampe   Added areas, move...
403
404
405
      --all                      Operate on all deflated files in clone
      -f,--filter                Operate on all files matching blimp filter
      -a,--area (default remote) The area to use for remote up/downloads
386557e6   Göran Krampe   Added support for...
406
      ----------
ba6047f5   Göran Krampe   Added areas, move...
407
408
      -s,--stdio                 If given, use stdin/stdout for content.
      <filenames> (string...)    One or more filepaths to inflate/deflate
386557e6   Göran Krampe   Added support for...
409
410
  """
  let help = """
3e2f4c14   Göran Krampe   Added upload comm...
411
  
aaf9bcef   Göran Krampe   Upped to version ...
412
413
414
    blimp is a little utility program for handling large files
    in git repositories. Its inspired by git-fat and s3annex
    but doesn't rely on S3 for storage - it uses rsync like git-fat.
386557e6   Göran Krampe   Added support for...
415
416
    It is a single binary without any dependencies. Its not as advanced
    as git-fat but basically does the same thing.
aaf9bcef   Göran Krampe   Upped to version ...
417
418
419
  
    Manual use:
  
386557e6   Göran Krampe   Added support for...
420
421
    Use "blimp d mybigfile" to deflate a file, typically before commit.
    Use "blimp i mybigfile" to inflate it back to original content.
aaf9bcef   Göran Krampe   Upped to version ...
422
  
3e2f4c14   Göran Krampe   Added upload comm...
423
    When deflated the file only has this content:
33f6041b   Andreas Rumpf   make blimp compil...
424
  
386557e6   Göran Krampe   Added support for...
425
      "blimphash:" <filename> "-" <md5sum>
aaf9bcef   Göran Krampe   Upped to version ...
426
  
386557e6   Göran Krampe   Added support for...
427
    Deflate also copies the real content to your local blimpstore:
33f6041b   Andreas Rumpf   make blimp compil...
428
  
386557e6   Göran Krampe   Added support for...
429
      <blimpstore>/<filename>-<md5sum>
aaf9bcef   Göran Krampe   Upped to version ...
430
  
386557e6   Göran Krampe   Added support for...
431
    ...and if configured also uploads it to "remote", using rsync.
aaf9bcef   Göran Krampe   Upped to version ...
432
433
  
    Configuration is in these locations in order:
33f6041b   Andreas Rumpf   make blimp compil...
434
  
aaf9bcef   Göran Krampe   Upped to version ...
435
      ./.blimp.conf
386557e6   Göran Krampe   Added support for...
436
      <gitroot>/.blimp.conf
aaf9bcef   Göran Krampe   Upped to version ...
437
438
439
440
441
442
443
444
445
446
447
448
      ~/<blimpstore>/.blimp.conf
      ~/.blimp.conf
  
    This way you can have settings per directory, per git clone,
    per store and per user. A default blimpstore with a commented .blimp.conf
    is created in ~/blimpstore if you run blimp and no .blimp.conf is found.
  
    Edit ~/blimpstore/.blimp.conf (or in another location) and set a proper
    remote and the proper rsync password. This ensures that its also properly
    synced with a master rsync repository that is typically shared.
  
    Inflate will bring back the original content by copying from
386557e6   Göran Krampe   Added support for...
449
    your local blimpstore, and if its not there, first download from the remote.
aaf9bcef   Göran Krampe   Upped to version ...
450
451
452
453
454
    Use this whenever you need to work/edit the big file - in order to get
    its real content.
  
    The filenames given are all processed. If -s is used content is processed via
    stdin/stdout and only one filename can be passed. This is used when running blimp
386557e6   Göran Krampe   Added support for...
455
    via a git filter (smudge/clean), see below.
aaf9bcef   Göran Krampe   Upped to version ...
456
457
458
459
460
  
    The remove command (no single character shortcut) will remove the file(s) content
    both from the local blimpstore and from the remote. This only removes
    the current content version, not older versions. The file itself is first
    inflated, if needed, and not deleted. This only "unblimps" the file.
33f6041b   Andreas Rumpf   make blimp compil...
461
  
ba6047f5   Göran Krampe   Added areas, move...
462
    The push command (no single character shortcut) will force upload the given file
3e2f4c14   Göran Krampe   Added upload comm...
463
    from the local blimpstore to the remote. This is normally done automatically,
ba6047f5   Göran Krampe   Added areas, move...
464
    but this way you can make sure they are synced onto the remote.
33f6041b   Andreas Rumpf   make blimp compil...
465
  
ba6047f5   Göran Krampe   Added areas, move...
466
467
    The upload and download commands are used to distribute artifacts typically in a
    build script. If no --area is given, they use the standard "remote" area.
33f6041b   Andreas Rumpf   make blimp compil...
468
  
386557e6   Göran Krampe   Added support for...
469
    In order to have blimp work automatically you can:
33f6041b   Andreas Rumpf   make blimp compil...
470
  
386557e6   Göran Krampe   Added support for...
471
    * Create a .gitattributes file with lines like:
ec7d9613   Göran Krampe   Added option --fi...
472
      *.png filter=blimp binary
33f6041b   Andreas Rumpf   make blimp compil...
473
  
386557e6   Göran Krampe   Added support for...
474
475
476
477
    * Configure blimp as a filter by running:
      git config filter.blimp.clean "blimp -s d %f"
      git config filter.blimp.smudge "blimp -s i %f"
  
ba6047f5   Göran Krampe   Added areas, move...
478
    The above git config can be done by running "blimp init" just like git.
386557e6   Göran Krampe   Added support for...
479
480
481
  
    When the above is done (per clone) git will automatically run blimp deflate
    just before committing and blimp inflate when operations are done.
33f6041b   Andreas Rumpf   make blimp compil...
482
  
386557e6   Göran Krampe   Added support for...
483
484
    This means that if you clone a git repository that already has a .gitattributes
    file in it that uses the blimp filter, then you should do:
33f6041b   Andreas Rumpf   make blimp compil...
485
  
ba6047f5   Göran Krampe   Added areas, move...
486
487
      blimp init
      blimp inflate --filter
33f6041b   Andreas Rumpf   make blimp compil...
488
  
ba6047f5   Göran Krampe   Added areas, move...
489
    This will configure the blimp filter and then find and inflate all deflated
386557e6   Göran Krampe   Added support for...
490
    files throughout the clone.
aaf9bcef   Göran Krampe   Upped to version ...
491
  """
db95b901   Göran Krampe   Various fixes, mo...
492
  
74272cdb   Göran Krampe   First commit
493
  ################################ main #####################################
78569137   Göran Krampe   Added directory e...
494
495
  # Set some dirs
  homeDir = getHomeDir()
33f6041b   Andreas Rumpf   make blimp compil...
496
  homeDir = homeDir[0..^2] # Not sure why it keeps a trailing "/" on Linux
78569137   Göran Krampe   Added directory e...
497
498
  currentDir = getCurrentDir()
  gitRootDir = gitRoot()
74272cdb   Göran Krampe   First commit
499
  
dab0c5b0   Göran Krampe   Added --version, ...
500
  # Using lapp to get args, on parsing failure this will show usage automatically
386557e6   Göran Krampe   Added support for...
501
  var args = parse(synopsis)
dab0c5b0   Göran Krampe   Added --version, ...
502
  verbose = args["verbose"].asBool
aaf9bcef   Göran Krampe   Upped to version ...
503
  stdio = args["stdio"].asBool
ec7d9613   Göran Krampe   Added option --fi...
504
505
  onAllDeflated = args["all"].asBool
  onAllFiltered = args["filter"].asBool
ba6047f5   Göran Krampe   Added areas, move...
506
  area = args["area"].asString
aaf9bcef   Göran Krampe   Upped to version ...
507
508
509
510
511
512
  
  # Can't do verbose with -s, that messes up stdout,
  # read in all of stdin once and for all
  if stdio:
    verbose = false
    try:
ce54e838   Göran Krampe   Fix broken merge,...
513
      stdinContent = readAllX(stdin)
aaf9bcef   Göran Krampe   Upped to version ...
514
515
516
517
      close(stdin)
    except:
      quit("Failed reading stdin", 1)
  
dab0c5b0   Göran Krampe   Added --version, ...
518
  # Parse configuration files, may shadow and override each other
78569137   Göran Krampe   Added directory e...
519
  parseConfFile(currentDir / ".blimp.conf")
386557e6   Göran Krampe   Added support for...
520
  if not gitRootDir.isNil and gitRootDir != currentDir:
78569137   Göran Krampe   Added directory e...
521
    parseConfFile(gitRootDir / ".blimp.conf")
74272cdb   Göran Krampe   First commit
522
  
ba6047f5   Göran Krampe   Added areas, move...
523
524
525
526
527
  if existsDir(homeDir / "blimpstore"):
    parseConfFile(homeDir / "blimpstore" / ".blimp.conf")
  
  parseConfFile(homeDir / ".blimp.conf")
  
dab0c5b0   Göran Krampe   Added --version, ...
528
529
  # If we haven't gotten a blimpstore yet, we set a default one
  if blimpStore.isNil:
78569137   Göran Krampe   Added directory e...
530
    blimpStore = homeDir / "blimpstore"
74272cdb   Göran Krampe   First commit
531
  
ba6047f5   Göran Krampe   Added areas, move...
532
533
534
  # Check if we have a configured remoteArea
  if areas.hasKey(area):
    remoteArea = areas[area]
74272cdb   Göran Krampe   First commit
535
  
386557e6   Göran Krampe   Added support for...
536
  # Let's just show what we have :)
dab0c5b0   Göran Krampe   Added --version, ...
537
  if verbose: dumpConfig()
e914743a   Göran Krampe   Added remove comm...
538
  
dab0c5b0   Göran Krampe   Added --version, ...
539
  # These two are special, they short out
386557e6   Göran Krampe   Added support for...
540
  if args.showHelp: quit(synopsis & help)
dab0c5b0   Göran Krampe   Added --version, ...
541
  if args.showVersion: quit("blimp version: " & versionAsString)
74272cdb   Göran Krampe   First commit
542
  
aaf9bcef   Göran Krampe   Upped to version ...
543
544
545
546
  # Check blimpVersion
  if not blimpVersion.isNil and blimpVersion != versionAsString:
    quit("Wrong version of blimp, configuration wants: " & blimpVersion)
  
386557e6   Göran Krampe   Added support for...
547
  
33f6041b   Andreas Rumpf   make blimp compil...
548
  # Ok, let's see
3e2f4c14   Göran Krampe   Added upload comm...
549
550
551
  var filenames = initSet[string]()
  
  # Add upp all files to operate on in a Set
386557e6   Göran Krampe   Added support for...
552
  if args.hasKey("filenames"):
3e2f4c14   Göran Krampe   Added upload comm...
553
554
555
556
557
558
559
560
561
    for f in args["filenames"].asSeq:
      if f.asString != "":
        filenames.incl(f.asString)
  if onAllDeflated:
    for fn in allDeflated():
      filenames.incl(fn)
  if onAllFiltered:
    for fn in allFiltered():
      filenames.incl(fn)
dab0c5b0   Göran Krampe   Added --version, ...
562
563
564
565
  
  # Make sure the local blimpstore is setup.
  setupBlimpStore()
  
ba6047f5   Göran Krampe   Added areas, move...
566
  
74272cdb   Göran Krampe   First commit
567
  # Do the deed
ba6047f5   Göran Krampe   Added areas, move...
568
  let command = args["command"].asString
386557e6   Göran Krampe   Added support for...
569
  if command != "":
ba6047f5   Göran Krampe   Added areas, move...
570
571
572
573
574
    # Should we install the filter?
    if command == "init":
      ensureBlimpFilter()
      echo("Installed blimp filter")
      quit(0)
543fb248   Göran Krampe   Added deinit to r...
575
576
577
578
    elif command == "deinit":
      removeBlimpFilter()
      echo("Removed blimp filter")
      quit(0)
ba6047f5   Göran Krampe   Added areas, move...
579
    elif command == "d" or command == "deflate":
3e2f4c14   Göran Krampe   Added upload comm...
580
581
      for fn in filenames:
        deflate(fn)
386557e6   Göran Krampe   Added support for...
582
    elif command == "i" or command == "inflate":
3e2f4c14   Göran Krampe   Added upload comm...
583
584
      for fn in filenames:
        inflate(fn)
386557e6   Göran Krampe   Added support for...
585
586
    elif command == "remove":
      for fn in filenames:
3e2f4c14   Göran Krampe   Added upload comm...
587
        remove(fn)
ba6047f5   Göran Krampe   Added areas, move...
588
589
590
    elif command == "push":
      for fn in filenames.items:
        push(fn)
3e2f4c14   Göran Krampe   Added upload comm...
591
    elif command == "upload":
3e2f4c14   Göran Krampe   Added upload comm...
592
593
      for fn in filenames.items:
        upload(fn)
ba6047f5   Göran Krampe   Added areas, move...
594
595
596
    elif command == "download":
      for fn in filenames.items:
        download(fn)
386557e6   Göran Krampe   Added support for...
597
    else:
ba6047f5   Göran Krampe   Added areas, move...
598
      quit("Unknown command: \"" & command & "\", use --help for valid commands.", 6)
74272cdb   Göran Krampe   First commit
599
600
601
  
  # All good
  quit(0)