11import base64
2- import shutil
3- import tempfile
42import urllib2
53import json
64import os
@@ -83,17 +81,26 @@ def github_push(user, commit_message, repo_name, project):
8381 root = find_project_root (paths )
8482 except :
8583 root = ''
84+ expected_paths = set ()
85+
86+ def update_expected_paths (new_path ):
87+ # This adds the path *and* its parent directories to the list of expected paths.
88+ # The parent directories are already keys in next_tree, so if they aren't present in expected_paths
89+ # then, when iterating over next_tree to see which files have been deleted, we would have to treat
90+ # directories as special cases.
91+ split_path = new_path .split ('/' )
92+ expected_paths .update ('/' .join (split_path [:p ]) for p in range (2 , len (split_path ) + 1 ))
8693
8794 src_root = root + 'src/'
8895 worker_src_root = root + 'worker_src/'
8996 project_sources = project .source_files .all ()
9097 has_changed = False
9198 for source in project_sources :
92- repo_path = ''
9399 if source .target == 'worker' :
94100 repo_path = worker_src_root + source .file_name
95101 else :
96102 repo_path = src_root + source .file_name
103+ update_expected_paths (repo_path )
97104 if repo_path not in next_tree :
98105 has_changed = True
99106 next_tree [repo_path ] = InputGitTreeElement (path = repo_path , mode = '100644' , type = 'blob' ,
@@ -109,24 +116,13 @@ def github_push(user, commit_message, repo_name, project):
109116 next_tree [repo_path ]._InputGitTreeElement__content = our_content
110117 has_changed = True
111118
112- expected_source_files = [src_root + x .file_name for x in project_sources ]
113- for path in next_tree .keys ():
114- if not path .startswith (src_root ):
115- continue
116- if path not in expected_source_files :
117- del next_tree [path ]
118- print "Deleted file: %s" % path
119- has_changed = True
120-
121119 # Now try handling resource files.
122-
123120 resources = project .resources .all ()
124-
125121 resource_root = root + 'resources/'
126-
127122 for res in resources :
128123 for variant in res .variants .all ():
129124 repo_path = resource_root + variant .path
125+ update_expected_paths (repo_path )
130126 if repo_path in next_tree :
131127 content = variant .get_contents ()
132128 if git_sha (content ) != next_tree [repo_path ]._InputGitTreeElement__sha :
@@ -137,10 +133,21 @@ def github_push(user, commit_message, repo_name, project):
137133 next_tree [repo_path ]._InputGitTreeElement__sha = blob .sha
138134 else :
139135 print "New resource: %s" % repo_path
136+ has_changed = True
140137 blob = repo .create_git_blob (base64 .b64encode (variant .get_contents ()), 'base64' )
141138 print "Created blob %s" % blob .sha
142139 next_tree [repo_path ] = InputGitTreeElement (path = repo_path , mode = '100644' , type = 'blob' , sha = blob .sha )
143140
141+ # Manage deleted files
142+ for path in next_tree .keys ():
143+ if not (any (path .startswith (root ) for root in (src_root , resource_root , worker_src_root ))):
144+ continue
145+ if path not in expected_paths :
146+ del next_tree [path ]
147+ print "Deleted file: %s" % path
148+ has_changed = True
149+
150+ # Compare the resource dicts
144151 remote_manifest_path = root + 'appinfo.json'
145152 remote_wscript_path = root + 'wscript'
146153
@@ -168,6 +175,7 @@ def github_push(user, commit_message, repo_name, project):
168175
169176 # This one is separate because there's more than just the resource map changing.
170177 if their_manifest_dict != our_manifest_dict :
178+ has_changed = True
171179 if remote_manifest_path in next_tree :
172180 next_tree [remote_manifest_path ]._InputGitTreeElement__sha = NotSet
173181 next_tree [remote_manifest_path ]._InputGitTreeElement__content = generate_manifest (project , resources )
@@ -211,10 +219,12 @@ def github_push(user, commit_message, repo_name, project):
211219
212220 return False
213221
222+
214223def get_root_path (path ):
215224 path , extension = os .path .splitext (path )
216225 return path .split ('~' , 1 )[0 ] + extension
217226
227+
218228@git_auth_check
219229def github_pull (user , project ):
220230 g = get_github (user )
@@ -257,7 +267,7 @@ def github_pull(user, project):
257267 for resource in media :
258268 path = resource_root + resource ['file' ]
259269 if project_type == 'pebblejs' and resource ['name' ] in {
260- 'MONO_FONT_14' , 'IMAGE_MENU_ICON' , 'IMAGE_LOGO_SPLASH' , 'IMAGE_TILE_SPLASH' }:
270+ 'MONO_FONT_14' , 'IMAGE_MENU_ICON' , 'IMAGE_LOGO_SPLASH' , 'IMAGE_TILE_SPLASH' }:
261271 continue
262272 if path not in paths_notags :
263273 raise Exception ("Resource %s not found in repo." % path )
0 commit comments