def store_keymap(zipfile_name, keyboard, keymap_name, storage_directory): """Store a copy of the keymap in storage. """ start_dir = os.getcwd() keymap_path = find_keymap_path(keyboard, keymap_name) os.chdir(keymap_path + '/..') try: zip_command = ['zip', '-r', zipfile_name, keymap_name] if os.path.exists(zipfile_name): os.remove(zipfile_name) try: logging.debug('Zipping Keymap: %s', zip_command) check_output(zip_command) except CalledProcessError as build_error: logging.error('Could not zip keymap, Return Code %s, Command %s', build_error.returncode, build_error.cmd) logging.error(build_error.output) os.remove(zipfile_name) return False qmk_storage.save_file(zipfile_name, os.path.join(storage_directory, zipfile_name)) os.remove(zipfile_name) finally: os.chdir(start_dir)
def store_source(zipfile_name, directory, storage_directory): """Store a copy of source in storage. """ if directory in ZIP_EXCLUDES: zip_command = [ 'zip', '-x ' + '-x'.join(ZIP_EXCLUDES[directory]), '-r', zipfile_name, directory ] else: zip_command = ['zip', '-r', zipfile_name, directory] if exists(zipfile_name): os.remove(zipfile_name) try: logging.debug('Zipping Source: %s', zip_command) check_output(zip_command) except CalledProcessError as build_error: logging.error('Could not zip source, Return Code %s, Command %s', build_error.returncode, build_error.cmd) logging.error(build_error.output) os.remove(zipfile_name) return False qmk_storage.save_file(zipfile_name, os.path.join(storage_directory, zipfile_name), 'application/zip') os.remove(zipfile_name) return True
def store_firmware_binary(result): """Called while PWD is qmk_firmware to store the firmware hex. """ firmware_file = 'qmk_firmware/%s' % result['firmware_filename'] if not exists(firmware_file): return False result['firmware'] = open(firmware_file, 'r').read() qmk_storage.save_file(firmware_file, '%(id)s/%(firmware_filename)s' % result, 'text/plain')
def store_firmware_source(result): """Called while PWD is the top-level directory to store the firmware source. """ # Store the keymap source qmk_storage.save_file(path.join('qmk_firmware', result['keymap_archive']), path.join(result['id'], result['keymap_archive'])) # Store the full source result['source_archive'] = 'qmk_firmware-%(keyboard)s-%(keymap)s.zip' % (result) result['source_archive'] = result['source_archive'].replace('/', '-') store_source(result['source_archive'], 'qmk_firmware', result['id']) result['firmware_keymap_url'] = ['/'.join((API_URL, 'v1', 'compile', result['id'], 'keymap'))] result['firmware_source_url'] = ['/'.join((API_URL, 'v1', 'compile', result['id'], 'source'))]
def store_firmware_binary(result): """Called while PWD is qmk_firmware to store the firmware hex. """ firmware_file = 'qmk_firmware/%s' % result['firmware_filename'] firmware_storage_path = '%(id)s/%(firmware_filename)s' % result if not path.exists(firmware_file): return False qmk_storage.save_file(firmware_file, firmware_storage_path) result['firmware_binary_url'] = [ path.join(API_URL, 'v1', 'compile', result['id'], 'download') ]
def store_firmware_binary(result): """Called while PWD is qmk_firmware to store the firmware hex. """ firmware_file = 'qmk_firmware/%s' % result['firmware_filename'] firmware_storage_path = '%(id)s/%(firmware_filename)s' % result mime_type = 'text/plain' if result['firmware_filename'].endswith( '.hex') else 'application/octet-stream' if not path.exists(firmware_file): return False qmk_storage.save_file(firmware_file, firmware_storage_path, mime_type) result['firmware_binary_url'] = [ path.join(API_URL, 'v1', 'compile', result['id'], 'download') ]
def store_firmware_binary(result): """Called while PWD is qmk_firmware to store the firmware hex. """ firmware_storage_path = '%(id)s/%(firmware_filename)s' % result if not result['firmware_filename'] or not path.exists(result['firmware_filename']): return False qmk_storage.save_file(result['firmware_filename'], firmware_storage_path) result['firmware_binary_url'] = [path.join(API_URL, 'v1', 'compile', result['id'], 'download')] if result['public_firmware']: file_ext = result["firmware_filename"].split(".")[-1] file_name = f'compiled/{result["keyboard"]}/default.{file_ext}' qmk_storage.save_file(result['firmware_filename'], file_name, bucket=qmk_storage.COMPILE_S3_BUCKET, public=True)
def store_firmware_source(result): """Called while PWD is the top-level directory to store the firmware source. """ result['source_archive'] = 'qmk_firmware-%(keyboard)s-%(keymap)s.zip' % ( result) result['source_archive'] = result['source_archive'].replace('/', '-') zip_command = [ 'zip', '-x', 'qmk_firmware/.build/*', '-x', 'qmk_firmware/.git/*', '-r', result['source_archive'], 'qmk_firmware' ] try: logging.debug('Zipping Source: %s', zip_command) check_output(zip_command) except CalledProcessError as build_error: logging.error('Could not zip source, Return Code %s, Command %s', build_error.returncode, build_error.cmd) logging.error(build_error.output) qmk_storage.save_file(result['source_archive'], '%(id)s/%(source_archive)s' % result, 'text/plain') remove(result['source_archive'])
def test_save_file(): """Make sure we can store a file and retrieve it. """ test_key = 'qmk_compiler_test_unique_key_name' # Make sure our test key doesn't exist try: qmk_storage.get(test_key) raise RuntimeError('%s exists on S3 when it should not!' % test_key) except Exception as e: if e.__class__.__name__ != 'NoSuchKey': raise # Write it to S3 with NamedTemporaryFile(mode='w', encoding='utf-8') as tempfile: tempfile.write('hello') tempfile.flush() qmk_storage.save_file(tempfile.name, test_key) # Make sure we can retrieve it saved_file = qmk_storage.get(test_key) qmk_storage.delete(test_key) assert saved_file == 'hello'