Prathamesh Sarjerao Vaidya commited on
Commit
d4481e9
·
1 Parent(s): 564f836

Update main.yml & check.yml

Browse files
.github/workflows/check.yml CHANGED
@@ -117,7 +117,7 @@ jobs:
117
  pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
118
 
119
  # Create and run upload script
120
- - name: Upload to Google Drive (PR Preview)
121
  env:
122
  GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
123
  run: |
@@ -129,11 +129,15 @@ jobs:
129
  from googleapiclient.http import MediaFileUpload
130
  import mimetypes
131
 
 
132
  credentials_json = os.environ['GOOGLE_CREDENTIALS']
133
  credentials_info = json.loads(credentials_json)
134
  credentials = service_account.Credentials.from_service_account_info(credentials_info)
135
 
 
136
  service = build('drive', 'v3', credentials=credentials)
 
 
137
  FOLDER_ID = '1-8HJcWxsUUQIj9OMXQeoeULS06RA9Hg9'
138
 
139
  def get_mime_type(file_path):
@@ -141,8 +145,9 @@ jobs:
141
  return mime_type or 'application/octet-stream'
142
 
143
  def upload_file(file_path, parent_folder_id, drive_service):
144
- file_name = f"PR_PREVIEW_{os.path.basename(file_path)}"
145
 
 
146
  query = f"name='{file_name}' and '{parent_folder_id}' in parents and trashed=false"
147
  results = drive_service.files().list(q=query).execute()
148
  items = results.get('files', [])
@@ -150,23 +155,45 @@ jobs:
150
  media = MediaFileUpload(file_path, mimetype=get_mime_type(file_path), resumable=True)
151
 
152
  if items:
 
153
  file_id = items[0]['id']
154
  updated_file = drive_service.files().update(
155
  fileId=file_id,
156
  media_body=media
157
  ).execute()
158
- print(f'Updated PR Preview: {file_name}')
159
  else:
 
160
  file_metadata = {
161
  'name': file_name,
162
- 'parents': [parent_folder_id]
163
  }
164
  file = drive_service.files().create(
165
  body=file_metadata,
166
  media_body=media,
167
  fields='id'
168
  ).execute()
169
- print(f'Uploaded PR Preview: {file_name}')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
  def upload_directory(local_path, parent_folder_id, drive_service, exclude_dirs=None, exclude_files=None):
172
  if exclude_dirs is None:
@@ -177,8 +204,20 @@ jobs:
177
  import fnmatch
178
 
179
  for root, dirs, files in os.walk(local_path):
 
180
  dirs[:] = [d for d in dirs if d not in exclude_dirs]
181
 
 
 
 
 
 
 
 
 
 
 
 
182
  for file in files:
183
  # Skip excluded file patterns (like *.md)
184
  should_skip = False
@@ -190,17 +229,29 @@ jobs:
190
  if should_skip:
191
  print(f'Skipping {file} (excluded file type)')
192
  continue
193
-
194
  file_path = os.path.join(root, file)
195
  try:
196
- upload_file(file_path, parent_folder_id, drive_service)
197
  except Exception as e:
198
  print(f'Error uploading {file_path}: {e}')
199
 
200
- print("Starting PR preview upload to Google Drive...")
 
 
 
 
 
 
 
 
 
 
 
 
201
  upload_directory('.', FOLDER_ID, service)
202
 
203
- print("PR preview upload completed - MD files were skipped, PDFs were uploaded!")
204
  EOF
205
 
206
  python upload_to_drive.py
 
117
  pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
118
 
119
  # Create and run upload script
120
+ - name: Upload to Google Drive
121
  env:
122
  GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
123
  run: |
 
129
  from googleapiclient.http import MediaFileUpload
130
  import mimetypes
131
 
132
+ # Load credentials from environment
133
  credentials_json = os.environ['GOOGLE_CREDENTIALS']
134
  credentials_info = json.loads(credentials_json)
135
  credentials = service_account.Credentials.from_service_account_info(credentials_info)
136
 
137
+ # Build the Drive service
138
  service = build('drive', 'v3', credentials=credentials)
139
+
140
+ # Target folder ID - IMPORTANT: This must be the shared folder ID
141
  FOLDER_ID = '1-8HJcWxsUUQIj9OMXQeoeULS06RA9Hg9'
142
 
143
  def get_mime_type(file_path):
 
145
  return mime_type or 'application/octet-stream'
146
 
147
  def upload_file(file_path, parent_folder_id, drive_service):
148
+ file_name = os.path.basename(file_path)
149
 
150
+ # Check if file already exists in the specific folder
151
  query = f"name='{file_name}' and '{parent_folder_id}' in parents and trashed=false"
152
  results = drive_service.files().list(q=query).execute()
153
  items = results.get('files', [])
 
155
  media = MediaFileUpload(file_path, mimetype=get_mime_type(file_path), resumable=True)
156
 
157
  if items:
158
+ # Update existing file
159
  file_id = items[0]['id']
