forked from Bobby-Seidensticker/html5slides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pf.py
1241 lines (1019 loc) · 37.9 KB
/
pf.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
pf.py - Pageforest Application Uploader
See http://pageforest.com for more information about the
Pageforest web application platform.
This utliity will deploy files to your pageforest application
so you can edit your application files on your local machine.
See pf.py --help for more information.
"""
import re
import os
import stat
import hmac
import hashlib
import urllib
import urllib2
from base64 import b64encode
from datetime import datetime
from fnmatch import fnmatch
from optparse import OptionParser
try:
try:
import json # Python 2.6
except ImportError:
from django.utils import simplejson as json # Django
except ImportError:
import simplejson as json # Please easy_install simplejson
VERSION = '1.10.1'
PF_DIST_SERVER = 'http://dist.pageforest.com'
PF_DIST_DIRECTORY = '/directory.json'
PF_FILENAME = 'pf.py'
# See http://code.google.com/closure/compiler/docs/gettingstarted_api.html
CLOSURE_API = 'http://closure-compiler.appspot.com/compile'
NAMESPACE_PATTERN = "namespace.module('%s', function (exports, require) {\n%s\n});\n"
# Swag at max content that can fit in a Blob
MAX_FILE_SIZE = 1024 * 1024 - 200
ADMIN = 'admin'
META_FILENAME = 'app.json'
MANIFEST_FILENAME = 'app.manifest'
MAKE_FILENAME = '.pfmake'
AUTOGEN_LINE = "# AUTOGENERATED"
AUTOGEN_EXPLAIN = "# All contents below AUTOGENERATED are managed by pf" \
"\n# Directives starting with '#!' will be preserved."
OPTIONS_FILENAME = '.pf'
DOC_ID_REGEX = r"[a-zA-Z0-9_-][a-zA-Z0-9\._-]{,99}"
ERROR_FILENAME = 'pferror.html'
IGNORE_FILENAMES = ('pf.py', 'pf', PF_FILENAME, OPTIONS_FILENAME, ERROR_FILENAME, MAKE_FILENAME,
'.*', '*~', '#*#', '*.bak', '*.rej', '*.orig')
LOCAL_COMMANDS = ['dir', 'offline', 'info', 'compile', 'make', 'config']
METAFILE_REMOVE = ('sha1', 'size', 'modified', 'created', 'docid')
SHA_EXCLUDED = METAFILE_REMOVE + ('application',)
commands = None
def intcomma(value):
if value is None:
return "---"
orig = str(value)
while True:
new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
if orig == new:
return new
orig = new
def project(d, keys):
"""
Return a dictionary that is the projection of properties
from the keys list.
>>> project({}, ['a'])
{}
>>> project({'a': 1, 'b': 2}, ['a'])
{'a': 1}
>>> project({'a': 1, 'b': 2}, ['b', 'c'])
{'b': 2}
"""
result = {}
for key in keys:
if key in d:
result[key] = d[key]
return result
def as_datetime(dct):
"""
Decode datetime objects from JSON dictionary.
"""
if dct.get('__class__', '') == 'Date' and 'isoformat' in dct:
isoformat = dct['isoformat'][:19]
return datetime.strptime(isoformat, '%Y-%m-%dT%H:%M:%S')
return dct
class ModelEncoder(json.JSONEncoder):
"""
Encode Date objects to JSON.
"""
def default(self, obj):
if isinstance(obj, datetime):
return {"__class__": "Date",
"isoformat": obj.isoformat() + 'Z'}
return json.JSONEncoder.default(self, obj)
class AuthRequest(urllib2.Request):
"""HTTP request with sessionkey cookie and referer."""
def __init__(self, url, *args, **kwargs):
urllib2.Request.__init__(self, url, *args, **kwargs)
self.add_header('Referer', 'http://%s.%s/' % (
options.application, options.server))
if (hasattr(options, 'session_key')):
self.add_header('Cookie', 'sessionkey=' + options.session_key)
if options.verbose:
print "HTTP %s %s" % (self.get_method(), url)
if self.get_data():
print "Data: %s" % self.get_data()
class PutRequest(AuthRequest):
"""Request to upload a file with HTTP PUT."""
def get_method(self):
return 'PUT'
class DeleteRequest(AuthRequest):
"""Request to remove a file with HTTP DELETE."""
def get_method(self):
return 'DELETE'
def hmac_sha1(key, message):
# Convert unicode strings to byte strings - hmac will throw type error
key = str(key)
message = str(message)
return hmac.new(key, message, hashlib.sha1).hexdigest()
def sign_in():
url = options.root_url + 'auth/challenge'
challenge = urllib2.urlopen(AuthRequest(url)).read()
if options.verbose:
print "Challenge: %s" % challenge
signature = hmac_sha1(options.secret, challenge)
reply = '|'.join((options.username, challenge, signature))
url = options.root_url + 'auth/verify/' + reply
if options.verbose:
print "Response: %s" % url
session_key = urllib2.urlopen(AuthRequest(url)).read()
if options.verbose:
print "Session key: %s" % session_key
return session_key
def ensure_meta():
if os.path.exists(META_FILENAME):
parsed = json.loads(open(META_FILENAME, 'r').read())
options.application = parsed['application']
options.save_app = False
elif if_yes("Missing %s - create one?" % META_FILENAME):
options.application = raw_input("Application id: ")
meta_file = open(META_FILENAME, 'w')
meta_file.write(to_json({'application': options.application,
'readers': ['public']}))
meta_file.close()
else:
exit(1)
def get_dist_file(file_name, file_info, chmod=False):
print "Updating %s to version %s." % (file_name, file_info['version'])
pf = urllib2.urlopen(PF_DIST_SERVER + "/" + file_info['path']).read()
pf_file = open(file_name, 'w')
pf_file.write(pf)
pf_file.close()
if chmod:
os.chmod(file_name, stat.S_IRWXU +
stat.S_IRGRP + stat.S_IXGRP +
stat.S_IROTH + stat.S_IXOTH)
return pf
class version_number():
def __init__(self, s):
(self.major, self.minor, self.patch) = map(int, s.split('.'))
def __str__(self):
return '.'.join(map(str, (self.major, self.minor, self.patch)))
def numeric(self):
return 1000000 * self.major + 1000 * self.minor + self.patch
def __cmp__(self, other):
return self.numeric() - other.numeric()
def check_pf():
"""Check for updates to the pf program from the pageforest distribution site"""
try:
if options.verbose:
print "Checking for updated %s version." % PF_FILENAME
dir_json = urllib2.urlopen(PF_DIST_SERVER + PF_DIST_DIRECTORY).read()
directory = json.loads(dir_json)
file_info = directory[PF_FILENAME]
local_version = version_number(VERSION)
remote_version = version_number(file_info['version'])
if local_version > remote_version:
print "You have a newer version (%s) of %s than published (%s) - lucky you!" % \
(local_version, PF_FILENAME, remote_version)
return
if local_version < remote_version:
print "New version of %s (%s) available - Changes: %s" % \
(PF_FILENAME, remote_version, file_info['versions'][str(remote_version)]['summary'])
if str(local_version) in file_info['obsolete'] or \
if_yes("Download %s now?" % PF_FILENAME):
pf = get_dist_file(PF_FILENAME, file_info, True)
except Exception, e:
print "Version check for %s failed: %s" % (PF_FILENAME, str(e))
if dir_json:
print dir_json
def load_application():
"""
Load application from META_FILENAME, or ask the user for it.
"""
if options.command == 'listapps':
options.application = 'www'
options.save_app = False
options.root_url = 'http://www.%s/' % options.server
if options.command == 'feature':
options.save_app = False
if options.application is None:
ensure_meta()
if options.local_only:
return
if options.docs:
options.root_url = 'http://%s.%s/' % (options.application, options.server)
if not hasattr(options, 'root_url'):
options.root_url = 'http://%s.%s.%s/' % (ADMIN, options.application, options.server)
print "Server: %s" % options.root_url
def load_options():
"""
Load saved options from options file. Don't override command line
provided options.
"""
options.local_only = options.command in LOCAL_COMMANDS
options.save_app = True
options.secret = None
file_options = {}
if os.path.exists(OPTIONS_FILENAME):
file_options = json.loads(open(OPTIONS_FILENAME, 'r').read())
for prop in ('files', 'secret', 'server', 'username', 'application'):
if getattr(options, prop, None) is None:
setattr(options, prop, file_options.get(prop))
if not options.server:
options.server = "pageforest.com"
if not options.files:
options.files = {}
if not options.local_only:
check_pf()
if not options.username:
options.username = raw_input("Username: ")
if not options.secret:
if not options.password:
from getpass import getpass
options.password = getpass("Password: ")
options.secret = hmac_sha1(options.password, options.username.lower())
def save_options():
"""
Save options in options file for later use.
"""
file_options = {}
for prop in ['username', 'secret', 'server']:
if getattr(options, prop) is not None:
file_options[prop] = getattr(options, prop)
if options.application is not None:
if options.save_app:
file_options['application'] = options.application
if hasattr(options, 'local_listing'):
files = {}
for path in options.local_listing:
files[path] = project(options.local_listing[path], ['time', 'size', 'sha1'])
file_options['files'] = files
else:
file_options['files'] = options.files
options_file = open(OPTIONS_FILENAME, 'w')
options_file.write(to_json(file_options))
options_file.close()
def config():
"""
Get configuration from command line.
"""
global options, commands
commands = [function.split('_')[0] for function in globals()
if function.endswith('_command')]
commands.sort()
usage = "usage: %prog [options] (" + '|'.join(commands) + ") [filenames]"
for command in commands:
usage += "\n%s: %s" % (command, globals()[command + '_command'].__doc__)
parser = OptionParser(usage=usage)
parser.add_option('-s', '--server', metavar='<hostname>',
help="deploy to this server (default: pageforest.com")
parser.add_option('-u', '--username')
parser.add_option('-p', '--password')
parser.add_option('-a', '--application')
parser.add_option('-d', '--docs', action='store_true')
parser.add_option('-v', '--verbose', action='store_true')
parser.add_option('-q', '--quiet', action='store_true')
parser.add_option('-g', '--debug', action='store_true')
parser.add_option('-r', '--raw', action='store_true',
help="Default is to upload all files using base64 encoding. "
"This option overrides and sends raw binary files.")
parser.add_option('-f', '--force', action='store_true',
help="Ignore sha1 hashes and get/put all files.")
parser.add_option('-n', '--noop', action='store_true',
help="don't perform update operations")
options, args = parser.parse_args()
if not args:
parser.error("No command specified.")
options.command = args.pop(0).lower().strip()
if not options.command:
parser.error("Empty command.")
# Prefix expansion.
for command in commands:
if command.startswith(options.command):
options.command = command
break
if options.command not in commands:
parser.error("Unsupported command: " + options.command)
return args
def url_from_path(path):
return options.root_url + urllib.quote(path)
def should_encode(filename):
"""
Some versions of python have problems with raw binary PUT's - treating data
as ascii and complaining. So, use base64 transfer encoding.
"""
if options.raw or filename == META_FILENAME or is_doc_path(filename):
return False
return True
def upload_file(filename):
"""
Upload one file to the server.
"""
url = url_from_path(filename)
local_info = options.local_listing[filename]
if local_info['size'] > MAX_FILE_SIZE:
print "Skipping %s - file too large (%s bytes)." % \
(filename, intcomma(local_info['size']))
return
# Compare if remote file has same hash as local one
if filename in options.listing:
info = options.listing[filename]
is_equal = info['sha1'] == local_info['sha1']
if options.verbose:
print "SHA1 %s (local) %s %s (server) for %s" % \
(local_info['sha1'],
is_equal and "==" or "!=",
info['sha1'],
filename)
if not options.force and is_equal:
return
elif options.verbose:
print "Could not find %s on server." % filename
file = open(get_local_path(filename), 'rb')
data = file.read()
file.close()
or_not = options.noop and " (Not!)" or ""
if not options.quiet:
print "Uploading: %s (%s bytes)%s" % (url, intcomma(len(data)), or_not)
if options.noop:
return
if should_encode(filename):
data = b64encode(data)
url += '?transfer-encoding=base64'
response = urllib2.urlopen(PutRequest(url), data)
if options.verbose:
print "Response: %s" % response.read()
def delete_file(filename):
"""
Delete one file from the server.
"""
url = url_from_path(filename)
or_not = options.noop and " (Not!)" or ""
if not options.quiet:
print "Deleting: %s%s" % (url, or_not)
if options.noop:
return
response = urllib2.urlopen(AuthRequest(url + '?method=delete'))
if options.verbose:
print "Response: %s" % response.read()
def download_file(filename):
"""
Download a file from the server.
Convert blob paths to be: docs/docid_blobs/blob_path
"""
url = url_from_path(filename)
info = options.listing[filename]
# Check if the local file is already up-to-date.
if not options.force:
local_info = options.local_listing.get(filename, {"sha1": "no-local-file"})
is_equal = info['sha1'] == local_info['sha1']
if options.verbose:
print "SHA1 %s (local) %s %s (server) for %s" % \
(local_info['sha1'],
is_equal and "==" or "!=",
info['sha1'],
filename)
if is_equal:
return
# Download file from Pageforest backend.
or_not = options.noop and " (Not!)" or ""
if not options.quiet:
print "Downloading: %s (%s bytes)%s" % (url, intcomma(info['size']), or_not)
if options.noop:
return
try:
response = urllib2.urlopen(AuthRequest(url))
except IOError, e:
print "Could not download file, %s (%s)." % (url, str(e))
return
filename = get_local_path(filename)
# Make directory if needed.
dirname = os.path.dirname(filename)
if dirname:
if os.path.exists(dirname) and not os.path.isdir(dirname):
print "Converting %s to %s (directory name conflict)" % (dirname, dirname + '.blob')
os.rename(dirname, dirname + '.blob')
if not os.path.exists(dirname):
if options.verbose:
print "Making directory: %s" % dirname
os.makedirs(dirname)
try:
outfile = open(filename, 'wb')
data = response.read()
if filename == META_FILENAME:
data = to_json(json.loads(data), exclude=METAFILE_REMOVE)
outfile.write(data)
outfile.close()
except IOError, e:
print "Could not write file, %s (%s)." % (filename, str(e))
def prefix_match(args, filename):
"""
Check if the filename starts with one of the prefixes.
"""
for arg in args:
if filename.startswith(arg):
return True
def pattern_match(patterns, filename):
"""
Check if the filename matches any of the patterns.
"""
for pattern in patterns:
if fnmatch(filename, pattern):
return True
def to_json(d, extra=None, include=None, exclude=None, indent=2):
"""
Serialize an object to json.
"""
assert isinstance(d, dict)
if exclude is None:
exclude = ()
result = {}
for name in d:
if exclude and name in exclude:
continue
if include and name not in include:
continue
result[name] = d[name]
if extra:
result.update(extra)
if indent is None:
return json.dumps(result, sort_keys=True,
separators=(',', ':'), cls=ModelEncoder)
else:
return json.dumps(result, sort_keys=True, cls=ModelEncoder,
indent=indent, separators=(',', ': ')) + '\n'
def is_data_path(filename):
return filename.startswith('docs')
def is_doc_path(filename):
return is_data_path(filename) and filename.count('/') == 1
def get_local_path(path):
"""
Paths are keys in the Pageforest store. There is no retriction
on paths being prefixes of other paths (unlike the filesytem, where
a file path to a *file* cannot be a sufix of another file path - only
*directories* can have prefix paths.
"""
if os.path.isdir(path):
return path + '.blob'
return path
def normalize_local_path(path):
"""
Inverse of get_local_path.
"""
path = path.replace(os.path.sep, '/')
if path.endswith('.blob'):
return path[:-5]
return path
def sha1_file(filename, data=None):
"""
Hash the contents of the file using SHA-1.
"""
if not os.path.exists(filename):
return None
if data is None:
if options.verbose:
print "Reading file for SHA1: %s" % filename
infile = open(filename, 'rb')
data = infile.read()
infile.close()
# Normalize document for sha1 computation.
if filename == META_FILENAME or is_doc_path(filename):
try:
data = to_json(json.loads(data), exclude=SHA_EXCLUDED)
if options.debug:
print "Computing sha1 hash from:\n---\n%s\n---" % data
except ValueError, e:
print "Invalid document format in file %s - not JSON" % filename
return None
sha1 = hashlib.sha1(data).hexdigest()
return sha1
def list_remote_path(path):
url = "%s%s?method=list&depth=0" % (options.root_url, path)
cursor_param = ""
count = 0
while True:
response = urllib2.urlopen(AuthRequest(url + cursor_param))
result = response.read()
result = json.loads(result, object_hook=as_datetime)
files = result['items']
count += len(files)
if path != '':
path_files = {}
for filename, info in files.items():
path_files[path + filename] = info
files = path_files
options.listing.update(files)
if options.verbose and count:
print "%s files" % intcomma(count)
if 'cursor' not in result:
break
cursor_param = "&cursor=%s" % result['cursor']
def list_remote_files(list_blobs=True):
"""
Get the list of files on the remote server, with metadata.
"""
options.listing = {}
try:
list_remote_path(options.docs and 'docs/' or '')
except urllib2.HTTPError, e:
# For newly created apps - listing will return error.
# Treat as empty on the server.
options.listing = {}
return
if not options.docs or not list_blobs:
return
# Check for child blobs under each document
# TODO: Very inefficient - need built-in LIST command for all docs and blobs.
docnames = options.listing.keys()
docnames.sort()
for docname in docnames:
list_remote_path(docname + '/')
def update_local_listing(local_path):
"""
Update the options.local_listing[path] to the latest information. Uses
the options.files cache if the file has not been more recently modified.
Local listing paths are stored normalized to be the corresponding server-side
(relative) path from the root of the application domain.
REVIEW: We don't allow for any blob key to be a prefix of any other
(i.e, a blob can't be both a directory and a file).
Note: The force flag will force all file signatures to be computed regardless of
file time in cache.
"""
path = normalize_local_path(local_path)
if path in options.files and \
not options.force and \
options.files[path]['time'] == int(os.path.getmtime(local_path)):
options.local_listing[path] = options.files[path]
else:
options.local_listing[path] = {
'sha1': sha1_file(local_path),
'time': int(os.path.getmtime(local_path)),
'size': os.path.getsize(local_path)
}
options.local_listing[path]['modified'] = \
datetime.fromtimestamp(os.path.getmtime(local_path))
def list_local_files():
"""
Get the list of all local files, with metadata is same format as remote file
listing.
"""
options.local_listing = {}
for dirpath, dirnames, filenames in os.walk('.'):
for dirname in dirnames:
if dirname.startswith('.'):
dirnames.remove(dirname)
for filename in filenames:
if pattern_match(IGNORE_FILENAMES, filename):
continue
path = os.path.relpath(os.path.join(dirpath, filename))
update_local_listing(path)
def check_args(args):
"""
Make sure file prefix args match options.docs. We don't
do both document and app file uploading in one pass.
"""
for arg in args:
if options.docs and not is_data_path(arg):
print "%s is not a document. Conflicts with -d option." % arg
exit(1)
if not options.docs and is_data_path(arg):
print "%s is a document - you must use the -d option." % arg
exit(1)
def get_command(args):
"""
Download all files for an app, except files that are already
up-to-date (same SHA-1 hash as remote).
"""
list_remote_files()
list_local_files()
filenames = options.listing.keys()
filenames.sort()
for filename in filenames:
if args and not prefix_match(args, filename):
continue
download_file(filename)
def put_command(args):
"""
Upload all files for an app, except files that are already
up-to-date (same SHA-1 hash as remote).
"""
list_remote_files()
list_local_files()
do_make()
update_manifest()
# Ensure that application is created before uploading other application files
if not options.docs:
upload_file(META_FILENAME)
paths = options.local_listing.keys()
paths.sort()
for path in paths:
if path == META_FILENAME:
continue
if (not options.docs) is is_data_path(path):
continue
if args and not prefix_match(args, path):
continue
upload_file(path)
def feature_command(args):
"""
Feature the current application in the application director.
This command is only availailable to Pageforest administrators.
"""
url = url_from_path(META_FILENAME)
if len(args) >= 1 and args[0] not in ('on', 'off'):
print "Bad arument - expect 'on' or 'off'"
return
featured = len(args) >= 1 and args[0] == 'on'
try:
response = urllib2.urlopen(AuthRequest(url))
except IOError, e:
print "Could not download file, %s (%s)." % (url, str(e))
return
data = json.loads(response.read())
tag = 'pf:featured'
is_featured = tag in data['tags']
print "Application %s is currently %sfeatured." % (options.application,
"" if is_featured else "not ")
if len(args) < 1:
return
if not featured:
data['tags'].remove(tag)
tag = '-' + tag
data['tags'] = list(set(data['tags'] + [tag]))
print "Tagging %s as %s" % (options.application, tag)
data = to_json(data)
response = urllib2.urlopen(PutRequest(url), data)
def make_command(args):
"""
If you have a .pfmake file (see config command), build the combined and minified
javascript files (this is done automatically on each put command so you need not
do this separately).
"""
list_local_files()
do_make(True)
def delete_command(args):
"""
Delete files from the server (leaves local files alone).
If no filename is given, the entire app is deleted.
"""
if not args:
if if_yes("Are you sure you want to DELETE %s and all it's files from %s" %
(options.application, options.server)):
delete_file(META_FILENAME)
return
list_remote_files(False)
filenames = options.listing.keys()
selected = []
for filename in filenames:
if args and not prefix_match(args, filename):
continue
selected.append(filename)
delete_files(selected)
def if_yes(prompt):
if options.force:
return True
answer = raw_input("%s (yes/no)? " % prompt)
f = answer.lower().startswith('y')
if not f:
print "I'll take that as a no."
return f
def delete_files(filenames):
if META_FILENAME in filenames:
filenames.remove(META_FILENAME)
if not filenames:
print "No files to delete."
return
filenames.sort()
if not options.noop:
for filename in filenames:
print_file_info(filename, options.listing[filename])
if not if_yes("Are you sure you want to DELETE %s files from %s" %
(intcomma(len(filenames)), options.server)):
return
for filename in filenames:
delete_file(filename)
def vacuum_command(args):
"""
List remote files that no longer exist locally, then delete them.
"""
list_remote_files(False)
list_local_files()
filenames = options.listing.keys()
selected = []
for filename in filenames:
if args and not prefix_match(args, filename):
continue
if filename in options.local_listing:
continue
selected.append(filename)
delete_files(selected)
def update_manifest(explicit=False):
"""
Update the manifest file AUTOGENERATED secion. We do this on
each application upload in case any files have changed that
require a new manifest file be written.
"""
if not os.path.exists(MANIFEST_FILENAME):
return
manifest_file = open(MANIFEST_FILENAME, 'r')
parts = manifest_file.read().partition('\n' + AUTOGEN_LINE)
manifest_file.close()
if parts[1] == '':
if explicit:
print "%s has no AUTOGENERATE section" % MANIFEST_FILENAME
return
commands = [line for line in parts[2].split('\n') if line.startswith('#!')]
excludes = []
for command in commands:
match = re.match(r'#!\s*EXCLUDE:\s*(.*)\s*$', command)
if options.verbose:
print "Excluding paths beginning with '%s'" % match.group(1)
if match:
excludes.extend(re.split(r",\s*", match.group(1)))
cached_files = []
hash_lines = []
paths = options.local_listing.keys()
paths.sort()
size = 0
for path in paths:
info = options.local_listing[path]
if path == MANIFEST_FILENAME or path == META_FILENAME or \
info['size'] > MAX_FILE_SIZE or \
is_data_path(path) or \
prefix_match(excludes, path):
continue
cached_files.append(path)
hash_lines.append("%s=%s" % (path, info['sha1']))
size += info['size']
manifest_lines = [parts[0], AUTOGEN_LINE, AUTOGEN_EXPLAIN]
manifest_lines.extend(commands)
manifest_lines.extend((
"# TOTAL FILES: %s (%s bytes)" % (intcomma(len(cached_files)), intcomma(size)),
"# SIGNATURE: %s" % hashlib.sha1('\n'.join(hash_lines)).hexdigest(),
"CACHE:",
))
manifest_lines.extend(cached_files)
manifest_file = open(MANIFEST_FILENAME, 'w')
manifest_file.write('\n'.join(manifest_lines) + '\n')
manifest_file.close()
# Make sure the listing for the manifest file is up to date
# so it will be uploaded if changed.
update_local_listing(MANIFEST_FILENAME)
def offline_command(args):
"""
Build an app.manifest file to enable your application to be used offline.
The manifest will be auto-updated on each subsequent PUT command.
See http://diveintohtml5.org/offline.html for details on using a manifest in your application.
"""
list_local_files()
if os.path.exists(MANIFEST_FILENAME) and not options.force:
print "%s already exists (use -f to overwrite)." % MANIFEST_FILENAME
if not os.path.exists(MANIFEST_FILENAME) or options.force:
print "Creating file %s." % MANIFEST_FILENAME
default_manifest = (
"CACHE MANIFEST\n"
"# Cache files for offline access - see http://diveintohtml5.org/offline.html\n"
"\n"
"/lib/beta/js/pf-client.min.js\n"
"/lib/beta/css/client.css\n"
"/static/images/appbar/green-left.png\n"
"/static/images/appbar/green-center.png\n"
"/static/images/appbar/green-right.png\n"
"/static/images/appbar/down.png\n"
"/static/images/appbar/logo.png\n"
"\n"
"NETWORK:\n"
"*\n\n"
)
manifest = open(MANIFEST_FILENAME, 'w')
manifest.write(default_manifest + AUTOGEN_LINE)
manifest.close()
print default_manifest + AUTOGEN_LINE
update_manifest(True)
def print_file_info(filename, metadata):
sha1 = metadata['sha1'] or '-' * 40
print '%s %s %s\t(%s bytes)' % (
sha1,
metadata['modified'].strftime('%Y-%m-%d %H:%M:%S'),
filename,
intcomma(metadata['size']))
def list_file_info(args, listing):
filenames = listing.keys()
filenames.sort()
count = 0
size = 0
for filename in filenames:
info = listing[filename]
if args and not prefix_match(args, filename):
continue
print_file_info(filename, info)
count += 1
if info['size']:
size += info['size']
print "%s files: %s Total bytes" % (intcomma(count), intcomma(size))
def list_command(args):
"""