160
  updated_file = drive_service.files().update(
161
  fileId=file_id,
162
  media_body=media
163
  ).execute()
164
+ print(f'Updated: {file_name} (ID: {updated_file.get("id")})')
165
  else:
166
+ # Create new file - CRITICAL: Must specify parent folder
167
  file_metadata = {
168
  'name': file_name,
169
+ 'parents': [parent_folder_id] # This line is crucial!
170
  }
171
  file = drive_service.files().create(
172
  body=file_metadata,
173
  media_body=media,
174
  fields='id'
175
  ).execute()
176
+ print(f'Uploaded: {file_name} (ID: {file.get("id")})')
177
+
178
+ def create_folder_if_not_exists(folder_name, parent_folder_id, drive_service):
179
+ """Create a folder if it doesn't exist and return its ID"""
180
+ # Check if folder already exists
181
+ query = f"name='{folder_name}' and '{parent_folder_id}' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false"
182
+ results = drive_service.files().list(q=query).execute()
183
+ items = results.get('files', [])
184
+
185
+ if items:
186
+ return items[0]['id']
187
+ else:
188
+ # Create new folder
189
+ folder_metadata = {
190
+ 'name': folder_name,
191
+ 'parents': [parent_folder_id],
192
+ 'mimeType': 'application/vnd.google-apps.folder'
193
+ }
194
+ folder = drive_service.files().create(body=folder_metadata, fields='id').execute()
195
+ print(f'Created folder: {folder_name} (ID: {folder.get("id")})')
196
+ return folder.get('id')
197
 
198
  def upload_directory(local_path, parent_folder_id, drive_service, exclude_dirs=None, exclude_files=None):
199
  if exclude_dirs is None:
 
204
  import fnmatch
205
 
206
  for root, dirs, files in os.walk(local_path):
207
+ # Remove excluded directories
208
  dirs[:] = [d for d in dirs if d not in exclude_dirs]
209
 
210
+ # Calculate relative path from the root
211
+ rel_path = os.path.relpath(root, local_path)
212
+ current_folder_id = parent_folder_id
213
+
214
+ # Create nested folders if needed
215
+ if rel_path != '.':
216
+ path_parts = rel_path.split(os.sep)
217
+ for part in path_parts:
218
+ current_folder_id = create_folder_if_not_exists(part, current_folder_id, drive_service)
219
+
220
+ # Upload files in current directory
221
  for file in files:
222
  # Skip excluded file patterns (like *.md)
223
  should_skip = False
 
229
  if should_skip:
230
  print(f'Skipping {file} (excluded file type)')
231
  continue
232
+
233
  file_path = os.path.join(root, file)
234
  try:
235
+ upload_file(file_path, current_folder_id, drive_service)
236
  except Exception as e:
237
  print(f'Error uploading {file_path}: {e}')
238
 
239
+ # Test folder permissions first
240
+ try:
241
+ # Try to list files in the target folder to verify access
242
+ test_query = f"'{FOLDER_ID}' in parents and trashed=false"
243
+ test_results = service.files().list(q=test_query, pageSize=1).execute()
244
+ print(f"Successfully accessed folder. Found {len(test_results.get('files', []))} items (showing 1 max)")
245
+ except Exception as e:
246
+ print(f"ERROR: Cannot access folder {FOLDER_ID}. Make sure the service account has Editor access to this folder.")
247
+ print(f"Service account email should be in the folder sharing settings.")
248
+ exit(1)
249
+
250
+ # Upload all files to Google Drive (excluding MD files)
251
+ print("Starting upload to Google Drive...")
252
  upload_directory('.', FOLDER_ID, service)
253
 
254
+ print("Upload completed - MD files were skipped, PDFs were uploaded!")
255
  EOF
256
 
257
  python upload_to_drive.py
.github/workflows/main.yml CHANGED
@@ -105,8 +105,10 @@ jobs:
105
  run: |
106
  pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
107
 
108
- # Create Google Drive upload script
109
- - name: Create upload script
 
 
110
  run: |
111
  cat > upload_to_drive.py << 'EOF'
112
  import os
@@ -240,12 +242,8 @@ jobs:
240
 
241
  print("Upload completed - MD files were skipped, PDFs were uploaded!")
242
  EOF
243
-
244
- # Upload to Google Drive
245
- - name: Upload to Google Drive
246
- env:
247
- GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
248
- run: python upload_to_drive.py
249
 
250
  # Push to Hugging Face (original functionality)
251
  - name: Push to Hugging Face hub
 
105
  run: |
106
  pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
107
 
108
+ # Create and run upload script
109
+ - name: Upload to Google Drive
110
+ env:
111
+ GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
112
  run: |
113
  cat > upload_to_drive.py << 'EOF'
114
  import os
 
242
 
243
  print("Upload completed - MD files were skipped, PDFs were uploaded!")
244
  EOF
245
+
246
+ python upload_to_drive.py
 
 
 
 
247
 
248
  # Push to Hugging Face (original functionality)
249
  - name: Push to Hugging Face hub